import type { FormSchemaGetter } from '#/adapter/form';
import { h } from 'vue';
import { getFileUploadConfig } from '#/components/upload/config';
import { getDictOptions } from '#/utils/dict';
import { getPostList } from '#/api/system/post/post';
import { getUserList } from '#/api/system/user/user';
// 字典标识常量
const DICT_KEYS = {
  // 任务类型字典标识
  TASK_TYPE: 'task_type',
  FORMT_TYPE: 'form_type',
  SUBFORM_TYPE: 'subForm_type',
};
// 获取任务类型字典选项
const getTaskTypeOptions = () => getDictOptions(DICT_KEYS.TASK_TYPE);
const getFormTypeOptions = () => getDictOptions(DICT_KEYS.FORMT_TYPE);
const getSubFormTypeOptions = () => getDictOptions(DICT_KEYS.SUBFORM_TYPE);
let getPostListOptions = [] as any;
let getUserListOptions = [] as any;
const getPostListfn = async () => {
  const { rows } = await getPostList({
    pageNum: 1,
    pageSize: 20,
  });
  getPostListOptions = rows.map((item) => ({
    label: item.postName,
    value: item.postId,
  }));
};
const getUserListfn = async () => {
  const { rows } = await getUserList({
    pageNum: 1,
    pageSize: 20,
  });
  console.log(rows);
  
  getUserListOptions = rows.map((item) => ({
    label: item.userName,
    value: item.userId,
  }));
};
getPostListfn();
getUserListfn();
export const drawerFormSchema: FormSchemaGetter = () => [
  {
    component: 'Select',
    fieldName: 'executePosition',
    label: '执行岗位',
    rules: 'required',
    componentProps: {
      placeholder: '请选择执行岗位',
      allowClear: true,
      options: getPostListOptions,
    },
  },
  {
    component: 'ApiSelect',
    fieldName: 'executorName',
    label: '执行人名称',
    rules: 'required',
    componentProps: {
      placeholder: '请选择执行人',
      allowClear: true,
      options: getUserListOptions,
    },
  },
  {
    component: 'Input',
    fieldName: 'taskName',
    label: '任务名称',
    rules: 'required',
    componentProps: {
      placeholder: '请输入任务名称',
      maxlength: 100,
    },
  },
  {
    component: 'DatePicker',
    fieldName: 'startTime',
    label: '开始时间',
    rules: 'required',
    componentProps: {
      placeholder: '请选择开始时间',
      showTime: true,
      format: 'YYYY-MM-DD HH:mm:ss',
    },
  },
  {
    component: 'DatePicker',
    fieldName: 'endTime',
    label: '截至时间',
    rules: 'required',
    componentProps: {
      placeholder: '请选择截至时间',
      showTime: true,
      format: 'YYYY-MM-DD 23:59:59',
    },
  },
  {
    component: 'Select',
    fieldName: 'formType',
    label: '表单类型',
    rules: 'required',
    componentProps: {
      placeholder: '请选择表单类型',
      allowClear: true,
      options: getFormTypeOptions(),
    },
  },
  {
    component: 'Select',
    fieldName: 'subFormType',
    label: '表单子类型',
    rules: 'required',
    componentProps: {
      placeholder: '请选择表单子类型',
      allowClear: true,
      options: getSubFormTypeOptions(),
    },
  },
  {
    component: 'Select',
    fieldName: 'taskType',
    label: '任务类型',
    rules: 'required',
    componentProps: {
      placeholder: '请选择任务类型',
      allowClear: true,
      options: getTaskTypeOptions(),
    },
  },
  // {
  //   component: 'ApiSelect',
  //   fieldName: 'ccPerson',
  //   label: '抄送人',
  //   componentProps: {
  //     placeholder: '请选择抄送人',
  //     allowClear: true,
  //     options: [
  //       {
  //         label: '抄送人1',
  //         value: '抄送人1',
  //       },
  //       {
  //         label: '抄送人2',
  //         value: '抄送人2',
  //       },
  //     ],
  //   },
  // },
  {
    component: 'Input',
    fieldName: 'description',
    label: '任务描述',
    componentProps: {
      type: 'textarea',
      placeholder: '请输入任务描述',
      maxlength: 500,
      showWordLimit: true,
      rows: 4,
    },
  },
  // {
  //   component: 'Upload',
  //   fieldName: 'descriptionImage',
  //   label: '描述图片',
  //   componentProps: {
  //     accept: 'image/*',
  //     listType: 'picture-card',
  //     maxCount: 1,
  //   },
  // },
  {
    component: 'Upload',
    fieldName: 'attachments',
    label: '附件',
    componentProps: (row: any) => {
      return {
        ...getFileUploadConfig(
          '.png,.jpg,.jpeg,.gif,.pdf,.doc,.docx,.xls,.xlsx',
          'text',
          5,
        ),
        onSuccess: (res: any) => {
          console.log('上传res', res);
          console.log('上传row', row);

          if (res.code === 200) {
            // 处理上传成功后的逻辑
            if (!row.attachments) {
              row.attachments = [];
            }
            // 添加新上传的文件信息
            // row.attachments.push({
            //   fileName: res.data.fileName,
            //   url: res.data.url,
            // });

            return res.data.fileName;
          }
          return Promise.reject(res);
        },
        onRemove: (file: any) => {
          // 处理文件移除逻辑
          if (row.attachments) {
            row.attachments = row.attachments.filter(
              (item: any) => item.url !== file.url,
            );
          }
        },
      };
    },
    renderComponentContent: () => {
      return {
        default: () =>
          h(
            'div',
            {
              class: 'flex items-center justify-center',
            },
            [
              h(
                'ElIcon',
                { class: 'el-upload__icon' },
                { default: () => h('Plus', { size: '18' }) },
              ),
              h(
                'div',
                {
                  class: 'el-upload__text',
                },
                [
                  h('div', {}, '上传附件'),
                  h(
                    'div',
                    { class: 'text-xs text-gray-500' },
                    '最多上传5个附件，支持png、jpg、jpeg、gif、pdf、doc、docx、xls、xlsx格式',
                  ),
                ],
              ),
            ],
          ),
      };
    },
  },
];
