<template>
  <el-row class="mb8">
    <el-col v-if="controlButton[0].display" :span="3">
      <el-button type="primary" plain size="mini" @click="prescriptionAddDruyAddChoice()">扫描添加</el-button>
    </el-col>
    <el-col v-if="controlButton[1].display" :span="3">
      <el-button type="primary" plain size="mini" @click="prescriptionAddDruyAddChoice()">常规处方</el-button>
    </el-col>
    <el-col v-if="controlButton[2].display" :span="3">
      <el-button type="primary" size="mini" @click="addDrugButton()">添加药品</el-button>
    </el-col>
    <!-- @click="removePrescription(item)" -->
    <el-col v-if="controlButton[3].display" :span="3">
      <el-button class="buttonPrescription" type="primary" plain size="mini">删除处方</el-button>
    </el-col>
  </el-row>
  <!-- 控制  border  -->
  <el-table :data="prescription" style="width: 100%" class="prescription" height="auto">
    <el-table-column v-if="controlData[0].display" fixed prop="id" label="编号" :width="`${controlData[0].width}`">
      <template #default="scope">
        {{
          prescription[scope.$index].id == null
            ? scope.$index + 1
            : prescription[scope.$index].id
        }}
      </template>
    </el-table-column>
    <!-- fixed -->
    <!-- 药名  -->
    <el-table-column v-if="controlData[1].display" fixed prop="medicineName" label="药名" :min-width="`${controlData[1].width}`">
      <!-- 判断 使用 那 一种 方法  -->
      <template #default="scope">
        <el-form-item label-width="0px" :prop="`${controlData[1].check[0].prop}.${scope.$index}.medicineName`" :rules="[
            {
              required: controlData[1].check[0].prop,
              message: controlData[1].check[0].message,
              trigger: controlData[1].check[0].trigger,
            },
          ]">
          <!-- 输入框模式 -->
          <!-- <el-input v-if="controlData[1].pattern[0].isUse" :disabled="controlData[1].prohibit" v-model="prescription[scope.$index].medicineName"></el-input> -->
          <el-input
            v-if="isTextOverflow(scope.row.medicineName)"
            type="textarea"
            rows="2"
            v-model="prescription[scope.$index].medicineName" 
            disabled
          />
          <el-input v-else-if="controlData[1].pattern[0].isUse" :disabled="controlData[1].prohibit" v-model="prescription[scope.$index].medicineName"></el-input>
          <!-- 下拉框 模式 -->
          <el-select v-if="controlData[1].pattern[1].isUse" v-model="prescription[scope.$index].medicineName" filterable placeholder="请选择" :disabled="controlData[1].prohibit">
            <el-option v-for="item in druyNameList" :key="item.value" :label="item.medicineName" :value="item.medicineName" @click="chooseMedicineName(item, scope.$index)">
            </el-option>
          </el-select>
          <!-- 输入框弹框模式  change -->
          <el-input v-if="controlData[1].pattern[2].isUse" :disabled="controlData[1].prohibit" v-model="prescription[scope.$index].medicineName" @focus="drugArchivesBulletFrameOperation(scope.$index)"></el-input>
        </el-form-item>
      </template>
    </el-table-column>
    <!-- 规格 -->
    <el-table-column v-if="controlData[12].display" prop="specification" label="规格" :min-width="`${controlData[12].width}`">
      <template #default="scope">
        <el-form-item label-width="0px" :prop="`${controlData[12].check[0].prop}.${scope.$index}.specification`" :rules="[
            {
              required: controlData[12].check[0].isCheck,
              message: controlData[12].check[0].message,
              trigger: controlData[12].check[0].trigger,
            },
          ]">
          <el-input :disabled="controlData[12].prohibit" v-model="prescription[scope.$index].specification"></el-input>
        </el-form-item>
      </template>
    </el-table-column>
    <!-- 用法 -->
    <el-table-column v-if="controlData[2].display" prop="usage" label="用法" :width="`${controlData[2].width}`">
      <template #default="scope">
        <el-form-item label-width="0px" :prop="`${controlData[2].check[0].prop}.${scope.$index}.usageValue`" :rules="[
            {
              required: controlData[2].check[0].isCheck,
              message: controlData[2].check[0].message,
              trigger: controlData[2].check[0].trigger,
            },
          ]">
          <el-select v-model="prescription[scope.$index].usageValue" filterable placeholder="请选择" :disabled="controlData[2].prohibit">
            <el-option v-for="item in commonUsageList" :key="item.value" :label="item.value" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </template>
    </el-table-column>
    <!-- 用量 -->
    <el-table-column v-if="controlData[3].display" prop="dosageValue" label="一次使用的量" :width="`${controlData[3].width}`">
      <template #default="scope">
        <el-row>
          <!-- calculationTotal -->
          <el-col :span="12">
            <el-form-item label-width="0px" :prop="`${controlData[3].check[0].prop}.${scope.$index}.dosageValue`" :rules="[
                {
                  required: controlData[3].check[0].isCheck,
                  message: controlData[3].check[0].message,
                  trigger: controlData[3].check[0].trigger,
                },
                {
                  pattern: /^(?!0\d)\d+$/,
                  message: '请输入一个有效的整数',
                  trigger: 'blur',
                },
                {
                  validator: checkValueRules,
                  trigger: 'blur',
                },
              ]">
              <el-input @input="calculationTotal(scope.$index)" v-model="prescription[scope.$index].dosageValue" :disabled="controlData[3].prohibit" />
            </el-form-item>
          </el-col>
          <!-- 单位 -->
          <el-col :span="12">
            <el-form-item label-width="0px" :prop="`${controlData[3].check[1].prop}.${scope.$index}.splitUnit`" :rules="[
                {
                  required: controlData[3].check[1].isCheck,
                  message: controlData[3].check[1].message,
                  trigger: controlData[3].check[1].trigger,
                },
              ]">
              <el-select v-model="prescription[scope.$index].splitUnit" filterable placeholder="请选择" :disabled="controlData[3].prohibit">
                <el-option v-for="dict in healthcare_packaging_unit" :key="dict.value" :label="dict.label" :value="dict.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </template>
    </el-table-column>
    <!-- 次数 -->
    <el-table-column v-if="controlData[4].display" prop="frequency" label="一天使用的次数" :width="`${controlData[4].width}`">
      <template #default="scope">
        <el-form-item label-width="0px" :prop="`${controlData[4].check[0].prop}.${scope.$index}.frequency`" :rules="[
            {
              required: controlData[4].check[0].isCheck,
              message: controlData[4].check[0].message,
              trigger: controlData[4].check[0].trigger,
            },
          ]">
          <el-select v-model="prescription[scope.$index].frequency" filterable placeholder="请选择" :disabled="controlData[4].prohibit">
            <el-option v-for="item in frequencyList" :key="item.value" :label="item.name" :value="parseInt(item.value)" @click="calculationTotal(scope.$index)">
            </el-option>
          </el-select>
        </el-form-item>
      </template>
    </el-table-column>
    <!-- 食用时间 120 -->
    <el-table-column v-if="controlData[5].display" prop="eatingTime" label="使用时间" :width="`${controlData[5].width}`">
      <template #default="scope">
        <el-form-item label-width="0px" :prop="`${controlData[5].check[0].prop}.${scope.$index}.eatingTime`" :rules="[
            {
              required: controlData[5].check[0].isCheck,
              message: controlData[5].check[0].message,
              trigger: controlData[5].check[0].trigger,
            },
          ]">
          <el-select v-model="prescription[scope.$index].eatingTime" filterable placeholder="请选择" :disabled="controlData[5].prohibit">
            <el-option v-for="item in eatingTimeList" :key="item.value" :label="item.value" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </template>
    </el-table-column>
    <!-- 食用天数 -->
    <el-table-column v-if="controlData[6].display" prop="eatingDays" label="使用天数" width="120" :width="`${controlData[6].width}`">
      <template #default="scope">
        <el-form-item label-width="0px" :prop="`${controlData[5].check[0].prop}.${scope.$index}.eatingDays`" :rules="[
            {
              required: controlData[5].check[0].isCheck,
              message: controlData[5].check[0].message,
              trigger: controlData[5].check[0].trigger,
            },
            { pattern: /^(?!0\d)\d+$/, message: '请输入一个有效的整数', trigger: 'blur' },
          ]">
          <el-input @input="calculationTotal(scope.$index)" v-model="prescription[scope.$index].eatingDays" :disabled="controlData[5].prohibit" />
        </el-form-item>
      </template>
    </el-table-column>
    <!-- 总量   -->
    <el-table-column v-if="controlData[7].display" prop="totalQuantity" label="总量" :width="`${controlData[7].width}`">
      <template #default="scope">
        <!-- 总量 可以 拆分 -->
        <!-- prescription[scope.$index].isSplit == 'Y' -->
        <el-row v-if="false">
          <el-col :span="8">
            <el-form-item label-width="0px" :prop="`${controlData[7].check[0].prop}.${scope.$index}.totalQuantity`" :rules="[
                {
                  required: controlData[7].check[0].isCheck,
                  message: controlData[7].check[0].message,
                  trigger: controlData[7].check[0].trigger,
                },
                {
                  pattern: /^(?!0\d)\d+$/,
                  message: '请输入一个有效的整数',
                  trigger: 'blur',
                },
                {
                  validator: checkValueRules,
                  trigger: 'blur',
                },
              ]">
              <el-input v-model="prescription[scope.$index].totalQuantity" :disabled="true"></el-input>
            </el-form-item>
          </el-col>
          <!-- 单位 -->
          <el-col :span="14">
            <el-form-item label-width="0px" :prop="`${controlData[7].check[1].prop}.${scope.$index}.splitUnit`" :rules="[
                {
                  required: controlData[7].check[1].isCheck,
                  message: controlData[7].check[1].message,
                  trigger: controlData[7].check[1].trigger,
                },
              ]">
              <el-select v-model="prescription[scope.$index].splitUnit" filterable placeholder="请选择" :disabled="controlData[7].prohibit">
                <el-option v-for="dict in healthcare_packaging_unit" :key="dict.value" :label="dict.label" :value="dict.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 总量 不 可以 拆分  -->
        <!-- prescription[scope.$index].isSplit == 'N' -->
        <el-row v-if="true">
          <el-col :span="8">
            <el-form-item label-width="0px" :prop="`${controlData[7].check[0].prop}.${scope.$index}.totalQuantity`" :rules="[
                {
                  required: controlData[7].check[0].isCheck,
                  message: controlData[7].check[0].message,
                  trigger: controlData[7].check[0].trigger,
                },
                {
                  pattern: /^(?!0\d)\d+$/,
                  message: '请输入一个有效的整数',
                  trigger: 'blur',
                },
                {
                  validator: checkValueRules,
                  trigger: 'blur',
                },
              ]">
              <el-input v-model="prescription[scope.$index].totalQuantity" prescription="请输入总量" :disabled="controlData[7].prohibit" @input="calculationTotal(scope.$index)"></el-input>
            </el-form-item>
          </el-col>
          <!-- 单位 -->
          <el-col :span="14">
            <el-form-item label-width="0px" :prop="`${controlData[7].check[1].prop}.${scope.$index}.packageUnit`" :rules="[
                {
                  required: controlData[7].check[1].isCheck,
                  message: controlData[7].check[1].message,
                  trigger: controlData[7].check[1].trigger,
                },
              ]">
              <el-select v-model="prescription[scope.$index].packageUnit" filterable placeholder="请选择单位" :disabled="controlData[7].prohibit">
                <el-option v-for="dict in healthcare_packaging_unit" :key="dict.value" :label="dict.label" :value="dict.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </template>
    </el-table-column>
    <!-- 单价 -->
    <el-table-column v-if="controlData[8].display" prop="unitPrice" label="单价" :width="`${controlData[8].width}`">
      <template #default="scope">
        <el-form-item label-width="0px" :prop="`${controlData[8].check[0].prop}.${scope.$index}.unitPrice`" :rules="[
            {
              required: controlData[8].check[0].isCheck,
              message: controlData[8].check[0].message,
              trigger: controlData[8].check[0].trigger,
            },
          ]">
          <el-button :disabled="controlData[8].prohibit" width="100%" @click="selectDrugPriceInput(item, scope)">
            {{ Number(prescription[scope.$index].unitPrice || 0).toFixed(2) }}
          </el-button>
        </el-form-item>
      </template>
    </el-table-column>
    <!-- 总价  -->
    <el-table-column v-if="controlData[9].display" prop="totalPrice" label="总价" :width="`${controlData[9].width}`">
      <template #default="scope">
        <el-form-item label-width="0px" :prop="`${controlData[9].check[0].prop}.${scope.$index}.totalPrice`" :rules="[
            {
              required: controlData[9].check[0].isCheck,
              message: controlData[9].check[0].message,
              trigger: controlData[9].check[0].trigger,
            },
          ]">
          {{
            Number(isInsured) === 0
              ? ceilPrice(
                  (parseFloat(prescription[scope.$index].totalPrice || 0) * (1 - (parseFloat(reimbursementRate || 0) / 100)))
                )
              : ceilPrice(prescription[scope.$index].totalPrice || 0)
          }}元
        </el-form-item>
      </template>
    </el-table-column>
    <!-- 备注 -->
    <el-table-column v-if="controlData[10].display" prop="remarks" label="备注" :min-width="`${controlData[10].width}`">
      <template #default="scope">
        <el-form-item label-width="0px" :prop="`${controlData[10].check[0].prop}.${scope.$index}.remarks`" :rules="[
            {
              required: controlData[10].check[0].isCheck,
              message: controlData[10].check[0].message,
              trigger: controlData[10].check[0].trigger,
            },
          ]">
          <!-- <el-input :disabled="controlData[10].prohibit" v-model="prescription[scope.$index].remarks" /> -->
          <el-input
            v-if="isTextOverflow(scope.row.remarks)"
            type="textarea"
            rows="2"
            v-model="prescription[scope.$index].remarks" 
            disabled
          />
          <el-input v-else :disabled="controlData[10].prohibit" v-model="prescription[scope.$index].remarks" />
        </el-form-item>
      </template>
    </el-table-column>
    <!-- 操作 -->
    <el-table-column v-if="controlData[11].display" fixed="right" label="操作" :width="`${controlData[11].width}`" class-name="small-padding fixed-width">
      <template #default="scope">
        <el-button :disabled="controlData[11].prohibit" link type="primary" icon="Edit" @click="removeDrug(scope.$index)">删除</el-button>
      </template>
    </el-table-column>
    </el-table>
  <!-- 展示总价和 -->
  <!-- <el-row>
    <el-col :span="24">
      <div style="text-align: right; margin-top: 20px;color: red">
        <span>所有药品总价和: {{ totalPriceSum }} 元</span>
      </div>
    </el-col>
  </el-row> -->
  <!-- 弹框 -->
  <DrugArchives ref="childComponentRef" :widthBf="sdada" :title="titleDrugArchives" :openAssembly="openDrugArchives" :queryParams="queryParamsDrugArchives" :controlQuery="controlQueryDrugArchives" :controlData="controlDataDrugArchives" @input-data="paginationDataFq()" @select-value="selectDrugData" />
