<!--
 * @Author: sjjh
 * @Date: 2025-09-05 22:37:10
 * @Description: 输血申请
-->
<template>
  <div class="bloodTransfusion-container">
    <el-transfer v-model="transferValue" :data="applicationList" filter-placeholder="项目代码/名称" filterable
      :titles="['未选择', '已选择']" />
    <div class="bloodTransfusion-form">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="120px" class="demo-ruleForm">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="项目类别" prop="patientName" style="width: 100%">
              <el-input v-model="form.patientName" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发往科室" prop="patientName" style="width: 100%">
              <el-input v-model="form.patientName" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="症状" prop="patientName" style="width: 100%">
              <el-input v-model="form.patientName" autocomplete="off" type="textarea" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="体征" prop="patientName" style="width: 100%">
              <el-input v-model="form.patientName" autocomplete="off" type="textarea" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="临床诊断" prop="patientName" style="width: 100%">
              <el-input v-model="form.patientName" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="其他诊断" prop="patientName" style="width: 100%">
              <el-input v-model="form.patientName" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="相关结果" prop="patientName" style="width: 100%">
              <el-input v-model="form.patientName" autocomplete="off" type="textarea" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="注意事项" prop="patientName" style="width: 100%">
              <el-input v-model="form.patientName" autocomplete="off" type="textarea" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>
<script setup name="BloodTransfusion">

import { getCurrentInstance, onBeforeMount, onMounted, reactive, ref } from 'vue';
import { patientInfo } from '../../../store/patient.js';
const { proxy } = getCurrentInstance();
const emits = defineEmits(['submitOk']);
const props = defineProps({});
import { getApplicationList, saveBloodTransfusio } from './api';
const state = reactive({});
const applicationListAll = ref();
const applicationList = ref();
const getList = () => {
  if (patientInfo.value?.inHospitalOrgId) {
    getApplicationList({
      pageSize: 10000,
      pageNum: 1,
      categoryCode: '28',
      organizationId: patientInfo.value.inHospitalOrgId,
      adviceTypes: '3', //1 药品 2耗材 3诊疗
    }).then((res) => {
      if (res.code === 200) {
       
        applicationListAll.value = res.data.records;
         applicationList.value = res.data.records.map((item) => {
          return {
            label: item.adviceName + item.adviceDefinitionId,
            key: item.adviceDefinitionId,
          };
        });
      } else {
        proxy.$message.error(res.message);
        applicationList.value = [];
      }
    });
  }
};
const transferValue = ref([]);
const form = reactive({
  patientName: '',
  patientSex: '',
  patientAge: '',
  patientPhone: '',
  patientAddress: '',
});
const rules = reactive({});
onBeforeMount(() => { });
onMounted(() => {
  getList();
});

const submit = () => {
  if (transferValue.value.length == 0) {
    return proxy.$message.error('请选择申请单');
  }
  let applicationListAllFilter = applicationListAll.value.filter((item) => {
    return transferValue.value.includes(item.adviceDefinitionId);
  });
  applicationListAllFilter = applicationListAllFilter.map((item) => {
    return {
      adviceDefinitionId: item.adviceDefinitionId,  /** 诊疗定义id */
      quantity: 1,// /** 请求数量 */
      unitCode: item.priceList[0].unitCode,   /** 请求单位编码 */
      unitPrice: item.priceList[0].price,     /** 单价 */
      totalPrice: item.priceList[0].price,   /** 总价 */
      positionId: item.positionId,//执行科室id
      ybClassEnum: item.ybClassEnum,//类别医保编码
      conditionId: item.conditionId,//诊断ID
      encounterDiagnosisId: item.encounterDiagnosisId,//就诊诊断id
      adviceType: item.adviceType,///** 医嘱类型 */
      definitionId: item.priceList[0].definitionId,//费用定价主表ID */
      definitionDetailId: item.definitionDetailId,//费用定价子表ID */
      accountId: patientInfo.value.accountId,// // 账户id
    };
  });
  saveBloodTransfusio({
    activityList: applicationListAllFilter,
    patientId: patientInfo.value.patientId, //患者ID
    encounterId: patientInfo.value.encounterId, // 就诊ID
    organizationId: patientInfo.value.inHospitalOrgId, // 医疗机构ID
    requestFormId: '', // 申请单ID
    name: '输血申请单',
    descJson: JSON.stringify(form),
    categoryEnum: '1', // 1 检验 2 检查 3 输血 4 手术
  }).then((res) => {
    if (res.code === 200) {
      proxy.$message.success(res.msg);
      applicationList.value = [];
      emits('submitOk');
    } else {
      proxy.$message.error(res.message);
    }
  });

};
defineExpose({ state, submit });
</script>
<style lang="scss" scoped>
.bloodTransfusion-container {
  .el-transfer {
    --el-transfer-panel-width: 400px !important;
  }

  height: 100%;
  width: 100%;

  .bloodTransfusion-form {
    padding: 8px 8px 0 8px;
    height: 100%;
    width: 100%;
    display: flex;
  }
}
</style>
