<template>
  <view class="container" style="paddingBottom:1rpx;">
    <view class="section">
      <view class="section-title">{{ title}}</view>
      <view class="form-view">
        <u--form labelPosition="left" :model="form" :rules="rules" ref="uForm" label-width="auto"
          :labelStyle="{ color: '#333333', fontSize: '30rpx' }">
          <u-form-item label="药品全称" prop="name" required >
              <u--input v-model="form.name"  placeholder="请填写药品全称"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="药品简称" prop="shortName" required >
              <u--input v-model="form.shortName"  placeholder="请填写药品简称"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="品牌" prop="brand" required >
              <u--input v-model="form.brand"  placeholder="请填写品牌"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="药品包装" prop="packaging" required >
              <u--input v-model="form.packaging"  placeholder="请填写药品包装"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="生产厂家" prop="manufacturers"  >
              <u--input v-model="form.manufacturers"  placeholder="请填写生产厂家"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
        
            <u-form-item label="药品编码" prop="code"  >
              <u--input v-model="form.code"  placeholder="请填写药品编码"
                inputAlign="right" border="none"></u--input>
            </u-form-item>

          <u-form-item label="药品剂型" prop="dosageFormName"  @click="handleDosageForm">
              <u--input v-model="form.dosageFormName" disabled disabledColor="#ffffff" placeholder="请选择药品剂型"
                inputAlign="right" border="none"></u--input>
              <u-icon slot="right" name="arrow-down"></u-icon>
            </u-form-item>
            <u-form-item label="治疗类型" prop="treatmentTypeName"  @click="handleTreatmentType">
              <u--input v-model="form.treatmentTypeName" disabled disabledColor="#ffffff" placeholder="请选择治疗类型"
                inputAlign="right" border="none"></u--input>
              <u-icon slot="right" name="arrow-down"></u-icon>
            </u-form-item>
            <u-form-item label="药品分类"  prop="classificationName"  @click="handleClassification">
              <u--input v-model="form.classificationName" disabled disabledColor="#ffffff" placeholder="请选择药品分类"
                inputAlign="right" border="none"></u--input>
              <u-icon slot="right" name="arrow-down"></u-icon>
            </u-form-item>

            <u-form-item label="类别" prop="categoryName"  @click="handleCategory">
              <u--input v-model="form.categoryName" disabled disabledColor="#ffffff" placeholder="请选择类别"
                inputAlign="right" border="none"></u--input>
              <u-icon slot="right" name="arrow-down"></u-icon>
            </u-form-item>
            <u-form-item label="包装单位" prop="packageUnitName"  @click="handlePackageUnit">
              <u--input v-model="form.packageUnitName" disabled disabledColor="#ffffff" placeholder="请选择包装单位"
                inputAlign="right" border="none"></u--input>
              <u-icon slot="right" name="arrow-down"></u-icon>
            </u-form-item>
        
            <u-form-item label="是否进口" prop="isImportName"  @click="handleIsImport">
              <u--input v-model="form.isImportName" disabled disabledColor="#ffffff" placeholder="请选择是否进口"
                inputAlign="right" border="none"></u--input>
              <u-icon slot="right" name="arrow-down"></u-icon>
            </u-form-item>
            <u-form-item label="规格" prop="specifications" >
              <u--input v-model="form.specifications"  placeholder="请填写规格"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="规格单位" prop="unitName"  @click="handleUnit">
              <u--input v-model="form.unitName" disabled disabledColor="#ffffff" placeholder="请选择规格单位"
                inputAlign="right" border="none"></u--input>
              <u-icon slot="right" name="arrow-down"></u-icon>
            </u-form-item>
            <u-form-item label="单个含量" prop="content"  >
              <u--input v-model="form.content"  placeholder="请填写单个含量"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="含量单位" prop="contentUnitName"  @click="handleContentUnit">
              <u--input v-model="form.contentUnitName" disabled disabledColor="#ffffff" placeholder="请选择含量单位"
                inputAlign="right" border="none"></u--input>
              <u-icon slot="right" name="arrow-down"></u-icon>
            </u-form-item>
            <u-form-item label="生产地址" prop="address"  >
              <u--input v-model="form.address"  placeholder="请填写生产地址"
                inputAlign="right" border="none"></u--input>
            </u-form-item>
            <u-form-item label="功能主治" prop="indications"  labelPosition="top">
            <u--textarea v-model="form.indications" placeholder="请填写功能主治" border="none"></u--textarea>
          </u-form-item>
          <u-form-item label="贮藏" prop="storage"  labelPosition="top">
            <u--textarea v-model="form.storage" placeholder="请填写贮藏" border="none"></u--textarea>
          </u-form-item>
          <u-form-item label="药品成分" prop="ingredients"  labelPosition="top">
            <u--textarea v-model="form.ingredients" placeholder="请填写药品成分" border="none"></u--textarea>
          </u-form-item>
          <u-form-item label="用法用量" prop="usage"  labelPosition="top">
            <u--textarea v-model="form.usage" placeholder="请填写用法用量" border="none"></u--textarea>
          </u-form-item>
          <u-form-item label="性状" prop="character"  labelPosition="top">
            <u--textarea v-model="form.character" placeholder="请填写性状" border="none"></u--textarea>
          </u-form-item>
          <u-form-item label="不良反应" prop="adverseReaction"  labelPosition="top">
            <u--textarea v-model="form.adverseReaction" placeholder="请填写不良反应" border="none"></u--textarea>
          </u-form-item>
          <u-form-item label="备注" prop="remark"  labelPosition="top">
            <u--textarea v-model="form.remark" placeholder="请填写备注" border="none"></u--textarea>
          </u-form-item>
        </u--form>
        <view class="form-btn">
          <u-button type="primary" text="提交" @click="submit"></u-button>
        </view>
      </view>
    </view>
    <u-toast ref="uToast"></u-toast>
    <u-picker itemHeight="88" :show="showPerson" :columns="personList" keyName="name" @cancel="handlePersonCancel"
      @confirm="handlePersonConfirm"></u-picker>
      <u-picker itemHeight="88" :show="showDosageForm" :columns="dosageFormList" keyName="dictLabel" @cancel="handleDosageFormCancel"
        @confirm="handleDosageFormConfirm"></u-picker>
        <u-picker itemHeight="88" :show="showTreatmentType" :columns="treatmentTypeList" keyName="dictLabel" @cancel="handleTreatmentTypeCancel"
        @confirm="handleTreatmentTypeConfirm"></u-picker>
        <u-picker itemHeight="88" :show="showClassification" :columns="classificationList" keyName="dictLabel" @cancel="handleClassificationCancel"
        @confirm="handleClassificationConfirm"></u-picker>
      <u-picker itemHeight="88" :show="showCategory" :columns="categoryList" keyName="dictLabel" @cancel="handleCategoryCancel"
      @confirm="handleCategoryConfirm"></u-picker>
      <u-picker itemHeight="88" :show="showPackageUnit" :columns="packageUnitList" keyName="dictLabel" @cancel="handlePackageUnitCancel"
      @confirm="handlePackageUnitConfirm"></u-picker>
      <u-picker itemHeight="88" :show="showIsImport" :columns="isImportList" keyName="dictLabel" @cancel="handleIsImportCancel"
      @confirm="handleIsImportConfirm"></u-picker>
      <u-picker itemHeight="88" :show="showUnit" :columns="unitList" keyName="dictLabel" @cancel="handleUnitCancel"
      @confirm="handleUnitConfirm"></u-picker>
      <u-picker itemHeight="88" :show="showContentUnit" :columns="contentUnitList" keyName="dictLabel" @cancel="handleContentUnitCancel"
      @confirm="handleContentUnitConfirm"></u-picker>
  </view>
