<template>
  <PageWrapper dense>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button
          @click="
            downloadExcel(
              projectInfoExport,
              '项目基础信息;数据',
              getForm().getFieldsValue()
            )
          "
          v-auth="'elevator:projectInfo:export'"
          >导出</a-button
        >
        <a-button
          type="primary"
          danger
          @click="multipleRemove(projectInfoRemove)"
          :disabled="!selected"
          v-auth="'elevator:projectInfo:remove'"
          >删除</a-button
        >
        <a-button type="primary" @click="handleAdd" v-auth="'elevator:projectInfo:add'"
          >新增</a-button
        >
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            stopButtonPropagation
            :actions="[
              {
                label: '查看',
                icon: IconEnum.PREVIEW,
                type: 'primary',
                ghost: true,
                auth: 'project:projectInfo:query',
                onClick: handCheck.bind(null, record),
              },
              {
                label: '修改',
                icon: IconEnum.EDIT,
                type: 'primary',
                ghost: true,
                auth: 'elevator:projectInfo:edit',
                onClick: handleEdit.bind(null, record),
              },
              {
                label: '删除',
                icon: IconEnum.DELETE,
                type: 'primary',
                danger: true,
                ghost: true,
                auth: 'elevator:projectInfo:remove',
                popConfirm: {
                  placement: 'left',
                  title: '是否删除项目基础信息;[' + record.id + ']?',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <ProjectInfoModal @register="registerModal" @reload="reload" />
  </PageWrapper>
</template>

<script setup lang="ts">
import { PageWrapper } from "@/components/Page";
import { BasicTable, useTable, TableAction } from "@/components/Table";
import {
  projectInfoList,
  projectInfoExport,
  projectInfoRemove,
} from "@/api/basicInfo/projectsInfo";
import { downloadExcel } from "@/utils/file/download";
import { useModal } from "@/components/Modal";
import ProjectInfoModal from "./ProjectInfoModal.vue";
import { formSchemas, columns } from "./projectInfo.data";
import { IconEnum } from "@/enums/appEnum";

defineOptions({ name: "ProjectInfo" });

const [registerTable, { reload, multipleRemove, selected, getForm }] = useTable({
  rowSelection: {
    type: "checkbox",
  },
  title: "项目基础信息;列表",
  api: projectInfoList,
  showIndexColumn: false,
  rowKey: "id",
  useSearchForm: true,
  formConfig: {
    schemas: formSchemas,
    baseColProps: {
      xs: 24,
      sm: 24,
      md: 24,
      lg: 6,
    },
  },
  columns: columns,
  actionColumn: {
    width: 250,
    title: "操作",
    key: "action",
    fixed: "right",
  },
});

const [registerModal, { openModal }] = useModal();

function handleEdit(record: Recordable) {
  openModal(true, { record, update: 1 });
}

function handleAdd() {
  openModal(true, { update: 2 });
}
function handCheck(record: Recordable) {
  openModal(true, { record, update: 3 });
}
async function handleDelete(record: Recordable) {
  await projectInfoRemove([record.id]);
  await reload();
}
</script>

<style scoped></style>
