<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleEdit" v-if="getCheckPerm('parameter-add')">
          新增设备类型</a-button
        >
      </template>
      <template #action="{ record }">
        <TableAction
          stopButtonPropagation
          :actions="[
            {
              label: '编辑',
              ifShow: getCheckPerm('parameter-updata'),
              onClick: handleEdit.bind(null, record),
            },
            {
              label: '删除',
              color: 'error',
              ifShow: getCheckPerm('parameter-delet'),
              onClick: handleDelete.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <EditModal @register="registerEditModal" @update="reload" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, h } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { Descriptions } from 'ant-design-vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { modelingConfigList, modelingConfigDelete } from '/@/api/parameter';
  import { searchForm, columns } from './data';
  import EditModal from './EditModal.vue';
  import { usePermissionStore } from '/@/store/modules/permission';

  export default defineComponent({
    components: {
      EditModal,
      BasicTable,
      TableAction,
      [Descriptions.name]: Descriptions,
      [Descriptions.Item.name]: Descriptions.Item,
    },
    setup() {
      const { t } = useI18n();
      const permissionStore = usePermissionStore();
      const { getCheckPerm } = permissionStore;
      const { createMessage, createConfirm } = useMessage();
      const [registerEditModal, { openModal: openEditModal }] = useModal();
      const [registerTable, { reload }] = useTable({
        api: modelingConfigList,
        columns: columns,
        useSearchForm: true,
        formConfig: searchForm,
        showTableSetting: true,
        showIndexColumn: false,
        rowKey: 'id',
        fetchSetting: {
          pageField: 'pageNum',
          sizeField: 'pageSize',
          listField: 'list',
          totalField: 'total',
        },
        actionColumn: {
          width: 100,
          title: '操作',
          fixed: 'right',
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
        canResize: false,
      });
      function handleDelete(record: Recordable) {
        createConfirm({
          iconType: 'warning',
          title: () => h('span', '温馨提示'),
          content: () => h('span', '确定要删除授权吗？'),
          onOk: async () => {
            await modelingConfigDelete({ id: record.id });
            reload();
            createMessage.success(t('common.optSuccess'));
          },
        });
      }
      function handleEdit(record: Recordable) {
        openEditModal(true, record);
      }
      return {
        registerTable,
        handleDelete,
        registerEditModal,
        handleEdit,
        reload,
        getCheckPerm,
      };
    },
  });
</script>
