<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button type="primary" v-auth="'mes:mes_unit: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_unit:edit' }]" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <MesUnitModal @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 MesUnitModal from './modules/MesUnitModal.vue';
import { columns, searchFormSchema } from './MesUnit.data';
import { list, deleteOne } from './MesUnit.api';
const [registerModal, { openModal }] = useModal();
const { tableContext } = useListPage({
  tableProps: { title: '单位信息', api: list, columns, canResize: true, formConfig: { labelWidth: 120, schemas: searchFormSchema, autoSubmitOnEnter: true }, actionColumn: { width: 120 } },
});
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); }
function getDropDownAction(record) {
  return [
    { label: '详情', onClick: handleDetail.bind(null, record) },
    { label: '删除', popConfirm: { title: '是否确认删除', confirm: handleDelete.bind(null, record) }, auth: 'mes:mes_unit:delete' },
  ];
}
</script>