</template>

<script setup name="PrescriptionList">
import { listCommonUsage, listEatingTime, listFrequency } from '@/api/healthcare/outpatientService/healthcareOsConventionalValue'

const { proxy } = getCurrentInstance()

// 价格向上取整到小数点后两位（进一法）
// 先缩放到整数避免浮点精度问题
function ceilPrice(value) {
  const num = Number(value)
  if (isNaN(num)) return '0.00'
  const scaled = Math.round(num * 10000)
  const cents = Math.ceil(scaled / 100)
  return (cents / 100).toFixed(2)
}

const { os_patient_type } = proxy.useDict('os_patient_type')
// os_patient_type
const { os_diagnosis_type } = proxy.useDict('os_diagnosis_type')
//
const { sys_user_sex } = proxy.useDict('sys_user_sex')
// 医药包装单位
const { healthcare_packaging_unit } = proxy.useDict('healthcare_packaging_unit')

common()
// 获取 常用用法
const commonUsageList = ref([]) // 用法
const eatingTimeList = ref([]) //  食用时间
const frequencyList = ref([]) //   次数
const totalPriceSum = ref(0) //所有药品总价和

function common() {
  listCommonUsage().then(res => {
    commonUsageList.value = res.rows
  })
  listEatingTime().then(res => {
    eatingTimeList.value = res.rows
  })
  listFrequency().then(res => {
    frequencyList.value = res.rows
  })
}

