<template>
    <div>
        <div id='prescription-print3' class="prescription-print">
            <img style="width:100%;height:80px" v-if="org_id == 10138" src="https://kuyi.shengws.com/bailin/bltotle.jpg" alt="">
            <div class="printTitle">检验申请单</div>

            <div style="border:1px solid #000;">
                <div style="display:flex;justify-content: space-between;border-bottom:1px solid #000;line-height:40px;padding:0 10px;">
                    <div>是否急诊：否</div>
                    <div>结算方式：{{ faber && faber.transBody ? getName(faber.transBody.outputlist1) : '' }}</div>
                    <div>金额：{{ total }}</div>
                </div>
                <div style="display:flex;justify-content: space-between;border-bottom:1px solid #000;line-height:40px;padding:0 10px;">
                    <div>姓名：{{advicePrint[0].patient.name? advicePrint[0].patient.name.indexOf("（") > -1 ? advicePrint[0].patient.name.substring(0,advicePrint[0].patient.name.indexOf("（")) : advicePrint[0].patient.name:""}}</div>
                    <div>性别：
                        <span v-if="advicePrint[0].patient.gender == 1">男</span>
                        <span v-if="advicePrint[0].patient.gender == 2">女</span>
                    </div>
                    <div>年龄：{{advicePrint[0].patient.age?advicePrint[0].patient.age:""}}岁</div>
                </div>
                <div style="margin-bottom:20px;padding:10px 10px 0;">病史摘要：{{history.history_of_present_illness?history.history_of_present_illness:''}}</div>
                <!-- <div style="margin-bottom:20px;padding:0 10px;">体格检查：
                    <span>体温：{{ history.temperature ? history.temperature + '℃' : '/' }}</span>
                    <span>脉搏：{{ history.pulse ? history.pulse + '次/分' : '/' }}</span>
                    <span>呼吸：{{ history.breathing ? history.breathing + '次/分' : '/' }}</span>
                    <span>血压：{{ history.sbp }}/{{ history.dbp }}mmHg</span>
                </div> -->
                <div style="margin-bottom:20px;padding:0 10px;">临床诊断：{{ getDiagnosis(advicePrint[0].info.diagnosis) }}</div>
                <div style="display:flex;margin-bottom:20px;padding:0 10px;">
                    <div>检验项目：</div>
                    <div>
                        <div v-for="item in projectPrint" style="margin-bottom:10px;">{{ item.team.project_team }}</div>
                        <div v-for="item in singleProjectPrint" style="margin-bottom:10px;">{{ item.project.project_name }}</div>
                    </div>
                </div>
                <div style="display:flex;justify-content: space-between;border-top:1px solid #000;line-height:40px;padding:0 10px;">
                    <div>开单医生：{{ doctor ? doctor : '' }}</div>
                    <div>开单日期：
                        {{getTime(pre_time) ? getTime(pre_time).split(' ')[0] : ''}}
                    </div>
                    <div>医生签字：{{ doctor ? doctor : '' }}</div>
                </div>
            </div>   
            <img style="width:100%;" v-if="org_id == 10138" src="https://kuyi.shengws.com/bailin/blend.jpg" alt="">
        </div>
    </div>

