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

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

export const queryFormSchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    fieldName: 'licenseName',
    label: '证照名称',
    componentProps: {
      placeholder: '请输入证照名称',
    },
  },
  {
    component: 'Select',
    fieldName: 'annualReviewToTask',
    label: '年审转任务',
    componentProps: {
      placeholder: '请选择年审转任务',
      clearable: true,
      options: [
        { label: '是', value: 1 },
        { label: '否', value: 0 },
      ],
    },
  },
  {
    component: 'Select',
    fieldName: 'renewToTask',
    label: '换证转任务',
    componentProps: {
      placeholder: '请选择换证转任务',
      clearable: true,
      options: [
        { label: '是', value: 1 },
        { label: '否', value: 0 },
      ],
    },
  },
];

export const tableColumns: VxeGridProps['columns'] = [
  {
    type: 'checkbox',
    width: 80,
  },
  {
    field: 'licenseName',
    title: '证照名称',
    minWidth: 150,
  },
  {
    field: 'annualReviewToTask',
    title: '年审是否转任务',
    minWidth: 120,
    formatter: (params: { cellValue: number }) => {
      return params.cellValue === 1 ? '是' : '否';
    },
  },
  {
    field: 'renewToTask',
    title: '换证是否转任务',
    minWidth: 120,
    formatter: (params: { cellValue: number }) => {
      return params.cellValue === 1 ? '是' : '否';
    },
  },
  {
    field: 'renewFrequency',
    title: '换证频率',
    minWidth: 120,
   formatter: (params: { cellValue: string; column: any; row: any }) => {
      const options =getDictOptions('renew_frequency');
      const option = options.find((item) => item.value === params.cellValue);
      return option ? option.label : params.cellValue;
    },
  },
  {
    field: 'renewAdvanceMonths',
    title: '换证提前时间/月',
    minWidth: 120,
  },
  {
    field: 'renewRequirements',
    title: '更换要求',
    minWidth: 200,
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 150,
  },
];

export const drawerFormSchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
    fieldName: 'id',
  },
  {
    component: 'Input',
    fieldName: 'licenseName',
    label: '证照名称',
    labelWidth: 120,
    componentProps: {
      placeholder: '请输入证照名称',
      maxlength: 100,
    },
    rules: 'required',
  },
  {
    component: 'RadioGroup',
    fieldName: 'annualReviewToTask',
    label: '年审转任务',
    labelWidth: 120,
    componentProps: {
      options: [
        { label: '是', value: 1 },
        { label: '否', value: 0 },
      ],
    },
    rules: 'required',
  },
  {
    component: 'Select',
    fieldName: 'taskStartMonth',
    label: '任务开始时间',
    labelWidth: 120,
    dependencies: {
      show: (values) => values.annualReviewToTask === 1,
      triggerFields: ['annualReviewToTask'],
    },
    componentProps: {
      placeholder: '请选择任务开始时间',
      options: Array.from({ length: 12 }, (_, i) => ({
        label: `${i + 1}月`,
        value: i + 1,
      })),
    },
  },
  {
    component: 'Select',
    fieldName: 'taskEndMonth',
    label: '任务截止时间',
    labelWidth: 120,
    dependencies: {
      show: (values) => values.annualReviewToTask === 1,
      triggerFields: ['annualReviewToTask'],
    },
    componentProps: {
      placeholder: '请选择任务截止时间',
      options: Array.from({ length: 12 }, (_, i) => ({
        label: `${i + 1}月`,
        value: i + 1,
      })),
    },
  },
  {
    component: 'RadioGroup',
    fieldName: 'renewToTask',
    label: '换证转任务',
    labelWidth: 120,
    componentProps: {
      options: [
        { label: '是', value: 1 },
        { label: '否', value: 0 },
      ],
    },
    rules: 'required',
  },
  {
    component: 'Select',
    componentProps: {
      placeholder: '请选择换证频率',
      options: getDictOptions('renew_frequency'),
    },
    dependencies: {
      show: (values) => values.renewToTask === 1,
      triggerFields: ['renewToTask'],
    },
    fieldName: 'renewFrequency',
    label: '换证频率',
    labelWidth: 120,
  },
  {
    component: 'InputNumber',
    dependencies: {
      show: (values) => values.renewToTask === 1,
      triggerFields: ['renewToTask'],
    },
    fieldName: 'renewAdvanceMonths',
    label: '换证提前时间/月',
    labelWidth: 120,
    componentProps: {
      placeholder: '请输入换证提前时间（月）',
      min: 0,
      style: {
        width: '100%',
      },
    },
  },
  {
    component: 'ApiSelect',
    fieldName: 'taskSource',
    label: '任务来源',
    labelWidth: 120,
    componentProps: {
      placeholder: '请选择任务来源',
      api: async () => {
        const resp: any = await requestClient.get('/system/user/allList');
        return Array.isArray(resp)
          ? resp.map((item: any) => ({
              label: item.nickName || item.userName,
              value: item.userId.toString(),
            }))
          : [];
      },
      filterable: true,
      labelField: 'label',
      valueField: 'value',
    },
  },
  {
    component: 'Input',
    fieldName: 'renewRequirements',
    label: '更换要求',
    labelWidth: 120,
    componentProps: {
      placeholder: '请输入更换要求',
      maxlength: 200,
      type: 'textarea',
      rows: 3,
    },
  },
];

