<template>
  <div>
    <BasicTable @register="tenantTable">
      <template #toolbar>
        <Authority value="api:yt:admin:tenant:post">
          <a-button type="primary" @click="handleCreate"> 新增租户</a-button>
        </Authority>
      </template>
      <template #img="{ record }">
        <TableImg
          :size="30"
          :showBadge="false"
          :simpleShow="true"
          :imgList="
            typeof record.icon !== 'undefined' && record.icon !== '' && record.icon != null
              ? [record.icon]
              : null
          "
        />
      </template>
      <template #status="{ record }">
        <Tag
          :color="
            record.tenantStatus === 'NORMAL'
              ? 'green'
              : record.tenantStatus === 'DISABLED'
              ? 'red'
              : 'orange'
          "
        >
          {{
            record.tenantStatus === 'NORMAL'
              ? '正常'
              : record.tenantStatus === 'DISABLED'
              ? '已禁用'
              : '已过期'
          }}
        </Tag>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'ant-design:usergroup-add-outlined',
              label: '租户管理员',
              onClick: handleTenantAdminDrawer.bind(null, record),
            },
            {
              icon: 'clarity:note-edit-line',
              label: '编辑',
              auth: 'api:yt:admin:tenant:update',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              label: '删除',
              auth: 'api:yt:admin:tenant:delete',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <TenantDrawer @register="tenantDrawer" @success="handleSuccess" />
    <TenantAdminDrawer @register="tenantAdminDrawer" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { BasicTable, TableImg, TableAction, useTable, FormSchema } from '/@/components/Table';
  import { Tag } from 'ant-design-vue';
  import { getBasicColumns } from './tenantBaseColumns';
  import { deleteTenant, getTenantPage } from '/@/api/tenant/tenantApi';
  import { useDrawer } from '/@/components/Drawer';
  import TenantDrawer from './TenantDrawer.vue';
  import TenantAdminDrawer from './TenantAdminDrawer.vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { Authority } from '/@/components/Authority';

  export default defineComponent({
    components: {
      BasicTable,
      TableImg,
      Tag,
      TableAction,
      TenantDrawer,
      TenantAdminDrawer,
      Authority,
    },
    setup() {
      const [tenantDrawer, { openDrawer }] = useDrawer();
      const [tenantAdminDrawer, { openDrawer: openTenantAdminDrawer }] = useDrawer();
      const searchFiled: FormSchema[] = [
        {
          field: 'tenantName',
          label: '租户名称',
          component: 'Input',
          componentProps: {
            placeholder: '请输入租户名称',
          },
          colProps: { span: 8 },
        },
      ];
      const [tenantTable, { reload, setLoading }] = useTable({
        title: '租户列表',
        api: getTenantPage,
        columns: getBasicColumns(),
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,

        formConfig: {
          labelWidth: 120,
          schemas: searchFiled,
        },
        actionColumn: {
          width: 200,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });
      async function handleDelete(record: Recordable) {
        try {
          setLoading(true);
          await deleteTenant([record.id]);
          reload();
          const { createMessage } = useMessage();
          createMessage.success('删除成功');
        } finally {
          setLoading(false);
        }
      }

      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
        });
      }

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
        });
      }

      function handleTenantAdminDrawer(record: Recordable) {
        openTenantAdminDrawer(true, {
          record,
        });
      }

      function handleSuccess() {
        reload();
      }

      return {
        tenantTable,
        handleEdit,
        handleDelete,
        handleCreate,
        tenantDrawer,
        tenantAdminDrawer,
        handleSuccess,
        handleTenantAdminDrawer,
        setLoading,
      };
    },
  });
</script>
