import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';
import { DictEnum } from '@vben/constants';
import { getDictOptions } from '#/utils/dict';
import { selectAllSysStation } from '#/api/system/infoEntry/stationInfo/stationInfo';
import { getFileUploadConfig } from '#/components/upload';
import { requestClient } from '#/api/request';

// 获取油站列表
const getStationList = async () => {
  try {
    const resp = await selectAllSysStation();
    const data = resp || [];
    return Array.isArray(data)
      ? data.map((item: any) => ({
          label: item.stationName,
          value: item.id.toString(),
        }))
      : [];
  } catch (error) {
    console.error('获取油站列表失败:', error);
    return [];
  }
};

// 获取执行人列表
const getExecutorList = async (stationId: string) => {
  try {
    if (!stationId) {
      console.error('stationId 不能为空');
      return [];
    }
    const resp = await requestClient.get('/system/user/stationList', {
      params: {
        stationId: stationId,
      },
    });
    const data = resp || [];
    console.log('获取执行人列表成功:', data);
    return Array.isArray(data)
      ? data.map((item: any) => ({
          label: item.userName,
          value: item.userId.toString(),
        }))
      : [];
  } catch (error) {
    console.error('获取执行人列表失败:', error);
    return [];
  }
};

// 抽屉表单schema
export const drawerFormSchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
    fieldName: 'id',
  },
  {
    component: 'ApiSelect',
    componentProps: {
      placeholder: '请选择检查油站',
      api: getStationList,
    },
    fieldName: 'stationId',
    label: '检查油站',
    rules: 'required',
  },
  {
    component: 'Input',
    componentProps: {
      placeholder: '请输入检查名称',
      maxlength: 100,
    },
    fieldName: 'checkName',
    label: '检查名称',
    rules: 'required',
  },
  {
    component: 'RadioGroup',
    componentProps: {
      disabled: true,
      options: [
        {
          label: '是',
          value: 1,
        },
      ],
    },
    fieldName: 'isTask',
    label: '是否转任务',
    rules: 'required',
  },
  {
    component: 'Input',
    componentProps: {
      placeholder: '请输入发现问题',
      maxlength: 500,
      type: 'textarea',
      showWordLimit: true,
    },
    fieldName: 'foundProblem',
    label: '发现问题',
  },
  {
    component: 'Select',
    componentProps: {
      placeholder: '请选择标签',
      options: getDictOptions('ledger_tag'),
    },
    fieldName: 'tags',
    label: '标签',
  },
  {
    component: 'Input',
    componentProps: {
      placeholder: '请输入处理建议',
      maxlength: 500,
      type: 'textarea',
      showWordLimit: true,
    },
    fieldName: 'suggestion',
    label: '处理建议',
  },
  {
    component: 'Input',
    componentProps: {
      placeholder: '请输入主要危害描述',
      maxlength: 500,
      type: 'textarea',
      showWordLimit: true,
    },
    fieldName: 'mainHarm',
    label: '主要危害描述',
  },
  {
    component: 'Select',
    componentProps: {
      placeholder: '请选择风险度',
      options: [
        {
          label: '低',
          value: 0,
        },
        {
          label: '中',
          value: 1,
        },
        {
          label: '高',
          value: 2,
        },
      ],
    },
    fieldName: 'riskLevel',
    label: '风险度',
  },
  {
    component: 'DatePicker',
    componentProps: {
      type: 'datetime',
      format: 'YYYY-MM-DD HH:mm:ss',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      placeholder: '请选择整改截止时间',
      style: {
        width: '100%',
      },
    },
    fieldName: 'rectifyDeadline',
    label: '整改截止时间',
  },
  {
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
    fieldName: 'problemImages',
  },
  {
    component: 'Upload',
    fieldName: 'problemImagesUrl',
    label: '问题图片',
    componentProps: (row: any) => {
      return {
        ...getFileUploadConfig(
          '.jpg,.jpeg,.png,.gif',
          'picture-card',
          5,
          '/common/upload',
        ),
        onSuccess: (res: any) => {
          // 图片上传成功处理
        },
      };
    },
    renderComponentContent: () => {
      return {
        default: () => '点击上传',
      };
    },
  },
  {
    component: 'ApiSelect',
    componentProps: (form: any) => {
      return {
        placeholder: '请选择执行人',
        api: () => getExecutorList(form.stationId || ''),
        params: {
          stationId: form.stationId || '',
        },
      };
    },
    dependencies: {
      show: () => true,
      triggerFields: ['stationId'],
    },
    fieldName: 'executorId',
    label: '执行人',
  },
  {
    component: 'ApiSelect',
    componentProps: (form: any) => {
      return {
        placeholder: '请选择抄送人',
        showSearch: true,
        multiple: true,
        filterOption: true,
        api: (form: any) => getExecutorList(form.stationId || ''),
        params: {
          stationId: form.stationId || '',
        },
      };
    },
    fieldName: 'ccTo',
    label: '抄送人',
  },
  
];

