<template>
  <div class="app-container" v-loading="pageLoading" loading-text="审批中。。。">
    <el-row :gutter="10" class="mb8" v-if="viewStatus">
      <el-col :span="1.5">
        <el-button
          v-if="viewStatus != 'view'"
          plain
          type="primary"
          icon="Edit"
          @click="handelApply"
          >审批通过</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-if="viewStatus != 'view'"
          type="primary"
          plain
          icon="Edit"
          @click="handleReject"
          >驳回</el-button
        >
      </el-col>
    </el-row>
    <el-row :gutter="10" class="mb8" v-else>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="submitAudit"
          >提交审核</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="EditPen" @click="handleTotalAmount">
          计算金额
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleSave"
          >批量保存</el-button
        >
      </el-col>
    </el-row>
    <el-button type="primary" plain icon="Printer" @click="handlePrint"> 打印单据 </el-button>
    <el-button type="primary" plain @click="handleExport" v-if="viewStatus == 'view'">
      导出
    </el-button>
    <el-form
      :model="receiptHeaderForm"
      ref="receiptHeaderRef"
      :inline="true"
      label-width="100px"
      :rules="rules"
    >
      <el-form-item label="单据号：" prop="busNo">
        <el-input
          v-model="receiptHeaderForm.busNo"
          placeholder="单据号："
          clearable
          style="width: 200px"
          :disabled="true"
        />
      </el-form-item>
      <el-form-item label="经手人：" prop="practitionerId" label-width="120px">
        <el-select
          v-model="receiptHeaderForm.practitionerId"
          placeholder=""
          clearable
          filterable
          :disabled="viewStatus == 'view' || viewStatus == 'apply'"
          style="width: 150px"
        >
          <el-option
            v-for="practitioner in practitionerListOptions"
            :key="practitioner.value"
            :label="practitioner.label"
            :value="practitioner.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="制单日期：">
        <el-date-picker
          v-model="receiptHeaderForm.occurrenceTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="datetime"
          :disabled="viewStatus == 'view' || viewStatus == 'apply'"
        />
      </el-form-item>
      <el-form-item label="供应商：" prop="supplierId">
        <el-select
          v-model="receiptHeaderForm.supplierId"
          placeholder=""
          clearable
          filterable
          style="width: 150px"
          :disabled="viewStatus == 'view' || viewStatus == 'apply'"
          v-if="viewStatus != 'apply' && viewStatus != 'view'"
        >
          <el-option
            v-for="supplier in supplierListOptions"
            :key="supplier.value"
            :label="supplier.label"
            :value="supplier.value"
          />
        </el-select>
        <el-input
          placeholder="供应商名称"
          v-model="receiptHeaderForm.supplierName"
          clearable
          disabled
          v-else-if="viewStatus == 'view' || viewStatus == 'apply'"
        />
      </el-form-item>
      <el-form-item label="药品类型：" prop="medicationType">
        <el-select
          v-model="receiptHeaderForm.medicationType"
          placeholder=""
          clearable
          filterable
          style="width: 150px"
          :disabled="disabledForm"
          @change="
            (value) => {
              itemType = value;
            }
          "
        >
          <el-option
            v-for="itemType in purchase_type"
            :key="itemType.value"
            :label="itemType.label"
            :value="itemType.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="仓库类型：" prop="purposeTypeEnum">
        <el-select
          v-model="receiptHeaderForm.purposeTypeEnum"
          placeholder=""
          clearable
          style="width: 150px"
          :disabled="disabledForm"
          @change="handleChangeLocationType"
        >
          <el-option
            v-for="dict in warehous_type"
            :key="dict.value"
            :label="dict.label"
            :disabled="disabledForm"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <el-tabs type="border-card">
      <el-tab-pane label="入库单明细">
        <el-row :gutter="10" class="mb8" v-if="!viewStatus">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="addNewRow"
              >添加行</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="Delete"
              :disabled="multiple"
              @click="deleteSelectedRows"
              >删除行</el-button
            >
          </el-col>
        </el-row>
        <el-form
          :model="form"
          :rules="tableRules"
          ref="formRef"
          :disabled="viewStatus == 'apply'"
        >
          <!-- @cell-mouse-enter="handleMouseEnter" -->
          <!-- v-click-outside-row="handleClickOutside" @row-click="handleRowClick" 点击行以外的部分自动保存 -->
          <el-scrollbar height="calc(100vh - 500px)">
            <el-table
              row-key="rowKey"
              v-loading="loading"
              :data="form.purchaseinventoryList"
              @selection-change="handleSelectionChange"
              ref="tableRef"
              @cell-mouse-leave="handleMouseLeave"
            >
              <el-table-column type="selection" width="50" align="center" />
              <el-table-column
                label="项目"
                align="center"
                key="name"
                prop="name"
                width="200"
                fixed
              >
                <template #default="scope">
                  <el-form-item
                    :prop="`purchaseinventoryList.${scope.$index}.name`"
                    :rules="tableRules.name"
                  >
                    <el-input
                      v-if="viewStatus == 'view'"
                      v-model="scope.row.name"
                      placeholder=""
                      disabled
                    />
                    <PopoverList
                      v-else
                      @search="handleSearch"
                      :width="1000"
                      :modelValue="scope.row.name"
                    >
                      <template #popover-content="{}">
                        <MedicineList
                          @selectRow="(row) => selectRow(row, scope.$index)"
                          :searchKey="medicineSearchKey"
                          :itemType="itemType"
                        />
                      </template>
                    </PopoverList>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="规格"
                align="center"
                key="statusEnum_enumText"
                prop="statusEnum_enumText"
                width="150"
              >
                <template #default="scope">
                  <el-form-item
                    :prop="`purchaseinventoryList.${scope.$index}.volume`"
                    :rules="tableRules.volume"
                  >
                    <el-input v-model="scope.row.volume" placeholder="" disabled />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="厂家/产地"
                align="center"
                key="manufacturerText"
                prop="manufacturerText"
                :show-overflow-tooltip="true"
                width="160"
              >
                <template #default="scope">
                  <el-form-item
                    :prop="`purchaseinventoryList.${scope.$index}.manufacturerText`"
                    :rules="tableRules.manufacturerText"
                  >
                    <el-input v-model="scope.row.manufacturerText" placeholder="" disabled />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="仓库"
                align="center"
                key="purposeLocationId"
                prop="purposeLocationId"
                :show-overflow-tooltip="true"
                width="180"
              >
                <template #default="scope">
                  <el-form-item
                    :prop="`purchaseinventoryList.${scope.$index}.purposeLocationId`"
                    :rules="tableRules.purposeLocationId"
                  >
                    <div class="select_wrapper_div">
                      <el-select
                        v-model="scope.row.purposeLocationId"
                        placeholder="请选择仓库"
                        :class="{ 'error-border': scope.row.error }"
                        clearable
                        :disabled="viewStatus == 'view'"
                      >
                        <el-option
                          v-for="(item, index) in locationList"
                          :key="index"
                          :label="item.name"
                          :value="item.id"
                          @click="handleLocationClick(item, scope.row, scope.$index)"
                        />
                      </el-select>
                    </div>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="采购数量"
                align="center"
                key="itemQuantity"
                prop="itemQuantity"
                width="120"
              >
                <template #default="scope">
                  <el-form-item
                    :prop="`purchaseinventoryList.${scope.$index}.itemQuantity`"
                    :rules="tableRules.itemQuantity"
                  >
                    <div class="select_wrapper_div">
                      <el-input
                        :disabled="viewStatus == 'view'"
                        v-model="scope.row.itemQuantity"
                        placeholder=""
                        @blur="handleTotalPrice(scope.$index)"
                        :class="{ 'error-border': scope.row.error }"
                      />
                    </div>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="计量单位"
                align="center"
                key="unitCode"
                prop="unitCode"
                :show-overflow-tooltip="true"
                width="90"
              >
                <template #default="scope">
                  <el-form-item
                    :prop="`purchaseinventoryList.${scope.$index}.unitCode`"
                    :rules="tableRules.unitCode"
                  >
                    <!-- :disabled="data.isEdit" -->
                    <div class="select_wrapper_div">
                      <el-select
                        v-model="scope.row.unitCode"
                        placeholder=" "
                        :class="{ 'error-border': scope.row.error }"
                        :clearable="false"
                        :disabled="viewStatus == 'view'"
                        @change="
                          (value) => handleUnitCodeChange(scope.row, scope.$index, value)
                        "
                      >
                        <template v-if="scope.row.unitList && scope.row.partPercent > 1">
                          <el-option
                            :label="scope.row.unitList.unitCode_dictText"
                            :value="scope.row.unitList.unitCode"
                          />
                          <el-option
                            :label="scope.row.unitList.minUnitCode_dictText"
                            :value="scope.row.unitList.minUnitCode"
                          />
                        </template>
                        <template v-if="scope.row.unitList && scope.row.partPercent == 1">
                          <el-option
                            :label="scope.row.unitList.unitCode_dictText"
                            :value="scope.row.unitList.unitCode"
                          />
                        </template>
                        <template v-if="!scope.row.unitList">
                          <el-option
                            :label="scope.row.unitCode_dictText"
                            :value="scope.row.unitCode"
                          />
                        </template>
                      </el-select>
                    </div>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="药品追溯码"
                align="center"
                key="traceNo"
                prop="traceNo"
                width="130"
              >
                <template #default="scope">
                  <el-tooltip
                    :content="formatContent(scope.row.traceNo)"
                    placement="top"
                    popper-class="custom-tooltip"
                  >
                    <el-form-item
                      :prop="`purchaseinventoryList.${scope.$index}.traceNo`"
                      :rules="tableRules.traceNo"
                    >
                      <el-input
                        :disabled="viewStatus == 'view'"
                        v-model="scope.row.traceNo"
                        @change="(value) => handleTraceNoInput(value, scope.row)"
                        placeholder=""
                        :id="'traceNo' + `${scope.$index}`"
                      />
                    </el-form-item>
                  </el-tooltip>
                </template>
              </el-table-column>
              <el-table-column
                label="追溯码包装"
                align="center"
                key="packagingLevels"
                prop="packagingLevels"
                width="130"
              >
                <template #default="scope">
                  <el-form-item
                    :prop="`purchaseinventoryList.${scope.$index}.packagingLevels`"
                    :rules="tableRules.packagingLevels"
                  >
                    <el-input
                      v-model="scope.row.packagingLevels"
                      placeholder=""
                      :disabled="viewStatus == 'view'"
                    />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="采购单价（包装单位） "
                align="center"
                key="price"
                prop="price"
                width="160"
              >
                <template #default="scope">
                  <el-form-item
                    :prop="`purchaseinventoryList.${scope.$index}.price`"
                    :rules="tableRules.price"
                  >
                    <div class="select_wrapper_div">
                      <el-input
                        v-model="scope.row.price"
                        placeholder=""
                        @blur="handleTotalPrice(scope.$index)"
                        :class="{ 'error-border': scope.row.error }"
                        :disabled="viewStatus == 'view'"
                      >
                        <template #suffix>元</template>
                      </el-input>
                    </div>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="合计金额 "
                align="center"
                key="totalPrice"
                prop="totalPrice"
                width="130"
              >
                <template #default="scope">
                  <el-form-item
                    :prop="`purchaseinventoryList.${scope.$index}.totalPrice`"
                  >
                    <el-input disabled v-model="scope.row.totalPrice" placeholder="">
                      <template #suffix>元</template>
                    </el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="产品批号"
                align="center"
                key="lotNumber"
                prop="lotNumber"
                width="160"
              >
                <template #default="scope">
                  <el-form-item
                    :prop="`purchaseinventoryList.${scope.$index}.lotNumber`"
                    :rules="tableRules.lotNumber"
                  >
                    <el-input
                      v-model="scope.row.lotNumber"
                      placeholder=""
                      maxlength="100"
                      :disabled="viewStatus == 'view'"
                    />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="生产日期"
                align="center"
                key="startTime"
                prop="startTime"
                width="150"
              >
                <template #default="scope">
                  <el-form-item
                    :prop="`purchaseinventoryList.${scope.$index}.startTime`"
                    :rules="tableRules.startTime"
                  >
                    <el-date-picker
                      v-model="scope.row.startTime"
                      type="date"
                      placeholder="选择日期"
                      format="YYYY-MM-DD"
                      value-format="YYYY-MM-DD"
                      :disabled="viewStatus == 'view'"
                      @change="changeValStart($event, scope.$index)"
                    />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="有效期至"
                align="center"
                key="endTime"
                prop="endTime"
                width="150"
              >
                <template #default="scope">
                  <el-form-item
                    :prop="`purchaseinventoryList.${scope.$index}.endTime`"
                    :rules="tableRules.endTime"
                  >
                    <el-date-picker
                      :disabled="viewStatus == 'view'"
                      v-model="scope.row.endTime"
                      type="date"
                      placeholder="选择日期"
                      format="YYYY-MM-DD"
                      value-format="YYYY-MM-DD"
                      @change="changeValEnd($event, scope.$index)"
                    />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="发票号 "
                align="center"
                key="invoiceNo"
                prop="invoiceNo"
                width="130"
              >
                <template #default="scope">
                  <el-form-item
                    :prop="`purchaseinventoryList.${scope.$index}.invoiceNo`"
                    :rules="tableRules.invoiceNo"
                  >
                    <el-input
                      v-model="scope.row.invoiceNo"
                      placeholder=""
                      style="text-align: center !important"
                      :disabled="viewStatus == 'view'"
                    />
                  </el-form-item>
                </template>
              </el-table-column>

              <el-table-column
                label="备注"
                align="center"
                key="remake"
                prop="remake"
                width="130"
              >
                <template #default="scope">
                  <el-form-item
                    :prop="`purchaseinventoryList.${scope.$index}.remake`"
                    :rules="tableRules.remake"
                  >
                    <el-input
                      v-model="scope.row.remake"
                      placeholder=""
                      :disabled="viewStatus == 'view'"
                    />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="货位"
                align="center"
                key="locationStoreName"
                prop="locationStoreName"
                width="180"
              >
                <template #default="scope">
                  <el-form-item
                    :prop="`purchaseinventoryList.${scope.$index}.locationStoreName`"
                    :rules="tableRules.locationStoreName"
                  >
                    <div class="select_wrapper_div">
                      <el-select
                        v-model="scope.row.locationStoreName"
                        placeholder="请选择货位"
                        :class="{ 'error-border': scope.row.error }"
                        clearable
                        :disabled="viewStatus == 'view'"
                      >
                      </el-select>
                    </div>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                label="总库存数量"
                align="center"
                key="quantity"
                prop="quantity"
                width="120"
              >
                <template #default="scope">
                  <el-form-item
                    :prop="`purchaseinventoryList.${scope.$index}.quantity`"
                    :rules="tableRules.quantity"
                  >
                    <el-input v-model="scope.row.quantity" placeholder="" disabled />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column
                v-if="!viewStatus"
                label="操作"
                align="center"
                width="80"
                class-name="small-padding fixed-width"
                fixed="right"
              >
                <template #default="scope">
                  <el-button
                    link
                    type="primary"
                    icon="Edit"
                    @click="handleScan(scope.row, scope.$index)"
                    >扫码</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </el-scrollbar>
        </el-form>
      </el-tab-pane>
    </el-tabs>
    <el-row
      :gutter="10"
      class="mb8"
      style="
        margin-top: 15px;
        display: flex;
        align-items: center;
        justify-content: flex-start;
      "
    >
      <el-col :span="3">
        <span>制单人：{{ userUserStore.name }}</span>
      </el-col>
      <!-- <el-col :span="2">
          <span>审核人：</span>
        </el-col>
        <el-col :span="2">
          <span>单据状态：</span>
        </el-col> -->
      <el-col :span="6">
        <el-row
          :gutter="8"
          style="display: flex; align-items: center; justify-content: flex-start"
        >
          <el-col :span="10">
            <span>合计金额：{{ totalAmount ? totalAmount.toFixed(4) : 0 }}</span>
          </el-col>
          <!-- <el-col :span="10">
              <el-input v-model="totalAmount" placeholder="" disabled />
            </el-col> -->
        </el-row>
      </el-col>
    </el-row>
    <!-- </el-dialog> -->
    <TraceNoDialog
      :ypName="ypName"
      :rowData="rowData"
      :openDialog="openTraceNoDialog"
      @submit="submit"
      @cancel="openTraceNoDialog = false"
    />
  </div>
