<template>
  <div class="app-container" v-loading="pageLoading">
    <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">
        <!-- v-if="scope.row.statusEnum == '1' || scope.row.statusEnum == '9'" -->
        <!-- v-hasPermi="['system:user:remove']" -->
        <el-button plain type="primary" icon="Plus" @click="handleSubmitApproval()"
          >提交审核</el-button
        >
        <!-- <el-button type="primary" plain icon="Plus" @click="submitAudit"
          >提交审核</el-button
        > -->
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Printer" :disabled="multiple" @click="handleDelete"
          >打印</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 @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: 260px"
          disabled
        />
      </el-form-item>
      <el-form-item label="领用部门：" prop="locationId" label-width="120px">
        <el-select
          v-model="receiptHeaderForm.locationId"
          placeholder=""
          clearable
          style="width: 150px"
          :disabled="data.isEdit"
          @change="handleChangepurposeLocationId"
        >
          <el-option
            v-for="supplier in issueDepartmentDto"
            :key="supplier.locationId"
            :label="supplier.name"
            :value="supplier.locationId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门经手人：" prop="practitionerId" label-width="160px">
        <el-select
          v-model="receiptHeaderForm.practitionerId"
          placeholder=""
          clearable
          style="width: 150px"
          :disabled="data.isEdit"
        >
          <!-- purchase_type -->
          <el-option
            v-for="supplier in practitionerListOptions"
            :key="supplier.value"
            :label="supplier.label"
            :value="supplier.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="data.isEdit"
        />
      </el-form-item>

      <el-form-item label="药品类型：" prop="medicationType">
        <el-select
          v-model="receiptHeaderForm.medicationType"
          placeholder=""
          clearable
          style="width: 150px"
          :disabled="data.isEdit"
          @change="
            (value) => {
              itemType = value;
            }
          "
        >
          <!-- 字典 purchase_type -->
          <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="headerLocationId">
        <el-select
          v-model="receiptHeaderForm.headerLocationId"
          placeholder="请选择仓库"
          clearable
          filterable
          style="width: 200px"
          :disabled="data.isEdit"
          @change="onHeaderWarehouseChange"
        >
          <el-option
            v-for="item in purposeTypeListOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </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="primary" plain icon="Plus" @click="confirmCurrentRow"
              >确认当前行</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="cancelEditRow"
              >取消行编辑</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="cancelEditAllRow"
              >取消所有行编辑</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>
        <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px;">
          <div></div>
          <el-input
            v-model="detailSearchKey"
            placeholder="请输入项目|厂家/产地|产品批号"
            style="width: 280px;"
            clearable
          >
            <template #prefix>
              <el-icon><Search /></el-icon>
            </template>
          </el-input>
        </div>
        <el-form :model="form" :rules="tableRules" ref="formRef">
          <el-table
            v-loading="loading"
            :data="filteredPurchaseinventoryList"
            @selection-change="handleSelectionChange"
            @row-click="handleRowClick"
            ref="tableRef"
            :disabled="viewStatus == 'apply'"
          >
            <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"
                  required
                >
                  <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="{}">
                      <transferManagement
                        @selectRow="(row) => selectRow(row, scope.$index)"
                        :searchKey="medicineSearchKey"
                        :itemType="itemType"
                        :orgLocationId="receiptHeaderForm.headerLocationId"
                      />
                    </template>
                  </PopoverList>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="规格" align="center" key="volume" prop="volume" width="200">
              <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="240"
            >
              <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="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="" disabled />
                </el-form-item>
              </template>
            </el-table-column>


            <el-table-column label="单价 " align="center" key="price" prop="price" width="130">
              <template #default="scope">
                <el-form-item
                  :prop="`purchaseinventoryList.${scope.$index}.price`"
                  :rules="tableRules.price"
                >
                  <div class="select_wrapper_div">
                    <el-input
                      disabled
                      v-model="scope.row.price"
                      placeholder=""
                      @blur="handleTotalPrice(scope.$index)"
                      :class="{ 'error-border': scope.row.error }"
                    >
                      <template #suffix>元</template>
                    </el-input>
                  </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"
                  required
                >
                  <div class="select_wrapper_div">
                    <el-select
                      :disabled="viewStatus == 'view'"
                      v-model="scope.row.unitCode"
                      placeholder=" "
                      :class="{ 'error-border': scope.row.error }"
                      @change="(value) => handleUnitCodeChange(scope.row, scope.$index, value)"
                    >
                      <template v-if="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.partPercent == 1">
                        <el-option
                          :label="scope.row.unitList.unitCode_dictText"
                          :value="scope.row.unitList.unitCode"
                        />
                      </template>
                    </el-select>
                  </div>
                </el-form-item>
              </template>
            </el-table-column>

            <el-table-column
              label="总库存数量"
              align="center"
              key="totalQuantity"
              prop="totalQuantity"
              width="120"
            >
              <template #default="scope">
                <el-form-item
                  :prop="`purchaseinventoryList.${scope.$index}.totalQuantity`"
                  :rules="tableRules.totalQuantity"
                >
                  <el-input v-model="scope.row.totalQuantity" placeholder="" disabled />
                </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"
                  required
                >
                  <div class="select_wrapper_div">
                    <el-input
                      v-model="scope.row.itemQuantity"
                      placeholder=""
                      :disabled="viewStatus == 'view'"
                      @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="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="" />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="国家医保码" align="center" key="ybNo" prop="ybNo" width="220">
              <template #default="scope">
                <el-form-item
                  :prop="`purchaseinventoryList.${scope.$index}.ybNo`"
                  :rules="tableRules.ybNo"
                  disabled
                >
                  <el-input v-model="scope.row.ybNo" placeholder="" disabled />
                </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" width="80" 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-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>制单人：{{ userStore.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>
    <TraceNoDialog :ypName="ypName" :rowData="rowData" :openDialog="openTraceNoDialog" @submit="submit" @cancel="openTraceNoDialog = false"/>
  </div>
</template>

<script setup name="requisitionManagement">
import {computed} from 'vue';
import {
  addTransferProduct,
  delTransferProduct,
  getBusNoInit,
  getCount,
  getInit,
  getPharmacyCabinetList,
  getTransferProductDetail,
  reject,
  requisitionIssueApproved,
  submitApproval,
} from '../components/api';
import PopoverList from '@/components/OpenHis/popoverList/index.vue';
import transferManagement from '../components/list.vue';
import {formatDate} from '@/utils/index';
import useUserStore from '@/store/modules/user';
import {useStore} from '@/store/store';
import useTagsViewStore from '@/store/modules/tagsView';
import TraceNoDialog from '@/components/OpenHis/TraceNoDialog/index.vue'

