<template>
  <div>
    <BasicTable @register="registerTable">
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'dataScope'">
          <Tag color="success">
            {{ formatDictValue(sysDataScopeOptions, record.dataScope) }}
          </Tag>
        </template>
        <template v-if="column.key === 'type'">
          <Tag :color="formatDictColor(sysCreateTypeOptions, record.type)">
            {{ formatDictValue(sysCreateTypeOptions, record.type) }}
          </Tag>
        </template>
        <template v-if="column.key === 'disable'">
          <Tag :color="formatDictColor(disableOptions, record.disable)">
            {{ formatDictValue(disableOptions, record.disable) }}
          </Tag>
        </template>
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                auth: ['sys:role:assign'],
                icon: 'icon-setting|iconfont',
                tooltip: '分配菜单',
                onClick: handleAssignMenu.bind(null, record),
                ifShow: record.type != 'sys',
              },
              {
                auth: ['sys:role:edit'],
                icon: 'icon-edit|iconfont',
                tooltip: '编辑',
                onClick: handleEdit.bind(null, record),
                ifShow: record.type != 'sys',
              },
              {
                auth: ['sys:role:remove'],
                icon: 'icon-delete|iconfont',
                tooltip: '删除',
                color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
                ifShow: record.type != 'sys',
              },
            ]"
          />
        </template>
      </template>

      <template #toolbar>
        <a-button
          v-auth="['sys:role:add']"
          type="primary"
          @click="handleCreate"
          preIcon="icon-plus|iconfont"
          >新增</a-button
        >
        <!-- <a-button
          v-auth="['sys:role:export']"
          color="warning"
          @click="handleExport"
          preIcon="ant-design:download-outlined"
          >导出</a-button
        > -->
      </template>
    </BasicTable>
    <FormModal @register="registerModal" @success="handleSuccess" />
    <FormModalAssignMenu @register="registerModalAssignMenu" />
  </div>
</template>
<script lang="ts" setup>
  import { onBeforeMount, ref } from 'vue';
  import { Tag } from 'ant-design-vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import FormModal from './FormModal.vue';
  import FormModalAssignMenu from './FormModalAssignMenu.vue';
  import { columns, searchFormSchema } from './data';

  // import { fetchList, removeObj, exportList } from '/@/api/modules/system/sysRoleApi';
  import { sysRoleQueryPage, sysRoleRemove } from '/@/api/sys/sysRoleApi';
  // import { listDictModel, downloadFile } from '/@/api/common';
  import { listDictModel } from '/@/api/common';
  import { formatDictValue, formatDictColor } from '/@/utils';

  const sysCreateTypeOptions = ref([]);
  const sysDataScopeOptions = ref([]);
  const disableOptions = ref([]);
  onBeforeMount(async () => {
    sysCreateTypeOptions.value = await listDictModel({ dictCode: 'sys_create_type' });
    sysDataScopeOptions.value = await listDictModel({ dictCode: 'sys_data_scope' });
    disableOptions.value = await listDictModel({ dictCode: 'sys_disable_type' });
  });

  const { createMessage } = useMessage();
  const [registerModal, { openModal }] = useModal();
  const [registerModalAssignMenu, { openModal: openModalAssignMenu }] = useModal();
  const [registerTable, { reload }] = useTable({
    title: '角色列表 ',
    api: sysRoleQueryPage,
    rowKey: 'id',
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      baseColProps: { xs: 24, sm: 12, md: 12, lg: 8 },
      resetButtonOptions: {
        preIcon: 'icon-delete|iconfont',
      },
      submitButtonOptions: {
        preIcon: 'icon-search|iconfont',
      },
    },
    showIndexColumn: false,
    useSearchForm: true,
    bordered: true,
    actionColumn: {
      width: 120,
      title: '操作',
      dataIndex: 'action',
    },
    beforeFetch: handleBeforeFetch,
  });

  // 新增按钮事件
  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  // 编辑按钮事件
  function handleEdit(record: Recordable) {
    console.log(record);
    openModal(true, {
      record,
      isUpdate: true,
    });
  }

  // 删除按钮事件
  async function handleDelete(record: Recordable) {
    console.log(record);
    await sysRoleRemove([record.id]);
    createMessage.success('删除成功！');
    await reload();
  }

  // 导出按钮事件
  // async function handleExport() {
  //   createConfirm({
  //     iconType: 'warning',
  //     title: '提示',
  //     content: '确认导出？',
  //     onOk: async () => {
  //       const params = getForm().getFieldsValue();
  //       const filepath = await exportList(params);
  //       downloadFile(filepath);
  //     },
  //   });
  // }

  // 弹窗回调事件
  async function handleSuccess({ isUpdate, values }) {
    console.log(isUpdate);
    console.log(values);
    await reload();
  }

  // 分配菜单按钮事件
  function handleAssignMenu(record: Recordable) {
    console.log(record);
    openModalAssignMenu(true, {
      record,
    });
  }

  function handleBeforeFetch(params) {
    return { ...params, orders: [{ field: 'sort', direction: 'ASC' }] };
  }
</script>