</template>

<script setup>
import {getMedicineBasic, addMedicineBasic, updateMedicineBasic } from '@/api/health/medicineBasic'
const { proxy } = getCurrentInstance()
  import { getDicts } from '@/api/system/dict/data.js'
import dayjs from 'dayjs'
import {onLoad,onReady} from "@dcloudio/uni-app";
  // 计算属性与监听属性是在vue中而非uniap中 需要注意！！！
import {reactive ,toRefs,ref,computed ,getCurrentInstance }from "vue";
const datePickShow = ref(false)
const title = ref("药品基础信息")
const flag = ref('add')

const dosageFormList = ref([])
const showDosageForm = ref(false)

const treatmentTypeList = ref([])
const showTreatmentType = ref(false)

const classificationList = ref([])
const showClassification = ref(false)

const categoryList = ref([])
const showCategory = ref(false)

const packageUnitList = ref([])
const showPackageUnit = ref(false)

const isImportList = ref([])
const showIsImport = ref(false)

const unitList = ref([])
const showUnit = ref(false)

const contentUnitList = ref([])
const showContentUnit = ref(false)


const data = reactive({
form: {
  id: null,
    name: null,
    classification: null,
    category: null,
    brand: null,
    packaging: null,
    manufacturers: null,
    treatmentType: null,
    isImport: null,
    ingredients: null,
    usage: null,
    dosageForm: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null,
    remark: null,
    code: null,
    specifications: null,
    unit: null,
    address: null,
    adverseReaction: null,
    content: null,
    contentUnit: null,
    character: null,
    storage: null,
    indications: null
},
rules: {
  name: [{ required: true, message: '药品全称不能为空', trigger:['change', 'blur'] }],
  shortName: [{ required: true, message: '药品简称不能为空', trigger: ['change', 'blur'] }],
  // classificationName: [{ required: true, message: '药品分类不能为空', trigger: ['change', 'blur'] }],
  // categoryName: [{ required: true, message: '类别不能为空', trigger: ['change', 'blur'] }],
  brand: [{ required: true, message: '品牌不能为空', trigger: ['change', 'blur'] }],
  packaging: [{ required: true, message: '药品包装不能为空', trigger: ['change', 'blur'] }],
  // manufacturers: [{ required: true, message: '生产厂家不能为空', trigger: ['change', 'blur'] }],
  // code: [{ required: true, message: '药品编码不能为空', trigger: ['change', 'blur'] }],
  // dosageFormName: [{ required: true, message: '药品剂型不能为空', trigger: ['change', 'blur'] }]
}
})
const { form, rules} = toRefs(data)