/** 领用保存 IssueDto：后端 Jackson 只认 yyyy-MM-dd HH:mm:ss；库存接口可能回传 2025/4/2 00:00:00 等 */
function toIssueDateTimeStr(val) {
  if (val == null || val === '') return undefined;
  if (typeof val === 'string') {
    const s = val.trim();
    if (/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(s)) return s;
    if (/^\d{4}-\d{2}-\d{2}$/.test(s)) return `${s} 00:00:00`;
  }
  const d = val instanceof Date ? val : new Date(val);
  if (Number.isNaN(d.getTime())) return undefined;
  return formatDate(d);
}

/** 总库存 totalQuantity 为最小单位；领用数量按当前计量单位折算成最小单位后再比较 */
function getRequisitionQtyInMinUnit(r) {
  const q = Number(r.itemQuantity ?? 0);
  if (!Number.isFinite(q) || q < 0) return NaN;
  const minCode = r.unitList?.minUnitCode;
  if (!minCode || r.unitCode === minCode) return q;
  const part = Number(r.partPercent ?? 1);
  return q * part;
}

function validateRequisitionQtyVsStock(r, lineNo) {
  const cap = Number(r.totalQuantity ?? 0);
  const reqMin = getRequisitionQtyInMinUnit(r);
  if (!Number.isFinite(reqMin)) {
    return `第${lineNo}行：领用数量请输入有效数字`;
  }
  if (cap > 0 && reqMin > cap + 1e-9) {
    const name = r.name || `第${lineNo}行`;
    return `${name}：领用数量（折合最小单位）不能超过当前仓库可领库存 ${cap}，请修改后再保存。`;
  }
  if (cap <= 0 && reqMin > 0) {
    const name = r.name || `第${lineNo}行`;
    return `${name}：当前仓库可领库存为 0，不能填写正数领用数量。`;
  }
  return null;
}

const tagsViewStore = useTagsViewStore();
const store = useStore();

const router = useRouter();
const route = useRoute();
const userStore = useUserStore();
const openTraceNoDialog = ref(false)
const rowData = ref({})
const ypName = ref('')
const currentIndex = ref(-1)

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 purposeLocationIdListOptions = ref(undefined); //目的仓库
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 row = ref({});
const rowIndex = ref(-1);
const totalAmount = ref(0);
const purposeTypeListOptions = ref(undefined);
const pageLoading = ref(false)
const props = defineProps({
  sourceTypeListOptions: {
    type: Object,
    required: false,
  },
  categoryListOptions: {
    type: Object,
    required: false,
  },
  busNoAdd: {
    type: String,
    required: false,
  },
  item: {
    type: Object,
    required: false,
  },
  editRow: {
    type: Object,
    required: false,
  },
});

const form = reactive({
  purchaseinventoryList: [],
});

const receiptHeaderForm = reactive({
  busNo: undefined,
  occurrenceTime: formatDate(new Date()),
});
const rowList = ref([]);
const data = reactive({
  isEdit: false,
  isAdding: true,
  queryParams: {
    pageNo: 1,
    pageSize: 10,
    searchKey: undefined, // 供应商名称
    busNo: undefined, // 编码
    statusEnum: undefined, // 状态
    // supplierId: undefined, //
    occurrenceTime: undefined, // 申请时间
    // practitionerId: undefined, // 经手人ID
  },
  rules: {
    medicationType: [{ required: true, message: '请选择药品类型', trigger: 'change' }],
    locationId: [{ required: true, message: '请选择领用部门', trigger: 'change' }],
    practitionerId: [{ required: true, message: '请选择部门经手人', trigger: 'change' }],
    // 领用出库按「表头仓库」查 /app-common/inventory-item-info，未选仓库会查不到库存并误报「仓库数量为0」
    headerLocationId: [{ required: true, message: '请先选择仓库（按仓库查询可领用库存）', trigger: 'change' }],
  },
  tableRules: {
    name: [{ required: true, message: '项目不能为空', trigger: 'change' }],
    unitCode: [{ required: true, message: '计量单位不能为空', trigger: 'change' }],

    itemQuantity: [{ required: true, message: '领用数量不能为空', trigger: 'blur' }],
    // lotNumber: [
    //   { required: true, message: "产品批号不能为空", trigger: "blur" },
    // ],
    // ybNo: [{ required: true, message: "追溯码不能为空", trigger: "blur" }],
    // startTime: [
    //   { required: true, message: "开始时间不能为空", trigger: "blur" },
    // ],
    // endTime: [{ required: true, message: "结束时间不能为空", trigger: "blur" }],
    // price: [{ required: true, message: "单价不能为空", trigger: "blur" }],
    // totalPrice: [{ required: true, message: "总价不能为空", trigger: "blur" }],
  },
});

const { queryParams, rules, tableRules } = toRefs(data);
const issueDepartmentDto = ref(undefined);
const supplierListOptions = ref(undefined);
const sourceLocationStoreIdListOptions = ref(undefined);
const purposeLocationStoreIdListOptions = ref(undefined);
const practitionerListOptions = ref(undefined);
const supplyStatusOptions = ref(undefined);
const selectedRows = ref([]); // 用于存储选中的行
const emit = defineEmits(['refresh']);
const tableRef = ref(undefined); // 表格引用
const currentRow = ref(undefined); // 当前操作的行
const medicineSearchKey = ref('');
const itemType = ref('');
const detailSearchKey = ref('');

const filteredPurchaseinventoryList = computed(() => {
  if (!detailSearchKey.value) {
    return form.purchaseinventoryList;
  }
  const searchKey = detailSearchKey.value.toLowerCase();
  return form.purchaseinventoryList.filter((item) => {
    const nameMatch = item.name ? item.name.toLowerCase().includes(searchKey) : false;
    const manufacturerMatch = item.manufacturerText ? item.manufacturerText.toLowerCase().includes(searchKey) : false;
    const lotNumberMatch = item.lotNumber ? item.lotNumber.toLowerCase().includes(searchKey) : false;
    return nameMatch || manufacturerMatch || lotNumberMatch;
  });
});