</template>

<script setup name="PurchaseDocument">
import {
  addPurchaseinventory,
  delPurchaseinventory,
  getCount,
  getDispensaryListAll,
  getInit,
  getInitBusNo,
  getPharmacyList,
  getPharmacyListAll,
  purchaseInventoryApproved,
  reject,
  submitApproval,
} from "./components/api";
import PopoverList from "@/components/OpenHis/popoverList/index.vue";
import MedicineList from "./components/medicineList.vue";
import TraceNoDialog from "@/components/OpenHis/TraceNoDialog/index.vue";
import {formatDate, formatDateymd} from "@/utils/index";
import {useStore} from "@/store/store";
import useUserStore from "@/store/modules/user";
import {ElMessage} from "element-plus";
import {nextTick, onMounted, onUnmounted, ref, watch} from "vue";
import useTagsViewStore from "@/store/modules/tagsView";
import {isEqual} from "lodash";
import templateJson from "./components/templateJson.json";
import {hiprint} from "vue-plugin-hiprint";

const route = useRoute();

const router = useRouter();
const tagsViewStore = useTagsViewStore();
const userUserStore = useUserStore();
const store = useStore();
const rowData = ref({});

const { proxy } = getCurrentInstance();
const {
  warehous_type,
  category_code,
  service_type_code,
  specialty_code,
  purchase_type,
} = proxy.useDict(
  "warehous_type",
  "category_code",
  "service_type_code",
  "specialty_code",
  "purchase_type"
);

