<template>
    <!-- Modal -->
    <a-modal v-model:visible="modalVisible" fullscreen title="处方详情" title-align="start" :footer="false"
          @cancel="handleClose">
         <div class="titlebox">
           <div class="bar"></div>
           <div class="name">基本信息</div>
         </div>
         <a-form :model="modalForm"    ref="modalFormRef" :auto-label-width="true">
           <a-row :gutter="24" style="margin-top: 35px;">
             <a-col :span="12">
               <a-form-item field="avatar" label="订单编号：">
                 <span>{{modalForm.order_prescription_id}}</span>
               </a-form-item>
             </a-col>
             <a-col :span="12">
               <a-form-item field="idCard" label="处方状态：" >
                 <div class="box" >
                   <div class="cardNum">{{formatPrescriptionStatus(modalForm.prescription_status)}}</div>
                 </div>
               </a-form-item>
             </a-col>
           </a-row>
           <a-row :gutter="24" >
             <a-col :span="12">
               <a-form-item field="idCard" label="开方医生：" >
                 <div class="box" >{{ modalForm.doctor_name }} </div>
               </a-form-item>
             </a-col>
             <a-col :span="12">
               <a-form-item field="idCard" label="审方药师：" >
                 <div class="box" >{{modalForm.pharmacist_name}} </div>
               </a-form-item>
             </a-col>
           </a-row>
           <a-row :gutter="24" >
             <a-col :span="12">
               <a-form-item field="idCard" label="就 诊  人：" >
                 <div class="box" >{{  modalForm.patient_name}}</div>
               </a-form-item>
             </a-col>
           </a-row>
           <a-row :gutter="24" >
             <a-col :span="12">
               <a-form-item field="idCard" label="开方时间：" >
                 <div class="box" > {{ modalForm.doctor_created_time }}</div>
               </a-form-item>
             </a-col>
             <a-col :span="12">
               <a-form-item field="idCard" label="审方时间：" >
                 <div class="box" > {{ modalForm.pharmacist_verify_time }}</div>
               </a-form-item>
             </a-col>
           </a-row>
           <a-row :gutter="24" >
             <a-col :span="12">
               <a-form-item field="idCard" label="诊断：" >
                 <div class="box" >{{ modalForm.order_prescription_icd }} </div>
               </a-form-item>
             </a-col>
           </a-row>
           <a-row :gutter="24" >
             <a-col :span="12">
               <a-form-item field="idCard" label="用法用量：" >
                 <span class="box" v-for="item in modalForm.order_prescription_product"> {{ item.product_name
 }}【{{item.frequency_use}}】</span>
               </a-form-item>
             </a-col>
           </a-row>
           <a-row :gutter="24" >
             <a-col :span="12">
               <a-form-item field="idCard" label="医嘱：" >
                 <div class="box" >{{ modalForm.doctor_advice }}</div>
               </a-form-item>
             </a-col>
           </a-row>
           <a-divider />
           <div class="titlebox">
           <div class="bar"></div>
           <div class="name">药品信息</div>
         </div>
           <a-row :gutter="24" style="margin-top: 35px;">
             <a-col :span="24">
               <a-form-item field="avatar" :hide-label="true">
                <a-list>
                <a-list-item
                  v-for="item in modalForm.order_prescription_product"
                  :key="item.product_item_id"
                >
                  <a-list-item-meta
                    :title="item.product_name + item.product_spec"
                    :description="item.manufacturer + '*' + item.prescription_product_num
