<template>
  <BasicTable @register="registerTable" :rowSelection="rowSelection">
    <template #tableTitle>
      <c-button
        v-show="hasPermission('system:tenant:add')"
        preIcon="ant-design:plus-outlined"
        type="primary"
        @click="handleAdd"
        style="margin-right: 5px"
      >
        新增
      </c-button>
      <a-dropdown v-if="selectedRowKeys.length > 0 && hasPermission('system:tenant:deleteBatch')">
        <template #overlay>
          <a-menu>
            <a-menu-item key="1" @click="batchHandleDelete">
              <Icon icon="ant-design:delete-outlined" />
              删除
            </a-menu-item>
          </a-menu>
        </template>
        <c-button>
          批量操作
          <Icon icon="mdi:chevron-down" />
        </c-button>
      </a-dropdown>
    </template>
    <template #action="{ record }">
      <TableAction :actions="getActions(record)" />
    </template>
  </BasicTable>
  <TenantModal @register="registerModal" @success="reload" />
  <TenantPermissionDrawer @register="registerTenantPermissionDrawer" />
  <TenantResetPasswordSuccessModel @register="registerDrawer2" style="width: 300px" :minHeight="200" />
</template>
<script lang="ts" name="system-tenant" setup>
  import { BasicTable, TableAction, useModal, useDrawer, useListPage } from '@jdlinker/ui';
  import { getTenantList, deleteTenant, batchDeleteTenant, resetPassword } from './tenant.api';
  import { columns, searchFormSchema } from './tenant.data';
  import TenantModal from './TenantModal.vue';
  import TenantPermissionDrawer from './TenantPermissionDrawer.vue';
  import TenantResetPasswordSuccessModel from './TenantResetPasswordSuccessModel.vue';
  import { usePermission } from '@jdlinker/func';

  const { hasPermission } = usePermission();
  const [registerModal, { openModal }] = useModal();
  const [registerDrawer2, { openModal: openResetSuccessModel }] = useModal();
  //注册Drawer
  const [registerTenantPermissionDrawer, { openDrawer: openTenantPermissionDrawer }] = useDrawer();
  // 列表页面公共参数、方法
  const { tableContext } = useListPage({
    designScope: 'tenant-template',
    tableProps: {
      title: '租户列表',
      api: getTenantList,
      columns,
      formConfig: {
        schemas: searchFormSchema,
        fieldMapToTime: [['fieldTime', ['beginDate', 'endDate'], 'YYYY-MM-DD HH:mm:ss']]
      }
    }
  });
  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

  /**
   * 操作列定义
   * @param record
   */
  function getActions(record) {
    return [
      {
        // label: '开通',
        icon: 'ant-design:appstore-add-outlined',
        tooltip: '授权',
        onClick: handlePermission.bind(null, record),
        ifShow: () => hasPermission('system:tenant:auth')
      },
      {
        // label: '重置密码',
        icon: 'ant-design:reload-outlined',
        tooltip: '重置密码',
        popConfirm: {
          title: '是否重置密码',
          placement: 'left',
          confirm: handleReset.bind(null, record)
        },
        ifShow: () => hasPermission('system:tenant:password')
      },
      {
        icon: 'clarity:note-edit-line',
        tooltip: '编辑',
        onClick: handleEdit.bind(null, record),
        ifShow: () => hasPermission('system:tenant:edit')
      },
      {
        icon: 'ant-design:delete-outlined',
        tooltip: '删除',
        color: 'error',
        popConfirm: {
          title: '是否确认删除',
          placement: 'left',
          confirm: handleDelete.bind(null, record)
        },
        ifShow: () => hasPermission('system:tenant:delete')
      }
    ];
  }

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

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

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

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

  function handlePermission(record) {
    openTenantPermissionDrawer(true, { tenantId: record.id, showFooter: true });
  }

  async function handleReset(record) {
    let result = await resetPassword({ preCode: record.preCode });
    sendPassword(JSON.parse(result));
  }

  function sendPassword(e) {
    openResetSuccessModel(true, {
      username: e.username,
      password: e.password
    });
  }
</script>
