<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button type="primary" v-auth="'mes:mes_raw_material_inspect_std:add'" @click="handleAdd" preIcon="ant-design:plus-outlined"
          >新增</a-button
        >
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            { label: '编辑', onClick: handleEdit.bind(null, record), auth: 'mes:mes_raw_material_inspect_std:edit' },
          ]"
          :dropDownActions="getDropDownAction(record)"
        />
      </template>
    </BasicTable>
    <MesRawMaterialInspectStdModal @register="registerModal" @success="reload" />
  </div>
</template>
<script lang="ts" setup>
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import MesRawMaterialInspectStdModal from './modules/MesRawMaterialInspectStdModal.vue';
  import { columns, searchFormSchema } from './MesRawMaterialInspectStd.data';
  import { list, deleteOne, setEnable } from './MesRawMaterialInspectStd.api';
  import { useMessage } from '/@/hooks/web/useMessage';

  const { createMessage } = useMessage();
  const [registerModal, { openModal }] = useModal();
  const { tableContext } = useListPage({
    tableProps: {
      title: '原材料检验标准',
      api: list,
      columns,
      canResize: true,
      formConfig: { labelWidth: 100, schemas: searchFormSchema, autoSubmitOnEnter: true },
      actionColumn: { width: 200 },
    },
  });
  const [registerTable, { reload }, { rowSelection }] = tableContext;

  function handleAdd() {
    openModal(true, { isUpdate: false, showFooter: true });
  }
  function handleEdit(record: Recordable) {
    openModal(true, { record, isUpdate: true, showFooter: true });
  }
  function handleDetail(record: Recordable) {
    openModal(true, { record, isUpdate: true, showFooter: false });
  }
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, reload);
  }
  async function handleEnable(record: Recordable) {
    await setEnable({ id: record.id, enableFlag: 1 });
    createMessage.success('已启用');
    reload();
  }
  async function handleDisable(record: Recordable) {
    await setEnable({ id: record.id, enableFlag: 0 });
    createMessage.success('已停用');
    reload();
  }
  function getDropDownAction(record) {
    const actions: any[] = [
      { label: '详情', onClick: handleDetail.bind(null, record) },
      {
        label: '删除',
        popConfirm: { title: '是否确认删除', confirm: handleDelete.bind(null, record) },
        auth: 'mes:mes_raw_material_inspect_std:delete',
      },
    ];
    if (record.enableFlag !== 1) {
      actions.push({
        label: '启用',
        auth: 'mes:mes_raw_material_inspect_std:enable',
        popConfirm: { title: '启用后将记录当前时间到生效日期', confirm: () => handleEnable(record) },
      });
    } else {
      actions.push({
        label: '停用',
        auth: 'mes:mes_raw_material_inspect_std:enable',
        popConfirm: { title: '停用后将清除生效日期', confirm: () => handleDisable(record) },
      });
    }
    return actions;
  }
</script>
