<template>
  <BasicTable @register="registerTable">
    <template #tableTitle>
      <c-button
        v-show="hasPermission('system:role:add')"
        preIcon="ant-design:plus-outlined"
        type="primary"
        @click="handleCreate"
        >新增
      </c-button>
      <c-button
        v-show="hasPermission('system:role:export')"
        preIcon="ant-design:export-outlined"
        type="primary"
        @click="onExportXls"
      >
        导出
      </c-button>
      <JUploadButton
        v-show="hasPermission('system:role:import')"
        type="primary"
        preIcon="ant-design:import-outlined"
        @click="onImportXls"
      >
        导入
      </JUploadButton>
      <Dropdown v-if="selectedRowKeys.length > 0">
        <template #overlay>
          <Menu>
            <MenuItem key="1" @click="batchHandleDelete">
              <Icon icon="ant-design:delete-outlined" />
              删除
            </MenuItem>
          </Menu>
        </template>
        <c-button>
          批量操作
          <Icon icon="mdi:chevron-down" />
        </c-button>
      </Dropdown>
    </template>
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
    </template>
  </BasicTable>
  <!--角色用户-->
  <RoleUserTable @register="roleUserDrawer" />
  <!--角色编辑-->
  <RoleDrawer @register="registerDrawer" @success="reload" :showFooter="showFooter" />
  <!--详情-->
  <RoleDesc @register="registerDesc" />
  <!--角色首页配置-->
  <RoleIndexModal @register="registerIndexModal" />
  <!--功能、数据权限配置-->
  <RoleDataConfigDrawer @register="roleDataConfigDrawer" />
</template>
<script lang="ts" setup>
  import { ref } from 'vue';
  import { BasicTable, TableAction, useDrawer, useModal, useListPage, JUploadButton } from '@jdlinker/ui';
  import RoleDrawer from './components/RoleDrawer.vue';
  import RoleDesc from './components/RoleDesc.vue';
  import RoleIndexModal from './components/RoleIndexModal.vue';
  import RoleUserTable from './components/RoleUserTable.vue';
  import { columns, searchFormSchema } from './role.data';
  import { list, deleteRole, batchDeleteRole, getExportUrl, getImportUrl } from './role.api';
  import { usePermission } from '@jdlinker/func';
  import RoleDataConfigDrawer from '../components/RoleDataConfigDrawer.vue';
  import { Dropdown, Menu, MenuItem } from 'ant-design-vue';

  const { hasPermission } = usePermission();
  const showFooter = ref(true);
  const [roleUserDrawer, { openDrawer: openRoleUserDrawer }] = useDrawer();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerIndexModal, { openModal: openIndexModal }] = useModal();
  const [registerDesc, { openDrawer: openRoleDesc }] = useDrawer();
  const [roleDataConfigDrawer, { openDrawer: openRoleDataConfigDrawer }] = useDrawer();

  // 列表页面公共参数、方法
  const { tableContext, onImportXls, onExportXls } = useListPage({
    designScope: 'role-template',
    tableProps: {
      title: '角色列表',
      api: list,
      columns,
      formConfig: {
        schemas: searchFormSchema
      },
      //自定义默认排序
      defSort: {
        column: 'createTime',
        order: 'desc'
      }
    },
    exportConfig: {
      name: '角色列表',
      url: getExportUrl
    },
    importConfig: {
      url: getImportUrl
    }
  });
  const [registerTable, { reload }, { selectedRowKeys }] = tableContext;

  /**
   * 新增
   */
  function handleCreate() {
    showFooter.value = true;
    openDrawer(true, {
      isUpdate: false
    });
  }
  /**
   * 编辑
   */
  function handleEdit(record: any) {
    showFooter.value = true;
    openDrawer(true, {
      record,
      isUpdate: true
    });
  }
  /**
   * 详情
   */
  function handleDetail(record) {
    showFooter.value = false;
    openRoleDesc(true, {
      record,
      isUpdate: true
    });
  }
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteRole({ id: record.id }, reload);
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDeleteRole({ ids: selectedRowKeys.value }, reload);
  }
  // /**
  //  * 角色授权弹窗
  //  */
  // function handlePerssion(record) {
  //   openRolePermissionDrawer(true, { roleId: record.id });
  // }
  /**
   * 首页配置弹窗
   */
  function handleIndexConfig(roleCode) {
    openIndexModal(true, { roleCode });
  }
  /**
   * 角色用户
   */
  function handleUser(record) {
    openRoleUserDrawer(true, record);
  }

  const relationData = (record) => {
    openRoleDataConfigDrawer(true, { roleId: record.id, type: '1' });
  };
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        icon: 'ant-design:user-outlined',
        tooltip: '用户',
        onClick: handleUser.bind(null, record),
        ifShow: () => hasPermission('system:role:user')
      },
      {
        icon: 'ant-design:appstore-add-outlined',
        tooltip: '授权',
        onClick: relationData.bind(null, record),
        ifShow: () => hasPermission('system:role:auth')
      }
    ];
  }

  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
        ifShow: () => hasPermission('system:role:edit')
      },
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
        ifShow: () => hasPermission('system:role:detail')
      },
      {
        label: '删除',
        placement: 'left',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record)
        },
        ifShow: () => hasPermission('system:role:delete')
      },
      {
        label: '首页配置',
        onClick: handleIndexConfig.bind(null, record.roleCode),
        ifShow: () => hasPermission('system:role:homePage')
      }
    ];
  }
</script>
