<template>
  <PageWrapper dense>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button
          @click="downloadExcel(RequirementContractualNormalExport, '合同常规要求数据', getForm().getFieldsValue())"
          v-auth="'productManagement:RequirementContractualNormal:export'"
          >导出</a-button
        >
        <a-button
          type="primary"
          danger
          @click="multipleRemove(RequirementContractualNormalRemove)"
          :disabled="!selected"
          v-auth="'productManagement:RequirementContractualNormal:remove'"
          >删除</a-button
        >
        <a-button
          type="primary"
          @click="handleAdd"
          v-auth="'productManagement:RequirementContractualNormal:add'"
          >新增</a-button
        >
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            stopButtonPropagation
            :actions="[
              {
                label: '修改',
                icon: IconEnum.EDIT,
                type: 'primary',
                ghost: true,
                auth: 'productManagement:RequirementContractualNormal:edit',
                onClick: handleEdit.bind(null, record),
              },
              {
                label: '删除',
                icon: IconEnum.DELETE,
                type: 'primary',
                danger: true,
                ghost: true,
                auth: 'productManagement:RequirementContractualNormal:remove',
                popConfirm: {
                  placement: 'left',
                  title: '是否删除合同常规要求[' + record.id + ']?',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <RequirementContractualNormalModal @register="registerModal" @reload="reload" />
  </PageWrapper>
</template>

<script setup lang="ts">
  import { PageWrapper } from '@/components/Page';
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { RequirementContractualNormalList, RequirementContractualNormalExport, RequirementContractualNormalRemove } from '@/api/contractReview/RequirementContractualNormal';
  import { downloadExcel } from '@/utils/file/download';
  import { useModal } from '@/components/Modal';
  import RequirementContractualNormalModal from './RequirementContractualNormalModal.vue';
  import { formSchemas, columns } from './RequirementContractualNormal.data';
  import { IconEnum } from '@/enums/appEnum';

  defineOptions({ name: 'RequirementContractualNormal' });

  const [registerTable, { reload, multipleRemove, selected, getForm }] = useTable({
    rowSelection: {
      type: 'checkbox',
    },
    title: '合同常规要求列表',
    api: RequirementContractualNormalList,
    showIndexColumn: false,
    rowKey: 'id',
    useSearchForm: true,
    formConfig: {
      schemas: formSchemas,
      baseColProps: {
        xs: 24,
        sm: 24,
        md: 24,
        lg: 6,
      },
    },
    columns: columns,
    actionColumn: {
      width: 200,
      title: '操作',
      key: 'action',
      fixed: 'right',
    },
  });

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

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

  function handleAdd() {
    openModal(true, { update: false });
  }

  async function handleDelete(record: Recordable) {
    await RequirementContractualNormalRemove([record.id]);
    await reload();
  }
</script>

<style scoped></style>