const viewStatus = ref("");
const startTimeOld = ref("");
const endTimeOld = ref("");
const purchaseinventoryList = ref([]);
const open = ref(false);
const loading = ref(false);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const visible = ref(false);
const openTraceNoDialog = ref(false);
const ypName = ref("");
const rowIndex = ref(-1);
const totalAmount = ref(0);
const editData = ref({});
const pageLoading = ref(false);
const form = reactive({
  purchaseinventoryList: [],
});
const forms = reactive({
  purchaseinventoryList: [],
});
const receiptHeaderForm = reactive({
  busNo: undefined,
  occurrenceTime: formatDate(new Date()),
  practitionerId: userUserStore.practitionerId,
});

const data = reactive({
  isEdit: false,
  isAdding: true,
  queryParams: {
    pageNo: 1,
    pageSize: 10,
    searchKey: undefined, // 供应商名称
    busNo: undefined, // 编码
    statusEnum: undefined, // 状态
    supplierId: undefined, // 供应商ID
    applyTimeStart: undefined, // 申请时间开始
    practitionerId: undefined, // 经手人ID
  },
  rules: {
    practitionerId: [{ required: true, message: "请选择经手人", trigger: "change" }],
    supplierId: [{ required: true, message: "请选择供应商", trigger: "change" }],
    purposeTypeEnum: [{ required: true, message: "请选择仓库类型", trigger: "change" }],
    medicationType: [{ required: true, message: "请选择药品类型", trigger: "change" }],
  },
  tableRules: {
    itemId: [{ required: true, message: '项目不能为空', trigger: 'blur' }],
    statusEnum_enumText: [{ required: true, message: '规格不能为空', trigger: 'blur' }],
    unitCode: [{ required: true, message: '计量单位不能为空', trigger: 'blur' }],
    purposeLocationId: [{ required: true, message: '目的仓库不能为空', trigger: 'blur' }],
    lotNumber: [{ required: true, message: '产品批号不能为空', trigger: 'change' }],
    startTime: [{ required: true, message: '开始时间不能为空', trigger: 'blur' }],
    endTime: [{ required: true, message: '结束时间不能为空', trigger: 'blur' }],
    price: [{ required: true, message: '单价不能为空', trigger: 'blur' }],
    invoiceNo: [{ required: true, message: '发票号不能为空', trigger: 'blur' }],
    totalPrice: [{ required: true, message: '总价不能为空', trigger: 'blur' }],
  },
});