watch(
  () => store.currentDataLYCK,
  (newVlaue) => {
    if (newVlaue && !route.query.supplyBusNo) {
      form.purchaseinventoryList = newVlaue?.purchaseinventoryList;
      receiptHeaderForm.busNo = newVlaue?.receiptHeaderForm.busNo;
      receiptHeaderForm.occurrenceTime = newVlaue?.receiptHeaderForm.occurrenceTime;
      receiptHeaderForm.locationId = newVlaue?.receiptHeaderForm.locationId;
      receiptHeaderForm.practitionerId = newVlaue?.receiptHeaderForm.practitionerId;
      getPharmacyCabinetList().then((res) => {
        purposeTypeListOptions.value = res.data;
      });
      receiptHeaderForm.medicationType = newVlaue?.receiptHeaderForm.medicationType;
    }
  },
  { immediate: true }
);

watch(
  () => receiptHeaderForm.locationId,
  (newVal) => {
    if (newVal) {
      getPharmacyCabinetList().then((res) => {
        purposeTypeListOptions.value = res.data;
      });
    } else {
      purposeTypeListOptions.value = [];
      receiptHeaderForm.headerLocationId = undefined;
    }
  }
);

watch(
  () => form.purchaseinventoryList,
  (newVlaue) => {
    if (newVlaue && newVlaue.length > 0) {
      if (viewStatus.value) {
        handleTotalAmount();
      }
    }
  },
  { immediate: true }
);
// 挂载时绑定事件
onMounted(() => {
  document.addEventListener('click', handleClickOutside);
});

// 卸载时移除事件
onUnmounted(() => {
  document.removeEventListener('click', handleClickOutside);
});

function addNewRow() {
  proxy.$refs['receiptHeaderRef'].validate((valid) => {
    if (valid) {
      // if (data.isAdding) {
      //   proxy.$message.warning('请先保存当前行后再新增！');
      //   return;
      // }
      const newRow = {
        id: '',
        supplyBusNo: '',
        occurrenceTime: '',
        typeEnum_enumText: '',
        statusEnum_enumText: '',
        purposeLocationName: '',
        approverId_dictText: '',
        applicantId_dictText: '',
        approvalTime: '',
        applyTime: '',
        applicantId: '',
        supplierId: '',
        detailJson: '',
        createTime: '',
        itemTable: '',
        itemQuantity: '',
        itemId: '',
        unitCode: '',
        remake: '',
        purposeTypeEnum: '',
        purposeLocationId: '',
        purposeLocationStoreId: '',
        practitionerId: '',
        practitionerName: '',
        traceNo: '',
        ybNo: '',
        invoiceNo: '',
        startTime: '',
        endTime: '',
        price: '',
        totalPrice: '',
        datailJson: '',
        totalQuantity: '',
        unitList: {},
        isEditing: true,
        error: false,
        isSave: false,
      };
      form.purchaseinventoryList.push(newRow);
      data.isAdding = true; // 设置标志位为 true，表示有未保存的
    }
  });
}

// 获取表格数据
function handleBlur(row, index) {
  let hasError = false;
  for (let key in row) {
    if (!row[key]) {
      row[key] = '';
    }
  }
  if (receiptHeaderForm.medicationType) {
    if (!row.itemTable) {
      if (receiptHeaderForm.medicationType == 1) {
        row.itemTable = 'med_medication_definition';
      } else {
        row.itemTable = 'adm_device_definition';
      }
    }
  }

  row.busNo = receiptHeaderForm.busNo;
  row.occurrenceTime = receiptHeaderForm.occurrenceTime;

  // 领用仓库（从表头获取）
  if (receiptHeaderForm.headerLocationId) {
    let sourceLocation = purposeTypeListOptions.value.filter((e) => {
      return e.id == receiptHeaderForm.headerLocationId;
    });
    if (sourceLocation && sourceLocation.length > 0) {
      row.sourceLocationId = sourceLocation[0].id;
      row.sourceTypeEnum = sourceLocation[0].formEnum;
    }
  }

  // 领用部门
  let purposeLocation = issueDepartmentDto.value.filter((e) => {
    return e.locationId == receiptHeaderForm.locationId;
  });
  console.log('purposeLocation', purposeLocation);
  row.purposeLocationId = purposeLocation[0].locationId;

  row.purposeTypeEnum = purposeLocation[0].typeEnum;

  // 部门经手人
  // row.practitionerId = receiptHeaderForm.practitionerId;
  let practitioner = practitionerListOptions.value.filter((e) => {
    return e.value == receiptHeaderForm.practitionerId;
  });
  console.log('practitioner', practitioner);
  row.practitionerId = practitioner[0].value;
  row.practitionerName = practitioner[0].label;
  console.log('row111111', row);
  // row.practitionerId = practitioner && practitioner[0].id ? practitioner[0].id : '';
  // 申请人
  row.applicantId = userStore.id;
}

