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

import {getSupplierOption} from '#/views/system/supplier/api';
import {getDrugCategoriesTree, getZoneOptions} from '#/views/business/product/drug-categories/api';


export const modalFormProps: VbenFormProps = {
  wrapperClass: 'grid-cols-12', // 24栅格,
  commonConfig: {
    formItemClass: 'col-span-12',
    // 所有表单项
    componentProps: {
      class: 'w-full',
    },
  },
  // handleSubmit: onSubmit,
  layout: 'horizontal',
  schema: [
    {
      component: 'VbenInput',
      fieldName: 'id',
      label: 'ID',
      formItemClass: 'col-span-6',
      dependencies: {
        show: false,
        triggerFields: ['id'],
      },
    },
    {
      component: 'VbenInput',
      fieldName: 'showSelect',
      label: 'ID',
      formItemClass: 'col-span-6',
      dependencies: {
        show: false,
        triggerFields: ['id'],
      },
    },
    {
      component: 'Avatar',
      fieldName: 'image',
      label: '产品图片',
      rules: 'required',
      formItemClass: 'col-span-6',
    },
    {
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        showSearch: true,
        filterOption: (input: string, option: any) => {
          // 自定义过滤逻辑，确保可以根据 name 进行搜索
          return option?.label.toLowerCase().indexOf(input.toLowerCase()) >= 0;
        },
        // 菜单接口转options格式
        afterFetch: (data: { id: number; name: string }[]) => {
          return data.map((item: any) => ({
            label: item.name,
            value: item.id,
          }));
        },
        api: getSupplierOption,
        placeholder: '请选择',
      },
      fieldName: 'supplier_id',
      formItemClass: 'col-span-6',
      label: '所属供应商',
      rules: 'required',
    },
    {
      component: 'ApiSelect',
      componentProps: {
        api: getZoneOptions,
        labelField: 'title',
        valueField: 'id',
        placeholder: '请选择分区',
        allowClear: true,
      },
      fieldName: 'zone_id',
      formItemClass: 'col-span-6',
      label: '所属分区',
    },
    {
      component: 'ApiTreeSelect',
      componentProps: {
        api: getDrugCategoriesTree,
        childrenField: 'children',
        labelField: 'category_name',
        valueField: 'id',
        placeholder: '请先选择分区',
        allowClear: true,
        treeDefaultExpandAll: true,
        immediate: false,  // 首次不立即加载
        alwaysLoad: true,  // 每次展开都重新加载
      },
      fieldName: 'category_id',
      formItemClass: 'col-span-6',
      label: '所属分类',
      dependencies: {
        componentProps(values, formApi) {
          return {
            // 使用 beforeFetch 在请求发出前动态获取最新的 zone_id
            beforeFetch: async () => {
              const currentValues = await formApi.getValues();
              return { zone_id: currentValues.zone_id };
            },
            placeholder: values.zone_id ? '请选择分类' : '请先选择分区',
          };
        },
        trigger(values, formApi) {
          // zone_id 变化时清空分类选择
          formApi.setFieldValue('category_id', undefined);
        },
        triggerFields: ['zone_id'],
      },
    },
    {
      component: 'VbenInput',
      componentProps: {
        placeholder: '请输入药品昵称',
      },
      fieldName: 'drug_name',
      formItemClass: 'col-span-6',
      label: '药品昵称',
      rules: 'required',
    },
    {
      component: 'VbenInput',
      componentProps: {
        placeholder: '请输入药品别名',
      },
      fieldName: 'drug_alias',
      formItemClass: 'col-span-6',
      label: '药品别名',
      rules: 'required',
    },
    {
      component: 'VbenInput',
      componentProps: {
        placeholder: '可留空，保存时按药名自动生成',
      },
      fieldName: 'pinyin_simple',
      formItemClass: 'col-span-6',
      label: '拼音首拼',
    },
    {
      component: 'VbenInput',
      componentProps: {
        placeholder: '可留空，保存时按药名自动生成',
      },
      fieldName: 'pinyin_full',
      formItemClass: 'col-span-6',
      label: '拼音全拼',
    },
    {
      component: 'VbenInput',
      componentProps: {
        placeholder: '可留空，保存时从药名提取数字',
      },
      fieldName: 'search_digits',
      formItemClass: 'col-span-6',
      label: '检索数字',
    },
    {
      component: 'VbenInput',
      componentProps: {
        placeholder: '请输入药品编号',
      },
      fieldName: 'drug_number',
      formItemClass: 'col-span-6',
      label: '药品编号',
      rules: 'required',
    },
    {
      component: 'VbenInput',
      componentProps: {
        placeholder: '请输入规格',
      },
      fieldName: 'specification',
      formItemClass: 'col-span-6',
      label: '规格',
      rules: 'required',
    },
    {
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入ERP抓取数量倍数',
        min: 0.01,
        precision: 2,
        step: 1,
        class: 'w-full',
      },
      defaultValue: 1,
      fieldName: 'erp_qty_factor',
      formItemClass: 'col-span-6',
      label: 'ERP抓取数量倍数',
      help: 'ERP拉取订单时，数量=订单数量×该倍数，默认1',
      rules: 'required',
    },
    {
      component: 'VbenSelect',
      componentProps: {
        options: [],
        placeholder: '请选择',
      },
      fieldName: 'time_id',
      formItemClass: 'col-span-6',
      label: '使用时间',
      rules: 'required',
    },
    {
      component: 'VbenSelect',
      componentProps: {
        options: [],
        placeholder: '请选择',
      },
      fieldName: 'type_id',
      formItemClass: 'col-span-6',
      label: '使用方法',
      rules: 'required',
    },
    {
      component: 'VbenSelect',
      componentProps: {
        options: [],
        placeholder: '请选择',
      },
      fieldName: 'unit_id',
      formItemClass: 'col-span-6',
      label: '单位',
      rules: 'required',
    },
    {
      component: 'VbenSelect',
      componentProps: {
        options: [],
        placeholder: '请选择',
      },
      fieldName: 'frequency_id',
      formItemClass: 'col-span-6',
      label: '频率',
      rules: 'required',
    },
    {
      component: 'RadioGroup',
      componentProps: {
        placeholder: '请选择',
        options: [
          {
            label: '草稿',
            value: 1,
          },
          {
            label: '下架',
            value: 2,
          },
          {
            label: '上架',
            value: 3,
          },
        ],
      },
      defaultValue: 1,
      formItemClass: 'col-span-12',
      fieldName: 'status',
      label: '商品状态',
      rules: 'required',
    },
    {
      component: 'RadioGroup',
      componentProps: {
        placeholder: '请选择',
        options: [
          {
            label: '是',
            value: 0,
          },
          {
            label: '否',
            value: 1,
          },
        ],
      },
      defaultValue: 1,
      formItemClass: 'col-span-12',
      fieldName: 'is_otc',
      label: '是否处方药',
      rules: 'required',
    },
    {
      component: 'Textarea',
      componentProps: {
        placeholder: '请输入药品主治功能',
      },
      fieldName: 'function',
      label: '药品主治功能',
      rules: 'required',
    },
    // {
    //   component: 'Textarea',
    //   componentProps: {
    //     placeholder: '请输入对应症状',
    //   },
    //   fieldName: 'indications',
    //   label: '对应症状',
    //   rules: 'required',
    // },
    {
      component: 'InputSelect',
      componentProps: {
        placeholder: '请输入对应症状',
      },
      fieldName: 'indications',
      label: '对应症状',
      rules: 'required',
    },
    {
      component: 'Avatar',
      fieldName: 'instruction',
      label: '产品说明书',
      rules: 'required',
      formItemClass: 'col-span-6',
    },
    {
      component: 'UploadImageSortable',
      fieldName: 'introduction_images',
      label: '商品介绍图',
      formItemClass: 'col-span-12',
      componentProps: {
        maxCount: 20,
        multiple: true,
      },
    },
  ],
  showDefaultActions: false,
};
export const uploadExcelProps: VbenFormProps = {
  wrapperClass: 'grid-cols-12', // 24栅格,
  commonConfig: {
    formItemClass: 'col-span-12',
    // 所有表单项
    componentProps: {
      class: 'w-full',
    },
  },
  // handleSubmit: onSubmit,
  layout: 'horizontal',
  schema: [
    {
      component: 'Upload',
      componentProps: {
        placeholder: '请选择',
        multiple: true,
      },
      fieldName: 'frequency_id',
      formItemClass: 'col-span-12',
      label: '导入excel',
      rules: 'file',
    },
  ],
  showDefaultActions: false,
};