const { queryParams, rules, tableRules } = toRefs(data);
const itemTypeOptions = ref(undefined); // 入库项目类型
const practitionerListOptions = ref(undefined); // 查询经手人列表
const supplierListOptions = ref(undefined); // 供应商列表
const selectedRows = ref([]); // 用于存储选中的行
const emit = defineEmits(["refresh"]);
const tableRef = ref(undefined); // 表格引用
const currentRow = ref({}); // 当前操作的行
const medicineSearchKey = ref("");
const itemType = ref("");
const price = ref(0);
const unitMap = ref({});
const currentEditRow = ref({});
const currentIndex = ref(-1);
const currentRowId = ref("");
const disabledForm = computed(() => {
  return form.purchaseinventoryList.length > 0;
});
const rowList = ref([]);
const saveRowObject = ref({});

watch(
  () => store.currentData,
  (newVlaue) => {
    if (newVlaue) {
      editData.value.editRow = newVlaue?.editRow;
      editData.value.item = newVlaue?.item;
      // 使用nextTick确保变量初始化完成后再执行edit
      nextTick(() => {
        edit();
      });
    } else {
      editData.value = {};
      // 使用nextTick确保变量初始化完成后再执行show
      nextTick(() => {
        show();
      });
    }
  },
  { immediate: true }
);
watch(
  () => route.query.view,
  (newVlaue) => {
    getPurchaseinventoryTypeList();
    if (newVlaue) {
      viewStatus.value = newVlaue;
    } else {
      viewStatus.value = "";
    }
  },
  { immediate: true }
);
watch(
  () => form.purchaseinventoryList,
  (newVlaue) => {
    if (newVlaue && newVlaue.length > 0) {
      if (viewStatus.value) {
        handleTotalAmount();
      }
    }
  },
  { immediate: true }
);
onMounted(() => {
  if (route.query.view) {
    viewStatus.value = route.query.view;
    // 查看模式下，不要清空store数据，让watch监听器自然处理
  } else {
    viewStatus.value = "";
    // 正常进入模式下，清空store数据，确保显示空表单
    store.clearCurrentData();
  }
  // 不再直接调用show()，而是让watch监听器根据store.currentData状态来决定
});

// 关键修复：在组件卸载时清除store数据，确保离开页面后再次进入时不会显示历史记录
onUnmounted(() => {
  document.removeEventListener("click", handleClickOutside);
  // 只有在非查看模式下才清除store数据
  if (!viewStatus.value) {
    store.clearCurrentData();
  }
});

function addNewRow() {
  // 先校验表头表单
  proxy.$refs['receiptHeaderRef'].validate((valid) => {
    if (valid) {
      // 检查表格中是否有未保存的行
      const unsavedRows = form.purchaseinventoryList.filter((row) => !row.isSave);

      if (unsavedRows.length > 0) {
        // 校验表格中未保存的行
        proxy.$refs['formRef'].validate((tableValid) => {
          if (!tableValid) {
            proxy.$message.warning('请先完善当前行的必填信息后再添加新行');
            return;
          }

          // 手动检查未保存行的必填字段
          const invalidRows = [];
          unsavedRows.forEach((row, index) => {
            const rowIndex = form.purchaseinventoryList.indexOf(row);
            const errors = [];

            // 检查必填字段
            if (!row.itemId || !row.name) {
              errors.push('项目');
            }
            if (!row.volume) {
              errors.push('规格');
            }
            if (!row.unitCode) {
              errors.push('计量单位');
            }
            if (!row.purposeLocationId) {
              errors.push('仓库');
            }
            if (!row.itemQuantity || row.itemQuantity === '' || row.itemQuantity === 0) {
              errors.push('采购数量');
            }
            if (!row.lotNumber) {
              errors.push('产品批号');
            }
            if (!row.startTime) {
              errors.push('生产日期');
            }
            if (!row.endTime) {
              errors.push('有效期至');
            }
            if (!row.price || row.price === '' || row.price === 0) {
              errors.push('采购单价');
            }
            if (!row.invoiceNo || row.invoiceNo === '') {
              errors.push('发票号');
            }
            if (!row.totalPrice || row.totalPrice === '' || row.totalPrice === 0) {
              errors.push('合计金额');
            }

            if (errors.length > 0) {
              invalidRows.push({
                rowIndex: rowIndex + 1,
                errors: errors.join('、'),
              });
            }
          });

          if (invalidRows.length > 0) {
            const errorMsg = invalidRows
              .map((item) => `第${item.rowIndex}行缺少：${item.errors}`)
              .join('；');
            proxy.$message.warning(errorMsg + '，请完善后再添加新行');
            return;
          }

          // 所有校验通过，允许新增
          const newRow = {
            rowKey: Date.now(),
            id: '',
            itemTable: '',
            itemQuantity: '',
            itemId: '',
            unitCode: '',
            remake: '',
            supplierId: '',
            purposeTypeEnum: '',
            purposeLocationId: null,
            purposeLocationStoreId: null,
            practitionerId: '',
            traceNo: '',
            invoiceNo: '-',
            startTime: '',
            endTime: '',
            price: '',
            totalPrice: '',
            sellPrice: '',
            minSellPrice: '',
            unitList: {}, // 单位列表
            isEditing: true, // 标记当前行是否正在编辑
            error: false, // 新增 error 字段
            isSave: false, // 当前行是否保存
          };
          form.purchaseinventoryList.push(newRow);
          data.isAdding = true; // 设置标志位为 true，表示有未保存的
        });
      } else {
        // 没有未保存的行，直接新增
        const newRow = {
          rowKey: Date.now(),
          id: '',
          itemTable: '',
          itemQuantity: '',
          itemId: '',
          unitCode: '',
          remake: '',
          supplierId: '',
          purposeTypeEnum: '',
          purposeLocationId: null,
          purposeLocationStoreId: null,
          practitionerId: '',
          traceNo: '',
          invoiceNo: '-',
          startTime: '',
          endTime: '',
          price: '',
          totalPrice: '',
          sellPrice: '',
          minSellPrice: '',
          unitList: {}, // 单位列表
          isEditing: true, // 标记当前行是否正在编辑
          error: false, // 新增 error 字段
          isSave: false, // 当前行是否保存
        };
        form.purchaseinventoryList.push(newRow);
        data.isAdding = true; // 设置标志位为 true，表示有未保存的
      }
    }
  });
}

function handleBlur(row, index) {
  let hasError = false;
  if (!row.itemTable) {
    if (receiptHeaderForm.medicationType === "1") {
      row.itemTable = "med_medication_definition";
    } else {
      row.itemTable = "adm_device_definition";
    }
  }
  row.practitionerId = receiptHeaderForm.practitionerId;
  row.busNo = receiptHeaderForm.busNo;
  row.occurrenceTime = receiptHeaderForm.occurrenceTime;
  row.supplierId = receiptHeaderForm.supplierId;
  row.purposeTypeEnum = receiptHeaderForm.purposeTypeEnum;
}
const handleMouseEnter = (row, column, cell, event) => {};

const handleMouseLeave = (row, column, cell, event) => {};

