<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #form-equipmentCategoryPicker="{ model, field }">
        <MesSearchPickerInput
          :model="model"
          :field="field"
          placeholder="请选择设备类别"
          :show-clear="!!model.equipmentCategoryId"
          @open="openCategorySelect"
          @clear="clearModelFields(model, ['equipmentCategoryId', 'equipmentCategoryName'])"
        />
      </template>
      <template #tableTitle>
        <a-button type="primary" v-auth="'mes:mes_xsl_equipment_part:add'" @click="handleAdd" preIcon="ant-design:plus-outlined">
          新增
        </a-button>
        <a-button
          type="primary"
          v-auth="'mes:mes_xsl_equipment_part:exportXls'"
          preIcon="ant-design:export-outlined"
          @click="onExportXls"
        >
          导出
        </a-button>
        <j-upload-button
          type="primary"
          v-auth="'mes:mes_xsl_equipment_part:importExcel'"
          preIcon="ant-design:import-outlined"
          @click="onImportXls"
        >
          导入
        </j-upload-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined" />
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button v-auth="'mes:mes_xsl_equipment_part:deleteBatch'">
            批量操作
            <Icon icon="mdi:chevron-down" />
          </a-button>
        </a-dropdown>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            { label: '编辑', onClick: handleEdit.bind(null, record), auth: 'mes:mes_xsl_equipment_part:edit' },
          ]"
          :dropDownActions="getDropDownAction(record)"
        />
      </template>
    </BasicTable>
    <MesXslEquipmentPartModal @register="registerModal" @success="handleSuccess" />
    <MesXslEquipmentCategorySelectModal @register="registerCategoryModal" @select="onCategorySelect" />
  </div>
</template>

<script lang="ts" name="xslmes-mesXslEquipmentPart" setup>
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import Icon from '/@/components/Icon';
  import MesSearchPickerInput from '../components/MesSearchPickerInput.vue';
  import MesXslEquipmentPartModal from './components/MesXslEquipmentPartModal.vue';
  import MesXslEquipmentCategorySelectModal from '/@/views/xslmes/mesXslEquipmentType/components/MesXslEquipmentCategorySelectModal.vue';
  import { clearModelFields, createStripIdNameBeforeFetch } from '../utils/mesSearchPickerUtil';
  import { columns, searchFormSchema } from './MesXslEquipmentPart.data';
  import { list, deleteOne, batchDelete, getExportUrl, getImportUrl } from './MesXslEquipmentPart.api';

  const SEARCH_ID_NAME_PAIRS = [{ idField: 'equipmentCategoryId', nameField: 'equipmentCategoryName' }];

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

  const { tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: '设备部位',
      api: list,
      columns,
      canResize: true,
      beforeFetch: createStripIdNameBeforeFetch(SEARCH_ID_NAME_PAIRS),
      formConfig: {
        schemas: searchFormSchema,
        labelWidth: 120,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
      },
      actionColumn: {
        width: 200,
        fixed: 'right',
      },
    },
    exportConfig: {
      name: '设备部位',
      url: getExportUrl,
    },
    importConfig: {
      url: getImportUrl,
      success: handleSuccess,
    },
  });

  const [registerTable, { reload, getForm }, { rowSelection, selectedRowKeys }] = tableContext;

  function openCategorySelect() {
    const v = getForm()?.getFieldsValue?.() || {};
    openCategoryModal(true, { equipmentCategoryId: v.equipmentCategoryId });
  }

  function onCategorySelect(payload: Recordable) {
    getForm()?.setFieldsValue?.({
      equipmentCategoryId: payload.equipmentCategoryId || '',
      equipmentCategoryName: payload.equipmentCategoryName || '',
    });
  }

  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 }, handleSuccess);
  }

  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }

  function handleSuccess() {
    selectedRowKeys.value = [];
    reload();
  }

  function getDropDownAction(record) {
    return [
      { label: '详情', onClick: handleDetail.bind(null, record) },
      {
        label: '删除',
        popConfirm: { title: '是否确认删除', confirm: handleDelete.bind(null, record) },
        auth: 'mes:mes_xsl_equipment_part:delete',
      },
    ];
  }
</script>
