<template>
  <div id='prescriptionPrint'>
    <div v-for="(item,index) in advicePrint" :key="index">
      <div id='prescription-print' class="prescription-print" style="page-break-after: always;">
        <div  style="float: right" v-if="item.med_type == '1111'">精一</div>
        <div  style="float: right" v-if="item.med_type == '1112'">精二</div>
        <div  style="float: right"
             v-if="item.med_type != '1111' && item.med_type != '1112'">普通
        </div>

        <div class="printTitle">{{ orgname }}处方笺</div>
        <div class="NoCell">
          <div><span style="font-size:20px;font-weight:bold;">No</span>
            <div class="underLine" style="width:120px;">
              {{ (item.prescription_number + (index + 1)).substring(item.prescription_number.length - 6) }}
            </div>
          </div>
          <div style="display:flex;">
            <div style="display: flex;flex-direction: column-reverse;">开具日期</div>
            <div class="underLine" style="width:60px;">
              {{ getTime(item.record_date) ? getTime(item.record_date).split(' ')[0].slice(0, 4) : '' }}
            </div>
            <div style="display: flex;flex-direction: column-reverse;">年</div>
            <div class="underLine" style="width:60px;">
              {{ getTime(item.record_date) ? getTime(item.record_date).split(' ')[0].slice(5, 7) : '' }}
            </div>
            <div style="display: flex;flex-direction: column-reverse;">月</div>
            <div class="underLine" style="width:60px;">
              {{ getTime(item.record_date) ? getTime(item.record_date).split(' ')[0].slice(8, 11) : '' }}
            </div>
            <div style="display: flex;flex-direction: column-reverse;">日</div>
          </div>
        </div>
        <div class="basicCell">
          <span>透析号</span>
          <span class="underLine" style="width:100px;">{{
              item.patient.dialysis_no ? item.patient.dialysis_no : ''
            }}</span>
          <span>治疗区</span>
          <span class="underLine" style="width:100px;">{{ item.info ? getDepart(item.info.departments) : '' }}</span>
          <span>费别</span>
          <span class="underLine" style="width:100px;"></span>
        </div>
        <div class="basicCell">
          <span>姓名</span>
          <span class="underLine" style="width:100px;">{{ item.patient.name ? item.patient.name : '' }}</span>
          <span>性别</span>
          <span class="underLine" style="width:60px;">
                <span v-if="item.patient.gender == 1">男</span>
                <span v-if="item.patient.gender == 2">女</span>
              </span>
          <span>年龄</span>
          <span class="underLine" style="width:60px;">{{ getAge(item.patient) ? getAge(item.patient) : '' }}岁</span>
          <span>电话</span>
          <span class="underLine" style="width:120px;">{{ item.patient.phone }}</span>
        </div>
        <div class="basicCell" style="margin-bottom:30px;">
          <span>临床诊断</span>
          <span class="underLine"
                style="width:100%;flex:1;text-align:left;padding-left:10px;">{{ getDiagnosis(item.info.diagnosis) }}</span>
        </div>
        <div class="prescriptionBox" style="border-top: 1px solid #000;border-bottom: 1px solid #000;">
          <div class="Rp">Rp:</div>
          <div class="drugsBox" v-for="(it,index) in item.advices" :key="index">
            <!-- <div class="drugsOne"><span style="font-weight:bold;">{{ index+1 + '.' }}</span>{{it.advice_name?it.advice_name:""}}&nbsp;&nbsp;{{it.single_dose}}{{it.single_dose_unit}}&nbsp;×&nbsp; {{it.prescribing_number}}{{it.prescribing_number_unit}}</div> -->
            <div class="drugsOne"><span style="font-weight:bold;">{{
                index + 1 + '.'
              }}</span>{{ it.advice_name ? it.advice_name : '' }}&nbsp;&nbsp;<span
                v-if="it.drug.min_unit != it.drug.dose_unit">{{ it.drug.dose }}{{ it.drug.dose_unit }}&nbsp;* &nbsp;</span>{{ it.drug.min_number }}{{ it.drug.min_unit }}/{{ it.drug.max_unit }}&nbsp;×&nbsp;
              {{ it.prescribing_number }}{{ it.prescribing_number_unit }}
            </div>
            <div style="margin-left:100px;">
              <span>用法：{{ it.single_dose }}{{ it.single_dose_unit }}</span>&nbsp;&nbsp;<span>{{ it.execution_frequency }}&nbsp;{{ it.delivery_way }}&nbsp;</span>&nbsp;&nbsp;<span>{{ it.advice_desc }}</span>
            </div>
          </div>
          <div class="drugsBox" v-for="(it,i) in item.additionalcharge" :key="i">
            <div class="drugsOne">{{ it.item_name ? it.item_name : '' }}:&nbsp;{{ it.price }}元/{{ it.count }}次</div>
          </div>

          <div class="drugsBox" v-for="(it,index) in item.project" :key="index">
            <div class="drugsOne"><span style="font-weight:bold;">{{
                index + 1 + '.'
              }}</span>{{ it.type == 2 ? getProjectName(it.project_id) ? getProjectName(it.project_id) : '' : it.good_info.good_name }}&nbsp;&nbsp;{{ it.single_dose }}{{ it.single_dose_unit }}&nbsp;×&nbsp;
              {{ it.count }}{{ unit }}
            </div>
            <div style="margin-left:100px;">
              <span>用法：{{ it.single_dose }}{{ it.single_dose_unit }}</span>&nbsp;&nbsp;<span>{{ it.delivery_way }}</span>&nbsp;&nbsp;<span>{{ it.advice_desc }}</span>
            </div>
          </div>
          <div class="drugsBox" v-for="(it,i) in item.additionalcharge" :key="i">
            <div class="drugsOne">
              {{ it.project_id ? it.project_id : '' }}:&nbsp;{{ it.price.toFixed(2) }}元/{{ it.count }}次
            </div>
          </div>
          <div style="text-align:center;">(以下空白)</div>

        </div>
        <div class="NoCell" style="margin-top:20px;">
          <div style="display:flex;">
            <span>医师</span>
            <span class="underLine" style="width:100px;">{{ item.doctor ? item.doctor : '' }}</span>
          </div>
          <div style="display:flex;">
            <span>金额</span>
            <span class="underLine" style="width:100px;"></span>
          </div>
        </div>
        <div class="NoCell">
          <div style="display:flex;">
            <span>药师（审核、核对、发药）</span>
            <span class="underLine" style="width:100px;"></span>
          </div>
          <div style="display:flex;">
            <span>调配</span>
            <span class="underLine" style="width:100px;"></span>
          </div>
        </div>
        <div style="page-break-after:always"></div>
      </div>
    </div>

  </div>