// 点击行时记录当前行
function handleRowClick(row) {
  if (row.id != currentRowId.value) {
    handleClickOutside();
  }
  currentEditRow.value = JSON.parse(JSON.stringify(row));
  if (row.id) {
    currentIndex.value = form.purchaseinventoryList.findIndex(
      (item) => item.id == row.id
    );
    currentRowId.value = row.id;
  } else {
    currentIndex.value = form.purchaseinventoryList.length - 1;
  }

  currentRow.value = row;
}
function saveRow(row, index) {
  form.purchaseinventoryList[index] = row;
  addPurchaseinventory(row).then((response) => {
    reset();
    data.isAdding = false; // 允许新增下一行
    proxy.$message.success("保存成功！");
    visible.value = false;
  });
}

function handleScan(row, index) {
  if (row.purposeLocationId && row.lotNumber) {
    rowData.value = row;
    rowData.value.locationId = row.purposeLocationId;
    rowData.value.itemType = receiptHeaderForm.medicationType;
    ypName.value = row.name;
    openTraceNoDialog.value = true;
    currentIndex.value = index;
  } else {
    proxy.$message.warning("请先选择仓库和产品批号！");
  }
}
function submit(value) {
  if (form.purchaseinventoryList[currentIndex.value].traceNo) {
    form.purchaseinventoryList[currentIndex.value].traceNo =
      form.purchaseinventoryList[currentIndex.value].traceNo + "," + value;
  } else {
    form.purchaseinventoryList[currentIndex.value].traceNo = value;
  }
  openTraceNoDialog.value = false;
}

function formatContent(value) {
  let content = "";
  if (value) {
    value.split(",").forEach((item, index) => {
      content += `[${index + 1}] ${item}\n`;
    });
    return content;
  }
}
// 药品列表搜索
function handleSearch(value) {
  medicineSearchKey.value = value;
}

const locationList = ref([]);
// 选择药品
function selectRow(rowValue, index) {
  form.purchaseinventoryList[index] = { id: form.purchaseinventoryList[index].id };
  price.value = 0;
  rowIndex.value = index;
  form.purchaseinventoryList[index].rowKey = Date.now();
  form.purchaseinventoryList[index].itemId = rowValue.definitionId;
  form.purchaseinventoryList[index].name = rowValue.name;
  form.purchaseinventoryList[index].volume = rowValue.volume;
  form.purchaseinventoryList[index].minUnitCode = rowValue.minUnitCode;
  form.purchaseinventoryList[index].unitCode = rowValue.unitCode;
  form.purchaseinventoryList[index].manufacturerText = rowValue.manufacturerText;
  form.purchaseinventoryList[index].partPercent = rowValue.partPercent;
  form.purchaseinventoryList[index].invoiceNo = '-';
  form.purchaseinventoryList[index].price = rowValue.purchaseAmount || 0;
  price.value = rowValue.purchaseAmount || 0;
  form.purchaseinventoryList[index].unitList = rowValue.unitList[0];
  unitMap.value['unitCode'] = rowValue.unitCode;
  unitMap.value['minUnitCode'] = rowValue.minUnitCode;
  unitMap.value['currentCode'] = rowValue.unitCode;
  form.purchaseinventoryList[index].itemQuantity = 0;
  form.purchaseinventoryList[index].totalPrice = 0;
}

let totalQuantity = 0;
// 选择仓库
function handleLocationClick(item, row, index) {
  getCount({
    itemId: row.itemId,
    orgLocationId: item.id,
  }).then((res) => {
    if (res.data) {
      totalQuantity = 0;
      res.data.forEach((item) => {
        totalQuantity += item.orgQuantity;
      });
      form.purchaseinventoryList[index].quantity =
        handleTotalQuantity(
          totalQuantity,
          row.partPercent,
          row.unitList.unitCode_dictText,
          row.unitList.minUnitCode_dictText
        ) || 0;

      form.purchaseinventoryList[index].itemTable = res.data[0].itemTable || '';
      form.purchaseinventoryList[index].startTime = formatDateymd(res.data[0].productionDate) || '';
      form.purchaseinventoryList[index].endTime = formatDateymd(res.data[0].expirationDate) || '';
      startTimeOld.value = form.purchaseinventoryList[index].startTime
        ? form.purchaseinventoryList[index].startTime
        : "";
      endTimeOld.value = form.purchaseinventoryList[index].endTime
        ? form.purchaseinventoryList[index].endTime
        : "";
    }
  });
}

// 处理总数量不整时显示的情况,例 10盒2片
function handleTotalQuantity(quantity, partPercent, unitCode, minUnitCode) {
  if (quantity % partPercent != 0) {
    return (
      Math.floor(quantity / partPercent).toString() +
      " " +
      unitCode +
      (quantity % partPercent).toString() +
      " " +
      minUnitCode
    );
  }
  return quantity / partPercent;
}

function getNextDayZeroTimeStamp(selectedTime) {
  const now = new Date(selectedTime);
  const nextDay = new Date(now);
  nextDay.setDate(now.getDate());
  return nextDay.getTime();
}

function changeValStart(val, index) {
  const selectedTime = val;
  const validTime = this.getNextDayZeroTimeStamp(selectedTime);
  if (form.purchaseinventoryList[index].endTime) {
    const endTime = formatDateymd(form.purchaseinventoryList[index].endTime);
    const getNextDayZeroTime = this.getNextDayZeroTimeStamp(endTime);
    if (getNextDayZeroTime < validTime) {
      proxy.$message.warning("生产日期必须小于等于有效期！");
      form.purchaseinventoryList[index].startTime = startTimeOld.value;
      return;
    }
  }
}
function changeValEnd(val, index) {
  const selectedTimes = val;
  const validTimes = this.getNextDayZeroTimeStamp(selectedTimes);
  if (form.purchaseinventoryList[index].startTime) {
    const startTime = formatDateymd(form.purchaseinventoryList[index].startTime);
    const getNextDayZeroTimes = this.getNextDayZeroTimeStamp(startTime);
    if (getNextDayZeroTimes > validTimes) {
      proxy.$message.warning("有效期必须大于等于生产日期！");
      form.purchaseinventoryList[index].endTime = endTimeOld.value;
      return;
    }
  }
}
// 切换仓库类型获取药房/药库/耗材库列表
function handleChangeLocationType(value) {
  // 确保locationList变量已初始化
  if (!locationList) {
    console.warn("locationList未初始化");
    return;
  }

  if (value == 16) {
    // 药房类型
    getPharmacyList().then((res) => {
      const list = res?.data || [];
      if (!list.length) {
        getPharmacyListAll().then((res2) => {
          locationList.value = res2?.data || [];
        });
      } else {
        locationList.value = list;
      }
    });
  } else if (value == 11) {
    // 根据用户说明，标识为11的是药库
    // 直接使用getDispensaryListAll获取药库数据，而不是从药房数据中过滤
    getDispensaryListAll()
      .then((res) => {
        const pharmacies = res?.data || [];
        console.log("===== 药库数据获取结果 =====");
        console.log("获取到的药库数量:", pharmacies.length);
        console.log("药库列表:", pharmacies);

        // 直接使用获取到的药库数据
        locationList.value = pharmacies;

        // 如果没有找到药库，显示提示信息
        if (pharmacies.length === 0) {
          console.warn("未获取到任何药库数据");
        }
      })
      .catch((error) => {
        console.error("获取药库列表失败:", error);
        locationList.value = [];
      });
  } else if (value == 17) {
    // 耗材库类型 - 确保只显示中心耗材库
    console.log("选择了耗材库类型，设置中心耗材库");

    // 只设置一个硬编码的中心耗材库，使用数字ID避免类型转换错误
    const centralSupplyWarehouse = {
      id: 1, // 使用数字ID而不是字符串
      locationStoreId: 1, // 使用数字ID而不是字符串
      locationStoreName: "中心耗材库",
      formEnum: "17",
      storeName: "中心耗材库",
      name: "中心耗材库",
    };

    // 直接设置locationList只包含中心耗材库
    locationList.value = [centralSupplyWarehouse];
    console.log("已设置仅包含中心耗材库的列表");

    // 可选：如果需要在后台记录日志，可以保留获取数据的逻辑但不覆盖显示
    getPharmacyListAll()
      .then((res) => {
        console.log("后台获取仓库数据（仅供参考，不影响显示）:", res?.data?.length || 0);
      })
      .catch((error) => {
        console.error("后台获取仓库数据失败（不影响显示）:", error);
      });
  }
}

