import { BasicColumn, FormSchema } from '@/components/Table';
import { h } from 'vue';
import { Switch, Tag } from 'ant-design-vue';
import { setRoleStatus } from '@/api/demo/system';
import { useMessage } from '@/hooks/web/useMessage';
import { uploadApi, fileUploadApi } from '@/api/sys/upload';
import { useUserStore } from '@/store/modules/user';
import { getAppEnvConfig } from '@/utils/env';
const envConfig = getAppEnvConfig();
const VITE_GLOB_ROLES_XQ = (envConfig as any).VITE_GLOB_ROLES_XQ;
const VITE_GLOB_ROLES_JD = (envConfig as any).VITE_GLOB_ROLES_JD;
const VITE_GLOB_ROLES_SH = (envConfig as any).VITE_GLOB_ROLES_SH;
/** 状态对应表 
 * VITE_GLOB_ROLES_XQ 县区填报
 * VITE_GLOB_ROLES_JD 监督
 * VITE_GLOB_ROLES_SH 审核
 * 
 * 县区创建项目,保存
 * 操作：保存   Draft（当前状态，拟办-显示：待办理） 
 * 操作：县区提交  submitted  --> 自动转Forwarded 填报显示-审核中
 * 监督科
 * Forwarded（待审核-显示：待办理）  -> 转发操作  -> Auditing（审核中-显示：已转发）
 * 审核科
 * Auditing（审核中-
 * workStatus= ToDo显示：待审核   
 * -> 审核（手动）
 * workStatus= Done显示：已审核）  
 * -> 当前用户审核完成 -> Summarized(所有用户审核完成，审核科-显示：已审核)
 * 监督科
 * Summarized（显示：待办理） 
 * ->归档 -> Completed  （监督科显示已归档，县区显示已办结）
 * -> 退回 -> Rejected （监督科显示：已退回，县区显示带办理）
 * 县区
 * Rejected 补充材料 -> submitted --> 自动转Forwarded 
 * 重复上面流程
 * 
 * **/
enum NodeTypes {
  Draft = 'Draft',
  Forwarded = 'Forwarded',
  Auditing = 'Auditing',
  Summarized = 'Summarized',
  Completed = 'Completed',
  Rejected = 'Rejected'
}

enum UserWordTodo {
  Todo = 'ToDo',
  Done = 'Done'
}
enum NodeMessage {
  TODO = '待办理',
  REJECTED = '已退回',
  COMPLETED = '已归档',
  FORWARDED = '已转发',
  WAIT_AUDIT = '待审核',
  AUDITED = '已审核',
  AUDITING = '审核中'
}


type CheckedType = boolean | string | number;
export const columns: BasicColumn[] = [
  {
    title: '编号',
    dataIndex: 'businessNo',
    customRender: ({ record }) => {
      const status = record.status;
      if (status === NodeTypes.Rejected) return '（' + NodeMessage.REJECTED + '）' +record.businessNo ;
      return record.businessNo;
    },
  },
  {
    title: '标题',
    dataIndex: 'title',
  },
  {
    title: '状态', dataIndex: 'status', customRender: ({ record }) => {
      const status = record.status;
      const userWordTodo = record.workStatus; // 假设record中包含userWordTodo字段
      const userStore = useUserStore();
      const roles = userStore.getUserInfo.roles;

      // 权限状态映射表
      const roleStatusMap = {
        [VITE_GLOB_ROLES_XQ]: (s: string) => {
          console.log('s===', s);
          if (s === NodeTypes.Draft || s === NodeTypes.Rejected) return NodeMessage.TODO;
          if (s === NodeTypes.Completed) return NodeMessage.COMPLETED;
          return NodeMessage.AUDITING;
        },
        [VITE_GLOB_ROLES_JD]: (s: string) => {
          if (s === NodeTypes.Forwarded || s === NodeTypes.Summarized) return NodeMessage.TODO;
          if (s === NodeTypes.Rejected) return NodeMessage.REJECTED;
          if (s === NodeTypes.Completed) return NodeMessage.COMPLETED;
          return NodeMessage.FORWARDED;
        },
        [VITE_GLOB_ROLES_SH]: (s: string, todo?: string) => {
          if (s === NodeTypes.Auditing) {
            if (todo === UserWordTodo.Todo) return NodeMessage.WAIT_AUDIT;
            if (todo === UserWordTodo.Done) return NodeMessage.AUDITED;
          }
          return NodeMessage.AUDITED;
        },
      };

      // 遍历角色数组，查找匹配的权限处理函数
      for (const role of roles) {
        if (roleStatusMap[role]) {
          return roleStatusMap[role](status, userWordTodo);
        }
      }

      return status;
    },
  },
  {
    title: '类型',
    dataIndex: 'type',
    customRender: ({ record }) => {
      const type = record.type;
      const text = type == 1 ? '耕地破坏鉴定' : '';
      return text;
    },
  },
  {
    title: '发起人',
    dataIndex: 'initiatorName',
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 180,
  },
];

export const searchFormSchema: FormSchema[] = [
  {
    field: 'key',
    label: '项目编号',
    component: 'Input',
    colProps: { span: 8 },
  },
];

export const formSchema: FormSchema[] = [
  {
    field: 'businessNo',
    label: '编号',
    required: true,
    component: 'Input',
    ifShow: false,
  },
  {
    field: 'title',
    label: '标题',
    required: true,
    component: 'Input',
  },
  {
    field: 'type',
    component: 'Select',
    label: '类型',
    required: true,
    componentProps: {
      options: [{ label: '耕地破坏鉴定', value: 1 }],
    },
  },
  {
    component: 'FileUpload',
    label: '文件上传',
    icon: 'ant-design:file-add-outlined',
    field: 'attachments',
    colProps: { span: 24 },
    componentProps: {
      api: fileUploadApi,
      maxNumber: 10,
      maxSize: 2,
      name: 'files',
      action: '/api/Platform/Upload',
      server: '',
    },
  },
];