</template>
<script>
import { jsGetAge, uParseTime } from '@/utils/tools'
import {getAllDoctorList,getPrescriptionPrint,getHisPatientDetail,getPatientCaseHistory} from "@/api/project/project"
import {getInitData} from "@/api/his/his"
export default {
    props:{
      patient_id:Number,
      record_date:String,
      prescription_id:Number,
      ids:String
    }, 
    data(){
      return {
        doctorList:[],
        advicePrint:{},
        patient:{},
        tableData:[],
        prescriptionInfo:[],
        hisPatient:{},
        department:[],
        prescriptions:[],
        projectList:[],
        orgname:"",
        diagnoses:[],
        pageArr:[],
        faber:{},
        total:0,
        projectPrint:[],
        time:'',
        doctor:'',
        org_id:'',
        singleProjectPrint:[]
      }
    },
   methods:{
    getPatientCaseHistory(){
        const params = {
          patient_id:this.patient_id
        }
        getPatientCaseHistory(params).then(response=>{
            if(response.data.state == 1){
                var history = response.data.data.history
                console.log("中国history222222",history)
                this.history = history
            }
        })
    },
     getAllDoctorList(){
      getAllDoctorList().then(response=>{
        if(response.data.state == 1){
               var doctor =  response.data.data.doctor
               
               this.doctorList = doctor
             }
         })   
     },

      getDoctor(id){
        var name = ""
        for(let i=0;i<this.doctorList.length;i++){
          if(id == this.doctorList[i].admin_user_id){
              name = this.doctorList[i].user_name
          }
        }
        return name
      },
      getTime(value, temp) {
        if (value != undefined) {
            return uParseTime(value, temp)
        }
        return ''
     },
       getPrescriptionPrint(){
         var params = {
          // patient_id:this.patient_id,
          // record_date:this.record_date,
          // prescription_id:this.prescription_id,
          patient_id:this.patient_id,
          record_date:this.record_date,
          prescription_id:this.prescription_id,
          ids:this.ids
        }
        console.log("params",params)
       getPrescriptionPrint(params).then(response=>{
         if(response.data.state == 1){
            var advicePrint =  response.data.data.advicePrint
            console.log("adviceprint9999",advicePrint)
            this.advicePrint = advicePrint
            this.prescriptions = advicePrint
            console.log("处方222222",this.prescriptions)
            var hisPatient = response.data.data.hisPatient
            console.log("hisPatient",hisPatient)
            this.hisPatient = hisPatient
            let projectPrint = []
            let total = 0
            this.advicePrint.map(item => {
              if(item.project.length > 0){
                  item.project.map(it => {
                        if(it.type == 2){
                            if(it.project.cost_classify == 3){
                                projectPrint.push(it)
                                total += it.project.price
                            }
                        }
                  })
                
              }
            })
            let data = []
            let data2 = []
            projectPrint.map(item => {
              if(item.team.id != 0){
                let status = data.some(it => (it.team.id == item.team.id))
                if(!status){
                  data.push(item)
                }
              }
              if(item.team.id == 0){
                data2.push(item)
              }
              
            })
          
            this.total = total
            this.pre_time = this.advicePrint[0].pre_time
            this.doctor = this.advicePrint[0].doctor
            this.projectPrint = data
            this.singleProjectPrint = data2
            console.log('99999999999999999',projectPrint)
            var projectlist =  response.data.data.projectlist

            var projectlist =  response.data.data.projectlist
            console.log("所有项目列表",projectlist)
            this.projectList = projectlist
            this.getPage()
            let outputlist1Name = response.data.data.his.patient_info ? JSON.parse(response.data.data.his.patient_info) : {};
            this.faber = outputlist1Name

            
          }
       })
     },
     getHisPatientDetail(){
          const params = {
              patient_id:this.patient_id
          }
       getHisPatientDetail(params).then(response=>{
          if(response.data.state == 1){
             var hisPatient =  response.data.data.hisPatient
             console.log("挂号病人",hisPatient)
             this.hisPatient = hisPatient
          }
       })
     },
     getInitData(){
       getInitData().then(response=>{
          if(response.data.state == 1){
            this.department = response.data.data.department
            this.diagnoses = response.data.data.diagnose.sort(this.compare('id'))
            console.log("争端",this.diagnoses)
          }
       })
     },
     getDepart(id){
         var name = ""
       for(let i=0;i<this.department.length;i++){
           if(id == this.department[i].id){
               name = this.department[i].name
           }
       }
       return name
     },
    getTotalOne(id) {

        var total = 0
        var addtotal = 0
        for (let i = 0; i < this.prescriptions.length; i++) {
          if(id == this.prescriptions[i].id){
            if (this.prescriptions[i].project != null) {
              for (let a = 0; a < this.prescriptions[i].project.length; a++) {
                total = total + this.prescriptions[i].project[a].price * this.prescriptions[i].project[a].count
              }
            }

            if (this.prescriptions[i].additionalcharge != null) {
              for (let a = 0; a < this.prescriptions[i].additionalcharge.length; a++) {
                addtotal = addtotal + this.prescriptions[i].additionalcharge[a].price * this.prescriptions[i].additionalcharge[a].count
              }
            }
              addtotal =  Math.floor(addtotal * 100) / 100
        }
       
       }

      for (let i = 0; i < this.prescriptions.length; i++) {
          if(id == this.prescriptions[i].id){
            if (this.prescriptions[i].advices != null) {
              for (let a = 0; a < this.prescriptions[i].advices.length; a++) {
                total = total + this.prescriptions[i].advices[a].price * this.prescriptions[i].advices[a].prescribing_number
              }
            }
          
            if (this.prescriptions[i].additionalcharge != null) {
              for (let a = 0; a < this.prescriptions[i].additionalcharge.length; a++) {
                addtotal = addtotal + this.prescriptions[i].additionalcharge[a].price * this.prescriptions[i].additionalcharge[a].count
              }
            }
              addtotal =  Math.floor(addtotal * 100) / 100
        }
       }

        return total + addtotal
      },
      
     getProjectName(id){
        var project_name = ""
        for(let i=0;i<this.projectList.length;i++){
           if(id == this.projectList[i].id){
               project_name = this.projectList[i].project_name
           }
        }
        return project_name
     },

     getDiagnosis(ids){
        let newIds = ids.split(',').sort(function(a,b){
          return a-b;
        })
        
        var name = ""
        let nameArr = []
        for(let i=0;i<this.diagnoses.length;i++){
          // if(id == this.diagnoses[i].id){
          //    name = this.diagnoses[i].class_name
          // }
          
          if(newIds.indexOf(this.diagnoses[i].id.toString()) > -1){
            // name += diagnoses[i].class_name + ' '
            nameArr.push(this.diagnoses[i].class_name)
          }
        }
        let newNameArr = []
        nameArr.map((item,index) => {
          if(item == '尿毒症'){
            newNameArr.push(item)
            nameArr.splice(index,1,'') 
          }
        })
        newNameArr.push(...nameArr)
        return newNameArr.join(' ')
     },
     compare(property) {
        return function(a,b){
          var value1 = a[property];
          var value2 = b[property];
          return value1 - value2;//升序排序
        }
      },
     getPage(){
        this.page = 1
        this.pageArr = []

        this.advicePrint.map(item => {
          let arr = []
          item.pageArr = []
          if(item.advices.length <= 5){
            this.page = 1
            arr.push(item.advices.length)
            item.pageArr.push(arr)
            
          }else if(item.advices.length > 5){
            this.page = parseInt(item.advices.length / 5)
            let num = item.advices.length % 5
            for (var i=0;i<this.page;i++){
              item.pageArr.push([5])
            }
            if(num != 0){
              item.pageArr.push([num])
            }
          }
        })
        // console.log('this.pageArr',this.pageArr)
      },
      getName(list) {
        console.log('list',list)
        let new_list = []
        for (let i = 0; i < list.length; i++) {
          if (list[i].aac031 == '1') {
            new_list.push(list[i])
          }
        }

        switch (new_list[0].bcc334) {
          case "A31001":
            return "深圳医保1档"
            break
          case "A31002":
            return "深圳医保2档"

            break
          case "A31003":
            return "深圳医保3档"

            break
          case "A31004":
            return "二档（少儿）"

            break
          case "A31005":
            return "学生二档"

            break
          case "A31006":
            return "大学生二档"

            break
          case "A32001":
            return "在职公务员"
            break
          case "A32002":
            return "在职驻深公务员"

            break
          case "A39301":
            return "家属统筹医疗"

            break
          case "A41001":
            return "工伤在职"

            break
          case "A51001":
            return "生育在职"

            break
          case "A52001":
            return "生育医疗一档"

            break
          case "A52002":
            return "生育医疗一档"

            break
          case "C31001":
            return "一档医疗退休"

            break
          case "C31002":
            return "二档医疗退休"
            break

        }



      },

   },
   created(){
      this.getAllDoctorList()
      this.getInitData()
      this.getPrescriptionPrint()
      this.getHisPatientDetail()
      this.getPatientCaseHistory()
      var xtuser = this.$store.getters.xt_user;
      this.orgname = xtuser.org.org_name;
      this.org_id = this.$store.getters.xt_user.org_id
     
   },
   watch:{
     ids:function(val){
      this.ids = val
      this.getPrescriptionPrint()
    }
   }
}
</script>


