<template>
  <!--引用表格-->
  <BasicTable @register="registerTable" :rowSelection="rowSelection">
    <!--插槽:table标题-->
    <template #tableTitle>
      <c-button
        v-show="hasPermission('system:dict:add')"
        preIcon="ant-design:plus-outlined"
        type="primary"
        @click="handleCreate"
        >新增</c-button
      >
      <c-button
        v-show="hasPermission('system:dict:export')"
        type="primary"
        preIcon="ant-design:export-outlined"
        @click="onExportXls"
      >
        导出
      </c-button>
      <j-upload-button
        v-show="hasPermission('system:dict:import')"
        type="primary"
        preIcon="ant-design:import-outlined"
        @click="onImportXls"
      >
        导入
      </j-upload-button>

      <a-dropdown v-if="selectedRowKeys.length > 0">
        <template #overlay>
          <a-menu>
            <a-menu-item v-if="hasPermission('system:dict:deleteBatch')" key="1" @click="batchHandleDelete">
              <Icon icon="ant-design:delete-outlined" />
              删除
            </a-menu-item>
          </a-menu>
        </template>
        <c-button
          >批量操作
          <Icon icon="ant-design:down-outlined" />
        </c-button>
      </a-dropdown>
    </template>
    <!--操作栏-->
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" />
    </template>
  </BasicTable>
  <!--字典弹窗-->
  <ConfigModal @register="registerModal" @success="handleSuccess" />
  <!--字典配置抽屉-->
  <ConfigItemList @register="registerDrawer" />
</template>

<script lang="ts" name="system-dict" setup>
  import { Icon } from '@jdlinker/ui';
  import { BasicTable, TableAction } from '@jdlinker/ui';
  import { useDrawer } from '@jdlinker/ui';
  import { useModal } from '@jdlinker/ui';
  import ConfigItemList from './components/ConfigItemList.vue';
  import ConfigModal from './components/ConfigModal.vue';
  import { columns, searchFormSchema } from './config.data';
  import { list, deleteConfigManage, batchDeleteConfigManage, getExportUrl, getImportUrl } from './config.api';
  import { usePermission } from '@jdlinker/func';
  import { Button as CButton } from '@jdlinker/ui';

  const { hasPermission } = usePermission();
  //字典model
  const [registerModal, { openModal }] = useModal();
  //字典配置drawer
  const [registerDrawer, { openDrawer }] = useDrawer();
  import { useListPage } from '@jdlinker/ui';

  // 列表页面公共参数、方法
  const { tableContext, onExportXls, onImportXls } = useListPage({
    designScope: 'dict-template',
    tableProps: {
      title: '数据字典',
      api: list,
      columns,
      formConfig: {
        schemas: searchFormSchema
      },
      // @ts-ignore
      actionColumn: {
        width: 240
      }
    },
    exportConfig: {
      name: '数据字典列表',
      url: getExportUrl
    },
    importConfig: {
      url: getImportUrl
    }
  });

  //注册table数据
  const [registerTable, { reload, updateTableDataRecord }, { rowSelection, selectedRowKeys }] = tableContext;

  /**
   * 新增事件
   */
  function handleCreate() {
    openModal(true, {
      isUpdate: false
    });
  }
  /**
   * 编辑事件
   */
  async function handleEdit(record: any) {
    openModal(true, {
      record,
      isUpdate: true
    });
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteConfigManage({ id: record.id }, reload);
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDeleteConfigManage({ ids: selectedRowKeys.value }, reload);
  }
  /**
   * 成功回调
   */
  function handleSuccess({ isUpdate, values }) {
    if (isUpdate) {
      updateTableDataRecord(values.id, values);
    } else {
      reload();
    }
  }

  /**
   * 字典配置
   */
  function handleItem(record) {
    openDrawer(true, {
      id: record.id
    });
  }
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        icon: 'clarity:note-edit-line',
        tooltip: '编辑',
        onClick: handleEdit.bind(null, record),
        ifShow: () => hasPermission('system:dict:edit')
      },
      {
        icon: 'ant-design:control-outlined',
        tooltip: '配置',
        onClick: handleItem.bind(null, record),
        ifShow: () => hasPermission('system:dict:config')
      },
      {
        icon: 'ant-design:delete-outlined',
        tooltip: '删除',
        color: 'error',
        popConfirm: {
          title: '确定删除吗?',
          confirm: handleDelete.bind(null, record)
        },
        ifShow: () => hasPermission('system:dict:delete')
      }
    ];
  }
</script>
