import { verifyString, verifyNumber, verifyListItem } from '@/utils/index'
import frequencyList from '@/baseData/frequency.js';
import usageList from '@/baseData/usage.js';
import unitList from '@/baseData/unit';
import categoryList from '@/baseData/drug-category';
import dosageUnitList from '@/baseData/dosageUnit.js';

export const drugFieldDict = [
  { name: '药品名称', field: 'name', required: true, verify: (val, label) => verifyString(val, label, 1) },
  { name: '药品规格', field: 'specification', required: true, verify: (val, label) => verifyString(val, label, 1) },
  { name: '包装单位', field: 'unit', required: true, verify: (val, label) => verifyListItem(val, unitList, true, label) },
  { name: '包装量', field: 'package_amount', verify: (val, label) => validateNumber(val, label, 2) },
  { name: '生产厂家', field: 'manufacturer', verify: (val, label) => verifyString(val, label) },
  { name: '商品条形码', field: 'barcode', verify: (val, label) => verifyString(val, label) },
  { name: '医保编码', field: 'insurance_code', required: true, verify: (val, label) => verifyString(val, label, 1) },
  { name: 'ERP编码', field: 'his_drug_code', verify: (val, label) => verifyString(val, label) },
  // 放宽分类校验，允许任意字符串；前端选择仍可用固定下拉，导入/编辑不会因为新分类报错
  { name: '药品分类', field: 'category', verify: (val, label) => verifyString(val, label) },
  { name: '药品拼音码', field: 'pinyin_code', verify: (val, label) => verifyString(val, label) },
  { name: '药库货品ID', field: 'product_id', verify: (val, label) => validateNumber(val, label) },
  { name: '通用名', field: 'generic_name', verify: (val, label) => verifyString(val, label) },
  { name: '通用名拼音码', field: 'generic_pinyin', verify: (val, label) => verifyString(val, label) },
  // 仅在线药品库使用：管理分类（处方药 / OTC）与剂型
  { name: '管理分类', field: 'manage_category', verify: (val, label) => verifyString(val, label) },
  { name: '剂型', field: 'dosage_form', verify: (val, label) => verifyString(val, label) },
  { name: '适用人群', field: 'apply_people', verify: (val, label) => verifyListItem(val, ['通用', '男', '女', '儿童'], false, label) },

  { name: '单次标准剂量', field: 'dosage', verify: (val, label) => validateNumber(val, label, 2) },
  { name: '剂量单位', field: 'dosage_unit', verify: (val, label) => verifyListItem(val, dosageUnitList, false, label) },
  { name: '给药标准频次', field: 'frequency', verify: (val, label) => verifyListItem(val, frequencyList.map(i => i.name), false, label) },
  { name: '给药标准方式', field: 'administration_method', verify: (val, label) => verifyListItem(val, usageList.map(i => i.name), false, label) },
  { name: '用药标准天数', field: 'days', verify: (val, label) => validateNumber(val, label) },
  { name: '发药标准数量', field: 'recommended_quantity', verify: (val, label) => validateNumber(val, label) },
  // 在线药品库扩展说明字段：用药证明 / 使用限制说明
  { name: '用药证明', field: 'medication_proof_desc', verify: (val, label) => verifyString(val, label) },
  { name: '使用限制说明', field: 'usage_restriction_desc', verify: (val, label) => verifyString(val, label) },
]

function validateNumber(val, label, len = 0, required = false) {
  if (!val && val !== 0 && !required) return [true, ''];
  return verifyNumber(val, label, len)
}

const templateData = drugFieldDict.reduce((obj, item) => {
  obj[item.name] = ''
  return obj
}, {})

export const templateList = [templateData];

/**
 * 获取药品数据 
 * @param {*} arr 药品列表
 * @param {*} keyProp 药品检验字段名称 excel读取的用中文名称校验 数据库读取的用字段名称校验
 * @param {*} verifyEveryField 是否验证每个药品字段 新增药品需要验证 编辑药品不需要验证
 * @returns Array<drug>
 */
export function getDrugList(arr, keyProp = 'name', verifyEveryField = true, libType = 'hlw') {
  return arr.map(drug => {

    return drugFieldDict.reduce((obj, item) => {
      const key = item[keyProp];

      if (!verifyEveryField && !(key in drug)) {
        return obj
      }
      const val = typeof drug[key] === 'string' ? drug[key].trim() : drug[key];

      let pass, res
      if (item.field === 'his_drug_code') {
        const required = libType === 'online'
          ;[pass, res] = verifyString(val, item.name, required ? 1 : 0)
      } else {
        ;[pass, res] = item.verify(val, item.name)
      }

      if (pass) {
        obj[item.field] = res
      } else {
        obj[item.field] = val;
        obj.msgs.push(res)
      }
      return obj
    }, { msgs: [] })
  })
}
