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

import {
  getDoctorOptionApi,
  getStoreOption,
} from '#/views/system/store/api';

/**
 * 审方页统一搜索表单（列表 / 卡片共用）
 */
export const formOptions: VbenFormProps = {
  collapsed: true,
  compact: true,
  commonConfig: {
    labelWidth: 80,
    componentProps: {
      class: 'w-full',
    },
  },
  wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
  schema: [
    {
      component: 'VbenInput',
      componentProps: {
        allowClear: true,
        placeholder: '处方订单号',
      },
      fieldName: 'prescription_no',
      label: '订单号',
    },
    {
      component: 'VbenInput',
      componentProps: {
        allowClear: true,
        placeholder: '就诊人姓名（模糊）',
      },
      fieldName: 'patient_name',
      label: '患者',
    },
    {
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        showSearch: true,
        filterOption: true,
        afterFetch: (data: { id: number; name: string }[]) => {
          return (data || []).map((item: any) => ({
            label: `${item.name}【${item.id}】`,
            value: item.id,
          }));
        },
        api: getStoreOption,
        placeholder: '请选择诊所',
      },
      fieldName: 'store_id',
      label: '诊所',
    },
    {
      component: 'ApiSelect',
      componentProps: {
        allowClear: true,
        showSearch: true,
        filterOption: true,
        afterFetch: (data: { id: number; name: string }[]) => {
          return (data || []).map((item: any) => ({
            label: item.name,
            value: item.id,
          }));
        },
        api: () => Promise.resolve([]),
        placeholder: '请先选择诊所',
        disabled: true,
      },
      fieldName: 'su_id',
      label: '医生',
      dependencies: {
        triggerFields: ['store_id'],
        componentProps: (values: Record<string, any>) => {
          const storeId = values?.store_id;
          if (!storeId) {
            return {
              api: () => Promise.resolve([]),
              placeholder: '请先选择诊所',
              disabled: true,
            };
          }
          return {
            api: () => getDoctorOptionApi(storeId),
            placeholder: '请选择医生',
            disabled: false,
          };
        },
        trigger: (values: Record<string, any>, formApi: any) => {
          // 诊所变更时清空已选医生，避免跨店脏值
          formApi?.setFieldValue?.('su_id', undefined);
        },
      },
    },
    {
      component: 'VbenSelect',
      componentProps: {
        allowClear: true,
        options: [
          { label: '线下就诊', value: 0 },
          { label: '在线问诊', value: 1 },
          { label: '在线复诊', value: 2 },
        ],
        placeholder: '请选择来源',
      },
      fieldName: 'is_online',
      label: '处方来源',
    },
    {
      component: 'VbenSelect',
      componentProps: {
        allowClear: true,
        options: [
          { label: '中药', value: 1 },
          { label: '西药', value: 2 },
          { label: '中成药', value: 3 },
          { label: '产品服务包', value: 5 },
        ],
        placeholder: '请选择类型',
      },
      fieldName: 'prescription_type',
      label: '药品类型',
    },
    {
      component: 'RangePicker',
      componentProps: {
        format: 'YYYY-MM-DD',
        class: 'w-full',
      },
      fieldName: 'search_time',
      label: '时间范围',
    },
  ],
  showCollapseButton: true,
  submitButtonOptions: {
    content: '查询',
  },
  resetButtonOptions: {
    content: '重置',
  },
  submitOnEnter: true,
};
