<template>
  <div class="page_historicalPrescription">
    <van-sticky>
      <div class="hisOrderNextOrLastTitle">
        <i class="iconfont icon-zuojiantou jiantou" @click="close"></i>
        <span class="titleName">处方调用</span>
        <span  @click="call"  v-show="getIsShow() == true">调用</span>
        <span  v-show="getIsShow() == false" style="color: gray">调用</span>

      </div>


      <div class="timeBox" v-if="all_prescription.length > 0">
        处方日期：
        <div class="toolOne toolThree" @click="startTimeShow = true">
          {{ startTime }}
          <van-icon name="arrow-down"/>
        </div>
        —
        <div class="toolOne toolThree" @click="endTimeShow = true">
          {{ endTime }}
          <van-icon name="arrow-down"/>
        </div>
      </div>
    </van-sticky>
    <div class="historyPreBox" v-if="all_prescription.length > 0" v-for="(item,index) in all_prescription" :key="index">
      <van-checkbox-group v-model="item.check_group">
        <div class="preOne" v-for="(subItem,index2) in item.prescriptions" :key="index2">
          <div class="preOneTitle">
            <van-checkbox :name="subItem.id">{{'处方' + (index2+ 1)}}</van-checkbox>
            <p class="preOneTip">{{getTime(item.record_date)}}</p>
          </div>
          <div class="preOneTitle newPreOneTitle">
            <p>医生：{{getDoctorName(item.doctor_id)}}</p>
            <p>科室：{{getDepartmentName(item.departments)}}</p>
          </div>
          <div v-if="subItem.type == 1">
            <!-- <p style="font-weight:bold;">医嘱内容：</p> -->
            <div  class="preOneContent" v-for="(advice_item,ad_index) in subItem.doctor_advice" :key="ad_index">
              <div>医嘱内容：<span class="preOneContentTitle">{{advice_item.advice_name}}</span></div>
              <div class="preParams">
                <span style="width:10%">{{advice_item.single_dose}}{{advice_item.single_dose_unit}}</span>
                <span style="width:25%">{{advice_item.delivery_way}}</span>
                <span style="width:25%">{{advice_item.execution_frequency}}</span>
                <span style="width:10%">{{advice_item.day}}天</span>
                <span style="width:10%">{{advice_item.prescribing_number}}{{advice_item.prescribing_number_unit}}</span>
                <span style="width:20%">{{advice_item.price}}元</span>
              </div>
              <p class="preOneContentTitle2">备注：{{advice_item.remark}}</p>
            </div>
          </div>
          <div v-if="subItem.type == 2">
            <!-- <p style="font-weight:bold;">项目内容：</p> -->

            <div  class="preOneContent" v-for="(project_item,p_index) in subItem.project" :key="p_index">
              <div>项目内容：<span class="preOneContentTitle">{{project_item.type == 2 ?project_item.project.project_name:project_item.good_info.good_name}}</span></div>
              <div class="preParams">
                <span style="width:10%">{{project_item.single_dose}}</span>
                <span style="width:25%">{{project_item.delivery_way}}</span>
                <span style="width:25%">{{project_item.execution_frequency}}</span>
                <span style="width:10%">{{project_item.number_days}}</span>
                <span style="width:10%">{{project_item.count }}{{project_item.unit}}</span>
                <span style="width:20%">{{project_item.price}}元</span>
              </div>
              <p class="preOneContentTitle2">备注：{{project_item.remark}}</p>
            </div>
          </div>
        </div>
      </van-checkbox-group>
    </div>
    <div class="noData" v-if='all_prescription.length <= 0'>
      <img src="../../../../assets/images/none.png" alt />
    </div>


    <van-popup v-model="startTimeShow" class="newPopup" position="bottom" :style="{ height: '40%' }">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        :min-date="minDate"
        :max-date="maxDate"
        @confirm="getstartTime"
        @cancel="startTimeShow = false"
      />
    </van-popup>
    <van-popup v-model="endTimeShow" class="newPopup" position="bottom" :style="{ height: '40%' }">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        :min-date="minDate"
        :max-date="maxDate"
        @confirm="getEndTime"
        @cancel="endTimeShow = false"
      />
    </van-popup>


  </div>
</template>