<style lang="scss" scoped>
.prescription-print{
  -webkit-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.27), 0 0 60px rgba(0, 0, 0, 0.06) inset;
    -moz-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.27), 0 0 40px rgba(0, 0, 0, 0.06) inset; 
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.27), 0 0 40px rgba(0, 0, 0, 0.06) inset;
    margin-bottom: 20px;
    padding:20px 10px;
}
.printTitle{
    font-size: 22px;
    text-align: center;
    font-weight: bold;
    margin-bottom: 10px;
}
.infoTitle{
    display: flex;
    margin-top:10px;
    line-height: 24px;
}
.infoTitle div{
    width: 200px;
}
.infoMain{
    display: flex;
    flex-wrap: wrap;
    margin-top:10px;
}
.infoMain div{
    width: 50%;
    line-height: 24px;
}
.prescriptionBox{
    padding:0 10px;
    min-height:400px;
}
.Rp{
    font-size: 22px;
    font-weight: bold;
}
.drugsBox{
    padding-left: 40px;
    margin-bottom: 10px;
}
.drugsBox div{
    line-height: 20px;
}
.drugsOne{
    line-height: 24px;
}
.drugsOne span{
    margin-right: 20px;
}
.doctorBox{
    display: flex;
    justify-content: space-between;
    padding:0 10px;
    line-height: 24px;
    border-bottom: 2px solid #000;
}
.actionBar{
    display: flex;
    justify-content: space-between; 
    line-height: 24px;
    padding:0 10px;
}
.actionBar p{
    width:150px;
}
.under_line{
  display: inline-block;
  border-bottom: 1px solid #000;
  flex: 1;
}
</style>
