// @ts-ignore: 忽略
import type { FormSchemaGetter } from '#/adapter/form';
// @ts-ignore: 忽略
import type { VxeGridProps } from '#/adapter/vxe-table';

import { DictEnum } from '@vben/constants';

import { ElButton } from 'element-plus';

// @ts-ignore: 忽略
// @ts-ignore: 忽略
import { getScheduleShiftManagementList } from '#/api/task';
// @ts-ignore: 忽略
import { getMultipleImageUploadConfig } from '#/components/upload';
import { getDictOptions } from '#/utils/dict';

export const drawerFormSchema: any | FormSchemaGetter = (
  handleClick: (count: number, event: any) => void,
) => [
  {
    component: 'SectionTitle',
    fieldName: '_section_title',
    hideLabel: true,
    componentProps: {
      title: '基本信息',
    },
  },
  {
    component: 'DatePicker',
    fieldName: 'shiftDate',
    label: '上班日期',
    componentProps: {
      type: 'datetime',
      placeholder: '请选择上班日期',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
    },
    rules: 'required',
  },
  {
    component: 'ApiSelect',
    fieldName: 'shiftId',
    label: '上班班次',
    componentProps: {
      placeholder: '请选择上班班次',
      api: () =>
        getScheduleShiftManagementList({
          pageNum: 1,
          pageSize: 10_000,
        }),
      afterFetch: (data: any) => {
        return data.rows?.map((item: any) => ({
          label: item.shiftName,
          value: item.id,
        }));
      },
    },
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'shiftLeaderId',
    label: '当班负责人',
    componentProps: {
      placeholder: '请输入负责人姓名',
      maxlength: 50,
      disabled: true,
    },
    rules: 'required',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    component: 'Input',
    fieldName: 'shiftLeaderName',
    label: '当班负责人',
    componentProps: {
      placeholder: '请选择当班负责人',
      // maxlength: 50,
      disabled: true,
    },
    rules: 'required',
    // renderComponentContent: (row: any, formApi: any) => {
    //   return {
    //     suffix: () => {
    //       return (
    //         <ElButton
    //           onClick={() =>
    //             handleClick(1, (users: any) => {
    //               if (users?.length) {
    //                 formApi.setFieldValue('shiftLeaderName', users[0].name);
    //                 formApi.setFieldValue('shiftLeaderId', users[0].id);
    //               }
    //             })
    //           }
    //           size="small"
    //           type="primary"
    //         >
    //           选择
    //         </ElButton>
    //       );
    //     },
    //   };
    // },
  },
  {
    component: 'Select',
    fieldName: 'weather',
    label: '天气',
    componentProps: {
      placeholder: '请选择天气情况',
      options: getDictOptions(DictEnum.WEATHER_TYPE),
    },
  },
  {
    component: 'InputNumber',
    fieldName: 'promotionCompletionRate',
    label: '促品完成率（%）',
    componentProps: {
      placeholder: '请输入促品完成率',
      min: 0,
      max: 100,
      step: 0.1,
    },
  },
  {
    component: 'Input',
    fieldName: 'shiftMembers',
    // rules: 'required',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    component: 'InputTag',
    fieldName: 'shiftMemberNames',
    label: '班员姓名',
    componentProps: {
      placeholder: '请输入班员姓名',
      maxlength: 200,
    },
    renderComponentContent: (row: any, formApi: any) => {
      return {
        suffix: () => {
          return (
            <ElButton
              onClick={() =>
                handleClick(30, (users: any) => {
                  if (users?.length) {
                    // 将选中的用户姓名添加到标签中
                    const shiftMembers = JSON.parse(row.shiftMembers || '[]');
                    const tags = row.shiftMemberNames || [];
                    // 从已有的班员姓名中删除已选择的用户

                    // const newTags = [...currentTags];
                    users.forEach((user: any) => {
                      const index = shiftMembers.findIndex(
                        (item: any) => item.id === user.id,
                      );
                      if (index === -1) {
                        shiftMembers.push({
                          id: user.id,
                          name: user.name,
                        });
                        tags.push(user.name);
                      }
                    });
                    formApi.setFieldValue(
                      'shiftMembers',
                      JSON.stringify(shiftMembers),
                    );
                    formApi.setFieldValue('shiftMemberNames', tags);
                  }
                })
              }
              size="small"
              type="primary"
            >
              选择
            </ElButton>
          );
        },
      };
    },
  },
  {
    component: 'SectionTitle',
    fieldName: '_section_title',
    // formItemClass: 'col-span-1',
    hideLabel: true,
    componentProps: {
      title: '班前会',
      // barColor: '#215acd', // 可选，自定义竖条颜色
      // fontSize: '30px', // 可选，自定义标题文字大小
    },
  },
  {
    component: 'Input',
    fieldName: 'lineupCheck',
    label: '列队检查',
    componentProps: {
      placeholder: '仪容仪表、精神面貌',
      type: 'textarea',
      rows: 3,
      maxlength: 200,
    },
  },
  {
    component: 'Input',
    fieldName: 'previousWorkReview',
    label: '前期工作回顾',
    componentProps: {
      placeholder: '请输入前期工作回顾',
      type: 'textarea',
      rows: 3,
      maxlength: 200,
    },
  },
  {
    component: 'SectionTitle',
    fieldName: '_section_title',
    hideLabel: true,
    componentProps: {
      title: '工作分工',
    },
  },
  {
    component: 'Input',
    fieldName: 'valuableInventoryId',
    label: '贵重物品盘点',
    componentProps: {
      placeholder: '请输入负责人ID',
      disabled: true,
    },
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    component: 'Input',
    fieldName: 'valuableInventory',
    label: '贵重物品盘点',
    componentProps: {
      placeholder: '请选择贵重物品盘点负责人',
      disabled: true,
    },
    renderComponentContent: (row: any, formApi: any) => {
      return {
        suffix: () => {
          return (
            <ElButton
              onClick={() =>
                handleClick(1, (users: any) => {
                  if (users?.length) {
                    formApi.setFieldValue('valuableInventory', users[0].name);
                    formApi.setFieldValue('valuableInventoryId', users[0].id);
                  }
                })
              }
              size="small"
              type="primary"
            >
              选择
            </ElButton>
          );
        },
      };
    },
  },
  {
    component: 'Input',
    fieldName: 'equipmentInventoryId',
    label: '设备物料盘点',
    componentProps: {
      placeholder: '请输入负责人ID',
      disabled: true,
    },
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    component: 'Input',
    fieldName: 'equipmentInventory',
    label: '设备物料盘点',
    componentProps: {
      placeholder: '请选择设备物料盘点负责人',
      disabled: true,
    },
    renderComponentContent: (row: any, formApi: any) => {
      return {
        suffix: () => {
          return (
            <ElButton
              onClick={() =>
                handleClick(1, (users: any) => {
                  if (users?.length) {
                    formApi.setFieldValue('equipmentInventory', users[0].name);
                    formApi.setFieldValue('equipmentInventoryId', users[0].id);
                  }
                })
              }
              size="small"
              type="primary"
            >
              选择
            </ElButton>
          );
        },
      };
    },
  },
  {
    component: 'Input',
    fieldName: 'giftInventoryId',
    label: '赠品盘点',
    componentProps: {
      placeholder: '请输入负责人ID',
      disabled: true,
    },
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    component: 'Input',
    fieldName: 'giftInventory',
    label: '赠品盘点',
    componentProps: {
      placeholder: '请选择赠品盘点负责人',
      disabled: true,
    },
    renderComponentContent: (row: any, formApi: any) => {
      return {
        suffix: () => {
          return (
            <ElButton
              onClick={() =>
                handleClick(1, (users: any) => {
                  if (users?.length) {
                    formApi.setFieldValue('giftInventory', users[0].name);
                    formApi.setFieldValue('giftInventoryId', users[0].id);
                  }
                })
              }
              size="small"
              type="primary"
            >
              选择
            </ElButton>
          );
        },
      };
    },
  },
  {
    component: 'Input',
    fieldName: 'vehicleGuidanceId',
    label: '车辆指引',
    componentProps: {
      placeholder: '请输入负责人ID',
      disabled: true,
    },
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    component: 'Input',
    fieldName: 'vehicleGuidance',
    label: '车辆指引',
    componentProps: {
      placeholder: '请选择车辆指引负责人',
      disabled: true,
    },
    renderComponentContent: (row: any, formApi: any) => {
      return {
        suffix: () => {
          return (
            <ElButton
              onClick={() =>
                handleClick(1, (users: any) => {
                  if (users?.length) {
                    formApi.setFieldValue('vehicleGuidance', users[0].name);
                    formApi.setFieldValue('vehicleGuidanceId', users[0].id);
                  }
                })
              }
              size="small"
              type="primary"
            >
              选择
            </ElButton>
          );
        },
      };
    },
  },
  {
    component: 'Input',
    fieldName: 'fuelMachineCleaningId',
    label: '油机清洁',
    componentProps: {
      placeholder: '请输入负责人ID',
      disabled: true,
    },
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    component: 'Input',
    fieldName: 'fuelMachineCleaning',
    label: '油机清洁',
    componentProps: {
      placeholder: '请选择油机清洁负责人',
      disabled: true,
    },
    renderComponentContent: (row: any, formApi: any) => {
      return {
        suffix: () => {
          return (
            <ElButton
              onClick={() =>
                handleClick(1, (users: any) => {
                  if (users?.length) {
                    formApi.setFieldValue('fuelMachineCleaning', users[0].name);
                    formApi.setFieldValue('fuelMachineCleaningId', users[0].id);
                  }
                })
              }
              size="small"
              type="primary"
            >
              选择
            </ElButton>
          );
        },
      };
    },
  },
  {
    component: 'Input',
    fieldName: 'forecourtCleaningId',
    label: '前庭清洁',
    componentProps: {
      placeholder: '请输入负责人ID',
      disabled: true,
    },
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    component: 'Input',
    fieldName: 'forecourtCleaning',
    label: '前庭清洁',
    componentProps: {
      placeholder: '请选择前庭清洁负责人',
      disabled: true,
    },
    renderComponentContent: (row: any, formApi: any) => {
      return {
        suffix: () => {
          return (
            <ElButton
              onClick={() =>
                handleClick(1, (users: any) => {
                  if (users?.length) {
                    formApi.setFieldValue('forecourtCleaning', users[0].name);
                    formApi.setFieldValue('forecourtCleaningId', users[0].id);
                  }
                })
              }
              size="small"
              type="primary"
            >
              选择
            </ElButton>
          );
        },
      };
    },
  },
  {
    component: 'Input',
    fieldName: 'toiletCleaningId',
    label: '卫生间清洁',
    componentProps: {
      placeholder: '请输入负责人ID',
      disabled: true,
    },
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    component: 'Input',
    fieldName: 'toiletCleaning',
    label: '卫生间清洁',
    componentProps: {
      placeholder: '请选择卫生间清洁负责人',
      disabled: true,
    },
    renderComponentContent: (row: any, formApi: any) => {
      return {
        suffix: () => {
          return (
            <ElButton
              onClick={() =>
                handleClick(1, (users: any) => {
                  if (users?.length) {
                    formApi.setFieldValue('toiletCleaning', users[0].name);
                    formApi.setFieldValue('toiletCleaningId', users[0].id);
                  }
                })
              }
              size="small"
              type="primary"
            >
              选择
            </ElButton>
          );
        },
      };
    },
  },
  {
    component: 'Input',
    fieldName: 'storeCleaningId',
    label: '便利店清洁',
    componentProps: {
      placeholder: '请输入负责人ID',
      disabled: true,
    },
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    component: 'Input',
    fieldName: 'storeCleaning',
    label: '便利店清洁',
    componentProps: {
      placeholder: '请选择便利店清洁负责人',
      disabled: true,
    },
    renderComponentContent: (row: any, formApi: any) => {
      return {
        suffix: () => {
          return (
            <ElButton
              onClick={() =>
                handleClick(1, (users: any) => {
                  if (users?.length) {
                    formApi.setFieldValue('storeCleaning', users[0].name);
                    formApi.setFieldValue('storeCleaningId', users[0].id);
                  }
                })
              }
              size="small"
              type="primary"
            >
              选择
            </ElButton>
          );
        },
      };
    },
  },
  {
    component: 'Input',
    fieldName: 'garbageDisposalId',
    label: '垃圾处理',
    componentProps: {
      placeholder: '请输入负责人ID',
      disabled: true,
    },
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    component: 'Input',
    fieldName: 'garbageDisposal',
    label: '垃圾处理',
    componentProps: {
      placeholder: '请选择垃圾处理负责人',
      disabled: true,
    },
    renderComponentContent: (row: any, formApi: any) => {
      return {
        suffix: () => {
          return (
            <ElButton
              onClick={() =>
                handleClick(1, (users: any) => {
                  if (users?.length) {
                    formApi.setFieldValue('garbageDisposal', users[0].name);
                    formApi.setFieldValue('garbageDisposalId', users[0].id);
                  }
                })
              }
              size="small"
              type="primary"
            >
              选择
            </ElButton>
          );
        },
      };
    },
  },
  {
    // component: 'Input',
    fieldName: 'otherDivision',
    label: '其它分工',
    slot: 'otherDivision',
    hideLabel: false,
  },
  {
    component: 'Input',
    fieldName: 'keyEnhancement',
    label: '重点强化',
    componentProps: {
      placeholder: '重点工作指引，服务手势操及全员演练',
      type: 'textarea',
      rows: 3,
      maxlength: 200,
    },
  },
  {
    component: 'Input',
    fieldName: 'atmosphereBuilding',
    label: '营造氛围',
    componentProps: {
      placeholder: '齐喊口号，鼓舞斗志',
      type: 'textarea',
      rows: 3,
      maxlength: 200,
    },
  },
  {
    component: 'SectionTitle',
    fieldName: '_section_title',
    hideLabel: true,
    componentProps: {
      title: '班组总结',
    },
  },
  {
    component: 'Input',
    fieldName: 'teamSummary',
    label: '班组总结',
    componentProps: {
      placeholder: '请输入班组总结',
      type: 'textarea',
      rows: 3,
      maxlength: 200,
    },
  },
  {
    component: 'SectionTitle',
    fieldName: '_section_title',
    hideLabel: true,
    componentProps: {
      title: '班前会拍照',
    },
  },
  {
    component: 'Input',
    fieldName: 'meetingPhotos',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    component: 'Upload',
    fieldName: 'meetingPhotoPaths',
    label: '班前会拍照现场',
    componentProps: (row: any) => {
      return {
        ...getMultipleImageUploadConfig(5),
        multiple: true,
        // 调整上传框大小的样式
        onSuccess: (res: any, uploadFile: any, uploadFiles: any) => {
          if (res.code === 200) {
            const paths: string[] = [];
            uploadFiles.forEach((item: any) => {
              if (item.response?.code === 200) {
                paths.push(item.response.data.fileName);
              }
            });
            row.meetingPhotos = paths.join(',');
            return res.data;
          }
        },
      };
    },
    renderComponentContent: () => {
      return {
        default: () => '点击上传照片',
      };
    },
  },
  {
    component: 'SectionTitle',
    fieldName: '_section_title',
    hideLabel: true,
    componentProps: {
      title: '班后待跟进事项',
    },
  },
  {
    component: 'Input',
    fieldName: 'followUpItems',
    label: '班后待跟进事项',
    componentProps: {
      placeholder: '请输入班后待跟进事项',
      type: 'textarea',
      rows: 3,
      maxlength: 200,
    },
  },
  {
    component: 'SectionTitle',
    fieldName: '_section_title',
    hideLabel: true,
    componentProps: {
      title: '评价',
    },
  },
  {
    component: 'Input',
    fieldName: 'evaluationComments',
    hideLabel: true,
    // componentProps: {
    //   placeholder: '请输入评语',
    //   type: 'textarea',
    //   rows: 3,
    //   maxlength: 200,
    // },
    slot: 'evaluationComments',
  },
  // {
  //   component: 'Input',
  //   fieldName: 'otherInfo',
  //   label: '其它信息',
  //   componentProps: {
  //     placeholder: '请输入其它信息',
  //     type: 'textarea',
  //     rows: 3,
  //     maxlength: 200,
  //   },
  // },
];

export const defautTableColumns: VxeGridProps['columns'] = [
  // {
  //   field: 'employeeId',
  //   title: '员工编号',
  // },
  {
    field: 'employeeName',
    title: '员工姓名',
    width: 100,
    fixed: 'left',
  },
];
