<template>
  <PageWrapper dense fixedHeight contentFullHeight>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" v-auth="'index:add'" @click="handleCreate">
          {{ t('新增') }}
        </a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="['authObjectList'].includes(column.dataIndex)">
          <a-button type="link" class="!p-0" @click="viewObject(record.id, record.authType)">{{
            t('查看授权对象 >')
          }}</a-button>
        </template>
        <template v-if="column.dataIndex == 'action'">
          <TableAction
            :actions="[
              {
                label: '编辑',
                auth: 'index:edit',
                onClick: handleEdit.bind(null, record),
              },
              {
                label: '删除',
                auth: 'index:delete',
                color: 'error',
                popConfirm: {
                  title: t('是否确认删除'),
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <AuthDrawer @register="registerAuthModal" @success="handleSuccess" />
    <AuthModal @register="registerModal" />
  </PageWrapper>
</template>
<script lang="ts">
  import { defineComponent, h } from 'vue';

  import { BasicTable, useTable, TableAction, BasicColumn, FormSchema } from '/@/components/Table';
  import AuthModal from './components/AuthModal.vue';
  import AuthDrawer from './components/AuthDrawer.vue';
  import { PageWrapper } from '/@/components/Page';
  import { Tag } from 'ant-design-vue';
  import { useMessage } from '/@/hooks/web/useMessage';

  import { deleteAuth, getAuth, getAuthObject, getAuthPageList } from '/@/api/system/authorize';
  import { usePermission } from '/@/hooks/web/usePermission';

  import { useModal } from '/@/components/Modal';
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n();
  export const columns: BasicColumn[] = [
    {
      title: t('编码'),
      dataIndex: 'code',
      sorter: true,
      resizable: true,
    },
    {
      title: t('名称'),
      dataIndex: 'name',
      sorter: true,
      resizable: true,
    },
    {
      title: t('对象类型'),
      dataIndex: 'authType',
      resizable: true,
      sorter: true,
      width: 120,
      customRender: ({ record }) => {
        const authType = record.authType;
        let color = authType == 1 ? 'green' : 'red';
        let text = authType == 1 ? t('用户') : t('角色');
        if (authType == 2) {
          text = t('全部用户');
          color = 'blue';
        }

        return h(Tag, { color: color }, () => text);
      },
    },
    {
      title: t('授权对象'),
      dataIndex: 'authObjectList',
      ellipsis: false,
      resizable: true,
      width: 130,
    },
    {
      title: t('创建人'),
      dataIndex: 'createUserName',
      resizable: true,
    },
    {
      title: t('创建时间'),
      dataIndex: 'createDate',
      sorter: true,
      resizable: true,
    },
    {
      title: t('最后修改人'),
      dataIndex: 'modifyUserName',
      resizable: true,
    },
    {
      title: t('最后修改时间'),
      dataIndex: 'modifyDate',
      sorter: true,
      resizable: true,
    },
  ];

  export const searchFormSchema: FormSchema[] = [
    {
      field: 'keyword',
      label: t('关键字'),
      component: 'Input',
      colProps: { span: 8 },
      componentProps: {
        placeholder: t('请输入名称或编码'),
      },
    },
  ];

  export default defineComponent({
    name: 'AuthManagement',
    components: { BasicTable, AuthDrawer, TableAction, AuthModal, PageWrapper },
    setup() {
      const { notification } = useMessage();
      const { hasPermission } = usePermission();

      const [registerAuthModal, { openModal: openAuthModal }] = useModal();
      const [registerModal, { openModal, setModalProps }] = useModal();
      const [registerTable, { reload }] = useTable({
        title: t('数据权限列表'),
        api: getAuthPageList,
        columns,
        formConfig: {
          rowProps: {
            gutter: 16,
          },
          schemas: searchFormSchema,
          showResetButton: false,
        },
        useSearchForm: true,
        showTableSetting: true,
        striped: false,
        actionColumn: {
          width: 120,
          title: t('操作'),
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
        tableSetting: {
          size: false,
        },
        customRow: (record) => {
          return {
            ondblclick: () => {
              if (hasPermission('index:edit')) {
                handleEdit(record);
              }
            },
          };
        },
      });

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

      function handleEdit(record: Recordable) {
        getAuth(record.id).then((res) => {
          openAuthModal(true, {
            record: res,
            isUpdate: true,
          });
        });
      }

      function handleDelete(record: Recordable) {
        deleteAuth([record.id]).then((_) => {
          reload();
          notification.success({
            message: t('提示'),
            description: t('删除成功'),
          }); //提示消息
        });
      }

      function handleSuccess() {
        reload();
      }
      function viewObject(id, type) {
        getAuthObject(id).then((res) => {
          openModal(true, res);
          setModalProps({ title: type == 1 ? t('查看用户授权对象') : t('查看角色授权对象') });
        });
      }
      return {
        registerTable,
        registerAuthModal,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        viewObject,
        registerModal,
        t,
      };
    },
  });
</script>