// 单位处理
function handleUnitCodeChange(row, index, value) {
  if (
    //小单位
    unitMap.value["unitCode"] != value &&
    unitMap.value["currentCode"] == unitMap.value["unitCode"]
  ) {
    // 总库存数
    form.purchaseinventoryList[index].quantity = totalQuantity;

    // 采购数量
    form.purchaseinventoryList[index].itemQuantity = undefined;
  } else {
    //大单位
    form.purchaseinventoryList[index].quantity =
      handleTotalQuantity(
        totalQuantity,
        row.partPercent,
        row.unitList.unitCode_dictText,
        row.unitList.minUnitCode_dictText
      ) || 0;

    form.purchaseinventoryList[index].itemQuantity = undefined;
  }
  form.purchaseinventoryList[index].totalPrice = undefined;
  unitMap.value["currentCode"] = value;
}

// 计算总价
function handleTotalPrice(index) {
  let purchaseItem = form.purchaseinventoryList[index];
  if (!purchaseItem.unitList) {
    purchaseItem.unitList = {
      unitCode: purchaseItem.unitCode,
      unitCode_dictText: purchaseItem.unitCode_dictText,
    };
  }
  if (purchaseItem.price > 0 && purchaseItem.itemQuantity > 0) {
    if (purchaseItem.unitCode == purchaseItem.unitList.unitCode) {
      //大单位
      form.purchaseinventoryList[index].totalPrice = (
        purchaseItem.price * purchaseItem.itemQuantity
      ).toFixed(4);
    } else {
      // 小单位
      form.purchaseinventoryList[index].totalPrice = (
        (purchaseItem.price / purchaseItem.partPercent) *
        purchaseItem.itemQuantity
      ).toFixed(4);
    }
  }
  if (form.purchaseinventoryList[index].itemQuantity == 0) {
    form.purchaseinventoryList[index].totalPrice = 0;
  }
}
// 批量保存
function handleSave(row, index) {
  rowList.value = [];
  form.purchaseinventoryList.map((row, index) => {
    if (row) {
      handleBlur(row);
      proxy.$refs["receiptHeaderRef"].validate((valid) => {
        if (valid) {
          proxy.$refs["formRef"].validate((valid) => {
            if (valid) {
              if (row.itemQuantity == 0) {
                proxy.$message.warning("采购数量不能为0！");
                return;
              }
              rowList.value.push(JSON.parse(JSON.stringify(row)));
              if (
                rowList._rawValue &&
                rowList._rawValue.length == form.purchaseinventoryList.length
              ) {
                addPurchaseinventorys(rowList._rawValue);
              }
            }
          });
        }
      });
    }
  });
}
function addPurchaseinventorys(rowList) {
  addPurchaseinventory(JSON.parse(JSON.stringify(rowList))).then((res) => {
    if (res.data) {
      proxy.$message.success("保存成功！");
      form.purchaseinventoryList.map((row, index) => {
        form.purchaseinventoryList[index].id = res.data[index];
        form.purchaseinventoryList[index].isSave = true;
      });
    }
  });
}

function handleTraceNoInput(value, row) {
  row.traceNo = value.concat(',');
}

/** 选择条数  */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  selectedRows.value = selection;
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

// 预览单据函数 - 简化实现，避免复杂嵌套数据结构
// 预览单据函数 - 完整修复版
function handlePrintPreview() {
  console.log("开始执行预览功能");

  // 检查是否存在数据
  if (!form.purchaseinventoryList || form.purchaseinventoryList.length === 0) {
    ElMessage.warning("没有数据可预览");
    return;
  }

  // 检查是否所有必填字段都已填写
  const hasEmptyFields = form.purchaseinventoryList.some((row) => {
    return !row.name || !row.itemQuantity || !row.price || !row.totalPrice;
  });

  if (hasEmptyFields) {
    ElMessage.warning("请确保所有药品信息完整后再预览");
    return;
  }

  // 检查templateJson是否已正确导入
  if (!templateJson) {
    console.error("错误：templateJson未定义，请检查导入");
    ElMessage.error("打印模板未加载，请刷新页面重试");
    return;
  }

  try {
    // 安全获取供应商名称
    let supplierName = "";
    if (supplierListOptions.value && receiptHeaderForm.supplierId) {
      const filteredSuppliers = supplierListOptions.value.filter((item) => {
        return item.value == receiptHeaderForm.supplierId;
      });
      supplierName = filteredSuppliers.length > 0 ? filteredSuppliers[0].label : "";
    }

    // 计算总金额
    const totalAmount = form.purchaseinventoryList.reduce((accumulator, currentRow) => {
      return accumulator + (Number(currentRow.totalPrice) || 0);
    }, 0);

    // 重新构建包含药品清单的数据结构
    const printData = {
      supplierName: supplierName || "",
      totalAmount: totalAmount.toFixed(2),
      ...receiptHeaderForm,
      purchaseinventoryList: form.purchaseinventoryList.map((item, index) => ({
        ...item,
        rowNumber: index + 1, // 添加序号
        itemQuantity: Number(item.itemQuantity) || 0,
        price: Number(item.price).toFixed(2),
        totalPrice: Number(item.totalPrice).toFixed(2),
      })),
    };

    console.log("打印数据：", printData);

    // 创建打印模板实例
    const hiprintTemplate = new hiprint.PrintTemplate({
      template: templateJson,
    });

    // 打开预览窗口（推荐）
    hiprintTemplate.print(
      printData,
      {},
      {
        callback: () => {
          console.log("预览窗口已打开");
        },
      }
    );
  } catch (error) {
    console.error("打印预览失败：", error);
    ElMessage.error("预览失败，请检查打印模板配置");
  }
}