"
                  >
                    <template #avatar>
                      <a-avatar shape="square">
                        <img
                          alt="avatar"
                          src="https://p1-arco.byteimg.com/tos-cn-i-uwbnlip3yd/3ee5f13fb09879ecb5185e440cef6eb9.png~tplv-uwbnlip3yd-webp.webp"
                        />
                      </a-avatar>
                    </template>
                  </a-list-item-meta>
                </a-list-item>
              </a-list>
               </a-form-item>
             </a-col>
           </a-row>
           <a-divider />
           <div class="titlebox">
           <div class="bar"></div>
           <div class="name">处方信息</div>
         </div>
           <a-row :gutter="24" style="margin-top: 35px;">
             <a-col :span="24">
                <a-form-item field="" :hide-label="true">
            <tableUnit
              :pagination="false"
              :columns="chufang_columns"
              :data="chufang_data"
            ></tableUnit>
            <!-- <prescription :data="modalForm.order_prescription"></prescription> -->
          </a-form-item>
             </a-col>
           </a-row>
           <a-divider />
           <div class="titlebox">
             <div class="bar"></div>
             <div class="name">病例信息</div>
             <a-space
          style="
            margin-right: 8px;
            flex: 1;
            display: flex;
            justify-content: flex-end;
          "
        >
          <a-button type="primary" status="warning" @click="openPatient"
            >查看完整病例信息</a-button
          >
        </a-space>
           </div>
           <a-row :gutter="24"  style="margin-top: 35px" v-if="modalForm.order_inquiry_case">
        <a-col :span="24">
          <a-form-item  label="就诊人姓名：">
            <span>{{modalForm.order_inquiry_case.name }}&nbsp;&nbsp;（{{modalForm.order_inquiry_case.sex == 1 ? '男' : '女'}}&nbsp;&nbsp;{{ modalForm.order_inquiry_case.age }}岁）</span>
          </a-form-item>
        </a-col>
      </a-row>
       <a-row :gutter="24" v-if="modalForm.order_inquiry_case">
        <a-col :span="24">
              <a-form-item field="inquiry_no" label="病情描述：">
                <span>{{modalForm.order_inquiry_case.disease_desc}}</span>
              </a-form-item>
            </a-col> 
      </a-row>
      <!-- <a-row :gutter="24" v-if="modalForm.order_inquiry_case">
        <a-col :span="24">
              <a-form-item field="inquiry_no" label="确诊疾病：">
                <span>{{modalForm.order_inquiry_case.disease_class_name}}&nbsp;&nbsp;{{modalForm.user_doctor.department_custom_name}}</span>
              </a-form-item>
            </a-col> 
      </a-row>  -->
           <a-divider />
   
         </a-form>
        <div class="titlebox" >
           <div class="bar"></div>
           <div class="name">操作</div>
         </div>
         <a-row :gutter="24" style="margin-top: 35px;" >
           <a-col :span="24">
             <a-form-item field="" label="" no-style>
              <!--  -->
               <a-space style="margin-right: 8px;" v-if="modalForm.prescription_status==4">
                 <a-button type="primary"  @click="lookMedince" v-has="'admin:sysPrescriptionList:vieworder'">查看订单</a-button>
               </a-space>
               <a-space >
                 <a-button type="primary"  @click="downloadFile(downloadUrl)" v-has="'admin:sysPrescriptionList:download'" >下载</a-button>
               </a-space>
             </a-form-item>
           </a-col>
         </a-row>
       </a-modal>
        <!-- 患者详情弹窗 -->
     <patientModal
      :patientVisible="patientVisible"
      :data="patientData"
      :modalTitle="'病例详情'"
      @patientVisibleChange="
        () => {
          patientVisible = false;
        }
      "
    ></patientModal>
    <medinceModal :medinceVisible="medinceVisible" :modalForm="medinceData" @medinceVisibleChange="()=>{medinceVisible=false}"></medinceModal>
   </template>
   <script setup>
   import {ref,toRefs,reactive,watchEffect,watch} from 'vue';
   import {getCase} from '@/api/prescription/list';
   import { getProductDetail,inquiryCase} from '@/api/order/list';
//    import {
//   productList,
//   getProductDetail,
//   cancelProduct,
//   reportPrePscription,
//   lookAddress,
//   inquiryCase
// } from '@/api/order/list';
   import {formatPrescriptionStatus, formatPharmacistStatus} from "@/utils/format"
   const IMG_URL = import.meta.env.VITE_IMG_URL;
   const props = defineProps({
     // 是否显示
     modalVisible: {
       type: Boolean,
       default: false,
     },
     modalForm: {
       type: Object,
       default:{
        order_inquiry_case:{
          order_prescription_id:''
        },
        user_doctor:{}
       }
     },
   });
   let express = []; //物流信息；
   const cur_express = ref([]);
   const patientVisible=ref(false);
   const patientData=reactive({});
   const medinceVisible=ref(false);
   let chufang_data = ref([]);
