<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" v-if="getCheckPerm('role-add')"> 新增角色 </a-button>
      </template>
      <template #action="{ record }">
        <TableAction  v-if="record.id != 1"
          :actions="[
            {
              label: t('common.edit'),
              ifShow:getCheckPerm('role-update'),
              onClick: handleEdit.bind(null, record),
            },{
              label: t('routes.system.roleBut'),
              ifShow:getCheckPerm('role-giveMenu'),
              onClick: handleRole.bind(null, record),
            },{
              label: t('common.delText'),
              color: 'error',
              ifShow:getCheckPerm('role-delete'),
              popConfirm: {
                title: t('common.delConfirm'),
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <RoleDrawer @register="registerDrawer" @success="handleSuccess" />
    <TreeModal @register="register" @update="reload" />
  </div>
</template>
<script lang="ts">
  import { defineComponent, nextTick } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { getRoleListByPage, deleteRoleApi } from '/@/api/sys/system';
  import { useDrawer } from '/@/components/Drawer';
  import RoleDrawer from './RoleDrawer.vue';
  import TreeModal from './treeModal.vue';
  import { useModal } from '/@/components/Modal';
  import { usePermissionStore } from '/@/store/modules/permission';
  // import { useUserStore } from '/@/store/modules/user';

  import { columns, searchFormSchema } from './role.data';

  export default defineComponent({
    name: 'RoleManagement',
    components: { BasicTable, TableAction,  RoleDrawer, TreeModal },// 
    setup() {
      const { t } = useI18n();
      const [registerDrawer, { openDrawer }] = useDrawer();
      const [register, { openModal }] = useModal();
      const permissionStore = usePermissionStore();
      const { getCheckPerm } = permissionStore;
      // const userStore = useUserStore();
      // const { getCheckRole } = userStore;
      const [registerTable, { reload }] = useTable({
        title: t('routes.system.roleTitle'),
        api: getRoleListByPage,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        showIndexColumn: false,
        canResize: true,
        fetchSetting: {
          pageField: 'pageNum',
          sizeField: 'pageSize',
          listField: 'list',
          totalField: 'total',
        },
        actionColumn: {
          // ifShow: getCheckRole('super'),
          width: 140,
          title: t('common.operating'),
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: undefined,
        },
      });

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

      function handleRole(record: Recordable) {
        openModal(true, record);
      }

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

      async function handleDelete(record: Recordable) {
        try {
          console.log('roleId', [record.roleId]);
          const result = await deleteRoleApi({id:record.id});
          console.log('result', result);
          nextTick(reload);
        } catch (error) {
          console.log('error', error);
        }
      }

      function handleSuccess() {
        reload();
      }

      return {
        registerTable,
        registerDrawer,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        handleRole,
        register,
        reload,
        getCheckPerm,
        t,
      };
    },
  });
</script>