// 直接打印函数（不预览）
function handleDirectPrint() {
  console.log("开始执行直接打印");

  // 检查是否存在数据
  if (!form.purchaseinventoryList || form.purchaseinventoryList.length === 0) {
    ElMessage.warning("没有数据可打印");
    return;
  }

  // 检查是否所有必填字段都已填写
  const hasEmptyFields = form.purchaseinventoryList.some((row) => {
    return !row.name || !row.itemQuantity || !row.price || !row.totalPrice;
  });

  if (hasEmptyFields) {
    ElMessage.warning("请确保所有药品信息完整后再打印");
    return;
  }

  // 检查templateJson是否已正确导入
  if (!templateJson) {
    console.error("错误：templateJson未定义，请检查导入");
    ElMessage.error("打印模板未加载，请刷新页面重试");
    return;
  }

  try {
    // 安全获取供应商名称
    let supplierName = "";
    if (supplierListOptions.value && receiptHeaderForm.supplierId) {
      const filteredSuppliers = supplierListOptions.value.filter((item) => {
        return item.value == receiptHeaderForm.supplierId;
      });
      supplierName = filteredSuppliers.length > 0 ? filteredSuppliers[0].label : "";
    }

    // 计算总金额
    const totalAmount = form.purchaseinventoryList.reduce((accumulator, currentRow) => {
      return accumulator + (Number(currentRow.totalPrice) || 0);
    }, 0);

    // 重新构建包含药品清单的数据结构
    const printData = {
      supplierName: supplierName || "",
      totalAmount: totalAmount.toFixed(2),
      ...receiptHeaderForm,
      purchaseinventoryList: form.purchaseinventoryList.map((item, index) => ({
        ...item,
        rowNumber: index + 1,
        itemQuantity: Number(item.itemQuantity) || 0,
        price: Number(item.price).toFixed(2),
        totalPrice: Number(item.totalPrice).toFixed(2),
      })),
    };

    console.log("打印数据：", printData);

    // 创建打印模板实例
    const hiprintTemplate = new hiprint.PrintTemplate({
      template: templateJson,
    });

    // 直接打印到指定打印机
    hiprintTemplate.print2(printData, {
      printer: "", // 留空使用默认打印机，或指定打印机名称如 'EPSON LQ-80KFII'
      title: "采购入库单",
    });

    ElMessage.success("打印任务已发送");
  } catch (error) {
    console.error("打印失败：", error);
    ElMessage.error("打印失败，请检查打印机连接");
  }
}

// 选择打印机后打印
function handlePrintWithSelectPrinter() {
  console.log("开始选择打印机");

  // 检查是否存在数据
  if (!form.purchaseinventoryList || form.purchaseinventoryList.length === 0) {
    ElMessage.warning("没有数据可打印");
    return;
  }

  // 检查是否所有必填字段都已填写
  const hasEmptyFields = form.purchaseinventoryList.some((row) => {
    return !row.name || !row.itemQuantity || !row.price || !row.totalPrice;
  });

  if (hasEmptyFields) {
    ElMessage.warning("请确保所有药品信息完整后再打印");
    return;
  }

  // 检查templateJson是否已正确导入
  if (!templateJson) {
    console.error("错误：templateJson未定义，请检查导入");
    ElMessage.error("打印模板未加载，请刷新页面重试");
    return;
  }

  try {
    // 安全获取供应商名称
    let supplierName = "";
    if (supplierListOptions.value && receiptHeaderForm.supplierId) {
      const filteredSuppliers = supplierListOptions.value.filter((item) => {
        return item.value == receiptHeaderForm.supplierId;
      });
      supplierName = filteredSuppliers.length > 0 ? filteredSuppliers[0].label : "";
    }

    // 计算总金额
    const totalAmount = form.purchaseinventoryList.reduce((accumulator, currentRow) => {
      return accumulator + (Number(currentRow.totalPrice) || 0);
    }, 0);

    // 重新构建包含药品清单的数据结构
    const printData = {
      supplierName: supplierName || "",
      totalAmount: totalAmount.toFixed(2),
      ...receiptHeaderForm,
      purchaseinventoryList: form.purchaseinventoryList.map((item, index) => ({
        ...item,
        rowNumber: index + 1,
        itemQuantity: Number(item.itemQuantity) || 0,
        price: Number(item.price).toFixed(2),
        totalPrice: Number(item.totalPrice).toFixed(2),
      })),
    };

    console.log("打印数据：", printData);

    // 创建打印模板实例
    const hiprintTemplate = new hiprint.PrintTemplate({
      template: templateJson,
    });

    // 获取打印机列表并选择
    hiprint.hiwebSocket.getPrinterList((printerList) => {
      if (!printerList || printerList.length === 0) {
        ElMessage.error("未检测到打印机，请检查打印服务");
        return;
      }

      // 这里可以弹出选择框让用户选择打印机
      // 简单示例：使用第一台打印机
      const selectedPrinter = printerList[0].name;

      hiprintTemplate.print2(printData, {
        printer: selectedPrinter,
        title: "采购入库单",
      });

      ElMessage.success(`正在使用 ${selectedPrinter} 打印`);
    });
  } catch (error) {
    console.error("打印失败：", error);
    ElMessage.error("打印失败，请检查打印服务连接");
  }
}

// 工具函数：格式化打印数据
function formatPrintData() {
  // 安全获取供应商名称
  let supplierName = "";
  if (supplierListOptions.value && receiptHeaderForm.supplierId) {
    const filteredSuppliers = supplierListOptions.value.filter((item) => {
      return item.value == receiptHeaderForm.supplierId;
    });
    supplierName = filteredSuppliers.length > 0 ? filteredSuppliers[0].label : "";
  }

  // 计算总金额
  const totalAmount = form.purchaseinventoryList.reduce((accumulator, currentRow) => {
    return accumulator + (Number(currentRow.totalPrice) || 0);
  }, 0);

  // 返回格式化后的数据
  return {
    supplierName: supplierName || "",
    totalAmount: totalAmount.toFixed(2),
    totalQuantity: form.purchaseinventoryList.reduce(
      (sum, item) => sum + (Number(item.itemQuantity) || 0),
      0
    ),
    printDate: new Date().toLocaleString("zh-CN"),
    ...receiptHeaderForm,
    purchaseinventoryList: form.purchaseinventoryList,
  };
}

function deleteSelectedRows() {
  let length = selectedRows.value.length;
  let ids = [];
  if (selectedRows.value[0].id) {
    ids = selectedRows.value.map((item) => {
      return item.id;
    });
  }
  if (selectedRows.value[length - 1].isSave) {
    delPurchaseinventory(ids).then((res) => {
      if (res.code == 200) {
        proxy.$message.success("删除成功");
      }
    });
  } else {
    if (length > 1) {
      delPurchaseinventory(ids.slice(0, -1)).then((res) => {
        if (res.code == 200) {
          proxy.$message.success("删除成功");
        }
      });
    }
  }
  form.purchaseinventoryList = form.purchaseinventoryList.filter(
    (row) => !selectedRows.value.includes(row)
  );
  data.isAdding = false;
  currentIndex.value = -1;
}