export const querySchema: FormSchemaGetter = () => [
  {
    component: 'Select',
    componentProps: {
      options: getDictOptions(DictEnum.SYS_NORMAL_DISABLE),
    },
    fieldName: 'inspectGasStation',
    label: '检查油站',
  },
  {
    component: 'DatePicker',
    componentProps: {
      type: 'daterange',
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      startPlaceholder: '开始日期',
      endPlaceholder: '结束日期',
    },
    fieldName: 'checkTime',
    label: '检查时间',
  },
  {
    component: 'Input',
    componentProps: {
      placeholder: '请输入检查名称',
      maxlength: 100,
    },
    fieldName: 'checkName',
    label: '检查名称',
  },
  {
    component: 'Select',
    componentProps: {
      options: getDictOptions(DictEnum.SYS_NORMAL_DISABLE),
    },
    fieldName: 'labelStatus',
    label: '标签',
  },
  {
    component: 'Select',
    componentProps: {
      options: getDictOptions(DictEnum.SYS_NORMAL_DISABLE),
    },
    fieldName: 'executorSelect',
    label: '执行人',
  },
  {
    component: 'DatePicker',
    componentProps: {
      type: 'daterange',
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      startPlaceholder: '开始日期',
      endPlaceholder: '结束日期',
    },
    fieldName: 'processTime',
    label: '处理时间',
  }
];

export const columns: VxeGridProps['columns'] = [
  {
    field: 'action',
    fixed: 'left',
    slots: { default: 'action' },
    resizable: false,
    title: '操作',
    width: 180,
  },
  {
    title: '检查油站',
    field: 'stationName',
    minWidth: 130,
  },
  {
    title: '检查名称',
    field: 'checkName',
    minWidth: 130,
  },
  {
    title: '检查人',
    field: 'createBy',
    minWidth: 130,
  },
  {
    title: '检查时间',
    field: 'createTime',
    minWidth: 130,
  },
  {
    title: '标签',
    field: 'tagsName',
    minWidth: 130,
  },
  {
    title: '发现问题',
    field: 'foundProblem',
    minWidth: 130,
  },
  {
    title: '主要危害描述',
    field: 'mainHarm',
    minWidth: 130,
  },
  {
    title: '风险度',
    field: 'riskLevel',
    formatter: (params: { cellValue: string | number }) => {
      return params.cellValue === 0 ? '低' : params.cellValue === 1 ? '中' : '高';
    },
    minWidth: 130,
  },
  {
    title: '处理建议',
    field: 'suggestion',
    minWidth: 130,
  },
];
export const mockData = [
  {
    inspectGas: '中山路加油站',
    checkName: '油罐安全附件检查',
    Inspector: '王保瑞',
    inspectionTime: '2024-06-01 08:00',
    labeltext: '安全',
    discoverProblem: '液位计读数漂移',
    mainHazardDescription: '计量失准导致溢油风险',
    riskLevel: '中等',
    handlingSuggestions: '立即校准并更换探头',
  },
  {
    inspectGas: '北环大道油站',
    checkName: '消防器材巡检',
    Inspector: '王凯',
    inspectionTime: '2024-06-01 09:15',
    labeltext: '消防',
    discoverProblem: '灭火器压力不足',
    mainHazardDescription: '火灾初期无法扑灭',
    riskLevel: '高',
    handlingSuggestions: '重新充装并记录',
  },
  {
    inspectGas: '城南综合能源站',
    checkName: '加油机滤芯检查',
    Inspector: '李威',
    inspectionTime: '2024-06-01 10:30',
    labeltext: '设备',
    discoverProblem: '滤芯堵塞超标',
    mainHazardDescription: '杂质进入车辆油箱',
    riskLevel: '中等',
    handlingSuggestions: '停机更换滤芯',
  },
  {
    inspectGas: '高速服务区东站',
    checkName: 'POS系统巡检',
    Inspector: '张敏',
    inspectionTime: '2024-06-01 11:45',
    labeltext: '系统',
    discoverProblem: '小票打印机卡纸',
    mainHazardDescription: '交易凭证无法出具',
    riskLevel: '低',
    handlingSuggestions: '清理纸屑并测试',
  },
  {
    inspectGas: '城西快充站',
    checkName: '充电桩接地检测',
    Inspector: '刘洋',
    inspectionTime: '2024-06-01 13:00',
    labeltext: '电气',
    discoverProblem: '接地电阻偏高',
    mainHazardDescription: '漏电触电风险',
    riskLevel: '高',
    handlingSuggestions: '重新打接地极',
  },
];