// 详情
function getTransferProductDetails() {
  if (route.query.supplyBusNo) {
    // 编辑
    data.isEdit = true;
    getTransferProductDetail(receiptHeaderForm.busNo).then((res) => {
      form.purchaseinventoryList = res.data;
      receiptHeaderForm.occurrenceTime = formatDate(res.data[0].occurrenceTime)
      form.purchaseinventoryList.map((e, index) => {
        form.purchaseinventoryList[index].statusMaxvalue = false;
        e.volume = e.totalVolume;
        e.name = e.itemName;
        if (e.sourceLocationId) {
          // 源仓库（领用仓库）
          receiptHeaderForm.headerLocationId = e.sourceLocationId;
        }
        if (e.purposeLocationId) {
          //目的仓库
          receiptHeaderForm.locationId = e.purposeLocationId;
        }
        if (e.practitionerId) {
          // 经手人
          receiptHeaderForm.practitionerId = e.practitionerId;
        }
        if (e.itemType) {
          receiptHeaderForm.medicationType = e.itemType;
        }
        getPharmacyCabinetList().then((res) => {
          purposeTypeListOptions.value = res.data;
          // form.purchaseinventoryList[index].purposeLocationId = '';
          // handleLocationClick(1, row, index)
        });
        form.purchaseinventoryList[index].totalQuantity = form.purchaseinventoryList[index]
          .totalQuantity
          ? form.purchaseinventoryList[index].totalQuantity
          : 0;
        if (e.unitList && e.unitCode) {
          form.purchaseinventoryList[index].unitList = e.unitList[0]; //计量单位回显数组
          form.purchaseinventoryList[index].unitCode =
            form.purchaseinventoryList[index].measurementUnitCode_dictText;
          form.purchaseinventoryList[index].unitCode_dictText =
            form.purchaseinventoryList[index].measurementUnitCode_dictText;
          if (
            form.purchaseinventoryList[index].measurementUnitCode ==
            form.purchaseinventoryList[index].unitList.unitCode
          ) {
            // 回显大单位
            getMaxCounts(e, index, 1);
          }
          console.log(form.purchaseinventoryList, e.unitList, '获取详情12');
        }
        if (e.price) {
          // 单价   大单位单价
          if (
            form.purchaseinventoryList[index].measurementUnitCode ==
            form.purchaseinventoryList[index].unitList.minUnitCode
          ) {
            form.purchaseinventoryList[index].price =
              form.purchaseinventoryList[index].price /
                form.purchaseinventoryList[index].partPercent || '';
            form.purchaseinventoryList[index].price =
              form.purchaseinventoryList[index].price.toFixed(4);
            // parseFloat(form.purchaseinventoryList[index].price.toFixed(4))
          } else {
            if (form.purchaseinventoryList[index].price > 1) {
              form.purchaseinventoryList[index].price =
                form.purchaseinventoryList[index].price.toFixed(4);
            }
          }
          if (
            form.purchaseinventoryList[index].price > 0 &&
            form.purchaseinventoryList[index].itemQuantity > 0
          ) {
            form.purchaseinventoryList[index].totalPrice =
              form.purchaseinventoryList[index].price *
              form.purchaseinventoryList[index].itemQuantity;
            form.purchaseinventoryList[index].totalPrice =
              form.purchaseinventoryList[index].totalPrice.toFixed(4);
          }
        } else {
          form.purchaseinventoryList[index].price = 0;
          form.purchaseinventoryList[index].totalPrice = 0;
        }
      });
    });
  }
}

// 取消行编辑
function cancelEditRow() {
  const findIndexId = form.purchaseinventoryList.findIndex((e) => e.id == currentRow.value.id);
  form.purchaseinventoryList[findIndexId] = {};
  // currentRow.value
}
// 点击行时记录当前行
function handleRowClick(row) {
  // getMedicineList({ pageNum:1,
  //     pageSize:50, purchaseFlag:0, }).then((res) => {
  // });
  currentRow.value = row;
}

// 监听表格外的点击事件
function handleClickOutside(event) {
  // if (tableRef.value && !tableRef.value.$el.contains(event.target)) {
  //   if (currentRow.value) {
  //     handleSave(currentRow.value);
  //     currentRow.value = null; // 清空当前行
  //   }
  // }
}

function saveRow(row, index) {
  form.purchaseinventoryList[index] = row;
  addTransferProduct(row).then((response) => {
    reset();
    data.isAdding = false; // 允许新增下一行
    proxy.$message.success('保存成功！');
    visible.value = false;
  });
}
// 驳回
function handleReject() {
  reject(route.query.supplyBusNo).then((res) => {
    if (res.code == 200) {
      proxy.$modal.msgSuccess('操作成功');
      tagsViewStore.delView(router.currentRoute.value);
      store.clearCurrentDataLYCK();
      // 跳转到审核页面
      router.replace({
        path: '/financialManagement/medicationmanagement/billapproval',
        query: { type: 'requisitionManagement' },
      });
    }
  });
}
function handelApply() {
  pageLoading.value = true
  requisitionIssueApproved(route.query.supplyBusNo).then((res) => {
    if (res.code == 200) {
      pageLoading.value = false
      proxy.$modal.msgSuccess('操作成功');
      tagsViewStore.delView(router.currentRoute.value);
      store.clearCurrentDataLYCK();
      // 跳转到审核页面
      router.replace({
        path: '/financialManagement/medicationmanagement/billapproval',
        query: { type: 'requisitionManagement' },
      });
    }
  }).catch(() => {
    pageLoading.value = false
  });
}
/** 提交审核按钮 */
function handleSubmitApproval() {
  let length = form.purchaseinventoryList.length;
  if (length < 1) {
    proxy.$modal.msgWarning('请先添加单据');
  } else if (!form.purchaseinventoryList[length - 1].isSave) {
    proxy.$modal.msgWarning('第' + length + '行单据未保存，请先保存');
  } else {
    // 提交审批前逐行校验库存，防止超库存单据进入审批
    for (let i = 0; i < form.purchaseinventoryList.length; i++) {
      const line = form.purchaseinventoryList[i];
      if (!line) continue;
      const err = validateRequisitionQtyVsStock(line, i + 1);
      if (err) {
        proxy.$modal.msgWarning(err);
        return;
      }
    }
    submitApproval(receiptHeaderForm.busNo).then((response) => {
      if (response.code == 200) {
        proxy.$modal.msgSuccess('提交审批成功');
        tagsViewStore.delView(router.currentRoute.value);
        store.clearCurrentDataLYCK();
        router.replace({ path: 'requisitionManagementList' });
      } else {
        proxy.$modal.msgError(response.msg || '提交审批失败');
      }
    }).catch((error) => {
      console.error('提交审批失败:', error);
      proxy.$modal.msgError(error.message || '提交审批失败，请重试');
    });
  }
}
function toLastView(visitedViews, view) {
  const latestView = visitedViews.slice(-1)[0];
  if (view.name == 'RequisitionManagement') {
    //调拨单据号删除
    sessionStorage.setItem('busNoLY', '');
  }
  if (latestView) {
    router.push(latestView.fullPath);
  } else {
    if (view.name === 'Dashboard') {
      router.replace({ path: '/redirect' + view.fullPath });
    } else {
      router.push('/');
    }
  }
}
// 药品列表搜索
function handleSearch(value) {
  medicineSearchKey.value = value;
}

