<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
      >
      <JUploadButton v-show="hasPermission('system:dict:import')" type="primary" preIcon="ant-design:import-outlined" @click="onImportXls"
        >导入</JUploadButton
      >
      <c-button v-show="hasPermission('system:dict:cache')" type="primary" preIcon="ant-design:sync-outlined" @click="handlerRefreshCache"
        >刷新缓存</c-button
      >
      <c-button v-show="hasPermission('system:dict:recycle')" type="primary" preIcon="ant-design:hdd-outlined" @click="openRecycleModal(true)"
        >回收站</c-button
      >

      <Dropdown v-if="selectedRowKeys.length > 0">
        <template #overlay>
          <Menu>
            <MenuItem v-if="hasPermission('system:dict:deleteBatch')" key="1" @click="batchHandleDelete">
              <Icon icon="ant-design:delete-outlined" />
              删除
            </MenuItem>
          </Menu>
        </template>
        <c-button
          >批量操作
          <Icon icon="ant-design:down-outlined" />
        </c-button>
      </Dropdown>
    </template>
    <!--操作栏-->
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" />
    </template>
  </BasicTable>
  <!--字典弹窗-->
  <DictModal @register="registerModal" @success="handleSuccess" />
  <!--字典配置抽屉-->
  <DictItemList @register="registerDrawer" />
  <!--回收站弹窗-->
  <DictRecycleBinModal @register="registerModal1" @success="reload" />
</template>

<script lang="ts" name="system-dict" setup>
  import { BasicTable, TableAction, useDrawer, useModal, useListPage, JUploadButton } from '@jdlinker/ui';
  import DictItemList from './components/DictItemList.vue';
  import DictModal from './components/DictModal.vue';
  import DictRecycleBinModal from './components/DictRecycleBinModal.vue';
  import { useMessage, removeAuthCache, setAuthCache, DB_DICT_DATA_KEY, usePermission } from '@jdlinker/func';
  import { columns, searchFormSchema } from './dict.data';
  import { list, deleteDict, batchDeleteDict, getExportUrl, getImportUrl, refreshCache, queryAllDictItems } from './dict.api';
  import { Menu, MenuItem, Dropdown } from 'ant-design-vue';

  const { hasPermission } = usePermission();
  const { createMessage } = useMessage();
  //字典model
  const [registerModal, { openModal }] = useModal();
  //字典配置drawer
  const [registerDrawer, { openDrawer }] = useDrawer();

  //回收站model
  const [registerModal1, { openModal: openRecycleModal }] = useModal();

  // 列表页面公共参数、方法
  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 handleDetail(record) {
  //   openModal(true, {
  //     record,
  //     isUpdate: true
  //   });
  // }
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteDict({ id: record.id }, reload);
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDeleteDict({ ids: selectedRowKeys.value }, reload);
  }
  /**
   * 成功回调
   */
  function handleSuccess({ isUpdate, values }) {
    if (isUpdate) {
      updateTableDataRecord(values.id, values);
    } else {
      reload();
    }
  }
  /**
   * 刷新缓存
   */
  async function handlerRefreshCache() {
    const result = await refreshCache();
    if (result.success) {
      const res = await queryAllDictItems();
      removeAuthCache(DB_DICT_DATA_KEY);
      setAuthCache(DB_DICT_DATA_KEY, res.result);
      createMessage.success('刷新缓存完成！');
    } else {
      createMessage.error('刷新缓存失败！');
    }
  }
  /**
   * 字典配置
   */
  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>