onLoad((option) => {
  form.value.id = option.id
     flag.value = option.flag
     if(flag.value==null){
      if(form.value.id!=null){
      title.value="药品基础信息-修改"
     }else{
      title.value="药品基础信息-新增"
     }
     }else{
      title.value="药品基础信息-复制"
     }
    getData()
})
onReady(() => {
     form.value.dosingTime = dayjs(new Date().getTime()).format("YYYY-MM-DD HH:mm:ss")
})
function dictStr(val, arr) {
      let str = ''
      arr.map(item => {
        if (item.dictValue === val) {
          str = item.dictLabel
        }
      })
      return str
 }
function  getData() {
     // 类型
     getDicts('dosage_form').then(res => {
      dosageFormList.value =[res.data] 
      })
       // 类型
     getDicts('mar_type').then(res => {
      treatmentTypeList.value =[res.data] 
      })
       // 类型
     getDicts('medicine_classification').then(res => {
      classificationList.value =[res.data] 
      })
       // 类型
     getDicts('medicine_category').then(res => {
      categoryList.value =[res.data] 
      })
       // 类型
     getDicts('package_unit').then(res => {
      packageUnitList.value =[res.data] 
      })
       // 类型
     getDicts('is_import').then(res => {
      isImportList.value =[res.data] 
      })
       // 类型
     getDicts('medical_unit').then(res => {
      unitList.value =[res.data] 
      })
       // 类型
     getDicts('content_unit').then(res => {
      contentUnitList.value =[res.data] 
      })

    if(form.value.id!=null){
      getMedicineBasic(form.value.id).then(res => {
        form.value = res.data   
        getDicts('dosage_form').then(result => {
          form.value.dosageFormName=dictStr(form.value.dosageForm, result.data)
         })
         getDicts('mar_type').then(result => {
          form.value.treatmentTypeName=dictStr(form.value.treatmentType, result.data)
         })
         getDicts('medicine_classification').then(result => {
          form.value.classificationName=dictStr(form.value.classification, result.data)
         })
         getDicts('medicine_category').then(result => {
          form.value.categoryName=dictStr(form.value.category, result.data)
         })
         getDicts('package_unit').then(result => {
          form.value.packageUnitName=dictStr(form.value.packageUnit, result.data)
         })
         getDicts('is_import').then(result => {
          form.value.isImportName=dictStr(form.value.isImport, result.data)
         })
         getDicts('medical_unit').then(result => {
          form.value.unitName=dictStr(form.value.unit, result.data)
         })
         getDicts('content_unit').then(result => {
          form.value.contentUnitName=dictStr(form.value.contentUnit, result.data)
         })
      })
    }
}

