<template>
  <BasicTable @register="registerTable" :rowSelection="rowSelection">
    <template #tableTitle>
      <c-button
        v-show="hasPermission('system:position:add')"
        type="primary"
        preIcon="ant-design:plus-outlined"
        @click="handleAdd"
      >
        新增
      </c-button>
      <c-button
        v-show="hasPermission('system:position:export')"
        type="primary"
        preIcon="ant-design:export-outlined"
        @click="onExportXls"
      >
        导出
      </c-button>
      <j-upload-button
        v-show="hasPermission('system:position:import')"
        type="primary"
        preIcon="ant-design:import-outlined"
        @click="onImportXls"
      >
        导入
      </j-upload-button>
      <Dropdown v-if="selectedRowKeys.length > 0">
        <template #overlay>
          <Menu>
            <MenuItem v-if="hasPermission('system:position: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="getActions(record)" />
    </template>
  </BasicTable>
  <PositionModal @register="registerModal" @success="reload" />
</template>
<script lang="ts" name="system-position" setup>
  import { Dropdown, Menu, MenuItem } from 'ant-design-vue';
  import { BasicTable, TableAction, useListPage, useModal } from '@jdlinker/ui';
  import { getPositionList, deletePosition, batchDeletePosition, getExportUrl, getImportUrl } from './position.api';
  import { columns, searchFormSchema } from './position.data';
  import PositionModal from './PositionModal.vue';
  import { usePermission } from '@jdlinker/func';

  const { hasPermission } = usePermission();
  const [registerModal, { openModal }] = useModal();

  // 列表页面公共参数、方法
  const { onExportXls, onImportXls, tableContext } = useListPage({
    designScope: 'position-template',
    tableProps: {
      title: '职务列表',
      api: getPositionList,
      columns,
      formConfig: {
        schemas: searchFormSchema
      }
    },
    exportConfig: {
      name: '职务列表',
      url: getExportUrl
    },
    importConfig: {
      url: getImportUrl
    }
  });

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

  /**
   * 操作列定义
   * @param record
   */
  function getActions(record) {
    return [
      {
        icon: 'clarity:note-edit-line',
        tooltip: '编辑',
        onClick: handleEdit.bind(null, record),
        ifShow: () => hasPermission('system:position:edit')
      },
      {
        icon: 'ant-design:delete-outlined',
        tooltip: '删除',
        color: 'error',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record)
        },
        ifShow: () => hasPermission('system:position:delete')
      }
    ];
  }

  /**
   * 新增事件
   */
  function handleAdd() {
    openModal(true, {
      isUpdate: false
    });
  }

  /**
   * 编辑事件
   */
  function handleEdit(record) {
    openModal(true, {
      record,
      isUpdate: true
    });
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deletePosition({ id: record.id }, reload);
  }

  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDeletePosition({ ids: selectedRowKeys.value }, reload);
  }
</script>