/**计算合计金额 */
function handleTotalAmount() {
  totalAmount.value = form.purchaseinventoryList.reduce((accumulator, currentRow) => {
    return accumulator + (Number(currentRow.totalPrice) || 0);
  }, 0);
}

/** 重置操作表单 */
function reset() {
  proxy.resetForm('receiptHeaderRef');
  form.purchaseinventoryList = [];
  // 完全重置所有表单字段
  Object.assign(receiptHeaderForm, {
    busNo: undefined,
    practitionerId: userUserStore.practitionerId,
    occurrenceTime: formatDate(new Date()),
    supplierId: undefined,
    medicationType: "1",
    purposeTypeEnum: undefined,
  });
  // 重置编辑数据
  editData.value = {};
}
// 显示弹框
function show() {
  data.isEdit = false;
  data.isAdding = false;
  reset();
  visible.value = true;
  getPurchaseinventoryTypeList();
  getInitBusNo().then((res) => {
    if (res.data && res.data.busNo) {
      receiptHeaderForm.busNo = res.data.busNo;
    }
  });
}
// 显示弹框
function edit() {
  data.isAdding = false;
  data.isEdit = true;
  visible.value = true;
  receiptHeaderForm.value = editData.value.item.length > 0 ? editData.value.item[0] : {};
  receiptHeaderForm.busNo = editData.value.editRow.supplyBusNo;
  receiptHeaderForm.supplierId = editData.value.editRow.supplierId;
  receiptHeaderForm.supplierName = editData.value.item[0].supplierName;
  receiptHeaderForm.practitionerId = editData.value.editRow.practitionerId;
  receiptHeaderForm.occurrenceTime = editData.value.item[0].occurrenceTime
    ? formatDate(editData.value.item[0].occurrenceTime)
    : formatDate(new Date());
  //仓库类型
  receiptHeaderForm.purposeTypeEnum = editData.value.editRow.purposeTypeEnum
    ? editData.value.editRow.purposeTypeEnum.toString()
    : editData.value.item[0].purposeTypeEnum.toString();
  receiptHeaderForm.medicationType =
    editData.value.item[0].itemTable == "med_medication_definition" ? "1" : "2";
  total.value = form.purchaseinventoryList.length;
  handleChangeLocationType(
    editData.value.editRow.purposeTypeEnum
      ? editData.value.editRow.purposeTypeEnum.toString()
      : editData.value.item[0].purposeTypeEnum.toString()
  );
  setTimeout(() => {
    form.purchaseinventoryList = editData.value.item.map((item) => {
      return {
        ...item,
        name: item.itemName,
        volume: item.totalVolume,
        // manufacturerText: item.supplierName,
        quantity: item.totalQuantity,
        startTime: formatDateymd(item.startTime),
        endTime: formatDateymd(item.endTime),
        applyTime: formatDate(item.applyTime),
        isSave: true,
      };
    });
  }, 100);

  loading.value = false;
  // 使用 Promise 确保数据加载完成
  Promise.resolve().then(() => {
    form.purchaseinventoryList = editData.value.item.map((item) => {
      return {
        ...item,
        name: item.itemName,
        volume: item.totalVolume,
        manufacturer: item.supplierName,
        quantity: item.totalQuantity,
        // 仓库字段回显（Promise 阶段再保障一次）
        purposeLocationId:
          item.purposeLocationId || editData.value.editRow?.purposeLocationId || null,
        locationStoreName: item.locationStoreName || "",
        purposeLocationStoreId: item.purposeLocationStoreId || null,
        startTime: formatDateymd(item.startTime),
        endTime: formatDateymd(item.endTime),
        applyTime: formatDate(item.applyTime),
        isSave: true,
      };
    });
  });
}

function getPurchaseinventoryTypeList() {
  getInit().then((response) => {
    itemTypeOptions.value = response.data.itemTypeOptions; // 活动标记
    practitionerListOptions.value = response.data.practitionerListOptions; // 预约必填标记
    supplierListOptions.value = response.data.supplierListOptions; // 供应商列表
  });
}

// reject
function handleReject() {
  reject(route.query.supplyBusNo).then((res) => {
    if (res.code == 200) {
      proxy.$modal.msgSuccess("操作成功");
      tagsViewStore.delView(router.currentRoute.value);
      store.clearCurrentData();
      // 跳转到审核页面
      router.replace({
        path: '/aaaa/billapproval',
        query: { type: 'purchaseDocument' },
      });
    }
  });
}
function handelApply() {
  pageLoading.value = true;
  purchaseInventoryApproved(route.query.supplyBusNo)
    .then((res) => {
      if (res.code == 200) {
        pageLoading.value = false;
        proxy.$modal.msgSuccess("操作成功");
        tagsViewStore.delView(router.currentRoute.value);
        store.clearCurrentData();
        // 跳转到审核页面
        router.replace({
          path: '/aaaa/billapproval',
          query: { type: 'purchaseDocument' },
        });
      }
    })
    .catch(() => {
      pageLoading.value = false;
    });
}

/** 提交审核 */
function submitAudit() {
  let length = form.purchaseinventoryList.length;
  if (length < 1) {
    proxy.$modal.msgWarning("请先添加单据");
  } else if (!form.purchaseinventoryList[length - 1].isSave) {
    proxy.$modal.msgWarning("第" + length + "行单据未保存，请先保存");
  } else {
    submitApproval(receiptHeaderForm.busNo).then((res) => {
      if (res.code == 200) {
        proxy.$modal.msgSuccess("提交审批成功");
        emit("refresh");
        visible.value = false;
        // 关闭当前页
        tagsViewStore.delView(router.currentRoute.value);
        // 跳转到退货单管理页面
        router.replace({ path: "purchaseinventory" });
      }
    });
  }
}
function handleClickOutside() {
  if (
    currentIndex.value != -1 &&
    !isEqual(currentEditRow.value, form.purchaseinventoryList[currentIndex.value])
  ) {
    handleSave(form.purchaseinventoryList[currentIndex.value], currentIndex.value);
  }
}

defineExpose({
  show,
  edit,
});
getPurchaseinventoryTypeList();

// 导出
const exportRequiredParams = ref({
  pageNo: 1,
  pageSize: 10,
  busNo: route.query.supplyBusNo,
});
function handleExport() {
  proxy.downloadGet(
    '/inventory-manage/purchase/excel-out',
    {
      ...exportRequiredParams.value,
    },
    `入库单据明细_${proxy.formatDateStr(new Date(), 'YYYY-MM-DD')}.xlsx`
  );
}
</script>
<style scoped>
.custom-tree-node {
  display: flex;
  align-items: center;
}

.title {
  font-weight: bold;
  font-size: large;
  margin-bottom: 10px;
}

.error-border {
  border: 1px solid red;
}
</style>
