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',
  commonConfig: {
    formItemClass: 'col-span-12',
    componentProps: {
      class: 'w-full',
    },
  },
  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) => {
          return option?.label.toLowerCase().indexOf(input.toLowerCase()) >= 0;
        },
        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: 'drug_number',
      formItemClass: 'col-span-6',
      label: '产品编号',
      rules: 'required',
    },
    {
      component: 'VbenInput',
      componentProps: {
        placeholder: '请输入规格',
      },
      fieldName: 'specification',
      formItemClass: 'col-span-6',
      label: '规格',
      rules: 'required',
    },
    {
      component: 'VbenSelect',
      componentProps: {
        options: [],
        placeholder: '请选择',
      },
      fieldName: 'unit_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: 'Textarea',
      componentProps: {
        placeholder: '请输入产品功能描述',
      },
      fieldName: 'function',
      label: '产品功能',
    },
    {
      component: 'Avatar',
      fieldName: 'instruction',
      label: '产品说明书',
      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',
  commonConfig: {
    formItemClass: 'col-span-12',
    componentProps: {
      class: 'w-full',
    },
  },
  layout: 'horizontal',
  schema: [
    {
      component: 'Upload',
      componentProps: {
        placeholder: '请选择',
        multiple: true,
      },
      fieldName: 'frequency_id',
      formItemClass: 'col-span-12',
      label: '导入excel',
      rules: 'file',
    },
  ],
  showDefaultActions: false,
};

