<template>
  <div>
    <BasicTable @register="registerTable" @fetch-success="onFetchSuccess">
      <template #toolbar>
        <a-button v-auth="'AUTH_SYSTEM_AUTHORITY:ADD'" type="primary" @click="handleCreate"> 新增</a-button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'clarity:note-edit-line',
                tooltip: '编辑权限',
                onClick: handleEdit.bind(null, record),
                ifShow: hasPermission('AUTH_SYSTEM_AUTHORITY:EDIT'),
              },
              {
                icon: 'ant-design:delete-outlined',
                color: 'error',
                tooltip: '删除权限',
                popConfirm: {
                  title: '是否确认删除？',
                  confirm: handleDelete.bind(null, record),
                },
                ifShow: hasPermission('AUTH_SYSTEM_AUTHORITY:DELETE'),
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <MenuDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" name="AUTH_SYSTEM_AUTHORITY">
  import { defineComponent, nextTick } from 'vue';
  import { handleFetch } from '/@/utils';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import * as AuthorityApi from '/@/api/system/authorityApi';

  import { useDrawer } from '/@/components/Drawer';
  import MenuDrawer from './MenuDrawer.vue';
  import { usePermission } from '/@/hooks/web/usePermission';
  import { columns, searchFormSchema } from './menu.data';
  import componentSetting from '/@/settings/componentSetting';

  const { table } = componentSetting;

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

  export default defineComponent({
    name: 'MenuManagement',
    components: { BasicTable, MenuDrawer, TableAction },
    setup() {
      const { hasPermission } = usePermission();
      const [registerDrawer, { openDrawer }] = useDrawer();
      const [registerTable, { reload, expandAll }] = useTable({
        title: '菜单列表',
        api: (params) => AuthorityApi.all(handleParams(params)),
        columns,
        formConfig: {
          labelWidth: formConfig.labelWidth,
          schemas: searchFormSchema,
        },
        isTreeTable: true,
        pagination: false,
        striped: false,
        useSearchForm: true,
        showTableSetting,
        bordered,
        showIndexColumn,
        canResize: false,
        actionColumn: {
          width: 80,
          title: '操作',
          dataIndex: 'action',
          fixed: 'right',
        },
        afterFetch: (result) => {
          return handleFetch(result);
        },
      });

      const handleParams = (params) => {
        const handledParams: any = { authorityId: 0, orderByClause: 'sort_order asc' };
        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 handleDelete(record: Recordable) {
        AuthorityApi.remove(record.id).then((_) => {
          reload();
        });
      }

      function handleSuccess() {
        reload();
      }

      function onFetchSuccess() {
        // 演示默认展开所有表项
        // nextTick(expandAll);
      }

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