<template>
  <CollapseContainer title="系统配置" :canExpan="false">
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <c-button v-show="hasPermission('system:setting:add')" type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</c-button>
        <c-button v-show="hasPermission('system:setting:cache')" type="primary" @click="handleRefreshCache" preIcon="ant-design:reload-outlined"> 刷新缓存</c-button>
        <Dropdown v-if="selectedRowKeys.length > 0 && hasPermission('system:setting:deleteBatch')">
          <template #overlay>
            <Menu>
              <MenuItem 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>
    <!-- 表单区域 -->
    <SysConfigDrawer @register="registerDrawer" @success="reload" :showFooter="showFooter" />
    <SysConfigModal @register="registerModal" @success="handleSuccess" />
  </CollapseContainer>
</template>

<script lang="ts" name="system-sysConfig" setup>
  import { CollapseContainer, BasicTable, TableAction, useDrawer, useModal, useListPage } from '@jdlinker/ui';
  import { ref } from 'vue';
  import { useUserStore } from '@jdlinker/func';
  import SysConfigDrawer from './components/sysConfigDrawer.vue';
  import SysConfigModal from './components/sysConfigModal.vue';
  import { columns, searchFormSchema } from './sysConfig_data';
  import { list, deleteOne, batchDelete, getImportUrl, getExportUrl, refreshCache } from './sysConfig_api';
  import { usePermission } from '@jdlinker/func';
  import { Menu, MenuItem, Dropdown } from 'ant-design-vue';

  const { hasPermission } = usePermission();
  const userStore = useUserStore();
  //注册model
  const [registerModal, { openModal }] = useModal();
  //注册Drawer
  const [registerDrawer, { openDrawer }] = useDrawer();
  const showFooter = ref(true);
  //注册table数据
  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: 'sys_config',
      api: list,
      columns,
      scroll: { y: 285 },
      formConfig: {
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
        fieldMapToNumber: [],
        fieldMapToTime: []
      },
      actionColumn: {
        width: 120,
        fixed: 'right'
      }
    },
    exportConfig: {
      name: 'sys_config',
      url: getExportUrl
    },
    importConfig: {
      url: getImportUrl,
      success: handleSuccess
    }
  });

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

  /**
   * 新增事件
   */
  function handleAdd() {
    openDrawer(true, { isUpdate: 0, showFooter: true });
  }
  /**
   * 编辑事件
   */
  function handleEdit(record: any) {
    if (userStore.getSysOpenModal) {
      openModal(true, { record, isUpdate: true, showFooter: true });
    } else {
      openDrawer(true, { record, isUpdate: 1, showFooter: true });
    }
  }
  /**
   * 详情
   */
  function handleDetail(record: any) {
    openDrawer(true, { record, isUpdate: 2 });
  }
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }

  /** 清理缓存 */
  async function handleRefreshCache() {
    await refreshCache();
    reload();
  }
  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        icon: 'clarity:info-standard-line',
        tooltip: '详情',
        onClick: handleDetail.bind(null, record),
        ifShow: () => hasPermission('system:setting:detail')
      },
      {
        icon: 'clarity:note-edit-line',
        tooltip: '编辑',
        onClick: handleEdit.bind(null, record),
        ifShow: () => hasPermission('system:setting:edit')
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        tooltip: '删除',
        popConfirm: {
          title: '是否确认删除',
          placement: 'left',
          confirm: handleDelete.bind(null, record)
        },
        ifShow: () => hasPermission('system:setting:delete')
      }
    ];
  }
  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record)
      },
      {
        label: '删除',
        popConfirm: {
          title: '确定删除吗?',
          confirm: handleDelete.bind(null, record)
        }
      }
    ];
  }
</script>
<style scoped></style>
