<template>
  <div>
    <BasicTable
      @selection-change="useSelectionChange"
      :rowSelection="{ type: 'checkbox' }"
      @register="registerTable"
      :clickToRowSelect="false"
    >
      <template #toolbar>
        <Authority value="api:yt:tenantProfile:post">
          <a-button type="primary" @click="handleAdd"> 新增租户配置 </a-button>
        </Authority>
        <Authority value="api:yt:tenantProfile:delete">
          <Popconfirm
            title="您确定要批量删除数据"
            ok-text="确定"
            cancel-text="取消"
            @confirm="handleMutilteDelete"
          >
            <a-button color="error" :disabled="disabled"> 批量删除 </a-button>
          </Popconfirm>
        </Authority>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '设为默认',
              icon: 'ant-design:eye-outlined',
              onClick: handleDefault.bind(null, record),
              ifShow: !record.default,
            },
            {
              label: '编辑',
              auth: 'api:yt:tenantProfile:update',
              icon: 'clarity:note-edit-line',
              onClick: handleEdit.bind(null, record),
            },
            {
              label: '删除',
              auth: 'api:yt:tenantProfile:delete',
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record),
              },
              ifShow: !record.default,
            },
          ]"
        />
      </template>
    </BasicTable>
    <TenantSettingDrawer
      :getChildrenData="echoEditData"
      @register="registerDrawer"
      @success="handleSuccess"
    />
  </div>
</template>
<script lang="ts">
  import { defineComponent, reactive, ref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useDrawer } from '/@/components/Drawer';
  import TenantSettingDrawer from './useDrawer.vue';
  import { columns, searchFormSchema } from './config';
  import { useMessage } from '/@/hooks/web/useMessage';
  import {
    getTableTenantProfileApi,
    deleteTenantProfileApi,
    setTenantProfileIsDefaultApi,
  } from '/@/api/tenant/tenantApi';
  import { Authority } from '/@/components/Authority';
  import { Popconfirm } from 'ant-design-vue';

  export default defineComponent({
    name: 'Index',
    components: { BasicTable, TenantSettingDrawer, TableAction, Authority, Popconfirm },
    setup() {
      const disabled = ref(true);
      let echoEditData = reactive({});
      let selectedRowKeys: Array<string> = [];
      const [registerDrawer, { openDrawer }] = useDrawer();
      const { createMessage } = useMessage();
      const [registerTable, { reload, getSelectRows }] = useTable({
        title: '租户配置列表',
        clickToRowSelect: false,
        api: getTableTenantProfileApi,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        actionColumn: {
          width: 200,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });

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

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
        });
        echoEditData = record;
      }
      async function handleDelete(record: Recordable) {
        let ids = record.id.id;
        await deleteTenantProfileApi(ids);
        createMessage.success('删除成功');
        reload();
      }
      const useSelectionChange = () => {
        selectedRowKeys = getSelectRows();
        let isJudge = selectedRowKeys.map((m: any) => m.default);
        if (isJudge.includes(true)) {
          disabled.value = true;
        } else {
          disabled.value = false;
        }
        if (isJudge.length === 0) {
          disabled.value = true;
        }
      };
      const handleMutilteDelete = () => {
        disabled.value = true;
        selectedRowKeys.forEach(async (f: any) => {
          await deleteTenantProfileApi(f.id.id);
          createMessage.success('删除成功');
          reload();
          selectedRowKeys.length = 0;
          setTimeout(() => {
            disabled.value = false;
          }, 3000);
        });
      };

      const handleDefault = async (record: Recordable) => {
        let id = record.id.id;
        const obj = {
          headers: {
            normalizedNames: {},
            lazyUpdate: null,
          },
          params: {
            updates: null,
            cloneFrom: null,
            encoder: {},
            map: null,
            interceptorConfig: {
              ignoreLoading: false,
              ignoreErrors: false,
              resendRequest: false,
            },
          },
        };
        const data = await setTenantProfileIsDefaultApi(id, 'default', obj);
        if (!data) return createMessage.error('设置该租户配置为默认失败');
        createMessage.success('设置该租户配置为默认成功');
        reload();
      };
      function handleSuccess() {
        reload();
      }
      return {
        handleDefault,
        disabled,
        echoEditData,
        registerTable,
        registerDrawer,
        handleAdd,
        handleEdit,
        handleDelete,
        handleMutilteDelete,
        handleSuccess,
        useSelectionChange,
      };
    },
  });
</script>
