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

import { getSupplierOption } from '#/views/system/supplier/api';

import { getDeliveryWarehouseProductTypeOptions } from '../api';

/**
 * 仓侧建品弹窗表单
 * 不填药品编号/ERP倍数/分区（后端默认）；必填报价供审核绑仓
 */
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'],
      },
    },
    {
      // 隐藏：驱动 status 显隐（audit_status==1 才可改上下架）
      component: 'VbenInput',
      fieldName: 'audit_status',
      label: '审核状态',
      formItemClass: 'col-span-6',
      dependencies: {
        show: false,
        triggerFields: ['audit_status'],
      },
    },
    {
      component: 'ApiSelect',
      componentProps: {
        allowClear: false,
        showSearch: true,
        filterOption: (input: string, option: any) =>
          option?.label?.toLowerCase?.().indexOf(input.toLowerCase()) >= 0,
        afterFetch: (data: { label: string; value: number }[]) =>
          (data || []).map((item) => ({
            label: item.label,
            value: item.value,
          })),
        api: getDeliveryWarehouseProductTypeOptions,
        placeholder: '请选择商品类型',
      },
      fieldName: 'type',
      formItemClass: 'col-span-6',
      label: '商品类型',
      rules: 'selectRequired',
    },
    {
      component: 'Avatar',
      fieldName: 'image',
      label: '产品图片',
      rules: 'required',
      formItemClass: 'col-span-6',
    },
    {
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        showSearch: true,
        filterOption: (input: string, option: any) =>
          option?.label?.toLowerCase?.().indexOf(input.toLowerCase()) >= 0,
        afterFetch: (data: { id: number; name: string }[]) =>
          (data || []).map((item: any) => ({
            label: item.name,
            value: item.id,
          })),
        api: getSupplierOption,
        placeholder: '请选择',
      },
      fieldName: 'supplier_id',
      formItemClass: 'col-span-6',
      label: '所属供应商',
      rules: 'required',
    },
    {
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入报价',
        min: 0.0001,
        precision: 4,
        class: 'w-full',
      },
      fieldName: 'upload_quote',
      formItemClass: 'col-span-6',
      label: '报价',
      help: '审核通过后将按此报价绑定到本配送仓',
      rules: 'required',
    },
    {
      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: 'specification',
      formItemClass: 'col-span-6',
      label: '规格',
      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',
    },
    {
      // 新建不展示；未审核(audit_status==0)不展示；已审核可选草稿/下架/上架
      component: 'RadioGroup',
      componentProps: {
        options: [
          { label: '草稿', value: 1 },
          { label: '下架', value: 2 },
          { label: '上架', value: 3 },
        ],
      },
      fieldName: 'status',
      formItemClass: 'col-span-12',
      label: '商品状态',
      dependencies: {
        show(values) {
          return !!values?.id && Number(values?.audit_status) === 1;
        },
        triggerFields: ['id', 'audit_status'],
      },
    },
    {
      component: 'RadioGroup',
      componentProps: {
        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: '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,
};
