<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">
                <div>分区/床位号:{{main_collection.zone.name}}/{{main_collection.number.number}}</div> 
                <div>姓名:{{main_collection.patient.name}}</div> 
                <div>抗凝剂: {{ main_collection.prescription.anticoagulant ? anticoagulants_confit[main_collection.prescription.anticoagulant].name + ' 5000iu/瓶' : ''}}</div> 
                <div>用量:  
                  <span v-if="main_collection.prescription.anticoagulant == 1">{{main_collection.prescription.anticoagulant_zongliang?main_collection.prescription.anticoagulant_zongliang+'mg':''}}</span>
                  <span v-if="main_collection.prescription.anticoagulant == 2">{{main_collection.prescription.anticoagulant_zongliang?main_collection.prescription.anticoagulant_zongliang+'iu':''}}</span>
                  <span v-if="main_collection.prescription.anticoagulant == 3">{{main_collection.prescription.anticoagulant_zongliang?main_collection.prescription.anticoagulant_zongliang+'iu':''}}</span>
                  <span v-if="main_collection.prescription.anticoagulant == 4">{{main_collection.prescription.anticoagulant_zongliang?main_collection.prescription.anticoagulant_zongliang+'mg':''}}</span>
                  <span v-if="main_collection.prescription.anticoagulant == 5">{{main_collection.prescription.anticoagulant_zongliang?main_collection.prescription.anticoagulant_zongliang+'mg':''}}</span>
                  <span v-if="main_collection.prescription.anticoagulant == 6">{{main_collection.prescription.anticoagulant_zongliang?main_collection.prescription.anticoagulant_zongliang+'iu':''}}</span>
                  <span v-if="main_collection.prescription.anticoagulant == 7">{{main_collection.prescription.anticoagulant_zongliang?main_collection.prescription.anticoagulant_zongliang+'iu':''}}</span>
                </div> 
                <div>用法:静脉注射</div> 
                <div>打印时间:{{currentDate}}</div>
              </div>
           </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import { getScheduleList,getPrintList } 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:{}
      }
    },
    components: {
      BreadCrumb
    },
    created() {
     this.getPrintList()
     this.org_id = this.$store.getters.xt_user.org.id
     this.currentDate = this.getCurrentDate()
   
      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
     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
    },
    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 { .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; } }'

        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("list222222222",list)
           this.printObj = list
         }
      })
     }
    }
  }
</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;
}
</style>