//

// 校验 方法
function checkValueRules(rule, value, callback) {
  const num = Number(value)
  const minValue = 0
  const maxValue = 1000
  if (isNaN(num)) {
    callback(new Error('请输入一个数字'))
  } else if (!Number.isInteger(num)) {
    callback(new Error('请输入一个整数'))
  } else if (num < minValue || num > maxValue) {
    callback(new Error(`请输入整数过大`))
  } else {
    callback()
  }
}

// 父组件 传递过来的 数据
const props = defineProps({
  // 数据 列表
  prescriptionData: {
    type: Array
  },
  isInsured: {
    type: [Number, String],
    default: 1
  },
  reimbursementRate: {
    type: [Number, String],
    default: 0
  },
  // 控制 纽约 的 显示 隐藏
  controlButton: {
    type: Array,
    default: () => [
      {
        id: 0,
        name: '',
        display: true
      },
      {
        id: 1,
        name: '',
        display: true
      },
      {
        id: 2,
        name: '',
        display: true
      },
      {
        id: 3,
        name: '',
        display: true
      }
    ]
  },
  // 数据
  controlData: {
    type: Array,
    // 控制 数据 列表 的 显示 隐藏
    // display ==>  vt. 显示        隐藏
    // prohibit ==> vt. 禁止
    default: () => [
      {
        id: 0,
        name: '编号',
        display: true,
        prohibit: false,
        pattern: [
          {
            id: 0,
            name: '',
            isUse: true
          },
          {
            id: 1,
            name: '',
            isUse: false
          }
        ],
        check: [
          {
            id: 0,
            prop: 'prescription',
            isCheck: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ]
      },
      {
        id: 1,
        name: '药名',
        display: true,
        prohibit: false,
        pattern: [
          {
            id: 0,
            name: '输入框模式',
            isUse: true
          },
          {
            id: 1,
            name: '下拉框模式',
            isUse: false
          },
          {
            id: 2,
            name: '输入框弹框模式',
            isUse: false
          }
        ],
        check: [
          {
            id: 0,
            prop: 'prescription',
            isCheck: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ]
      },
      {
        id: 2,
        name: '用法',
        display: true,
        prohibit: false,
        pattern: [
          {
            id: 0,
            name: '',
            isUse: true
          },
          {
            id: 1,
            name: '',
            isUse: false
          }
        ],
        check: [
          {
            id: 0,
            prop: 'prescription',
            isCheck: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ]
      },
      {
        id: 3,
        name: '用量',
        display: true,
        prohibit: false,
        pattern: [
          {
            id: 0,
            name: '',
            isUse: true
          },
          {
            id: 1,
            name: '',
            isUse: false
          }
        ],
        check: [
          {
            id: 0,
            prop: 'prescription',
            isCheck: true,
            message: '不能为空',
            trigger: 'blur'
          },
          {
            id: 1,
            prop: 'prescription',
            isCheck: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ]
      },
      {
        id: 4,
        name: '次数',
        display: true,
        prohibit: false,
        pattern: [
          {
            id: 0,
            name: '',
            isUse: true
          },
          {
            id: 1,
            name: '',
            isUse: false
          }
        ],
        check: [
          {
            id: 0,
            prop: 'prescription',
            isCheck: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ]
      },
      {
        id: 5,
        name: '食用时间',
        display: true,
        prohibit: false,
        pattern: [
          {
            id: 0,
            name: '',
            isUse: true
          },
          {
            id: 1,
            name: '',
            isUse: false
          }
        ],
        check: [
          {
            id: 0,
            prop: 'prescription',
            isCheck: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ]
      },
      {
        id: 6,
        name: '食用天数',
        display: true,
        prohibit: false,
        pattern: [
          {
            id: 0,
            name: '',
            isUse: true
          },
          {
            id: 1,
            name: '',
            isUse: false
          }
        ],
        check: [
          {
            id: 0,
            prop: 'prescription',
            isCheck: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ]
      },
      {
        id: 7,
        name: '总量',
        display: true,
        prohibit: false,
        pattern: [
          {
            id: 0,
            name: '',
            isUse: true
          },
          {
            id: 1,
            name: '',
            isUse: false
          }
        ],
        check: [
          {
            id: 0,
            prop: 'prescription',
            isCheck: true,
            message: '不能为空',
            trigger: 'blur'
          },
          {
            id: 1,
            prop: 'prescription',
            isCheck: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ]
      },
      {
        id: 8,
        name: '单价',
        display: true,
        prohibit: false,
        pattern: [
          {
            id: 0,
            name: '',
            isUse: true
          },
          {
            id: 1,
            name: '',
            isUse: false
          }
        ],
        check: [
          {
            id: 0,
            prop: 'prescription',
            isCheck: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ]
      },
      {
        id: 9,
        name: '总价',
        display: true,
        prohibit: false,
        pattern: [
          {
            id: 0,
            name: '',
            isUse: true
          },
          {
            id: 1,
            name: '',
            isUse: false
          }
        ],
        check: [
          {
            id: 0,
            prop: 'prescription',
            isCheck: true,
            message: '不能为空',
            trigger: 'blur'
          },
          {
            id: 1,
            prop: 'prescription',
            isCheck: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ]
      },
      {
        id: 10,
        name: '备注',
        display: true,
        prohibit: false,
        pattern: [
          {
            id: 0,
            name: '',
            isUse: true
          },
          {
            id: 1,
            name: '',
            isUse: false
          }
        ],
        check: [
          {
            id: 0,
            prop: 'prescription',
            isCheck: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ]
      },
      {
        id: 11,
        name: '操作',
        display: true,
        prohibit: false,
        pattern: [
          {
            id: 0,
            name: '',
            isUse: true
          },
          {
            id: 1,
            name: '',
            isUse: false
          }
        ],
        check: [
          {
            id: 0,
            prop: 'prescription',
            isCheck: true,
            message: '不能为空',
            trigger: 'blur'
          }
        ]
      }
    ]
  }
})

// 监听 数据
watch(
  () => props.prescriptionData,
  (newValue, oldValue) => {
    prescription.value = newValue
  }
)

console.log('数据:', props.controlButton)

/** 表格选择事件 */
const emit = defineEmits(['select-value', 'update:prescriptionData', 'input-data'])

// v-model 的 数据 绑定 的 事件  双向绑定
const handleInputChange = event => {
  emit('update:prescriptionData', event.target.value)
}

// 双向绑定 props 里面 的 数据 不能 修改
const prescription = ref(props.prescriptionData)

// 更新 数据
const requestValueUpdate = () => {
  // 父 组件 的 方法
  props.onUpdate()
}

// 添加 药品 按钮
function addDrugButton() {
  console.log('父亲data:', props.prescriptionData, prescription)
  if (prescription.value == null) {
    prescription.value = []
  }
  prescription.value.push({
    usageValue: commonUsageList.value[0].value, //
    splitUnit: '1', // 单位
    frequency: Number(frequencyList.value[0].value), // 次数
    eatingTime: eatingTimeList.value[0].value, // 食用时间
    packageUnit: '1' // 单位1
  })
}

// 删除 药品
function removeDrug(index) {
  prescription.value.splice(index, 1)
}

// 获取 药品 列表 下拉框
/* 获取 药品 名称 列表 药品 基本 档案 药品 名称 是 唯一的  */
import { gainMedicineArchiveList } from '@/api/healthcare/outpatientService/patientDiagnosis'
// reactive
const druyNameList = ref([])
const druyName = ref([])
//
function getDruyNameListQuer() {
  // 进行 了 判断
  // 查看 后端 是 如何 处理
  gainMedicineArchiveList({ medicineName: druyName.value }).then(response => {
    druyNameList.value = response.rows
    console.log('药品名称:', response.rows)
  })
}

// 药名 下拉框 的 选择 后 操作  进行 赋值 药品 的 名字
// 控制 是否 显示 可以 拆分 总量  总量  可以 拆分  总量  不 可以 拆分
const isDisplayTotalQuantity = ref(0)
function chooseMedicineName(value, index) {
  //
  console.log('value:', value)
  prescription.value[index].splitUnit = value.splitUnit
  prescription.value[index].packageUnit = value.packageUnit
  prescription.value[index].isSplit = value.isSplit
  // 控制 总量  总价  显示 隐藏
  // Y 是 可以 拆分
  if (value.isSplit == 'Y') {
    isDisplayTotalQuantity.value = 0
  } else {
    isDisplayTotalQuantity.value = 1
  }
}

// getDruyNameListQuer();

// 计算 总量  总价
function calculationTotal(index) {
  // 总量
  const totalAmount = ref(0)

  // 修改 一直 不可 拆分  总数  注解
  // 判断 是否 为 Y 是 可以 拆分
  // if (prescription.value[index].isSplit == "Y") {
  //   totalAmount.value =
  //     prescription.value[index].dosageValue *
  //     prescription.value[index].frequency *
  //     prescription.value[index].eatingDays;
  //   // 总量
  //   if (!Number.isNaN(totalAmount.value)) {
  //     prescription.value[index].totalQuantity = totalAmount.value;
  //   } else {
  //     prescription.value[index].totalQuantity = 0;
  //   }
  // }

  // console.log('totalAmount:', totalAmount)

  // 总价
  const totalPrice = prescription.value[index].totalQuantity * prescription.value[index].unitPrice
  if (!Number.isNaN(totalPrice)) {
    prescription.value[index].totalPrice = totalPrice
  } else {
    prescription.value[index].totalPrice = 0
  }
  console.log('totalPrice11111:', form.value.prescription)
  // 更新总价和
  totalPriceSum.value = ceilPrice(form.value.prescription
    .reduce((sum, prescri) => {
      return sum + prescri.reduce((innerSum, drug) => innerSum + parseFloat(drug.totalPrice || 0), 0)
    }, 0))
}

// 药品 档案 弹框
import DrugArchives from '@/components/Healthcare/OutpatientService/DrugArchives'
import drugArchivesUse from '@/components/Healthcare/OutpatientService/DrugArchives/use.ts'
//
let { controlQueryDrugArchives, queryParamsDrugArchives, controlDataDrugArchives, openDrugArchives, titleDrugArchives } = drugArchivesUse()

//
controlQueryDrugArchives.value[2].display = false

// 显示 数据 控制
controlDataDrugArchives.value[6].display = false
controlDataDrugArchives.value[12].display = false
controlDataDrugArchives.value[11].display = false

// 控制 弹框 的 宽
const sdada = ref('90%')
//
const childComponentRef = ref(null)
// 药品 在 地址
const drugIndex = ref(null)

// 获取 组件 药品 档案 弹框 里面 的 方法
const openChildDialog = () => {
  childComponentRef.value.openDialog()
}

// 显示 组件 药品 档案 弹框 操作
function drugArchivesBulletFrameOperation(index) {
  // 显示 弹框
  // console.log('显示 弹框', index)
  drugIndex.value = index
  queryParamsDrugArchives.value.medicineName = prescription.value[index].medicineName
  openChildDialog()
}

const paginationDataFq = value => {
  openDrugArchives.value = value
}
// 获取 药品档案 的 弹框 里面 的 数据
const selectDrugData = value => {
  childComponentRef.value.closeDialog()
  // 药品 名称
  prescription.value[drugIndex.value].medicineName = value.medicineName
  // 商品 规格
  prescription.value[drugIndex.value].specification = value.specification
  // 药品 使用 单位  最小包装单位
  // prescription.value[drugIndex.value].splitUnit = value.splitUnit;
  // 包装单位
  // prescription.value[drugIndex.value].packageUnit = value.packageUnit;
  // 是否 可以 拆分
  prescription.value[drugIndex.value].isSplit = value.isSplit
  // 控制 可以 拆分 总量  总价  显示 隐藏
  if (value.isSplit == 'Y') {
    isDisplayTotalQuantity.value = 0
    //
    if (value.state == 0) {
      // 最小单位数量
      prescription.value[drugIndex.value].unitQuantity = value.minNumber
    }
    //
    if (value.state == 1) {
      // 最小包装数量
      prescription.value[drugIndex.value].unitQuantity = value.unitQuantity
    }
  } else {
    isDisplayTotalQuantity.value = 1
  }
}

// 判断文本是否超出限制长度（比如超过20个字符）
const isTextOverflow = (text) => {
  const maxLength = 12; // 根据实际情况调整这个值
  return text && text.length > maxLength;
};

// 结果 弹框 子组件
</script>

<style scoped>
.prescriptionAdd {
  margin-bottom: 20px;
}
.prescription {
  margin-bottom: 20px;
}
.buttonPrescription {
  left: 0px;
}
.el-button.is-disabled, .el-button.is-disabled:hover {
    color: #161718;
}
</style>
