import type { VbenFormProps } from '#/adapter/form';

import { getProcessOptionPidApi } from '#/views/system/process/api';
import {
  CALC_METHOD_OPTIONS,
  UNIT_OPTIONS,
} from '#/views/system/process/constants';

export const modalFormProps: VbenFormProps = {
  wrapperClass: 'grid-cols-12',
  commonConfig: {
    formItemClass: 'col-span-12',
    componentProps: {
      class: 'w-full',
    },
  },
  layout: 'horizontal',
  schema: [
    {
      component: 'VbenInput',
      fieldName: 'id',
      label: 'ID',
      dependencies: {
        show: false,
        triggerFields: ['id'],
      },
    },
    {
      component: 'VbenInput',
      fieldName: 'node_type',
      label: '节点类型',
      dependencies: {
        show: false,
        triggerFields: ['node_type'],
      },
    },
    {
      component: 'VbenInput',
      fieldName: 'formLevel',
      label: '表单层级',
      dependencies: {
        show: false,
        triggerFields: ['formLevel'],
      },
    },
    {
      component: 'VbenInput',
      fieldName: 'rule_id',
      label: '所属煎法',
      dependencies: {
        show: false,
        triggerFields: ['rule_id'],
      },
    },
    {
      component: 'VbenInput',
      fieldName: 'real_id',
      label: '真实ID',
      dependencies: {
        show: false,
        triggerFields: ['real_id'],
      },
    },
    {
      component: 'VbenInput',
      fieldName: 'has_children',
      label: '是否有子级',
      dependencies: {
        show: false,
        triggerFields: ['has_children'],
      },
    },
    {
      component: 'VbenInput',
      fieldName: 'level_label',
      label: '级别',
      componentProps: {
        disabled: true,
      },
      dependencies: {
        show(values) {
          return [2, 3].includes(Number(values.formLevel));
        },
        trigger(values, form) {
          const labels: Record<number, string> = {
            2: '煎法',
            3: '备注',
          };
          form.setFieldValue(
            'level_label',
            labels[Number(values.formLevel)] ?? '',
          );
        },
        triggerFields: ['formLevel'],
      },
    },
    {
      component: 'VbenInput',
      componentProps: {
        placeholder: '请输入名称',
      },
      fieldName: 'name',
      label: '名称',
      dependencies: {
        show(values) {
          return Number(values.formLevel) !== 3;
        },
        triggerFields: ['formLevel'],
      },
    },
    {
      component: 'VbenInput',
      componentProps: {
        placeholder: '请输入备注名称',
      },
      fieldName: 'note',
      label: '名称',
      dependencies: {
        show(values) {
          return Number(values.formLevel) === 3;
        },
        triggerFields: ['formLevel'],
      },
    },
    {
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入毫升',
        class: 'w-full',
        min: 1,
      },
      fieldName: 'volume',
      label: '毫升',
      dependencies: {
        show(values) {
          return Number(values.formLevel) === 3;
        },
        triggerFields: ['formLevel'],
      },
    },
    {
      component: 'RadioGroup',
      componentProps: {
        optionType: 'button',
        buttonStyle: 'solid',
        options: CALC_METHOD_OPTIONS.map((item) => ({
          label: item.label,
          value: item.value,
        })),
      },
      fieldName: 'calc_method',
      label: '计算方式',
      dependencies: {
        show(values) {
          return Number(values.formLevel) === 2;
        },
        triggerFields: ['formLevel'],
      },
    },
    {
      component: 'ApiSelect',
      componentProps: {
        placeholder: '请选择父级制剂',
        api: getProcessOptionPidApi,
        filterOption: (input: string, option: any) =>
          option?.label.toLowerCase().indexOf(input.toLowerCase()) >= 0,
        showSearch: true,
        afterFetch: (data: { id: number; name: string }[]) => {
          return data.map((item: any) => ({
            label: item.name,
            value: item.id,
          }));
        },
      },
      fieldName: 'pid',
      label: '父级制剂',
      dependencies: {
        show(values) {
          return Number(values.formLevel) === 2;
        },
        triggerFields: ['formLevel'],
      },
    },
    {
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入计算价格',
        class: 'w-full',
        min: 0,
      },
      fieldName: 'price',
      label: '计算价格',
      dependencies: {
        show(values) {
          return Number(values.formLevel) === 2;
        },
        triggerFields: ['formLevel'],
      },
    },
    {
      component: 'RadioGroup',
      componentProps: {
        optionType: 'button',
        buttonStyle: 'solid',
        options: UNIT_OPTIONS.map((unit) => ({
          label: unit,
          value: unit,
        })),
      },
      fieldName: 'unit',
      label: '计算单位',
      dependencies: {
        show(values) {
          return Number(values.formLevel) === 2;
        },
        triggerFields: ['formLevel'],
      },
    },
  ],
  showDefaultActions: false,
};
