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

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

import { selectAllSysStationAreaList } from '#/api/system/infoEntry/stationInfo/stationInfo';
import { getDictOptions } from '#/utils/dict';
import { renderDict } from '#/utils/render';

// 字典标识常量
const DICT_KEYS = {
  // 竞争关系字典标识
  COMPETITIVE_RELATION: 'competitive_relation',
  // 油站性质字典标识
  GAS_STATION: 'gas_station',
  // 状态字典标识
  STATUS: 'status',
};

// 获取竞争关系字典选项
const getCompetitiveRelationOptions = () =>
  getDictOptions(DICT_KEYS.COMPETITIVE_RELATION);

// 获取油站性质字典选项
const getGasStationOptions = () => getDictOptions(DICT_KEYS.GAS_STATION);

export const queryFormSchema: FormSchemaGetter = () => [
  {
    component: 'ApiSelect',
    fieldName: 'stationId',
    label: '我方油站',
    componentProps: {
      placeholder: '请输入我方油站名称',
      api: selectAllSysStationAreaList,
      labelField: 'stationName',
      valueField: 'id',
    },
  },
  {
    component: 'Input',
    fieldName: 'competitorName',
    label: '竞争油站',
    componentProps: {
      placeholder: '请输入竞争油站名称',
    },
  },
  {
    component: 'Select',
    componentProps: {
      placeholder: '请选择油站性质',
      options: getGasStationOptions(),
    },
    fieldName: 'stationNatureCode',
    label: '油站性质',
  },
  {
    component: 'Select',
    componentProps: {
      placeholder: '请选择竞争关系',
      options: getCompetitiveRelationOptions(),
    },
    fieldName: 'competitiveRelationCode',
    label: '竞争关系',
  },
  {
    component: 'InputNumber',
    fieldName: 'distanceKm',
    label: '距离',
    componentProps: {
      placeholder: '请输入与我方距离',
      min: 0,
      precision: 2,
      style: {
        width: '100%',
      },
    },
  },
  {
    component: 'Select',
    componentProps: {
      placeholder: '请选择状态',
      options: [
        { label: '启用', value: 1 },
        { label: '禁用', value: 2 },
      ],
    },
    fieldName: 'statusCode',
    label: '状态',
  },
];

export const tableColumns: VxeGridProps['columns'] = [
  {
    type: 'checkbox',
    width: 80,
  },
  {
    field: 'stationName',
    title: '我方油站',
    minWidth: 150,
  },
  {
    field: 'competitorName',
    title: '竞争油站',
    minWidth: 150,
  },
  {
    field: 'stationNatureName',
    title: '油站性质',
    minWidth: 120,
  },
  {
    field: 'competitiveRelationName',
    title: '竞争关系',
    minWidth: 120,
  },
  {
    field: 'distanceKm',
    title: '与我方距离(km)',
    minWidth: 120,
    formatter: (params: { cellValue: number; column: any; row: any }) =>
      params.cellValue || '-',
  },
  {
    field: 'competitorAddress',
    title: '竞争油站地址',
    minWidth: 200,
    formatter: (params: { cellValue: string; column: any; row: any }) =>
      params.cellValue || '-',
  },
  {
    field: 'statusCode',
    title: '状态',
    minWidth: 100,
    slots: {
      default: ({ row }) => {
        return renderDict(
          row.statusCode.toString(),
          DictEnum.SYS_DISABLED_STATUS,
        );
      },
    },
  },
  {
    field: 'updateTime',
    title: '操作时间',
    minWidth: 150,
  },
  {
    field: 'updateBy',
    title: '操作人',
    minWidth: 100,
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 150,
  },
];

export const drawerFormSchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
    fieldName: 'id',
  },
  {
    component: 'ApiSelect',
    fieldName: 'stationId',
    label: '我方油站',
    componentProps: {
      placeholder: '请输入油站名称',
      maxlength: 100,
      api: async () => {
        const resp = await selectAllSysStationAreaList();
        const data = resp || [];
        return Array.isArray(data)
          ? data.map((item: any) => ({
              label: item.stationName,
              value: item.id.toString(),
            }))
          : [];
      },
    },
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'competitorName',
    label: '竞争油站名称',
    componentProps: {
      placeholder: '请输入竞争油站名称',
      maxlength: 100,
    },
    help: '请准确填写油站所属企业及油站名称',
    rules: 'required',
  },
  {
    component: 'Select',
    componentProps: {
      placeholder: '请选择竞争关系',
      options: getCompetitiveRelationOptions(),
    },
    fieldName: 'competitiveRelationCode',
    label: '竞争关系',
    rules: 'required',
  },
  {
    component: 'Select',
    componentProps: {
      placeholder: '请选择油站性质',
      options: getGasStationOptions(),
    },
    fieldName: 'stationNatureCode',
    label: '油站性质',
    rules: 'required',
  },
  {
    component: 'InputNumber',
    fieldName: 'distanceKm',
    label: '与我方距离(km)',
    componentProps: {
      placeholder: '请输入与我方距离',
      min: 0,
      // precision: 2,
      style: { width: '100%' },
    },
    rules: 'required',
  },
  {
    component: 'Input',
    fieldName: 'competitorAddress',
    label: '竞争油站地址',
    componentProps: {
      placeholder: '请输入竞争油站地址',
      maxlength: 200,
    },
  },
  {
    component: 'Input',
    fieldName: 'latitude',
    label: '竞争油站坐标纬度',
    componentProps: {
      placeholder: '请输入纬度',
      // min: -90,
      // max: 90,
      // precision: 8,
      style: { width: '100%' },
    },
  },
  {
    component: 'Input',
    fieldName: 'longitude',
    label: '竞争油站坐标经度',
    componentProps: {
      placeholder: '请输入经度',
      // min: -180,
      // max: 180,
      // precision: 8,
      style: { width: '100%' },
    },
  },
  {
    component: 'RadioGroup',
    componentProps: {
      options: getDictOptions(DictEnum.SYS_DISABLED_STATUS),
      isButton: true,
    },
    fieldName: 'statusCode',
    defaultValue: '1',
    label: '状态',
  },
];
