<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button v-auth="'AUTH_SYSTEM_ROLE:ADD'" type="primary" @click="handleCreate"> 新增</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'distributorId'">
          <span v-if="record.distributorId === 0"> 华润角色</span>
          <span v-else> 经销商角色</span>
        </template>
        <template v-if="column.dataIndex === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'ant-design:carry-out-outlined',
                tooltip: '分配权限',
                onClick: handleAuth.bind(null, record),
              },
              {
                tooltip: '删除',
                icon: 'ant-design:delete-outlined',
                color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  confirm: handleDelete.bind(null, record),
                },
                ifShow: hasPermission('AUTH_SYSTEM_ROLE:DELETE') && ifShowBtn(record),
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <RoleDrawer @register="registerDrawer" @success="handleSuccess" />
    <AuthDrawer @register="registerAuthDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" name="AUTH_SYSTEM_ROLE">
  import { defineComponent } from 'vue';
  import { usePermission } from '/@/hooks/web/usePermission';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import * as RoleApi from '/@/api/system/roleApi';

  import { useDrawer } from '/@/components/Drawer';
  import RoleDrawer from './RoleDrawer.vue';
  import AuthDrawer from './AuthDrawer.vue';

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

  const { table } = componentSetting;

  const { formConfig, showTableSetting, bordered, showIndexColumn } = table;

  export default defineComponent({
    name: 'RoleManagement',
    components: { BasicTable, RoleDrawer, AuthDrawer, TableAction },
    setup() {
      const { hasPermission } = usePermission();
      const [registerDrawer, { openDrawer }] = useDrawer();
      const [registerAuthDrawer, { openDrawer: openAuthDrawer }] = useDrawer();
      const [registerTable, { reload }] = useTable({
        title: '角色列表',
        api: async (params) => {
          const { pageNum, pageSize } = params;
          const res = await RoleApi.search(Object.assign(handleParams(params), { pageNum, pageSize }));
          return res;
        },
        columns,
        formConfig: {
          labelWidth: formConfig.labelWidth,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting,
        bordered,
        showIndexColumn,
        canResize: false,
        actionColumn: {
          width: 110,
          title: '操作',
          dataIndex: 'action',
          fixed: undefined,
        },
      });

      const handleParams = (params) => {
        const { pageNum, pageSize } = params;
        const handledParams: any = { pageNum, pageSize };
        const searchKeys = searchFormSchema
          .filter((item) => ['Input'].includes(item.component))
          .map((item) => item.field);
        Object.keys(params).forEach((key) => {
          const value = params[key];
          if (value !== undefined && value !== '') {
            if (typeof params[key] === 'object') {
              handledParams[key] = params[key].value;
            }
            if (searchKeys.includes(key)) {
              handledParams[key] = `%${params[key]}%`;
            }
          }
        });
        return handledParams;
      };

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

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

      function handleAuth(record: Recordable) {
        openAuthDrawer(true, {
          record,
          isUpdate: true,
        });
      }

      function handleDelete(record: Recordable) {
        RoleApi.remove(record.id).then((_) => {
          reload();
        });
      }

      function handleSuccess() {
        reload();
      }

      function ifShowBtn(record: Recordable) {
        const systemRole = [
          'systemManager',
          'securityManager',
          'auditManager',
          'clusterManager',
          'calManager',
          'departManager',
        ];
        if (systemRole.includes(record.name)) {
          return false;
        } else {
          return true;
        }
      }

      return {
        hasPermission,
        registerTable,
        registerDrawer,
        registerAuthDrawer,
        handleCreate,
        handleEdit,
        handleAuth,
        handleDelete,
        handleSuccess,
        ifShowBtn,
      };
    },
  });
</script>