// 选择药品
function selectRow(rowValue, index) {
  rowIndex.value = index;
  // form.purchaseinventoryList[index].sourceLocationId = receiptHeaderForm.sourceLocationId
  // form.purchaseinventoryList[index].purposeLocationId = receiptHeaderForm.purposeLocationId
  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].unitList = rowValue.unitList[0];
  form.purchaseinventoryList[index].lotNumber = rowValue.lotNumber;
  form.purchaseinventoryList[index].ybNo = rowValue.ybNo;
  // 出库仓库：优先表头当前所选仓库（避免先选药后选仓时行上一直为空）
  form.purchaseinventoryList[index].sourceLocationId =
    receiptHeaderForm.headerLocationId || form.purchaseinventoryList[index].sourceLocationId || '';
  getPharmacyCabinetList().then((res) => {
    purposeTypeListOptions.value = res.data;
    handleLocationClick(1, rowValue, index)
  });
  form.purchaseinventoryList[index].itemQuantity = 0;
  form.purchaseinventoryList[index].totalPrice = 0;
  store.setCurrentDataLYCK({
    purchaseinventoryList: form.purchaseinventoryList,
    receiptHeaderForm: receiptHeaderForm,
  });
}

/** 多条库存记录时取可领数量最大的一条（避免仅取 res.data[0] 恰好为 0） */
function pickBestOrgQuantityRow(list) {
  if (!Array.isArray(list) || list.length === 0) return null;
  return list.reduce((best, cur) => {
    const cq = Number(cur?.orgQuantity ?? 0);
    const bq = Number(best?.orgQuantity ?? 0);
    return cq > bq ? cur : best;
  });
}

/** 表头「仓库」变化：同步每行 sourceLocationId 并重新拉库存（修复先选药品后选仓库行上仍无仓库 ID） */
function onHeaderWarehouseChange() {
  const hid = receiptHeaderForm.headerLocationId;
  form.purchaseinventoryList.forEach((r) => {
    r.sourceLocationId = hid || '';
  });
  form.purchaseinventoryList.forEach((r, idx) => {
    if (hid && r.itemId) {
      handleLocationClick(1, {}, idx);
    }
  });
}

// 选择仓库 / 选药品后拉取该仓库存
function handleLocationClick(item, row, index) {
  const r = form.purchaseinventoryList[index];
  let orgLocationId = r.sourceLocationId || receiptHeaderForm.headerLocationId || '';
  if (!orgLocationId) {
    proxy.$message.warning('请先在表头选择「仓库」。库存按仓库维度查询，未选仓库无法匹配您看到的总库存。');
    r.totalQuantity = 0;
    r.price = 0;
    return;
  }
  if (!r.sourceLocationId) {
    r.sourceLocationId = orgLocationId;
  }

  const lotTrimmed =
    r.lotNumber != null && String(r.lotNumber).trim() !== '' ? String(r.lotNumber).trim() : null;

  const runGet = (withLot) => {
    const params = { itemId: r.itemId, orgLocationId };
    if (withLot && lotTrimmed) {
      params.lotNumber = lotTrimmed;
    }
    return getCount(params);
  };

  const applyFromDto = (d, syncLotFromPick) => {
    if (syncLotFromPick && d.lotNumber != null && d.lotNumber !== '') {
      r.lotNumber = d.lotNumber;
    }
    r.itemTable = d.itemTable || '';
    r.totalQuantity = d.orgQuantity || 0;
    if (d.price) {
      r.price = d.price.toFixed(4);
    } else {
      r.price = 0;
    }
    r.supplierId = d.supplierId || '';
    r.startTime = toIssueDateTimeStr(d.productionDate) || '';
    r.endTime = toIssueDateTimeStr(d.expirationDate) || '';
    r.unitCode = r.unitList.minUnitCode;
    r.unitCode_dictText = r.unitList.minUnitCode_dictText;
    if (r.unitCode == r.unitList.minUnitCode) {
      r.price = d.price / r.partPercent || '';
      r.price = r.price.toFixed(4);
    } else if (r.price > 1) {
      r.price = r.price.toFixed(4);
    }
  };

  const persistStore = () => {
    store.setCurrentDataLYCK({
      purchaseinventoryList: form.purchaseinventoryList,
      receiptHeaderForm: receiptHeaderForm,
    });
  };

  runGet(true)
    .then((res) => {
      const list = res.data || [];
      const d = pickBestOrgQuantityRow(list);
      if (d) {
        applyFromDto(d, false);
        if (Number(r.totalQuantity) <= 0) {
          proxy.$message.warning('仓库数量为0，无法调用！');
        }
        persistStore();
        return;
      }
      if (lotTrimmed) {
        return runGet(false).then((res2) => {
          const list2 = res2.data || [];
          const d2 = pickBestOrgQuantityRow(list2);
          if (d2) {
            applyFromDto(d2, true);
            if (Number(r.totalQuantity) <= 0) {
              proxy.$message.warning('仓库数量为0，无法调用！');
            } else {
              proxy.$message.info(
                '所选批号在本仓库无对应库存或批号不一致，已按仓库实物回显批号与可领数量，请核对。'
              );
            }
          } else {
            r.totalQuantity = 0;
            r.price = 0;
            proxy.$message.warning('仓库数量为0，无法调用！');
          }
          persistStore();
        });
      }
      r.totalQuantity = 0;
      r.price = 0;
      proxy.$message.warning('仓库数量为0，无法调用！');
      persistStore();
    })
    .catch(() => {
      r.totalQuantity = 0;
      r.price = 0;
      persistStore();
    });
}
// 切换仓库类型获取药房/药库列表
// function handleChangeLocationType(value) {
//   if (value == 16) {
//     getPharmacyList().then((res) => {
//       locationList.value = res.data;
//     });
//   } else if (value == 11) {
//     getDispensaryList().then((res) => {
//       locationList.value = res.data;
//     });
//   }
// }