const chufang_columns = reactive([
  {
    title: '处方编号',
    dataIndex: 'prescription_code',
  },
  {
    title: '处方状态',
    dataIndex: 'prescription_status',
  },
  {
    title: '药师审核状态',
    dataIndex: 'pharmacist_audit_status',
  },
  {
    title: '医嘱',
    dataIndex: 'doctor_advice',
  },
  {
    title: '药师驳回原因',
    dataIndex: 'pharmacist_fail_reason',
  },
  {
    title: '处方链接',
    dataIndex: 'url',
    slotName: 'url',
  },
]);
 const downloadUrl=ref('')
   const emits = defineEmits(['familyVisibleChange']);
   const {modalVisible,modalForm} = toRefs(props);
   watch(modalForm.value,()=>{
       let data_arr = [{}];
      chufang_columns.forEach((item) => {
        if (item.dataIndex == 'prescription_status') {
          data_arr[0][item.dataIndex] = formatPrescriptionStatus(
            modalForm.value[item.dataIndex]
          );
        } else if (item.dataIndex == 'pharmacist_audit_status') {
          data_arr[0][item.dataIndex] = formatPharmacistStatus(
            modalForm.value[item.dataIndex]
          );
        } else if (item.dataIndex == 'url') {
          data_arr[0][item.dataIndex] =
            IMG_URL +
            '/applet/prescription/' +
            modalForm.value.order_prescription_id +
            '.pdf';
            downloadUrl.value=data_arr[0][item.dataIndex];
        } else if (item.dataIndex == 'pharmacist_fail_reason') {
          data_arr[0][item.dataIndex] = modalForm.value[item.dataIndex]
            ? modalForm.value[item.dataIndex]
            : '暂无';
        } else {
          data_arr[0][item.dataIndex] =modalForm.value[item.dataIndex];
        }
      });
      chufang_data.value= data_arr;
    
   },{immediate:true,deep:true})
  const handleGetCase=()=>{
    getCase(modalForm.value.order_inquiry_case.inquiry_case_id
).then(data=>{
  if(data.code==200){
    Object.assign(patientData,data.data)
    }
    })
  }
   const medinceData=reactive({
      order_product_refund: {},
    order_product_item: [],
    order_product_logistics: null,
    user_doctor: null,
    express:[],
    order_prescription: null,
    order_product_id:'',
    patientData:{}
   })
  const lookMedince=()=>{
    medinceVisible.value=true;
     getDetail(modalForm.value.order_product_id);
  };

  //药品详情
  const getDetail = async (order_product_id) => {
  chufang_data.vlaue=[];
  Object.assign(patientData,{});
  const { code, data, message } = await getProductDetail(order_product_id);
  if (code == 200) {
    Object.assign(medinceData, data);
    let result = data.order_prescription;
    express = data.order_product_logistics
      ? JSON.parse(data.order_product_logistics.logistics_content)
      : [];
    //cur_express.value = express.length > 1 && express.slice(0, 1);
   
    if(data.order_inquiry_case && data.order_inquiry_case.inquiry_case_id){
        const caseInfo= await inquiryCase(data.order_inquiry_case.inquiry_case_id);
        Object.assign(patientData,caseInfo.data);

      }
  }
   medinceData.chufang_columns=chufang_columns;
    medinceData.chufang_data=chufang_data.value;
    medinceData.express= express;//cur_express.value;
    medinceData.order_product_id=order_product_id;
    medinceData.patientData=patientData;
};

   // Akiraka 20230210 关闭弹窗
   const handleClose = () => {
     emits('familyVisibleChange', false);
   };
   const openPatient=() =>{
    handleGetCase();
    patientVisible.value=true;
   }
   const downloadFile=(fileUrl)=>{
    const request = new XMLHttpRequest()
    request.open('GET', fileUrl)
    request.responseType = 'blob'
    request.onload = (res) => {
        if (res.target.status == 200) {
            const url = window.URL.createObjectURL(res.currentTarget.response)
            const link = document.createElement('a')
            link.href = url
            link.setAttribute('download', '处方'+new Date().getTime())
            link.click()
        }
    }
    request.send()
}

   </script>
   <style scoped>
     .cardNum {
       width: 150px;
     }
    .arco-form-item-layout-horizontal:first-child,
     .arco-form-item-layout-horizontal:nth-child(2) {
       align-items: center;
     }
     .cellbox{
       margin-top: 35px;
     }
     .cellbox .cell{
       width:50%;
       border-bottom:1px dashed #efefef;
       margin-bottom: 20px;
     }
     .cellbox .cell:first-child{
       border: none;
     }
    
     .box {
       display: flex;
       align-items: center;
     }
   
     .cert .arco-form-item-label-col {
       flex: 0 0 8px !important;
     }
   
   </style>