import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

import { getDictOptions } from '#/utils/dict';
import { requestClient } from '#/api/request';

// 字典标识常量
const DICT_KEYS = {
  // 交接环节字典标识
  HANDOVER_LINK: 'handover_link',
};

// 获取交接环节字典选项
const getHandoverLinkOptions = () => getDictOptions(DICT_KEYS.HANDOVER_LINK);

// 负责人选项
const principalOptions = [
  { label: '接收人', value: 0 },
  { label: '移交人', value: 1 },
];

// 获取油站列表
const getStationList = async () => {
  const response = await requestClient.get('/sysStation/station/selectAllSysStationList');
  return response.map((item: any) => ({
    label: item.stationName,
    value: item.id,
  }));
};

// 通过stationId获取人员列表
const getPersonList = async (stationId: number) => {
  const response = await requestClient.get('/system/user/stationList', {
    params: { stationId },
  });
  return response.map((item: any) => ({
    label: item.userName,
    value: item.userId,
  }));
};

export const queryFormSchema: FormSchemaGetter = () => [
  {
    component: 'Select',
    fieldName: 'link',
    label: '环节',
    componentProps: {
      placeholder: '请选择环节',
      options: getHandoverLinkOptions(),
    },
  },
  {
    component: 'Input',
    fieldName: 'item',
    label: '事项',
    componentProps: {
      placeholder: '请输入事项',
    },
  },
  {
    component: 'Select',
    fieldName: 'principal',
    label: '负责人',
    componentProps: {
      placeholder: '请选择负责人',
      options: principalOptions,
    },
  },
  {
    component: 'Select',
    fieldName: 'needSupervisor',
    label: '监交人',
    componentProps: {
      placeholder: '请选择是否需要监交人',
      options: [
        { label: '是', value: 1 },
        { label: '否', value: 0 },
      ],
    },
  },
];

// 发起交接表单 schema
export const initiateFormSchema: FormSchemaGetter = (props) => [
  {
    component: 'ApiSelect',
    fieldName: 'stationId',
    label: '交接油站',
    componentProps: {
      api: async () => {
        const resp = await getStationList();
        const data = resp || [];
        return Array.isArray(data)
          ? data.map((item: any) => ({
              label: item.label,
              value: item.value,
            }))
          : [];
      },
      labelField: 'label',
      valueField: 'value',
      placeholder: '请选择交接油站',
      onChange: props?.handleStationChange,
    },
    rules: 'required',
  },
  {
    component: 'Select',
    fieldName: 'handoverPersonId',
    label: '监交人',
    dependencies: {
      show: (values) => !!values.stationId,
      triggerFields: ['stationId'],
    },
    componentProps: {
      placeholder: props?.handoverPersonOptions?.length ? '请选择监交人' : '请先选择交接油站',
      options: props?.handoverPersonOptions || [],
    },
    rules: 'required',
  },
  {
    component: 'DatePicker',
    fieldName: 'handoverStartDate',
    label: '交接开始日期',
    componentProps: {
      placeholder: '请选择交接开始日期',
      type: 'date',
      format: 'YYYY-MM-DD',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      style:{
        width:'100%'
      }
    },
    rules: 'required',
  },
  {
    component: 'InputNumber',
    fieldName: 'handoverDuration',
    label: '线上交接',
    componentProps: {
      placeholder: '请输入线上交接事项建议时长',
      min: 0,
      style: {
        width: '100%',
      },
    },
    rules: 'required',
  },
  {
    component: 'InputNumber',
    fieldName: 'handoverDurationOnSite',
    label: '现场交接',
    componentProps: {
      placeholder: '请输入现场交接事项建议时长',
      min: 0,
      style: {
        width: '100%',
      },
    },
    rules: 'required',
  },
  {
    component: 'InputNumber',
    fieldName: 'handoverDurationAfter',
    label: '交接后续',
    componentProps: {
      placeholder: '请输入交接后续事项建议时长',
      min: 0,
      style: {
        width: '100%',
      },
    },
    rules: 'required',
  },
];