// 单位处理
function handleUnitCodeChange(row, index, value) {
  // 防止点击和已选一样的问题
  if (
    !form.purchaseinventoryList[index].statusMaxvalue &&
    row.measurementUnitCode == value &&
    route.query.supplyBusNo
  ) {
    return;
  }
  if (row.minUnitCode == value) {
    //最小计量单位
    // form.purchaseinventoryList[index].totalQuantity = oldtotalQuantity.value;
    // form.purchaseinventoryList[index].itemQuantity = olditemQuantity.value;
    form.purchaseinventoryList[index].totalQuantity = form.purchaseinventoryList[index]
      .oldtotalQuantity
      ? form.purchaseinventoryList[index].oldtotalQuantity
      : form.purchaseinventoryList[index].totalQuantity;
    form.purchaseinventoryList[index].itemQuantity = form.purchaseinventoryList[index]
      .olditemQuantity
      ? form.purchaseinventoryList[index].olditemQuantity
      : form.purchaseinventoryList[index].itemQuantity;
    form.purchaseinventoryList[index].itemQuantity = 0;
    form.purchaseinventoryList[index].totalPrice = 0;
    // 单价
    form.purchaseinventoryList[index].price =
      form.purchaseinventoryList[index].price / row.partPercent;
    form.purchaseinventoryList[index].price = form.purchaseinventoryList[index].price.toFixed(4);
    // if (!form.purchaseinventoryList[index].itemQuantity) {
    // }
  } else {
    form.purchaseinventoryList[index].itemQuantity = 0;
    form.purchaseinventoryList[index].totalPrice = 0;
    getMaxCounts(row, index);
  }
  form.purchaseinventoryList[index].statusMaxvalue = true;
  store.setCurrentDataLYCK({
    purchaseinventoryList: form.purchaseinventoryList,
    receiptHeaderForm: receiptHeaderForm,
  });
  // 切换成大的计量单位
  //   oldtotalQuantity.value = form.purchaseinventoryList[index].totalQuantity;
  //   form.purchaseinventoryList[index].totalQuantity =
  //     form.purchaseinventoryList[index].totalQuantity / row.partPercent;

  //   const integerPart1 = Math.floor(form.purchaseinventoryList[index].totalQuantity); // 获取整数部分
  //   const decimalPart1 = form.purchaseinventoryList[index].totalQuantity - integerPart1; // 获取小数部分

  //   if (decimalPart1) {
  //     form.purchaseinventoryList[index].totalQuantity =
  //       integerPart1 +
  //       form.purchaseinventoryList[index].unitList.unitCode_dictText +
  //       parseFloat(decimalPart1.toFixed(1)) * row.partPercent +
  //       form.purchaseinventoryList[index].unitCode_dictText;
  //   }

  //   // 数量
  //   olditemQuantity.value = form.purchaseinventoryList[index].itemQuantity;
  //   form.purchaseinventoryList[index].itemQuantity =
  //     form.purchaseinventoryList[index].itemQuantity / row.partPercent;

  //   const integerPart = Math.floor(form.purchaseinventoryList[index].itemQuantity); // 获取整数部分
  //   const decimalPart = form.purchaseinventoryList[index].itemQuantity - integerPart; // 获取小数部分

  //   if (decimalPart) {
  //     form.purchaseinventoryList[index].itemQuantity =
  //       integerPart +
  //       form.purchaseinventoryList[index].unitList.unitCode_dictText +
  //       parseFloat(decimalPart.toFixed(1)) * row.partPercent +
  //       form.purchaseinventoryList[index].unitCode_dictText;
  //   }
  //   form.purchaseinventoryList[index].price =
  //     form.purchaseinventoryList[index].price * row.partPercent;
  //   form.purchaseinventoryList[index].price = form.purchaseinventoryList[index].price.toFixed(4);
  // }
}

