<template>
  <div style="max-height: 750px; overflow-y: auto">
    <div
      class="prescription-section"
      v-for="(prescription, pIndex) in tcmPrescriptionList"
      :key="prescription.id"
    >
      <div class="section-header">
        <div class="title first-prescription">
          <i class="el-icon-notebook-2"></i>
          <span>{{ '处方' + (pIndex + 1) }}</span>
          <el-icon
            class="add-icon"
            title="新增处方"
            @click="addNewPrescription()"
            style="font-size: 26px !important; margin-left: 10px; color: #409eff"
          >
            <plus />
          </el-icon>
          <el-icon
            class="add-icon"
            title="删除处方"
            @click="deletePrescription(prescription)"
            style="font-size: 26px !important; margin-left: 10px; color: red"
          >
            <Delete />
          </el-icon>
        </div>
        <div class="summary">
          <span class="summary-item">
            药品数:
            {{ prescription.prescriptionList ? prescription.prescriptionList.length : '0' }}种
          </span>
          <span class="summary-item">总价: ¥ {{ calculatePrescriptionTotal(pIndex) }}</span>
        </div>
      </div>
      <div>
        <div style="margin-top: 10px">
          <el-button type="primary" @click="handleAddMedicine(pIndex)" plain> 添加药品 </el-button>
          <el-button type="primary" @click="handleSaveBatch(pIndex)" plain> 保存 </el-button>
          <el-button type="primary" @click="handleSave(pIndex)" plain> 签发 </el-button>
          <el-button type="warning" plain @click="handleSingOut(pIndex)"> 撤回 </el-button>
          <el-button type="danger" plain @click="handleDelete(pIndex)" :disabled="false">
            删除
          </el-button>
          <el-button type="primary" plain @click="handlePrint(pIndex)" :disabled="false">
            打印
          </el-button>
        </div>
        <div
          class="doctor-station"
          style="padding: 10px 10px 10px 0; margin-left: 0; font-size: 15px"
        >
          <span class="doctor-station" style="margin-left: 0"> 诊断： </span>
          <el-select
            v-model="prescription.conditionDefinitionId"
            placeholder="诊断"
            style="width: 180px"
          >
            <el-option
              v-for="item in diagnosisList"
              :key="item.definitionId"
              :label="item.name"
              :value="item.definitionId"
              @click="handleDiagnosisChange(item, pIndex)"
            />
          </el-select>
          <span class="doctor-station"> 费用性质： </span>
          <el-select v-model="prescription.accountId" placeholder="费用性质" style="width: 120px">
            <el-option
              v-for="item in contractList"
              :key="item.accountId"
              :label="item.contractName"
              :value="item.accountId"
            />
          </el-select>
          <span class="doctor-station"> 用法： </span>
          <el-select v-model="prescription.methodCode" placeholder="用法" style="width: 120px">
            <el-option
              v-for="dict in method_code"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
          <span class="doctor-station"> 煎药方式： </span>
          <el-select v-model="decoctionMethod" placeholder="" style="width: 120px">
            <el-option
              v-for="dict in method_of_decocting_medicine"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
          <span class="doctor-station"> 频次： </span>
          <el-select v-model="prescription.rateCode" placeholder="频次" style="width: 120px">
            <el-option
              v-for="dict in rate_code"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
          <span class="doctor-station"> 用药天数： </span>
          <el-input
            v-model="prescription.dispensePerDuration"
            placeholder=" "
            style="width: 70px"
          />
          <span class="doctor-station"> 付数： </span>
          <el-input
            v-model="prescription.chineseHerbsDoseQuantity"
            placeholder=" "
            style="width: 70px"
            @input="
              (value) => {
                let price = 0;
                prescription.prescriptionList.forEach((item) => {
                  price += item.totalPrice;
                });
                prescription.totalAmount = prescription.dispensePerDuration * value * price;
              }
            "
          />
          <span class="doctor-station"> 是否代煎： </span>
          <el-checkbox
            v-model="prescription.sufferingFlag"
            :true-value="'1'"
            :false-value="'0'"
            placeholder=" "
            @change="
              (value) => {
                console.log(value, prescription.sufferingFlag, 123456789);
              }
            "
          />
          <span class="doctor-station" style="float: right"> </span>
        </div>
        <el-table
          ref="prescriptionRef"
          :data="prescription.prescriptionList"
          row-key="uniqueKey"
          border
          @cell-click="clickRow"
          @row-dblclick="(row) => clickRowDb(row, pIndex)"
          v-loading="loading"
          :expand-row-keys="prescription.expandOrder"
        >
          <el-table-column type="expand" width="1" style="width: 0">
            <template #default="scope">
              <el-form
                :model="scope.row"
                :rules="rowRules"
                :ref="'formRef' + pIndex + '-' + scope.$index"
              >
                <div
                  class="expend_div"
                  style="padding: 16px; background: #f8f9fa; border-radius: 8px"
                >
                  <template v-if="scope.row.adviceType == 1">
                    <div style="display: flex; align-items: center; margin-bottom: 16px; gap: 8px">
                      <span class="medicine-title">
                        {{
                          scope.row.adviceName +
                          ' ' +
                          ' [' +
                          Number(scope.row.unitPrice).toFixed(2) +
                          ' 元' +
                          '/' +
                          scope.row.unitCode_dictText +
                          ']'
                        }}
                      </span>
                      <el-form-item prop="lotNumber" label="药房：">
                        <el-select
                          v-model="scope.row.inventoryId"
                          style="width: 380px; margin-right: 20px"
                          placeholder="药房"
                        >
                          <el-option
                            v-for="item in scope.row.stockList"
                            :key="item.inventoryId"
                            :value="item.inventoryId"
                            :label="
                              item.locationName +
                              '  ' +
                              '批次号: ' +
                              item.lotNumber +
                              '  ' +
                              '  库存：' +
                              item.quantity / scope.row.partPercent +
                              item.unitCode_dictText +
                              '  单价：' +
                              item.price.toFixed(2) +
                              '/' +
                              item.unitCode_dictText
                            "
                            @click="handleNumberClick(item, scope.$index, pIndex)"
                          />
                        </el-select>
                      </el-form-item>
                      <div class="form-group">
                        <!-- 单次剂量 -->
                        <el-form-item
                          label="数量："
                          prop="minUnitQuantity"
                          class="required-field"
                          data-prop="minUnitQuantity"
                        >
                          <el-input-number
                            :min="0"
                            v-model="scope.row.minUnitQuantity"
                            controls-position="right"
                            :controls="false"
                            style="width: 70px; margin-right: 20px"
                            :ref="(el) => (inputRefs.minUnitQuantity = el)"
                            @input="
                              () => {
                                nextTick(() => {
                                  scope.row.totalPrice =
                                    scope.row.minUnitQuantity * scope.row.unitPrice;
                                });
                              }
                            "
                            @keyup.enter.prevent="
                              handleEnter('minUnitQuantity', scope.row, scope.$index, pIndex)
                            "
                          />
                        </el-form-item>
                        <!-- 剂量单位 -->
                        <el-select
                          v-model="scope.row.minUnitCode"
                          style="width: 80px; margin-right: 20px"
                          placeholder=" "
                        >
                          <template v-for="item in scope.row.unitCodeList" :key="item.value">
                            <el-option
                              v-if="
                                scope.row.unitCodeList.length == 3
                                  ? item.type == unitMap['minUnit']
                                  : item.type == unitMap['unit']
                              "
                              :value="item.value"
                              :label="item.label"
                            />
                          </template>
                        </el-select>
                        <!-- 单次剂量 -->
                        <el-form-item
                          label="特殊煎法："
                          prop="dosageInstruction"
                          class="required-field"
                          data-prop="dosageInstruction"
                        >
                          <el-select
                            v-model="scope.row.dosageInstruction"
                            style="width: 90px; margin-right: 20px"
                            placeholder=" "
                          >
                            <template v-for="item in dosage_instruction" :key="item.value">
                              <el-option :value="item.value" :label="item.label" />
                            </template>
                          </el-select>
                        </el-form-item>
                      </div>
                      <span class="total-amount">
                        总金额：{{
                          scope.row.totalPrice ? scope.row.totalPrice + ' 元' : '0.00 元'
                        }}
                      </span>
                      <el-button
                        type="primary"
                        @click="handleSaveSign(scope.row, scope.$index, pIndex)"
                      >
                        确定
                      </el-button>
                    </div>
                  </template>
                </div>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column label="" align="center" prop="groupId" width="60">
            <template #header>
              <el-checkbox
                v-model="prescription.checkAll"
                @change="
                  (value) => {
                    prescription.prescriptionList.forEach((item, index) => {
                      prescription.groupIndexList.push(index);
                      item.check = value;
                    });
                  }
                "
              />
            </template>
            <template #default="scope">
              <el-checkbox
                v-model="scope.row.check"
                @dblclick.stop=""
                placeholder=""
                @change="(value) => handleCheckBoxChange(value, scope.$index, scope.row, pIndex)"
              />
            </template>
          </el-table-column>
          <el-table-column label="医嘱" align="center" prop="productName" width="400">
            <template #default="scope">
              <template v-if="getRowDisabled(scope.row)">
                <el-popover
                  :popper-style="{ padding: '0' }"
                  placement="bottom-start"
                  :visible="scope.row.showPopover"
                  :width="1200"
                >
                  <tcmMedicineList
                    ref="adviceTableRef"
                    :popoverVisible="scope.row.showPopover"
                    :adviceQueryParams="adviceQueryParams"
                    :patientInfo="props.patientInfo"
                    @selectAdviceBase="(row) => selectAdviceBase(scope.row.uniqueKey, row, pIndex)"
                  />
                  <template #reference>
                    <el-input
                      :ref="'adviceRef' + pIndex + '-' + scope.$index"
                      style="width: 50%"
                      v-model="scope.row.adviceName"
                      placeholder="请选择项目"
                      @input="handleChange"
                      @click="handleFocus(scope.row, scope.$index, pIndex)"
                      @keyup.enter.stop="handleFocus(scope.row, scope.$index, pIndex)"
                      @keydown="
                        (e) => {
                          if (!scope.row.showPopover) return;
                          // 拦截上下键和回车事件
                          if (['ArrowUp', 'ArrowDown', 'Enter'].includes(e.key)) {
                            e.preventDefault();
                            // 传递事件到弹窗容器
                            adviceTableRef.handleKeyDown(e);
                          }
                        }
                      "
                      @blur="handleBlur(scope.row)"
                    />
                  </template>
                </el-popover>
              </template>
              <span v-else>{{ scope.row.adviceName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" prop="" width="90">
            <template #default="scope">
              <el-tag v-if="scope.row.statusEnum == 2" type="success">已签发</el-tag>
              <el-tag v-else-if="!scope.row.requestId && scope.row.statusEnum == 1" type="warning">
                待保存
              </el-tag>
              <el-tag v-else-if="scope.row.statusEnum == 1" type="primary">待签发</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="数量" align="center" prop="">
            <template #default="scope">
              <span v-if="!scope.row.isEdit">
                {{
                  scope.row.quantity
                    ? formatNumber(scope.row.quantity) +
                      ' ' +
                      scope.row.doseUnitCode_dictText +
                      ' (' +
                      scope.row.chineseHerbsDoseQuantity +
                      '付)'
                    : ''
                }}
              </span>
            </template>
          </el-table-column>
          <el-table-column label="总金额" align="right" prop="" header-align="center">
            <template #default="scope">
              <span v-if="!scope.row.isEdit" style="text-align: right">
                {{ scope.row.totalPrice ? Number(scope.row.totalPrice).toFixed(2) + ' 元' : '-' }}
              </span>
            </template>
          </el-table-column>
          <el-table-column label="特殊煎法" align="right" prop="" header-align="center">
            <template #default="scope">
              <span v-if="!scope.row.isEdit" style="text-align: right">
                {{
                  scope.row.dosageInstruction_dictText ? scope.row.dosageInstruction_dictText : '-'
                }}
              </span>
            </template>
          </el-table-column>
          <el-table-column label="药房/科室" align="center" prop="" width="240">
            <template #default="scope">
              <span v-if="!scope.row.isEdit">
                {{ scope.row.positionName }}
              </span>
            </template>
          </el-table-column>
          <el-table-column label="诊断" align="center" prop="diagnosisName" width="200">
            <template #default="scope">
              <span v-if="!scope.row.isEdit">
                {{ scope.row.diagnosisName || scope.row.conditionDefinitionName }}
              </span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <tcmdiagnosisDialog ref="tcmDianosis" :patientInfo="props.patientInfo" @flush="getListInfo()" />
  </div>
</template>

<script setup>
import {
  getContract,
  getDiagnosisDefinitionList,
  getOrgTree,
  getTcmAdviceList,
  getTcmDiagnosis,
  savePrescription,
  saveTcmAdvice,
  signOutTcmAdvice,
  signTcmAdvice,
} from '@/views/doctorstation/components/api';
import tcmMedicineList from './tcmMedicineList';
import {computed, getCurrentInstance, nextTick} from 'vue';
import {formatNumber} from '@/utils/his';
import Decimal from 'decimal.js';
import tcmdiagnosisDialog from './tcmdiagnosisDialog';
import {Delete} from '@element-plus/icons-vue';
import {ElMessage} from 'element-plus';
import printUtils, {PRINT_TEMPLATE} from '@/utils/printUtils';
import {advicePrint} from '@/api/public';

const emit = defineEmits(['selectDiagnosis']);
const total = ref(0);
const queryParams = ref({});
const form = ref({});
const adviceQueryParams = ref({});
const rowIndex = ref(-1);
const groupIndex = ref(1);
const diagnosisList = ref([]);
const patientId = ref('');
const encounterId = ref('');
const accountId = ref('');
const encounterDiagnosisId = ref('');

let prescriptionIdCounter = 1;
const generatePrescriptionId = () => {
  return `prescription_${Date.now()}_${prescriptionIdCounter++}`;
};

const tcmPrescriptionList = ref([
  {
    id: generatePrescriptionId(),
    prescriptionList: [],
    conditionDefinitionId: '',
    accountId: '',
    methodCode: '',
    rateCode: '',
    dispensePerDuration: '',
    chineseHerbsDoseQuantity: '',
    sufferingFlag: '0',
    checkAll: false,
    groupIndexList: [],
    expandOrder: [],
    totalAmount: 0,
    nextId: 1,
    isAdding: false,
  },
]);
const unitCodeList = ref([]);
const adviceTableRef = ref([]);
const organization = ref([]);
const loading = ref(false);
const rowRules = ref({
  minUnitQuantity: [{ required: true, message: '请输入单次剂量', trigger: 'change' }],
});
const unitMap = ref({
  dose: 'dose',
  minUnit: 'minUnit',
  unit: 'unit',
});
const buttonDisabled = computed(() => {
  return !props.patientInfo;
});
const props = defineProps({
  patientInfo: {
    type: Object,
    required: true,
  },
  activeTab: {
    type: String,
  },
});
const prescriptionRef = ref();
const stockList = ref([]);
const contractList = ref([]);
const tcmDiagnosisList = ref([]);
const conditionId = ref('');
const checkAll = ref(false);
const { proxy } = getCurrentInstance();
const inputRefs = ref({}); // 存储输入框实例
const requiredProps = ref([]); // 存储必填项 prop 顺序
const totalAmount = ref(0);
const tcmDianosis = ref();
const { method_code, unit_code, rate_code, distribution_category_code, dosage_instruction } =
  proxy.useDict(
    'method_code',
    'unit_code',
    'rate_code',
    'distribution_category_code',
    'dosage_instruction'
  );

onMounted(() => {
  document.addEventListener('keydown', escKeyListener);
});

onBeforeUnmount(() => {
  document.removeEventListener('keydown', escKeyListener);
});

function calculatePrescriptionTotal(pIndex) {
  const prescription = tcmPrescriptionList.value[pIndex];
  if (!prescription.prescriptionList) return 0;

  return prescription.prescriptionList
    .reduce((total, item) => {
      return total + (Number(item.totalPrice) || 0);
    }, 0)
    .toFixed(2);
}

function getList() {
  getDiagnosisDefinitionList(queryParams.value).then((res) => {
    total.value = res.data.total;
  });
}

async function getListInfo(addNewRow) {
  // 重置所有处方的添加状态
  tcmPrescriptionList.value.forEach((prescription) => {
    prescription.isAdding = false;
  });
  
  // 如果没有encounterId,不调用接口
  if (!props.patientInfo || !props.patientInfo.encounterId) {
    console.warn('【中医处方】patientInfo或encounterId为空,不调用接口')
    tcmPrescriptionList.value = []
    return
  }
  
  console.log('【中医处方】调用API,encounterId:', props.patientInfo.encounterId)
  await getTcmAdviceList({ encounterId: props.patientInfo.encounterId }).then(async (res) => {
    console.log('【中医处方】获取数据成功,处方数量:', res.data?.length || 0)
    // 按 groupId 分组数据
    const groupedData = {};
    res.data.forEach((item) => {
      const groupId = item.groupId || 'default';
      if (!groupedData[groupId]) {
        groupedData[groupId] = [];
      }
      groupedData[groupId].push({
        ...item,
        doseQuantity: JSON.parse(item.contentJson)?.doseQuantity,
        doseUnitCode_dictText: JSON.parse(item.contentJson)?.doseUnitCode_dictText,
      });
    });

    await getContract({ encounterId: props.patientInfo.encounterId }).then((res) => {
      contractList.value = res.data;
    });
    // 更新处方列表
    const groupIds = Object.keys(groupedData);
    tcmPrescriptionList.value = groupIds.map((groupId, index) => {
      const prescription = tcmPrescriptionList.value[index] || {
        id: generatePrescriptionId(),
        prescriptionList: [],
        conditionDefinitionId: '',
        accountId: contractList.value[0].accountId,
        methodCode: '',
        rateCode: '',
        dispensePerDuration: '',
        chineseHerbsDoseQuantity: '',
        sufferingFlag: '0',
        checkAll: false,
        groupIndexList: [],
        expandOrder: [],
        totalAmount: 0,
        nextId: 1,
        isAdding: false,
      };

      if (!prescription.id) {
        prescription.id = generatePrescriptionId();
      }

      prescription.prescriptionList = groupedData[groupId];
      return prescription;
    });

    // 如果没有处方数据，确保至少有一个空处方
    if (tcmPrescriptionList.value.length === 0) {
      tcmPrescriptionList.value.push({
        id: generatePrescriptionId(),
        prescriptionList: [],
        conditionDefinitionId: '',
        accountId: contractList.value[0].accountId,
        methodCode: '',
        rateCode: '',
        dispensePerDuration: '',
        chineseHerbsDoseQuantity: '',
        sufferingFlag: '0',
        checkAll: false,
        groupIndexList: [],
        expandOrder: [],
        totalAmount: 0,
        nextId: 1,
        isAdding: false,
      });
    }
    tcmPrescriptionList.value.accountId = contractList.value[0].accountId;
    if (props.activeTab == 'prescription' && addNewRow) {
      handleAddMedicine(0);
    }
  }).catch(error => {
    console.error('获取医嘱列表异常:', error);
    // 保持当前列表不变，不要清空
  });

  tcmDiagnosisList.value = getFromDiagnosis(props.patientInfo.encounterId);
  accountId.value = props.patientInfo.accountId;
}

function getDiagnosisInfo() {
  // 如果没有encounterId,不调用接口
  if (!props.patientInfo || !props.patientInfo.encounterId) {
    console.warn('【中医诊断】patientInfo或encounterId为空,不调用接口')
    diagnosisList.value = []
    return
  }
  
  diagnosisList.value = [];
  console.log('【中医诊断】调用API,encounterId:', props.patientInfo.encounterId)
  getTcmDiagnosis({ encounterId: props.patientInfo.encounterId }).then((res) => {
    if (res.data.illness.length > 0) {
      res.data.illness.forEach((item, index) => {
        diagnosisList.value.push({
          name: item.name + '-' + res.data.symptom[index].name,
          definitionId: item.definitionId,
          encounterDiagnosisId: item.encounterDiagnosisId,
          conditionId: item.conditionId,
        });
      });
    }
    console.log('【中医诊断】获取数据成功,诊断数量:', diagnosisList.value.length)
    // 默认选择第一个诊断
    if (diagnosisList.value.length > 0) {
      const firstDiagnosis = diagnosisList.value[0];
      tcmPrescriptionList.value.forEach((prescription) => {
        prescription.diagnosisName = firstDiagnosis.name;
        prescription.conditionId = firstDiagnosis.conditionId;
        prescription.encounterDiagnosisId = firstDiagnosis.encounterDiagnosisId;
        prescription.conditionDefinitionId = firstDiagnosis.definitionId;
      });
    }
  });
}

function getRowDisabled(row) {
  return row.isEdit;
}

// 新增医嘱
function handleAddMedicine(pIndex) {
  const prescription = tcmPrescriptionList.value[pIndex];
  if (!prescription.conditionId) {
    proxy.$modal.msgWarning('请选择诊断');
    return;
  }
  if (prescription.isAdding) {
    proxy.$modal.msgWarning('请先保存当前医嘱');
    return;
  }
  prescription.isAdding = true;
  let groupId = '';
  if (prescription.prescriptionList.length > 0) {
    groupId = prescription.prescriptionList[0].groupId;
  }
  // 在数组最前方添加一行，让新增行显示在最上边
  prescription.prescriptionList.unshift({
    uniqueKey: prescription.nextId++,
    showPopover: false,
    check: false,
    isEdit: true,
    statusEnum: 1,
    groupId: groupId,
  });

  nextTick(() => {
    const refName = 'adviceRef' + pIndex + '-0';
    if (proxy.$refs[refName] && proxy.$refs[refName][0]) {
      proxy.$refs[refName][0].focus();
    }
  });
}

/**
 * 点击行赋值
 */
function clickRow(row) {
  emit('selectDiagnosis', row);
}

// 行双击打开编辑块，仅待发送的可编辑
function clickRowDb(row, pIndex) {
  const prescription = tcmPrescriptionList.value[pIndex];
  if (row.statusEnum == 1) {
    row = { ...row, ...JSON.parse(row.contentJson), uniqueKey: row.uniqueKey };
    row.isEdit = true;
    row.doseUnitCode == JSON.parse(JSON.stringify(row.minUnitCode));
    const index = prescription.prescriptionList.findIndex(
      (item) => item.uniqueKey === row.uniqueKey
    );
    prescription.prescriptionList[index] = row;
    prescription.expandOrder = [row.uniqueKey];
  }
}

function handleDiagnosisChange(item, pIndex) {
  const prescription = tcmPrescriptionList.value[pIndex];
  prescription.diagnosisName = item.name;
  prescription.conditionId = item.conditionId;
  prescription.encounterDiagnosisId = item.encounterDiagnosisId;
}

function handleFocus(row, index, pIndex) {
  rowIndex.value = index;
  row.showPopover = true;
}

function handleBlur(row) {
  row.showPopover = false;
}

function handleChange(value) {
  adviceQueryParams.value.searchKey = value;
}

function addNewPrescription() {
  tcmPrescriptionList.value.push({
    id: generatePrescriptionId(),
    prescriptionList: [],
    conditionDefinitionId: '',
    accountId: accountId.value,
    methodCode: '',
    rateCode: '',
    dispensePerDuration: '',
    chineseHerbsDoseQuantity: '',
    sufferingFlag: '0',
    checkAll: false,
    groupIndexList: [],
    expandOrder: [],
    totalAmount: 0,
    nextId: 1,
    isAdding: false,
  });
}

function deletePrescription(prescription) {
  if (tcmPrescriptionList.value.length <= 1) {
    proxy.$modal.msgError('至少保留一张处方');
    return;
  }

  const index = tcmPrescriptionList.value.findIndex((p) => p.id === prescription.id);
  if (index !== -1) {
    const prescriptionData = tcmPrescriptionList.value[index];

    if (prescriptionData.prescriptionList && prescriptionData.prescriptionList.length > 0) {
      proxy.$modal.msgWarning('该处方单还有药品，请先删除所有药品后再删除处方单');
      return;
    }

    const hasChargedItems = prescriptionData.prescriptionList && prescriptionData.prescriptionList.some(item => item.statusEnum === 2);
    if (hasChargedItems) {
      proxy.$modal.msgWarning('该处方单已收费，不能删除');
      return;
    }

    tcmPrescriptionList.value.splice(index, 1);
  }
}

// 删除处方单
function handleDeletePrescriptionClick(prescriptionIndex) {
  if (!isPrescriptionDeletable(prescriptionIndex)) {
    const medicineCount = getPrescriptionMedicineCount(prescriptionIndex);
    if (medicineCount > 0) {
      proxy.$modal.msgWarning('该处方单还有药品，请先删除所有药品后再删除处方单');
      return;
    }
    
    // 检查是否有已签发的药品
    const prescription = tcmPrescriptionList.value[prescriptionIndex];
    const hasChargedItems = prescription.prescriptionList.some(item => item.statusEnum === 2);
    if (hasChargedItems) {
      proxy.$modal.msgWarning('该处方单已收费，不能删除');
      return;
    }
  }
  
  // 确认删除
  proxy.$modal.confirm('确定要删除这个处方单吗？').then(() => {
    tcmPrescriptionList.value.splice(prescriptionIndex, 1);
    proxy.$modal.msgSuccess('处方单删除成功');
  }).catch(() => {
    // 用户取消删除
  });
}

// 检查处方是否可删除
function isPrescriptionDeletable(prescriptionIndex) {
  const medicineCount = getPrescriptionMedicineCount(prescriptionIndex);
  if (medicineCount > 0) {
    return false;
  }
  
  // 检查是否有已签发的药品
  const hasChargedItems = tcmPrescriptionList.value.some(item => 
    item.prescriptionList && item.prescriptionList.some(med => med.statusEnum === 2)
  );
  return !hasChargedItems;
}

// 计算处方总价
function getPrescriptionTotalPrice(prescriptionIndex) {
  const prescription = tcmPrescriptionList.value[prescriptionIndex];
  let totalPrice = 0;
  if (prescription && prescription.prescriptionList) {
    prescription.prescriptionList.forEach(item => {
      // 使用decimal.js确保精度计算
      const quantity = new Decimal(item.minUnitQuantity || 0);
      const unitPrice = new Decimal(item.unitPrice || 0);
      totalPrice += quantity.mul(unitPrice).toNumber();
    });
  }
  return totalPrice;
}

// 获取处方中的药品数量
function getPrescriptionMedicineCount(prescriptionIndex) {
  const prescription = tcmPrescriptionList.value[prescriptionIndex];
  if (prescription && prescription.prescriptionList) {
    return prescription.prescriptionList.length;
  }
  return 0;
}

/**
 * 选择药品回调
 */
function selectAdviceBase(key, row, pIndex) {
  // ... 保持原有逻辑，但使用对应处方的数据 ...
  const prescription = tcmPrescriptionList.value[pIndex];
  getOrgList();
  unitCodeList.value = [];
  unitCodeList.value.push({ value: row.unitCode, label: row.unitCode_dictText, type: 'unit' });
  if (row.doseUnitCode != row.minUnitCode) {
    unitCodeList.value.push({
      value: row.doseUnitCode,
      label: row.doseUnitCode_dictText,
      type: 'dose',
    });
  }
  if (
    (row.partAttributeEnum == 1 || row.partAttributeEnum == 3) &&
    row.minUnitCode != row.unitCode
  ) {
    unitCodeList.value.push({
      value: row.minUnitCode,
      label: row.minUnitCode_dictText,
      type: 'minUnit',
    });
  }
  if (row.adviceType == 2 && row.minUnitCode != row.unitCode) {
    unitCodeList.value.push({
      value: row.minUnitCode,
      label: row.minUnitCode_dictText,
      type: 'minUnit',
    });
  }
  const rowData = JSON.parse(JSON.stringify(row));
  prescription.prescriptionList[rowIndex.value] = {
    ...prescription.prescriptionList[rowIndex.value], // 保留原有属性（如 uniqueKey, groupId）
    ...rowData, // 覆盖药品信息
    statusEnum: 1, // 【关键修复】强制状态为“待签发/草稿”，确保能被保存过滤器捕获
    adviceDefinitionId: rowData.adviceDefinitionId // 确保这个字段有值，后端会根据这个字段过滤空行
  };
  prescription.prescriptionList[rowIndex.value].orgId = undefined;
  prescription.prescriptionList[rowIndex.value].dose = undefined;
  prescription.prescriptionList[rowIndex.value].unitCodeList = unitCodeList.value;
  prescription.prescriptionList[rowIndex.value].doseUnitCode = row.doseUnitCode;
  prescription.prescriptionList[rowIndex.value].minUnitCode = row.minUnitCode;
  prescription.prescriptionList[rowIndex.value].unitCode =
    row.partAttributeEnum == 1 ? row.minUnitCode : row.unitCode;
  prescription.prescriptionList[rowIndex.value].definitionId = JSON.parse(
    JSON.stringify(row)
  ).chargeItemDefinitionId;

  // 库存列表 + 价格列表拼成批次号的下拉框
  if (row.adviceType == 1 || row.adviceType == 2) {
    if (row.inventoryList && row.inventoryList.length == 0) {
      prescription.expandOrder = [];
      proxy.$modal.msgWarning('该项目无库存');
      return;
    }
    stockList.value = row.inventoryList.map((item, index) => {
      return { ...item, ...row.priceList[index] };
    });
    prescription.prescriptionList[rowIndex.value].stockList = stockList.value;
    // 获取默认批次号的库存，如果没有让医生重新选
    let stock = stockList.value.filter((item) => {
      return item.lotNumber == row.defaultLotNumber;
    })[0];
    if (stock != {} && stock != undefined) {
      if (stock.quantity <= 0) {
        proxy.$modal.msgWarning('该项目库存不足，请选择其它库房');
        // return;
      }
      prescription.prescriptionList[rowIndex.value].lotNumber = stock.lotNumber;
      prescription.prescriptionList[rowIndex.value].inventoryId = stock.inventoryId;
      prescription.prescriptionList[rowIndex.value].locationId = stock.locationId;
      prescription.prescriptionList[rowIndex.value].unitPrice = stock.price;
      prescription.prescriptionList[rowIndex.value].positionName = stock.locationName;
    }
  } else {
    prescription.prescriptionList[rowIndex.value].orgId = JSON.parse(
      JSON.stringify(row)
    ).positionId;
    prescription.prescriptionList[rowIndex.value].unitPrice = row.priceList[0].price;
  }
  prescription.expandOrder = [key];
  nextTick(() => {
    if (row.adviceType == 1) {
      if (row.injectFlag == 1) {
        inputRefs.value['executeNum']?.focus();
      } else {
        inputRefs.value['dose']?.focus();
      }
    } else {
      inputRefs.value['quantity']?.focus();
    }
  });
}

function getOrgList() {
  getOrgTree().then((res) => {
    organization.value = res.data.records;
  });
}

function handleDelete(pIndex) {
  const prescription = tcmPrescriptionList.value[pIndex];
  let selectRow = prescription.prescriptionList.filter((item) => {
    return item.check;
  });
  if (selectRow.length == 0) {
    proxy.$modal.msgWarning('请选择要删除的医嘱');
    return;
  }
  let deleteList = [];
  for (let i = prescription.prescriptionList.length - 1; i >= 0; i--) {
    let deleteItem = prescription.prescriptionList[i];
    // 通过requestId判断是否已保存，如果选中项未保存 直接从数组中移除，如果已保存，调接口删除
    if (deleteItem.check && deleteItem.statusEnum == 1 && !deleteItem.requestId) {
      prescription.prescriptionList.splice(i, 1);
    } else if (deleteItem.check && deleteItem.statusEnum == 1 && deleteItem.requestId) {
      deleteList.push({
        requestId: deleteItem.requestId,
        prescriptionNo: deleteItem.prescriptionNo,
        dbOpType: '3',
        adviceType: deleteItem.adviceType,
        encounterId: deleteItem.encounterId,  // 🔧 BugFix#219: 添加就诊ID
        patientId: deleteItem.patientId,      // 🔧 BugFix#219: 添加患者ID
      });
    }
  }

  if (deleteList.length > 0) {
    savePrescription({ adviceSaveList: deleteList }).then((res) => {
      if (res.code == 200) {
        proxy.$modal.msgSuccess('操作成功');
        getListInfo(false);
      }
    });
    getListInfo();
  } else {
    proxy.$modal.msgWarning('所选医嘱不可删除，请先撤回后再删除');
    return;
  }
  prescription.expandOrder = [];
  prescription.isAdding = false;
  adviceQueryParams.value.adviceType = undefined;
}

function handleNumberClick(item, index, pIndex) {
  const prescription = tcmPrescriptionList.value[pIndex];
  prescription.prescriptionList[index].unitPrice = item.price;
  prescription.prescriptionList[index].locationId = item.locationId;
  prescription.prescriptionList[index].positionId = item.locationId;
  prescription.prescriptionList[index].positionName = item.locationName;
}

/**
 * 签发处方
 */
function handleSave(pIndex) {
  const prescription = tcmPrescriptionList.value[pIndex];
  if (prescription.expandOrder.length > 0) {
    proxy.$modal.msgWarning('请先保存当前医嘱');
    return;
  }
  if (
    prescription.prescriptionList[0] &&
    prescription.prescriptionList[0].isEdit &&
    !prescription.prescriptionList[0].adviceType
  ) {
    prescription.prescriptionList.shift();
    prescription.isAdding = false;
    prescription.expandOrder = [];
  }
  let saveList = prescription.prescriptionList.filter((item) => {
    return item.statusEnum == 1;
  });

  let validList = saveList.filter((item) => {
    return !item.requestId;
  });
  if (validList.length > 0) {
    proxy.$modal.msgWarning('存在未保存的医嘱，请先点击保存按钮后再执行签发');
    return;
  }
  if (saveList.length == 0) {
    proxy.$modal.msgWarning('当前无可签发处方');
    return;
  }
  if (!validateGroups(saveList)) {
    return;
  }
  let list = saveList.map((item) => {
    return {
      ...item,
      adviceType: item.adviceType,
      requestId: item.requestId,
      groupId: item.groupId,
      patientId: props.patientInfo.patientId,
      encounterId: props.patientInfo.encounterId,
      accountId: accountId.value,
      conditionId: prescription.conditionId,
      encounterDiagnosisId: prescription.encounterDiagnosisId,
      diagnosisName: prescription.diagnosisName,
      dbOpType: '1',
      uniqueKey: undefined,
      statusEnum: 2,
    };
  });
  signTcmAdvice({
    organizationId: props.patientInfo.orgId,
    sufferingFlag: prescription.sufferingFlag,
    adviceSaveList: list,
  })
    .then((res) => {
      if (res.code === 200) {
        proxy.$modal.msgSuccess('保存成功');
        getListInfo(false);
        prescription.nextId = 1;
      }
    })
    .finally(() => {
      loading.value = false;
    });
}

// 单行处方保存
function handleSaveSign(row, index, pIndex) {
  const prescription = tcmPrescriptionList.value[pIndex];
  const formRefName = 'formRef' + pIndex + '-' + index;
  proxy.$refs[formRefName][0].validate((valid) => {
    if (valid) {
      row.isEdit = false;
      prescription.isAdding = false;
      prescription.expandOrder = [];
      row.contentJson = undefined;
      row.patientId = props.patientInfo.patientId;
      row.encounterId = props.patientInfo.encounterId;
      row.accountId = prescription.accountId;
      row.quantity = row.minUnitQuantity;
      row.chineseHerbsDoseQuantity = prescription.chineseHerbsDoseQuantity;
      row.unitPrice =
        row.unitCodeList.find((item) => item.value == row.unitCode).type == 'unit'
          ? row.unitPrice
          : new Decimal(row.unitPrice).div(row.partPercent).toFixed(2);
      row.conditionDefinitionId = prescription.conditionDefinitionId;
      row.encounterDiagnosisId = prescription.encounterDiagnosisId;
      row.diagnosisName = prescription.diagnosisName;

      // 寻找当前选中的单位字典值
      const selectedUnit = row.unitCodeList.find((item) => item.value === row.minUnitCode);
      if (selectedUnit) {
        row.doseUnitCode_dictText = selectedUnit.label;
      }

      row.contentJson = JSON.stringify(row);
    }
  });
}

function handleSaveBatch(pIndex) {
  const prescription = tcmPrescriptionList.value[pIndex];

  let timestamp = new Date().getTime();
  let saveList = prescription.prescriptionList
    .filter((item) => {
      return item.statusEnum == 1;
    })
    .map((item, index) => {
      return {
        ...item,
        accountId: accountId.value,
        conditionId: prescription.conditionId,
        encounterDiagnosisId: prescription.encounterDiagnosisId,
        conditionDefinitionId: prescription.conditionDefinitionId,
        encounterId: props.patientInfo.encounterId,
        patientId: props.patientInfo.patientId,
        requestId: item.requestId,
        groupId: item.groupId ? item.groupId : timestamp.toString(),
        methodCode: prescription.methodCode,
        rateCode: prescription.rateCode,
        dispensePerDuration: prescription.dispensePerDuration,
        chineseHerbsDoseQuantity: prescription.chineseHerbsDoseQuantity,
        dbOpType: item.requestId ? '2' : '1',
      };
    });
  if (saveList.length == 0) {
    proxy.$modal.msgWarning('当前没有可保存医嘱');
    return;
  }
  if (
    prescription.chineseHerbsDoseQuantity == undefined ||
    prescription.chineseHerbsDoseQuantity == 0
  ) {
    proxy.$modal.msgWarning('请输入付数');
    return;
  }
  saveTcmAdvice({ adviceSaveList: saveList }).then((res) => {
    if (res.code === 200) {
      proxy.$modal.msgSuccess('保存成功');
      getListInfo(true);
      prescription.nextId = 1;
    }
  });
}

// 获取诊断列表
function getFromDiagnosis(encounterId) {
  try {
    const storageKey = `tcmDiagnosisList_${encounterId}`;
    const data = localStorage.getItem(storageKey);
    return data ? JSON.parse(data) : null;
  } catch (error) {
    console.error('从本地缓存读取失败:', error);
    return null;
  }
}
// 处理回车事件
const handleEnter = (currentProp, row, rowIndex, pIndex) => {
  const formRefName = 'formRef' + pIndex + '-' + rowIndex;
  proxy.$refs[formRefName].validateField(currentProp, (valid) => {
    if (valid) {
      const index = requiredProps.value.indexOf(currentProp);
      if (index === -1) return;
      // 跳转下一个或提交
      const nextIndex = index + 1;
      if (nextIndex < requiredProps.value.length) {
        setTimeout(() => {
          const nextProp = requiredProps.value[nextIndex];
          inputRefs.value[nextProp]?.focus(); // 直接调用 Element 的 focus 方法
        }, 100);
      } else {
        handleSaveSign(row, rowIndex, pIndex);
      }
    }
  });
};

function escKeyListener(e) {
  if (e.key === 'Escape') {
    // 查找正在添加的处方
    const addingPrescriptionIndex = tcmPrescriptionList.value.findIndex((p) => p.isAdding);
    if (addingPrescriptionIndex !== -1) {
      const prescription = tcmPrescriptionList.value[addingPrescriptionIndex];
      let index;
      if (prescription.expandOrder.length > 0) {
        index = prescription.prescriptionList.findIndex(
          (item) => item.uniqueKey == prescription.expandOrder[0]
        );
      }
      if (index == 0) {
        prescription.expandOrder = [];
      }
      prescription.prescriptionList.shift();
      prescription.isAdding = false;
    }
  }
}

// 签退
function handleSingOut(pIndex) {
  const prescription = tcmPrescriptionList.value[pIndex];
  let requestIdList = prescription.prescriptionList
    .filter((item) => {
      return item.check && item.statusEnum == 2;
    })
    .map((item) => {
      return item.requestId;
    });
  if (requestIdList.length == 0) {
    proxy.$modal.msgWarning('请选择已签发医嘱撤回');
  }

  signOutTcmAdvice(requestIdList).then((res) => {
    if (res.code == 200) {
      proxy.$modal.msgSuccess('操作成功');
      getListInfo(false);
    }
  });
}

// 处理行checkbox选中
/**
 * 处理行 checkbox 选中 (修改版)
 * 删除了根据 groupId 自动联动的逻辑，支持单选。
 */
function handleCheckBoxChange(value, index, row, pIndex) {
  const prescription = tcmPrescriptionList.value[pIndex];

  // 1. 基础逻辑：记录或移除当前选中行的索引
  if (value) {
    // 选中时，将当前行索引记录到 groupIndexList
    prescription.groupIndexList.push(index);
  } else {
    // 取消选中时，从 groupIndexList 中移除
    const targetIndex = prescription.groupIndexList.indexOf(index);
    if (targetIndex !== -1) {
      prescription.groupIndexList.splice(targetIndex, 1);
    }
  }

  // --- 原有的 groupId 联动逻辑已删除 ---
  // 这样医生就可以单独勾选某一味药进行“删除”或“打印”操作，而不会影响同处方的其他药物。
}

// 校验每个组号数量是否大于5和对应分组金额是否大于500
function validateGroups(saveList) {
  // 获取到全部组号和对应的金额
  const groups = saveList
    .filter((item) => item.groupId != null && item.groupId !== '') // 过滤掉 groupId 为空或空字符串的项
    .map((item) => ({
      groupId: item.groupId,
      totalPrice: item.totalPrice,
    }));
  // 计算每个组号数量,以及每组金额
  const counts = {};
  groups.forEach((item) => {
    const groupId = item.groupId;
    const totalPrice = Number(item.totalPrice);
    if (!counts[groupId]) {
      counts[groupId] = {
        count: 0,
        totalPrice: 0,
      };
    }
    counts[groupId].groupId = groupId;
    counts[groupId].count++;
    counts[groupId].totalPrice += totalPrice;
  });

  // 获取组数大于5的组号
  const countStr = Object.values(counts)
    .filter((group) => {
      return group.count > 5;
    })
    .map((group) => group.groupId);
  // 获取总金额大于500的组号
  const totalStr = Object.values(counts)
    .filter((group) => {
      return group.totalPrice > 500;
    })
    .map((group) => group.groupId);
  if (countStr.length > 0) {
    proxy.$modal.msgWarning('分组"' + countStr + '"数量超出限制');
    return false;
  } else if (totalStr.length > 0) {
    proxy.$modal.msgWarning('分组"' + totalStr + '"金额总和超出限制');
    return false;
  }
  return true;
}

async function handlePrint(pIndex) {
  const prescription = tcmPrescriptionList.value[pIndex];
  const selectedRows = prescription.prescriptionList.filter((item) => item.check);

  if (selectedRows.length === 0) {
    ElMessage.warning('请先选择要打印的处方');
    return;
  }

  // 检查是否有待签发或已退费的记录
  const invalidRows = selectedRows.filter(
    (item) => item.statusEnum === 1 || item.chargeStatus === 8
  );

  if (invalidRows.length > 0) {
    ElMessage.warning('待签发和已退费的项目不能打印，请重新选择');
    return;
  }

  try {
    const performPrint = async (rows) => {
      const requestIds = rows.map((item) => item.requestId).join(',');
      console.log('中药处方打印开始', requestIds);

      const apiParams = { requestIds, isPrescription: '1' };
      const res = await advicePrint(apiParams);

      const groupedRows = {};
      res.data.adviceItemList.forEach((row) => {
        const prescriptionNo = row.prescriptionNo;
        if (!groupedRows[prescriptionNo]) {
          groupedRows[prescriptionNo] = [];
        }
        row.contractName = res.data.contractName;
        groupedRows[prescriptionNo].push(row);
      });

      const groupedArray = Object.values(groupedRows);
      const result = groupedArray.map((group) => ({
        ...res.data,

        // 药费
        medTotalAmount:
          '¥' + group.reduce((sum, item) => sum + (item.totalPrice || 0), 0).toFixed(2) + '元',

        prescriptionList: group,
      }));

      // 使用中药医生处方打印模板
      return { result, template: PRINT_TEMPLATE.DOC_CHINESE_MEDICINE_PRESCRIPTION };
    };

    // 执行打印
    const { result, template } = await performPrint(selectedRows);
    printUtils.print(template, result);
    ElMessage.success('中药处方打印成功');
  } catch (error) {
    console.error('中药处方打印过程出错:', error);
    ElMessage.error('中药处方打印失败: ' + error.message);
  }
}
defineExpose({ getListInfo, getDiagnosisInfo });
</script>

<style lang="scss" scoped>
:deep(.el-table__expand-icon) {
  display: none !important;
}

.medicine-title {
  font-size: 16px;
  font-weight: 600;
  min-width: 160px;
  display: inline-block;
}

.total-amount {
  font-size: 16px;
  font-weight: 600;
  color: #409eff;
  white-space: nowrap;
}

.medicine-info {
  font-size: 15px;
  font-weight: 600;
  color: #606266;
  white-space: nowrap;
}

.form-group {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #fff;
  padding: 6px 10px;
  border-radius: 4px;
  border: 1px solid #ebeef5;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.04);
}

/* 调整element组件默认间距 */
// .el-select,
// .el-input-number {
//   margin-right: 0 !important;
// }

.el-input-number .el-input__inner {
  text-align: center;
}

.el-table__cell .el-form-item--default {
  margin-bottom: 0px;
}

.doctor-station {
  color: #606266;
  font-size: 14px;
  font-weight: 700;
  margin-left: 15px;
}

.prescription-section {
  background: white;
  padding: 0 10px 10px 10px;
  margin-bottom: 20px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.first-prescription {
  border-left: 2px solid #409eff;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #eee;
}

.section-header .title {
  display: flex;
  align-items: center;
  font-size: 16px;
  font-weight: bold;
  color: #1a2b6d;
}

.section-header .title i {
  margin-right: 10px;
  font-size: 24px;
}

.summary {
  display: flex;
  gap: 25px;
}

.summary-item {
  background: #f8f9fa;
  padding: 8px 15px;
  border-radius: 20px;
  font-size: 15px;
  color: #555;
}

.add-icon {
  cursor: pointer;
  border-radius: 50%;
  padding: 4px;
  transition: background-color 0.3s ease;
}

.add-icon:hover {
  background-color: #ecf5ff;
}
</style>