<script>

  import { uParseTime } from "@/utils/tools";
  import {
    GetCallHisPrescription,
  } from "@/api/his";
  export default {
    props: {
      all_prescription: Array,
      doctors: Array,
      departments: Array,
      srcPrescriptions:Array,
      patient_id:Number
    },
  data() {
    return {
      currentDate: new Date(),
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(2030, 10, 1),
      startTime: '',
      endTime: '',
      startTimeShow: false,
      endTimeShow: false,
      result: [],
      checkGroup:[],

    }
  },
  created() {
    var date = new Date();
    var year = date.getFullYear(); //获取完整的年份(4位)
    var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1).toString() : date.getMonth() + 1; //获取当前月份(0-11,0代表1月)
    var day = date.getDate() < 10 ? "0" + date.getDate().toString() : date.getDate(); //获取当前日(1-31)
    var last_month = date.getMonth() < 10 ? "0" + date.getMonth().toString() : date.getMonth(); //获取当前月份(0-11,0代表1月)
    this.endTime = year + "-" + month + "-" + day
    this.startTime = year + "-" + last_month + "-" + day
  },
  methods: {
    getIsShow(){
      let is_show = false
      for (let i = 0; i < this.all_prescription.length; i++){
        if(this.all_prescription[i].check_group.length > 0){
          is_show = true
        }
      }
      return is_show

    },
    getTime(time) {
      if (time == 0) {
        return "";
      }
      return uParseTime(time, "{y}-{m}-{d}");
    },
    call(){
      let checkGroup = []

      let allPrescriptions = []
      let targetPrescriptions = []
      let prescriptions = []
      // console.log(this.all_prescription)
      for (let i = 0; i < this.all_prescription.length; i++) {
        for (let a = 0; a < this.all_prescription[i].check_group.length; a++) {
          checkGroup.push(this.all_prescription[i].check_group[a])
        }
        for (let a = 0; a < this.all_prescription[i].prescriptions.length; a++) {
          allPrescriptions.push(this.all_prescription[i].prescriptions[a])
        }
      }

      for (let i = 0; i < allPrescriptions.length; i++){
        for (let a = 0; a < checkGroup.length; a++){
          if(allPrescriptions[i].id == checkGroup[a]){
            targetPrescriptions.push(allPrescriptions[i])
          }
        }
      }

      for (let i = 0; i < targetPrescriptions.length; i++) {
        var prescription = targetPrescriptions[i];
        for (let b = 0; b < prescription.doctor_advice.length; b++) {
          // console.log(prescription.doctor_advice[b].drug.is_special_diseases)
          if (prescription.med_type == "14" && prescription.doctor_advice[b].drug.is_special_diseases != 1) {
            prescription.doctor_advice.splice(b--,1)
          }
        }
        for (let b = 0; b < prescription.project.length; b++) {
          if(prescription.project[b].type == 2){
            if (prescription.med_type == "14" && prescription.project[b].project.disease_directory != 1) {
              prescription.project.splice(b--,1)
            }
          }else if(prescription.project[b].type == 3){
            if (prescription.med_type == "14" && prescription.project[b].good_info.is_special_diseases != 1) {
              prescription.project.splice(b--,1)
            }
          }
        }
      }



      if (targetPrescriptions.length > 0) {
        for (let i = 0; i < targetPrescriptions.length; i++) {
          var prescription = targetPrescriptions[i]
          let tempAdvice = []
          let tempProject = []
          let tempAddition = []
          for (let b = 0; b < prescription.doctor_advice.length; b++) {
            let index = b + 1
            let obj = {
              advice_id: 0,
              drug_name: prescription.doctor_advice[b].advice_name,
              single_dose: prescription.doctor_advice[b].single_dose,
              delivery_way: prescription.doctor_advice[b].delivery_way,
              execution_frequency: prescription.doctor_advice[b].execution_frequency,
              retail_price: prescription.doctor_advice[b].price.toString(),
              remark: prescription.doctor_advice[b].remark,
              day:prescription.doctor_advice[b].day,
              prescribing_number:prescription.doctor_advice[b].prescribing_number.toString(),
              single_dose_unit:prescription.doctor_advice[b].single_dose_unit,
              prescribing_number_unit:prescription.doctor_advice[b].prescribing_number_unit,
              medical_insurance_number:prescription.doctor_advice[b].med_list_codg,
              id:prescription.doctor_advice[b].drug_id,
              drug:prescription.doctor_advice[b].drug,
              groupno: index,
              hosp_appr_flag:"1",

            }
            if (obj.prescribing_number_unit == prescription.doctor_advice[b].drug.min_unit){
              obj['retail_price'] =  prescription.doctor_advice[b].drug.min_price
            } else if(obj.prescribing_number_unit == prescription.doctor_advice[b].drug.max_unit){
              obj['retail_price'] =  prescription.doctor_advice[b].drug.retail_price
            } else {
              obj['retail_price'] =  prescription.doctor_advice[b].drug.min_price
            }
            tempAdvice.push(obj)

          }


          for (let b = 0; b < prescription.project.length; b++) {
            let obj = {
              id: 0,
              project_id: prescription.project[b].project_id,
              project_name: prescription.project[b].project.project_name,
              statistical_classification: prescription.project[b].project.statistical_classification,
              single_dose: prescription.project[b].single_dose,
              delivery_way: prescription.project[b].delivery_way,
              execution_frequency: prescription.project[b].execution_frequency,
              number_days: prescription.project[b].day,
              total:prescription.project[b].count,
              price:prescription.project[b].price,
              remark:prescription.project[b].remark,
              medical_code:prescription.project[b].project.medical_code,
            }
            if (prescription.project[b].type == 2) {
              obj['statistical_classification'] = prescription.project[b].project.statistical_classification
              obj['medical_code'] = prescription.project[b].project.medical_code
              obj['project_name'] = prescription.project[b].project.project_name
              obj['type'] =2
              obj['price'] = prescription.project[b].project.price



            } else if (prescription.project[b].type == 3) {
              obj['statistical_classification'] = ''
              obj['medical_code'] = prescription.project[b].good_info.medical_insurance_number
              obj['project_name'] = prescription.project[b].good_info.good_name
              obj['type'] =3
              obj['price'] = prescription.project[b].good_info.packing_price


            }
            tempProject.push(obj)
          }
          let obj = {
            id: 0,
            advices: tempAdvice,
            project: tempProject,
            addition:tempAddition,
            type:prescription.type,
            order_status:0,
          }
          prescriptions.push(obj)
        }
      }

      if(this.srcPrescriptions.length == 1){
        if(this.srcPrescriptions[0].advices.length == 0 && this.srcPrescriptions[0].project.length == 0){
          this.srcPrescriptions = []
          this.srcPrescriptions = prescriptions
          this.$emit('call', this.srcPrescriptions)
        }else{

          this.srcPrescriptions =  this.srcPrescriptions.concat(prescriptions)
          this.$emit('call', this.srcPrescriptions)
        }
      }else{
        this.srcPrescriptions =  this.srcPrescriptions.concat(prescriptions)
        this.$emit('call', this.srcPrescriptions)
      }
      this.close()
    },
    close() {
      for (let i = 0; i < this.all_prescription.length; i++) {
         this.all_prescription[i].check_group = []
      }

      this.$parent.close(5);
    },getDoctorName(doctor_id){
      // console.log(doctor_id)
      for (let i = 0; i < this.doctors.length; i++){
        if(this.doctors[i].admin_user_id == doctor_id){
          return  this.doctors[i].user_name
        }
      }
    },getDepartmentName(department_id){
      for (let i = 0; i < this.departments.length; i++){
        if(this.departments[i].id == department_id){
          return  this.departments[i].name
        }
      }
    },GetCallHisPrescription(params){
      GetCallHisPrescription(params).then(response => {
        if (response.data.state == 1) {
          this.all_prescription = response.data.data.prescriptions
          for (let i = 0; i < this.all_prescription.length; i++){
            this.all_prescription[i].check_group = []
          }
        } else {
          this.$message.error(response.data.msg)

        }
      })

    },
    getstartTime(value) {
      this.page = 1
      let year = value.getFullYear()
      let month = value.getMonth() + 1
      let day = value.getDate()
      if (month >= 1 && month <= 9) {
        month = `0${month}`
      }
      if (day >= 1 && day <= 9) {
        day = `0${day}`
      }

      this.startTime = `${year}-${month}-${day}`
      this.startTimeShow = false

      let params={
        patient_id:this.patient_id,
        start_time: this.startTime,
        end_time:this.endTime
      }
      this.GetCallHisPrescription(params)


    },
    getEndTime(value) {
      this.page = 1
      let year = value.getFullYear()
      let month = value.getMonth() + 1
      let day = value.getDate()
      if (month >= 1 && month <= 9) {
        month = `0${month}`
      }
      if (day >= 1 && day <= 9) {
        day = `0${day}`
      }
      this.endTime = `${year}-${month}-${day}`
      this.endTimeShow = false


      let params={
        patient_id:this.patient_id,
        start_time: this.startTime,
        end_time:this.endTime
      }
      this.GetCallHisPrescription(params)
    },
  },
  }
</script>