function getMaxCounts(row, index, counts) {
  form.purchaseinventoryList[index].oldtotalQuantity =
    form.purchaseinventoryList[index].totalQuantity;
  form.purchaseinventoryList[index].totalQuantity =
    form.purchaseinventoryList[index].totalQuantity / row.partPercent;

  const integerPart1 = Math.floor(form.purchaseinventoryList[index].totalQuantity); // 获取整数部分
  const decimalPart1 = form.purchaseinventoryList[index].totalQuantity - integerPart1; // 获取小数部分

  if (decimalPart1) {
    form.purchaseinventoryList[index].totalQuantity =
      integerPart1 +
      form.purchaseinventoryList[index].unitList.unitCode_dictText +
      (decimalPart1 * row.partPercent).toFixed(0) +
      form.purchaseinventoryList[index].unitList.minUnitCode_dictText;
  }

  //领用数量
  if (counts) {
    form.purchaseinventoryList[index].olditemQuantity =
      form.purchaseinventoryList[index].itemQuantity * row.partPercent;
    form.purchaseinventoryList[index].itemMaxQuantity =
      form.purchaseinventoryList[index].itemQuantity;
    const integerPart = Math.floor(form.purchaseinventoryList[index].itemQuantity); // 获取整数部分
    const decimalPart = form.purchaseinventoryList[index].itemQuantity - integerPart; // 获取小数部分
    console.log(
      integerPart,
      decimalPart,
      form.purchaseinventoryList[index].itemQuantity,
      121212121
    );
    if (decimalPart) {
      form.purchaseinventoryList[index].itemQuantity =
        integerPart +
        form.purchaseinventoryList[index].unitList.unitCode_dictText +
        (decimalPart * row.partPercent).toFixed(0) +
        form.purchaseinventoryList[index].minUnitCode_dictText;
    }
  } else {
    // form.purchaseinventoryList[index].olditemQuantity =  form.purchaseinventoryList[index].itemQuantity
    // form.purchaseinventoryList[index].itemQuantity =
    // form.purchaseinventoryList[index].itemQuantity / row.partPercent;
    // form.purchaseinventoryList[index].itemMaxQuantity = form.purchaseinventoryList[index].itemQuantity
    // const integerPart = Math.floor(form.purchaseinventoryList[index].itemQuantity); // 获取整数部分
    // const decimalPart = form.purchaseinventoryList[index].itemQuantity - integerPart; // 获取小数部分

    // if(decimalPart){
    //   form.purchaseinventoryList[index].itemQuantity = integerPart + form.purchaseinventoryList[index].unitList.unitCode_dictText +
    //   (decimalPart*row.partPercent).toFixed(0) +
    //   form.purchaseinventoryList[index].unitList.minUnitCode_dictText
    // }
    // if(counts==form.purchaseinventoryList[index].unitCode&&route.query.supplyBusNo){  // 修复编辑时在大单位重复点击大单位的bug
    //   form.purchaseinventoryList[index].totalQuantity = form.purchaseinventoryList[index].oldtotalQuantity
    //   form.purchaseinventoryList[index].itemQuantity = form.purchaseinventoryList[index].itemMaxQuantity
    //   form.purchaseinventoryList[index].totalPrice =  (form.purchaseinventoryList[index].price * form.purchaseinventoryList[index].itemQuantity).toFixed(4);
    // }else{
    form.purchaseinventoryList[index].price =
      form.purchaseinventoryList[index].price * row.partPercent;
    form.purchaseinventoryList[index].price = form.purchaseinventoryList[index].price.toFixed(4);
    // }
  }
  console.log(form.purchaseinventoryList[index].price, '121212121');
}
// 计算总价
function handleTotalPrice(index) {
  const r = form.purchaseinventoryList[index];
  r.olditemQuantity = r.itemQuantity * (r.partPercent ?? 1);
  r.itemMaxQuantity = r.itemQuantity;
  let purchaseItem = r;
  if (purchaseItem.price > 0 && purchaseItem.itemQuantity > 0) {
    r.totalPrice = purchaseItem.price * purchaseItem.itemQuantity;
    r.totalPrice = r.totalPrice.toFixed(4);
  }
  if (r.itemQuantity == 0) {
    r.totalPrice = 0;
  }
  const qtyErr = validateRequisitionQtyVsStock(r, index + 1);
  r.error = !!qtyErr;
  store.setCurrentDataLYCK({
    purchaseinventoryList: form.purchaseinventoryList,
    receiptHeaderForm: receiptHeaderForm,
  });
}
// 保存
function handleSave(row, index) {
  rowList.value = [];
  for (let i = 0; i < form.purchaseinventoryList.length; i++) {
    const line = form.purchaseinventoryList[i];
    if (!line) continue;
    const err = validateRequisitionQtyVsStock(line, i + 1);
    if (err) {
      proxy.$message.warning(err);
      return;
    }
  }
  form.purchaseinventoryList.map((row, index) => {
    if (row) {
      // 触发校验
      handleBlur(row);
      proxy.$refs['receiptHeaderRef'].validate((valid) => {
        if (valid) {
          proxy.$refs['formRef'].validate((valid) => {
            if (valid) {
              let rows = JSON.parse(JSON.stringify(row));
              delete rows.itemMaxQuantity;
              // delete rows.olditemQuantity
              // delete rows.oldtotalSourceQuantity
              // delete rows.oldtotalPurposeQuantity
              if (rows.unitCode == rows.unitList.minUnitCode) {
                rows.itemQuantity = form.purchaseinventoryList[index].olditemQuantity
                  ? form.purchaseinventoryList[index].olditemQuantity
                  : form.purchaseinventoryList[index].itemQuantity;
              } else {
                rows.itemQuantity = form.purchaseinventoryList[index].itemMaxQuantity
                  ? form.purchaseinventoryList[index].itemMaxQuantity
                  : form.purchaseinventoryList[index].itemQuantity;
              }
              if (rows.unitCode == rows.unitCode_dictText) {
                if (rows.unitCode_dictText == rows.unitList.minUnitCode_dictText) {
                  rows.unitCode = rows.unitList.minUnitCode;
                  console.log(rows.unitCode_dictText, 2323232);
                } else {
                  rows.unitCode = rows.unitList.unitCode;
                  rows.unitCode_dictText = rows.unitList.unitCode_dictText;
                  console.log(rows.unitCode_dictText, 23232326565);
                }
              }
              rowList.value.push(JSON.parse(JSON.stringify(rows)));
              if (
                rowList._rawValue &&
                rowList._rawValue.length == form.purchaseinventoryList.length
              ) {
                addTransferProducts(rowList._rawValue);
              }
            }
          });
        }
      });
    }
  });
}
function addTransferProducts(rowList) {
  const payload = (Array.isArray(rowList) ? rowList : []).map((item) => ({
    ...item,
    startTime: toIssueDateTimeStr(item.startTime),
    endTime: toIssueDateTimeStr(item.endTime),
    occurrenceTime: toIssueDateTimeStr(item.occurrenceTime) ?? item.occurrenceTime,
  }));
  addTransferProduct(JSON.parse(JSON.stringify(payload))).then((res) => {
    // 当前行没有id视为首次新增
    // if (!row.id) {
    //   data.isAdding = false; // 允许新增下一行
    // }
    if (res.data) {
      proxy.$message.success('保存成功！');
      form.purchaseinventoryList.map((row, index) => {
        form.purchaseinventoryList[index].id = res.data[index];
        form.purchaseinventoryList[index].isSave = true;
      });
    }
  });
}
/** 选择条数  */
function handleSelectionChange(selection) {
  // selectedData.value = selection.map((item) => ({ ...item })); // 存储选择的行数据
  ids.value = selection.map((item) => item.id);
  selectedRows.value = selection;
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

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) {
    delTransferProduct(ids).then((res) => {
      if (res.code == 200) {
        proxy.$message.success('删除成功');
      }
    });
  } else {
    if (length > 1) {
      delTransferProduct(ids).then((res) => {
        if (res.code == 200) {
          proxy.$message.success('删除成功');
        }
      });
    }
  }
  form.purchaseinventoryList = form.purchaseinventoryList.filter(
    (row) => !selectedRows.value.includes(row)
  );
  // 允许新增下一行
  if (form.purchaseinventoryList && form.purchaseinventoryList.length > 0) {
    data.isEdit = true;
  } else {
    data.isEdit = false;
  }
  data.isAdding = false;
}

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

function handleScan(row,index){
  rowData.value = row
  rowData.value.itemType = receiptHeaderForm.medicationType
  ypName.value = row.name
  openTraceNoDialog.value = true;
  currentIndex.value = index
}

/** 重置操作表单 */
function reset() {
  // form.value = {
  //   id: undefined,
  //   name: undefined,
  //   categoryCode: undefined,
  //   cwTypeCode: undefined,
  //   fwTypeCode: undefined,
  //   specialtyCode: undefined,
  //   locationId: undefined,
  //   offeredOrgId: undefined,
  //   activeFlag: undefined,
  //   extraDetails: undefined,
  //   contact: undefined,
  //   appointmentRequiredFlag: undefined,
  //   chargeName: undefined,
  //   price: undefined,
  //   description: undefined,
  //   ybType: undefined,
  //   title: undefined,
  //   comment: undefined,
  // };
  // proxy.resetForm("purchaseinventoryRef");

  // receiptHeaderForm = {
  //   busNo: undefined,
  //   practitionerId: undefined,
  //   occurrenceTime: undefined,
  //   supplierId: undefined,
  //   medicationType: "1",
  //   purposeTypeEnum: undefined,
  // };
  proxy.resetForm('receiptHeaderRef');
  form.purchaseinventoryList = [];
}
// 显示弹框
function show() {
  data.isEdit = false;
  data.isAdding = false;
  reset();
  visible.value = true;
  // purposeTypeListOptions.value = props.purposeTypeListOptions;
  categoryListOptions.value = props.categoryListOptions;
  // sourceTypeListOptions.value = props.sourceTypeListOptions;
  receiptHeaderForm.busNo = props.busNoAdd;
  // // 设置默认值为字典中的第一个值
  // if (purchase_type.value.length > 0) {
  //   form.value.medicationType = purchase_type.value[0].value;
  // }
}