function handleDosageForm() {
        if (dosageFormList.value[0].length === 0) {
          proxy.$refs['uToast'].show({
            message: '药品剂型为空  ', type: 'warning'
          })
        } else {
          showDosageForm.value = true
        }
     }
  function   handleDosageFormConfirm(e) {
        form.value.dosageFormName = e.value[0].dictLabel
        form.value.dosageForm = e.value[0].dictValue
        showDosageForm.value = false
  }
 function handleDosageFormCancel() {
  showDosageForm.value = false
      }

      function handleTreatmentType() {
        if (treatmentTypeList.value[0].length === 0) {
          proxy.$refs['uToast'].show({
            message: '药品来源为空  ', type: 'warning'
          })
        } else {
          showTreatmentType.value = true
        }
     }
  function   handleTreatmentTypeConfirm(e) {
        form.value.treatmentTypeName = e.value[0].dictLabel
        form.value.treatmentType = e.value[0].dictValue
        showTreatmentType.value = false
  }
 function handleTreatmentTypeCancel() {
  showTreatmentType.value = false
      }

      function handleClassification() {
        if (classificationList.value[0].length === 0) {
          proxy.$refs['uToast'].show({
            message: '药品来源为空  ', type: 'warning'
          })
        } else {
          showClassification.value = true
        }
     }
  function   handleClassificationConfirm(e) {
        form.value.classificationName = e.value[0].dictLabel
        form.value.classification = e.value[0].dictValue
        showClassification.value = false
  }
 function handleClassificationCancel() {
  showClassification.value = false
      }

      function handleCategory() {
        if (categoryList.value[0].length === 0) {
          proxy.$refs['uToast'].show({
            message: '药品来源为空  ', type: 'warning'
          })
        } else {
          showCategory.value = true
        }
     }
  function   handleCategoryConfirm(e) {
        form.value.categoryName = e.value[0].dictLabel
        form.value.category = e.value[0].dictValue
        showCategory.value = false
  }
 function handleCategoryCancel() {
  showCategory.value = false
      }

      function handlePackageUnit() {
        if (packageUnitList.value[0].length === 0) {
          proxy.$refs['uToast'].show({
            message: '药品来源为空  ', type: 'warning'
          })
        } else {
          showPackageUnit.value = true
        }
     }
  function   handlePackageUnitConfirm(e) {
        form.value.packageUnitName = e.value[0].dictLabel
        form.value.packageUnit = e.value[0].dictValue
        showPackageUnit.value = false
  }
 function handlePackageUnitCancel() {
  showPackageUnit.value = false
      }

      function handleIsImport() {
        if (isImportList.value[0].length === 0) {
          proxy.$refs['uToast'].show({
            message: '药品来源为空  ', type: 'warning'
          })
        } else {
          showIsImport.value = true
        }
     }
  function   handleIsImportConfirm(e) {
        form.value.isImportName = e.value[0].dictLabel
        form.value.isImport = e.value[0].dictValue
        showIsImport.value = false
  }
 function handleIsImportCancel() {
  showIsImport.value = false
      }

      function handleUnit() {
        if (unitList.value[0].length === 0) {
          proxy.$refs['uToast'].show({
            message: '药品来源为空  ', type: 'warning'
          })
        } else {
          showUnit.value = true
        }
     }
  function   handleUnitConfirm(e) {
        form.value.unitName = e.value[0].dictLabel
        form.value.unit = e.value[0].dictValue
        showUnit.value = false
  }
 function handleUnitCancel() {
  showUnit.value = false
      }

      function handleContentUnit() {
        if (contentUnitList.value[0].length === 0) {
          proxy.$refs['uToast'].show({
            message: '药品来源为空  ', type: 'warning'
          })
        } else {
          showContentUnit.value = true
        }
     }
  function   handleContentUnitConfirm(e) {
        form.value.contentUnitName = e.value[0].dictLabel
        form.value.contentUnit = e.value[0].dictValue
        showContentUnit.value = false
  }
 function handleContentUnitCancel() {
  showContentUnit.value = false
      }
function submit() {
      proxy.$refs['uForm'].validate().then(() => {
        if (form.value.id != null) {
        if(flag.value==null){
          updateMedicineBasic(form.value).then(res => {
            proxy.$refs['uToast'].show({
            message: '修改成功', complete() {
              uni.navigateTo({ url: `/pages/health/medicineBasic/list` })
            }
          })
        })
          }else {
            form.value.id == null
            addMedicineBasic(form.value).then(res => {
            proxy.$refs['uToast'].show({
            message: '新增成功', complete() {
              uni.navigateTo({ url: `/pages/health/medicineBasic/list` })
            }
          })
        })
          }

        }else {
          addMedicineBasic(form.value).then(res => {
            proxy.$refs['uToast'].show({
            message: '新增成功', complete() {
              uni.navigateTo({ url: `/pages/health/medicineBasic/list` })
            }
          })
        })
        }
      })
    }
</script>

<style lang="scss" scoped>
.section {
  margin: 24rpx;
  padding: 0;
  background-color: #fff;
  border-radius: 16rpx;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.08);
  overflow: hidden;

  .section-title {
    padding: 16rpx 24rpx;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: #ffffff;
    line-height: 1.2;
    font-size: 28rpx;
    font-weight: 600;
    display: flex;
    align-items: center;
    
    &::before {
      content: '';
      width: 6rpx;
      height: 28rpx;
      background: #ffffff;
      border-radius: 3rpx;
      margin-right: 12rpx;
    }
  }

  .form-view {
    padding: 24rpx;

    .form-btn {
      padding-top: 16rpx;
    }
  }
}
</style>

<style lang="scss">
.form-btn .u-button {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
  border: none !important;
  border-radius: 24rpx !important;
  height: 80rpx !important;
  box-shadow: 0 4rpx 16rpx rgba(102, 126, 234, 0.4) !important;
}

.form-btn .u-button__text {
  font-size: 30rpx !important;
  font-weight: 500 !important;
  letter-spacing: 2rpx !important;
}
</style>