<template>
  <div class="main-contain">
    <div class="position">
      <bread-crumb :crumbs="crumbs"></bread-crumb>
      <el-row style="float:right;">
        <el-col :span="24">
          <el-button
            size="small"
            icon="el-icon-printer"
            type="primary"
            @click="printAction"
            >打印</el-button
          >
        </el-col>
      </el-row>
    </div>
    <div class="app-container" style="background-color: white;">
      <div id="print_content">
        <div class="print_main_content">
          <div class="table_panel">
            <div v-for="(main_collection,index) in scheduleData" :key="index" style="page-break-after: always;">
              <div class="signPrint" v-if="org_id != 10188 && org_id!=10217  && org_id!=10340 && org_id!=9671 && org_id!=9675">
                <div>
                 <span v-if="printObj.zone == 1">分区/</span>
                 <span v-if="printObj.number == 1">床位号:</span>
                 <span v-if="printObj.zone == 1">{{main_collection.zone.name}}/</span> 
                 <span v-if="printObj.number == 1">{{main_collection.number.number}}</span>
                </div>
                <div>姓名:
                  <span v-if="type ==1 && printObj.name == 1">{{main_collection.patient.name}} </span>
                  <span v-if="type ==2 && printObj.name == 1">{{main_collection.patient}} </span>
                </div>
                <div v-if="printObj.anticoagulant == 1">抗凝剂:
                  <span v-if="org_id == 10138 ">{{ main_collection.dialysissolution.anticoagulant ? anticoagulants_confit[main_collection.dialysissolution.anticoagulant].name + ' 5000iu/瓶' : ''}}</span>
                  <span v-else >
                   <span>{{getAnticoagulant(main_collection.dialysissolution.mode_id)}}</span>
                  </span>
                </div>
               <div v-if="org_id == 10188 || org_id == 10217 || org_id == 10340 || org_id == 9671 || org_id == 9675">首剂:{{main_collection.dialysissolution.anticoagulant_shouji}}</div>
                <div v-if="org_id == 10188 || org_id == 10217 || org_id == 10340 || org_id == 9671 || org_id == 9675">维持:{{main_collection.dialysissolution.anticoagulant_weichi}}</div>
                <div v-if="printObj.anticoagulant_zongliang == 1">
                  用量:
                  <span v-if="main_collection.dialysissolution.anticoagulant == 1">
                   {{main_collection.dialysissolution.anticoagulant_zongliang}}mg
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 2">
                    {{main_collection.dialysissolution.anticoagulant_zongliang}}mg
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 3">
                    {{main_collection.dialysissolution.anticoagulant_zongliang}}iu
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 4">
                    {{main_collection.dialysissolution.anticoagulant_zongliang}}mg
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 5">
                     {{main_collection.dialysissolution.anticoagulant_zongliang}}mg
                   </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 6">
                   {{main_collection.dialysissolution.anticoagulant_zongliang}}iu
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 7">
                    {{main_collection.dialysissolution.anticoagulant_zongliang}}iu
                   </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 8">
                     {{main_collection.dialysissolution.anticoagulant_zongliang}}iu
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 9">
                     {{main_collection.dialysissolution.anticoagulant_zongliang}}iu
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 10">
                     {{main_collection.dialysissolution.anticoagulant_zongliang}}iu
                    </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 11">
                    {{main_collection.dialysissolution.anticoagulant_zongliang}}iu
                   </span>
                </div>
                <div v-if="org_id == 10188 || org_id == 10217 || org_id == 10340 || org_id == 9671 || org_id == 9675">干体重:{{main_collection.assessmentbefor.dry_weight}}</div>
                <div v-if="org_id == 10188 || org_id == 10217 || org_id == 10340 || org_id == 9671 || org_id == 9675">透前体重:{{main_collection.assessmentbefor.weight_before}}</div>
                <div v-if="org_id == 10188 || org_id == 10217 || org_id == 10340 || org_id == 9671 || org_id == 9675">体重增加:{{(main_collection.assessmentbefor.weight_before - main_collection.assessmentbefor.dry_weight).toFixed(2)}}</div>
                <div>用法:静脉注射</div>
                <div>打印时间:{{currentDate}}</div>
              </div>
              <div class="signPrint" style="font-size:16px;" v-else>
                <div style="margin-left:50px;">姓名:
                  <span v-if="type ==1 && printObj.name == 1">{{main_collection.patient.name}} </span>
                  <span v-if="type ==2 && printObj.name == 1">{{main_collection.patient}} </span>
                  （{{ main_collection.patient.dialysis_no }}，{{ main_collection.patient.age }}岁）
                </div>
                <div class="printCell">
                  <span>
                    床号：{{ main_collection.number.number }}
                    <span style="width:auto;" v-if="main_collection.schedule_type == 1">上午</span>
                    <span style="width:auto;" v-if="main_collection.schedule_type == 2">下午</span>
                    <span style="width:auto;" v-if="main_collection.schedule_type == 3">晚上</span>
                  </span>
                  <span>{{ printDate }}</span>
                </div>
                <div class="printCell">
                  <span>干体重：{{main_collection.assessmentbefor.dry_weight}}Kg</span>
                  <span>抗凝剂：{{ main_collection.dialysissolution.anticoagulant ? anticoagulants_confit[main_collection.dialysissolution.anticoagulant].name : ''}}</span>
                </div>
                <div class="printCell">
                  <span>净重值：{{ main_collection.assessmentbefor.weight_before - main_collection.assessmentbefor.additional_weight }}Kg</span>
                 首剂：<span v-if="main_collection.dialysissolution.anticoagulant == 1">{{main_collection.dialysissolution.anticoagulant_shouji}} mg</span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 2">
                    {{main_collection.dialysissolution.anticoagulant_shouji}}mg
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 3">
                    {{main_collection.dialysissolution.anticoagulant_shouji}}iu
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 4">
                   {{main_collection.dialysissolution.anticoagulant_shouji}}mg
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 5">
                   {{main_collection.dialysissolution.anticoagulant_shouji}} mg
                   </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 6">
                   {{main_collection.dialysissolution.anticoagulant_shouji}}iu
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 7">
                    {{main_collection.dialysissolution.anticoagulant_shouji}}iu
                   </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 8">
                    {{main_collection.dialysissolution.anticoagulant_shouji}}iu
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 9">
                    {{main_collection.dialysissolution.anticoagulant_shouji}}iu
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 10">
                     {{main_collection.dialysissolution.anticoagulant_shouji}}iu
                    </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 11">
                    {{main_collection.dialysissolution.anticoagulant_shouji}}iu
                   </span>
                 
                </div>
                <div class="printCell">
                  <span>体重增加：{{ (main_collection.assessmentbefor.weight_before - main_collection.lastafterweight.weight_after).toFixed(2) }}Kg</span>
                  维持：{{main_collection.dialysissolution.anticoagulant_weichi}}
                   <span v-if="main_collection.dialysissolution.anticoagulant == 1">
                    mg/h
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 2">
                    mg/h
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 3">
                   iu/h
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 4">
                    mg/h
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 5">
                    mg/h
                   </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 6">
                   iu/h
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 7">
                   iu/h
                   </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 8">
                    iu/h
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 9">
                    iu/h
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 10">
                    iu/h
                    </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 11">
                    iu/h
                   </span>
              
                </div>
                <div class="printCell">
                  <span>处方脱水量：{{ main_collection.prescription.target_ultrafiltration }}L</span>
                  总量：{{main_collection.dialysissolution.anticoagulant_zongliang}}
                  <span v-if="main_collection.dialysissolution.anticoagulant == 1">
                    mg
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 2">
                    mg
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 3">
                    iu
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 4">
                   mg
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 5">
                    mg
                   </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 6">
                   iu
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 7">
                    iu
                   </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 8">
                     iu
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 9">
                   iu
                  </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 10">
                    iu
                    </span>
                  <span v-if="main_collection.dialysissolution.anticoagulant == 11">
                   iu
                   </span>
          
                </div>
              </div>
           </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import { getScheduleList,getPrintList,getNextScheduleWeekDay } from '@/api/schedule'
  import { parseTime } from '@/utils'
  import { uParseTime } from '@/utils/tools'
  import BreadCrumb from '@/xt_pages/components/bread-crumb'
  import print from 'print-js'
  const moment = require('moment')
  export default {
    name: 'SchedulePrint',
    data() {
      return {
        crumbs: [
          { path: false, name: '排班提醒' },
          { path: false, name: '排班提醒打印' }
        ],
        scheduleData:[],
        week_type: 0,
        day:'',
        td_1_width: '10%',
        td_2_width: '10%',
        td_3_width: '25%',
        td_4_width: '45%',
        modeOptions: null,
        anticoagulants_confit:null,
        currentDate:'',
        start_time:moment().week(moment().week() + 1).startOf('week').unix(),
        end_time:moment().week(moment().week() + 1).endOf('week').unix(),
        org_id:0,
        now_time:0,
        zone:"",
        pre_status:0,
        printObj:{},
        type:1,
        printDate:''
      }
    },
    components: {
      BreadCrumb
    },
    created() {
     this.type = this.$route.query.type
     this.getPrintList()
     this.org_id = this.$store.getters.xt_user.org.id
     console.log("机构ID",this.org_id)
     this.currentDate = this.getCurrentDate()

    this.printDate = moment().format("YYYY-MM-DD HH:mm:ss")
    this.modeOptions = this.$store.getters.treatment_mode
    this.anticoagulants_confit = this.$store.getters.anticoagulants_confit
    this.week_type = this.$route.query.week_type
    this.week_time  = this.$route.query.week_time
    this.zone = this.$route.query.zone
    this.pre_status = this.$route.query.prestatus

    if(parseInt(this.$route.query.type) == 1){
      const params = {
          week_type:this.week_type,
          week_time:this.week_time,
          zone:this.zone,
        }
       console.log("params",params)
       getScheduleList(params).then(response=>{
         if(response.data.state ==1){
           var list = response.data.data.list
          
           for(let i=0;i<list.length;i++){
              list[i].sort = list[i].number.sort
           }
           var arr = list.sort(this.compare('sort'))
           
           this.scheduleData = arr

             let a = response.data.data.day
              console.log("a",a)
              var myDate = new Date(a);
              var year = myDate.getFullYear(); //年
              var month = myDate.getMonth() + 1; //月
              var day = myDate.getDate(); //日
              var days = myDate.getDay();
              switch(days) {
              case 1:
                days = '星期一';
                break;
              case 2:
                days = '星期二';
                break;
              case 3:
                days = '星期三';
                break;
              case 4:
                days = '星期四';
                break;
              case 5:
                days = '星期五';
                break;
              case 6:
                days = '星期六';
                break;
              case 0:
                days = '星期日';
                break;
          }
          var str = year + "年" + month + "月" + day + "日  " + days;
          this.currentDate = str
          }
       })

      var now_time = parseInt(this.week_type) * 86400 + this.start_time
      console.log("now",now_time)
      this.now_time = now_time
    }
    if(parseInt(this.$route.query.type) == 2){
       this.getNextScheduleWeekDay()
    }

    },
    methods: {
      getAdvice:function(doctor_advice) {
        if (doctor_advice != null) {

        let name = ""
        for (let i = 0; i< doctor_advice.length; i++) {

          let prescribing_number = ''
          let single_dose = ''
          let drug_spec = ''

          if (doctor_advice[i].prescribing_number > 0) {
            prescribing_number = doctor_advice[i].prescribing_number + doctor_advice[i].prescribing_number_unit
          }
          if (doctor_advice[i].single_dose > 0) {
            single_dose = ' 单次用量 ' + doctor_advice[i].single_dose + doctor_advice[i].single_dose_unit
          }

          if (doctor_advice[i].drug_spec > 0) {
            drug_spec = doctor_advice[i].drug_spec + doctor_advice[i].drug_spec_unit
          }

          name = name + doctor_advice[i].advice_name + " " + drug_spec + " " + prescribing_number + " " + single_dose + " " + doctor_advice[i].delivery_way + " " + doctor_advice[i].execution_frequency + " " + doctor_advice[i].remark + "\n"

          if (doctor_advice[i].child.length > 0) {

            for (let a = 0; a < doctor_advice[i].child.length; a++) {

              if (doctor_advice[i].child[a].prescribing_number > 0) {
                doctor_advice[i].child[a]['presc'] = doctor_advice[i].child[a].prescribing_number + doctor_advice[i].child[a].prescribing_number_unit
              } else {
                doctor_advice[i].child[a]['presc'] = ''
              }

              if (doctor_advice[i].child[a].single_dose > 0) {
                doctor_advice[i].child[a]['single'] = " " + ' 单次用量 ' + " " + doctor_advice[i].child[a].single_dose + doctor_advice[i].child[a].single_dose_unit
              } else {
                doctor_advice[i].child[a]['single'] = ''
              }

              name = name + "▲" + doctor_advice[i].child[a].advice_name + ""
                + doctor_advice[i].child[a].advice_desc
                + doctor_advice[i].child[a].drug_spec_unit
                + doctor_advice[i].child[a].presc
                + doctor_advice[i].child[a].single + "\n"
            }

          }
        }
          return name


        }

      },
      printAction: function() {
        const style = '@media print {.signPrint{margin-left:30px;} .print_main_content { background-color: white; width:960px;  margin:0 auto; padding: 0 0 20px 0; } .order_title_panl { text-align: center; } .main_title { font-size: 18px; line-height: 40px; font-weight: 500; } .table_panel { } .table { width: 100%; border: 1px solid; border-collapse: collapse; padding: 2px; } thead tr td { border: 1px solid; text-align: center; font-size: 20px; padding: 15px 5px; } tbody tr td { border: 1px solid; text-align: center; font-size: 18px; padding: 10px 5px; } .proj { padding: 5px 0; text-align: left; } .proj_title { font-size: 16px; font-weight: 500; line-height: 25px; } .proj_item { font-size: 15px; line-height: 20px; } .zone_name { font-weight: 500; } .printCell span{display: inline-block;width: 180px;} @page {margin-top:10px;}}'
        const style1 = '@media print { .print_main_content { background-color: white; width:960px;  margin:0 auto; padding: 0 0 20px 0; } .order_title_panl { text-align: center; } .main_title { font-size: 18px; line-height: 40px; font-weight: 500; } .table_panel { } .table { width: 100%; border: 1px solid; border-collapse: collapse; padding: 2px; } thead tr td { border: 1px solid; text-align: center; font-size: 20px; padding: 15px 5px; } tbody tr td { border: 1px solid; text-align: center; font-size: 18px; padding: 10px 5px; } .proj { padding: 5px 0; text-align: left; } .proj_title { font-size: 16px; font-weight: 500; line-height: 25px; } .proj_item { font-size: 15px; line-height: 20px; } .zone_name { font-weight: 500; } .printCell span{display: inline-block;width: 170px;} @page {margin-top:10px;}}'
        if(this.org_id == 10188 || this.org_id == 10217 || this.org_id == 10340){
          printJS({
            printable: 'print_content',
            type: 'html',
            documentTitle: '  ',
            style: style1,
            scanStyles: false
          })
        }else{
          printJS({
            printable: 'print_content',
            type: 'html',
            documentTitle: '  ',
            style: style,
            scanStyles: false
          })
        }
        
      }, getSchedulesType: function(type) {
        let type_name = ''
        switch (type) {
          case 1:
            type_name = '上午'
            break
          case 2:
            type_name = '下午'
            break
          case 3:
            type_name = '晚上'
            break
        }
        return type_name
      },
      getCurrentDate: function () {
        console.log(this.$route.query.week_type)
        var myDate = new Date();
        var year = myDate.getFullYear(); //年
        var month = myDate.getMonth() + 1; //月
        var day = myDate.getDate() + 7; //日
        var newday = myDate.getDay();
        var days = parseInt(this.$route.query.week_type);
        var dayNum = parseInt(this.$route.query.week_type);
        switch(days) {
          case 1:
            days = '星期一';
            break;
          case 2:
            days = '星期二';
            break;
          case 3:
            days = '星期三';
            break;
          case 4:
            days = '星期四';
            break;
          case 5:
            days = '星期五';
            break;
          case 6:
            days = '星期六';
            break;
          case 7:
            days = '星期日';
            break;
        }
        if(newday > dayNum){
          let lastDay = day - (newday - dayNum)
          console.log("lastDay1",lastDay)
          var str = year + "年" + month + "月" + lastDay + "日  " + days;
          return str;
        }else if(newday < dayNum){
          let lastDay = day + (dayNum - newday)
          console.log("lastDay2",lastDay)
          var str = year + "年" + month + "月" + lastDay + "日  " + days;
          return str;
        }else{
          var str = year + "年" + month + "月" + day + "日  " + days;
          return str;
        }


      },

       compare(property) {
       return function (a, b) {
          var value1 = a[property];
          var value2 = b[property];
          return value1 - value2;
      }
     },
     getTime(val) {
         if(val == ""){
          return ""
         }else {
          return uParseTime(val, '{y}-{m}-{d}')
         }
      },
     getPrintList(){
      getPrintList().then(response=>{
         if(response.data.state == 1){
           var list = response.data.data.list
           console.log("打印设置",list)
           this.printObj = list
         }
      })
     },
    getNextScheduleWeekDay() {
        const start = moment().weekday(1).format('YYYY-MM-DD'); //本周一
        const end = moment().weekday(7).format('YYYY-MM-DD')
        var zone = this.zone
        // var newArr = []
        // for(let i=0;i<this.zoneList.length;i++){
        //    newArr.push(this.zoneList[i].id)
        // }
        // var arr = newArr.join(",")
        // var str = ""
        // if(this.zone == 0){
        //    str = arr
        // }
        // if(zone != 0){
        //   str = zone.join(",")
        // }
        var now_time = parseInt(this.week_type) * 86400 + this.start_time
        const params = {
          start_time:moment().week(moment().week() + 1).startOf('week').unix(),
          end_time:moment().week(moment().week() + 1).endOf('week').unix(),
          week_type:this.week_type,
          week_time:this.week_time,
          zone:this.zone,
          record_date:this.getTime(now_time)
        }
        // const params = {
        //   start_time:1609603200,
        //   end_time:1610208000,
        //   week_type:this.week_type,
        //   week_time:this.week_time,
        // }
      console.log("params",params)
      getNextScheduleWeekDay(params).then(response => {
        this.scheduleData = [];
        if (response.data.state == 1) {
          var scheduleData = response.data.data.schedule;

          for(let i=0;i<scheduleData.length;i++){
              scheduleData[i].sort = scheduleData[i].number.sort
          }
            var arr =   scheduleData.sort(this.compare('sort'))
            console.log("元旦快乐",arr)
            this.scheduleData = arr

            // var newList = []
            // for(let i=0;i<scheduleData.length;i++){
            //   if(scheduleData.prescription.id > 0){
            //      newList.push(scheduleData[i])
            //   }
            // }
            // var arr =   newList.sort(this.compare('sort'))
            // console.log("元旦快乐",arr)
            // this.scheduleData = arr
        } else {
          this.$message.error("网络错误");
          return false;
        }
      });
     },
    getAnticoagulant(name){
     
      if(name == 0){
         name = ""
       }
       if(name == 1){
         name = "无肝素"
       }
       if(name == 2){
         name = "普通肝素"
       }
       if(name == 3){
         name ="低分子肝素"
       }

       if(name == 4){
         name = "阿加曲班"
       }
        if(name == 5){
         name = "枸橼酸钠"
       }
        if(name == 6){
         name = "低分子肝素钙"
       }
        if(name == 7){
         name = "低分子肝素钠"
       }

       return name
     },
     getAnticoagulantZonliang(arr,mode_id){
       var name = 0
       if(arr.length > 0){
         for(let i=0;i<arr.length;i++){
          if(mode_id == arr[i].mode_id){
            name = arr[i].anticoagulant_zongliang
          }
        } 
       }
       return name
     }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.print_main_content {
  background-color: white;
  max-width: 1500px;
  margin: 0 auto;
  padding: 0 0 20px 0;

  .order_title_panl {
    text-align: center;

    .main_title {
      font-size: 18px;
      line-height: 40px;
      font-weight: 500;
    }
  }
  .table_panel {
    .table {
      width: 100%;
      border: 1px solid;
      border-collapse: collapse;
      padding: 2px;

      thead {
        tr {
          td {
            border: 1px solid;
            text-align: center;
            font-size: 20px;
            padding: 15px 5px;
          }
        }
      }
      tbody {
        tr {
          td {
            border: 1px solid;
            text-align: center;
            font-size: 18px;
            padding: 10px 5px;

            .proj {
              padding: 5px 0;
              text-align: left;

              .proj_title {
                font-size: 16px;
                font-weight: 500;
                line-height: 25px;
              }

              .proj_item {
                font-size: 15px;
                line-height: 20px;

                .zone_name {
                  font-weight: 500;
                }
              }
            }
          }
        }
      }
    }
  }
}
.signPrint{
  -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;
}
.printCell{
  span{
    display: inline-block;
    width: 180px;
  }
}
</style>