// 确认当前行
function confirmCurrentRow() {}
// 显示弹框
function edit() {
  data.isAdding = false;
  data.isEdit = true;
  // reset();
  visible.value = true;
  // purposeTypeListOptions.value = props.purposeTypeListOptions;
  categoryListOptions.value = props.categoryListOptions;
  // sourceTypeListOptions.value = props.sourceTypeListOptions;
  // receiptHeaderForm.busNo = props.busNoAdd;
  // receiptHeaderForm = props.item.length > 0 ? props.item[0] : {};
  // receiptHeaderForm.busNo = props.item.supplyBusNo;
  //源仓库
  // receiptHeaderForm.sourceLocationId = props.editRow.sourceLocationId
  //目的仓库
  receiptHeaderForm.locationId = props.editRow.locationId;
  receiptHeaderForm.sourceLocationStoreId = props.editRow.sourceLocationStoreId; //源货位
  // receiptHeaderForm.purposeTypeEnum = props.editRow.purposeTypeEnum
  // receiptHeaderForm.purposeLocationStoreId = props.editRow.purposeLocationStoreId // 目的仓位
  receiptHeaderForm.lotNumber = props.editRow.lotNumber;
  receiptHeaderForm.ybNo = props.editRow.ybNo;
  receiptHeaderForm.price = props.editRow.price;
  receiptHeaderForm.totalPrice = props.editRow.totalPrice;
  receiptHeaderForm.busNo = props.editRow.supplyBusNo;
  // receiptHeaderForm.supplierId = props.editRow.supplierId;
  // receiptHeaderForm.practitionerId = props.editRow.practitionerId;
  receiptHeaderForm.occurrenceTime = formatDate(props.editRow.occurrenceTime);
  // receiptHeaderForm.purposeTypeEnum = props.editRow.purposeTypeEnum.toString();
  receiptHeaderForm.locationId = props.editRow.purposeTypeEnum.toString();
  receiptHeaderForm.medicationType =
    props.editRow.itemTable == 'med_medication_definition' ? '1' : '2';
  total.value = form.purchaseinventoryList.length;
  // handleChangePurposeTypeEnum(props.editRow.purposeTypeEnum.toString());
  handleChangepurposeLocationId(props.editRow.locationId.toString());
  setTimeout(() => {
    form.purchaseinventoryList = props.item.map((item) => {
      return {
        ...item,
        name: item.itemName,
        volume: item.volume,
        manufacturerText: item.supplierName,
        totalQuantity: item.totalQuantity,
        // totalQuantity: item.totalQuantity,
        startTime: formatDate(item.startTime),
        endTime: formatDate(item.endTime),
        occurrenceTime: formatDate(item.occurrenceTime),
        isSave: true,
      };
    });
  }, 100);

  loading.value = false;
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 提交审核 */
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;
    //   }
    // });
  }
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  form.value = JSON.parse(JSON.stringify(row));
  form.value.fwTypeCode = form.value.typeCode;
  open.value = true;
  title.value = '编辑';
}

/** 删除按钮操作 */
function handleDelete(row) {
  const delId = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除以上数据?')
    .then(function () {
      return delTransferProduct({ ids: delId.join(',') });
    })
    .then(() => {
      proxy.$modal.msgSuccess('删除成功');
    })
    .catch(() => {});
}
/** 调拨管理查询下拉树结构 */
function getTransferProductTypeList() {
  data.isAdding = false;
  getInit().then((response) => {
    // busNoAdd.value = response.data.busNo; // 单据号新增
    issueDepartmentDto.value = response.data.issueDepartmentDto;
    practitionerListOptions.value = response.data.practitionerListOptions;
    supplierListOptions.value = response.data.supplierListOptions;
    supplyStatusOptions.value = response.data.supplyStatusOptions;
  });
}

const locationList = ref([]);
function getBusNoInitList() {
  if (route.query.supplyBusNo) {
    receiptHeaderForm.busNo = route.query.supplyBusNo;
    viewStatus.value = route.query.view;
    sessionStorage.setItem('busNoLY', '');
  } else {
    if (!sessionStorage.getItem('busNoLY')) {
      store.clearCurrentDataLYCK();
      getBusNoInit().then((response) => {
        receiptHeaderForm.busNo = response.data.busNo;
        sessionStorage.setItem('busNoLY', receiptHeaderForm.busNo);
        // busNoAdd.value = response.data.busNo; // 单据号新增
      });
    } else {
      receiptHeaderForm.busNo = sessionStorage.getItem('busNoLY');
    }
  }
}

function handleChangepurposeLocationId(value) {
  // // 当领用部门变化时，更新 receiptHeaderForm 中的 locationId
  // receiptHeaderForm.locationId = value;
  // // 获取该部门对应的经手人列表
  // if (value) {
  //   const selectedDepartment = issueDepartmentDto.value.find(dept => dept.locationId === value);
  //   if (selectedDepartment) {
  //     // 这里可以根据需要添加获取经手人列表的逻辑
  //     console.log('选中的部门:', selectedDepartment);
  //   }
  // }
}

// 取消所有行编辑
function cancelEditAllRow() {
  // 遍历所有行，重置编辑状态
  form.purchaseinventoryList.forEach((row) => {
    row.isEditing = false;
    row.error = false;
  });
  data.isAdding = false;
}

getTransferProductTypeList();
getBusNoInitList();
getTransferProductDetails();

// defineExpose({
//   show,
//   edit,
// });
// 导出
const exportRequiredParams = ref({
  pageNo: 1,
  pageSize: 10,
  busNo: route.query.supplyBusNo
});

// 追溯码对话框提交处理
function submit(traceNoData) {
  if (currentIndex.value >= 0 && form.purchaseinventoryList[currentIndex.value]) {
    form.purchaseinventoryList[currentIndex.value].traceNo = traceNoData.traceNo;
    form.purchaseinventoryList[currentIndex.value].ybNo = traceNoData.ybNo;
    proxy.$message.success('追溯码保存成功');
  }
  openTraceNoDialog.value = false;
}

function handleExport() {
  proxy.downloadGet(
    '/issue-manage/requisition/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>