</template>
<script>
import { jsGetAge, uParseTime } from '@/utils/tools'
import { getAllDoctorList, getHisPatientDetail, getPrescriptionPrint } 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: [],
      org_id: 0
    }
  },
  methods: {
    getAge(patient) {
      var thisLen = patient.id_card_no.length
      var birth = ''
      if (thisLen == 15) {
        birth = '19' + patient.id_card_no.substr(6, 6)
      } else {
        birth = patient.id_card_no.substr(6, 8)
      }
      var births =
          birth.substr(0, 4) +
          '-' +
          birth.substr(4, 2) +
          '-' +
          birth.substr(6, 2)
      return jsGetAge(births, '-')

    },

    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,
        p_type:1,

      }
      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
          var projectlist = response.data.data.projectlist
          console.log('所有项目列表', projectlist)
          this.projectList = projectlist
        }
      })
    },
    getHisPatientDetail() {
      const params = {
        patient_id: this.patient_id
      }
      getHisPatientDetail(params).then(response => {
        if (response.data.state == 1) {
          var hisPatient = response.data.data.hisHospitalRecord
          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
          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(id) {
      let arr = id.split(',')
      var name = ''
      for (let i = 0; i < this.diagnoses.length; i++) {
        if (arr.indexOf(String(this.diagnoses[i].id)) > -1) {
          name += this.diagnoses[i].class_name + ' '
        }
      }
      return name
    }

  },
  created() {
    this.getAllDoctorList()
    this.getInitData()
    this.getPrescriptionPrint()
    this.getHisPatientDetail()
    var xtuser = this.$store.getters.xt_user
    this.orgname = xtuser.org.org_name
    this.org_id = xtuser.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;
}

.infoTitle {
  display: flex;
  border-bottom: 2px solid #000;
  margin-top: 10px;
  line-height: 24px;
  padding: 0 10px;
}

.infoTitle p {
  width: 200px;
}

.infoMain {
  display: flex;
  flex-wrap: wrap;
  border-bottom: 2px solid #000;
  padding: 0 10px;
}

.infoMain div {
  width: 50%;
  line-height: 24px;
}

.prescriptionBox {
  padding: 0 10px;
  min-height: 450px;
}

.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;
}

.NoCell {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}

.underLine {
  display: inline-block;
  border-bottom: 1px solid #000;
  text-align: center;
  line-height: 24px;
}

.basicCell {
  display: flex;
}

.basicCell span {
  line-height: 24px;
  margin-bottom: 10px;
}
</style>