export const tableColumns: VxeGridProps['columns'] = [
  {
    type: 'checkbox',
    width: 80,
  },
  {
    field: 'item',
    title: '事项',
    minWidth: 150,
  },
  {
    field: 'requirementGuide',
    title: '要求/指引',
    minWidth: 200,
  },
  {
    field: 'link',
    title: '环节',
    minWidth: 120,
    formatter: (params: { cellValue: string; column: any; row: any }) => {
      const options = getHandoverLinkOptions();
      const option = options.find((item) => item.value === params.cellValue);
      return option ? option.label : params.cellValue;
    },
  },
  {
    field: 'principal',
    title: '负责人',
    minWidth: 120,
    formatter: (params: { cellValue: number; column: any; row: any }) => {
      const option = principalOptions.find((item) => item.value === params.cellValue);
      return option ? option.label : params.cellValue;
    },
  },
  {
    field: 'needSupervisor',
    title: '监交人',
    minWidth: 150,
    formatter: (params: { cellValue: number; column: any; row: any }) => {
      return params.cellValue === 1 ? '是' : '否';
    },
  },
  {
    field: 'isMandatory',
    title: '是否必做',
    minWidth: 120,
    formatter: (params: { cellValue: number; column: any; row: any }) => {
      return params.cellValue === 1 ? '是' : '否';
    },
  },
  {
    field: 'hasTextBox',
    title: '文本框',
    minWidth: 100,
    formatter: (params: { cellValue: number; column: any; row: any }) => {
      return params.cellValue === 1 ? '是' : '否';
    },
  },
  {
    field: 'hasPhoto',
    title: '拍照',
    minWidth: 100,
    formatter: (params: { cellValue: number; column: any; row: any }) => {
      return params.cellValue === 1 ? '是' : '否';
    },
  },
  {
    field: 'hasAttachment',
    title: '附件',
    minWidth: 100,
    formatter: (params: { cellValue: number; column: any; row: any }) => {
      return params.cellValue === 1 ? '是' : '否';
    },
  },
  {
    field: 'updateTime',
    title: '更新时间',
    minWidth: 180,
  },
  {
    field: 'createBy',
    title: '创建人',
    minWidth: 120,
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 150,
  },
];

export const drawerFormSchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
    fieldName: 'id',
  },
  {
    component: 'Select',
    fieldName: 'link',
    label: '环节',
    componentProps: {
      placeholder: '请选择环节',
      options: getHandoverLinkOptions(),
    },
    rules: 'required',
  },
  {
    component: 'Select',
    fieldName: 'principal',
    label: '负责人',
    componentProps: {
      placeholder: '请选择负责人',
      options: principalOptions,
    },
    rules: 'required',
  },
  {
    component: 'RadioGroup',
    fieldName: 'needSupervisor',
    label: '监交人',
    componentProps: {
      options: [
        { label: '是', value: 1 },
        { label: '否', value: 0 },
      ],
    },
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'item',
    label: '事项',
    componentProps: {
      placeholder: '请输入事项',
      maxlength: 100,
    },
    rules: 'required',
  },
  {
    component: 'RadioGroup',
    fieldName: 'isMandatory',
    label: '是否必做',
    componentProps: {
      options: [
        { label: '是', value: 1 },
        { label: '否', value: 0 },
      ],
    },
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'requirementGuide',
    label: '要求/指引',
    componentProps: {
      placeholder: '请输入要求/指引',
      maxlength: 200,
      type: 'textarea',
      rows: 3,
    },
  },
  {
    component: 'RadioGroup',
    fieldName: 'hasTextBox',
    label: '文本框',
    componentProps: {
      options: [
        { label: '是', value: 1 },
        { label: '否', value: 0 },
      ],
    },
    rules: 'required',
  },
  {
    component: 'RadioGroup',
    fieldName: 'hasPhoto',
    label: '拍照',
    componentProps: {
      options: [
        { label: '是', value: 1 },
        { label: '否', value: 0 },
      ],
    },
    rules: 'required',
  },
  {
    component: 'RadioGroup',
    fieldName: 'hasAttachment',
    label: '附件',
    componentProps: {
      options: [
        { label: '是', value: 1 },
        { label: '否', value: 0 },
      ],
    },
    rules: 'required',
  },
];

