<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="order_title_panl">
            <span class="main_title"> &nbsp;
              透析药品
            </span>
            <span  style="float:right;">打印日期: &nbsp;
               {{$route.query.schedule_date}}
            </span>
          </div>
          <div class="table_panel">

            <table class="table">
              <thead>
              <tr>
                <td width="50">序号</td>
                <td width="100">药品名称</td>
                <td width="100">规格</td>
                <td width="100">数量</td>
                
              </tr>
              </thead>
              <tbody>
               <tr v-for="(item,i) in tableList" :key="i">
                <td>{{i+1}}</td>
                <td>{{item.advice_name}}</td>
                 <td>{{item.specification_name}}</td>
                <td>
                  <span v-if="config.is_open == 1">{{getHisCount(item.child,item.max_unit,item.min_unit,item.min_number)}}</span>
                  <span v-if="config.is_open != 1">{{getCount(item.child)}}</span>
                </td>
               </tr>
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import { getDialysisAdviceToday } from '@/api/dialysis'
  import { parseTime } from '@/utils'
  import { uParseTime } from '@/utils/tools'
  import BreadCrumb from '@/xt_pages/components/bread-crumb'
  import print from 'print-js'
  import { getDataConfig } from '@/utils/data'
  export default {
    name: 'SchedulePrint',
    data() {
      return {
        crumbs: [
          { path: false, name: '排班管理' },
          { path: false, name: '排班打印' }
        ],
        td_1_width: '8%',
        td_2_width: '8%',
        td_3_width: '64%',
        td_4_width: '20%',
        month: 1,
        begin_day: 1,
        total:[],
        list:[],
        schedule_date:"",
        schedule_type:0,
        partition_type:0,
        keywords:"",
        loading:false,
        tableData:[],
        numberList:[],
        partitionArr:[],
        his_config:{},
        typeList:[],
        rowList:[],
        dialysate_formulation:[],
        body_fluid_option:[],
        displace_liqui_part_option:[],
        blood_access_option:[],
        hemodialysisPipelinesOptions:[],
        tableList:[],
        druglist:[],
        config:{}
      }
    },
    components: {
      BreadCrumb
    },
    created() {
      this.getDialysisAdviceSchedudeList()
    },
    methods: {
      getDialysisAdviceSchedudeList() {
          var params = {
            schedule_type:this.$route.query.schedule_type,
            partion_type:this.$route.query.partion_type,
            selected_date:this.$route.query.schedule_date,
          }
        console.log("params2332232wo",params)
       getDialysisAdviceToday(params).then(response=>{
          if(response.data.state == 1){
             var doctorList = []
             var druglist = response.data.data.drug
             this.druglist = druglist
             this.startdialogVisible = true
             var list = response.data.data.list
          
             var config = response.data.data.config
            
             this.config = config
             if(list!=null && list.length > 0){
               for(let i=0;i<list.length;i++){
                for(let j=0;j<list[i].xt_doctor_advice.length;j++){
                  doctorList.push(list[i].xt_doctor_advice[j])
                }
               }
             }

             if(config.is_open!=1){
               if(doctorList!=null && doctorList.length > 0){
                let dataInfo = {}
                  doctorList.forEach((item, index) => {
                  let { advice_name } = item
                  if (!dataInfo[advice_name]) {
                    dataInfo[advice_name] = {
                      advice_name:item.advice_name,
                      child: [],
                      count:0,
                      drug_id:item.drug_id,
                      specification_name:item.advice_desc +item.drug_spec_unit,
                      patient_id:item.patient_id,
                    }
                  }
                })
                let arr = Object.values(dataInfo)
                // console.log("arr23332323232",arr)


                if(arr.length > 0){
                  for(let i=0;i<doctorList.length;i++){
                    for(let j=0;j<arr.length;j++){
                      if(doctorList[i].advice_name == arr[j].advice_name){
                          arr[j].child.push(doctorList[i])
                      }
                    }
                  }
                }
               
                this.tableList = []
                for(let i=0;i<list.length;i++){
                  for(let j=0;j<arr.length;j++){
                    if(list[i].patient_id == arr[j].patient_id){
                      this.tableList.push(arr[j])
                    }
                  }
                }
               
              }
             }
                       
           var his_doctor_advice = []

           for(let i=0;i<list.length;i++){
             for(let j=0;j<list[i].his_doctor_advice.length;j++){
                his_doctor_advice.push(list[i].his_doctor_advice[j])
             }
           }
           console.log("his_doctor_advice",his_doctor_advice)
          
            var newHisArr = []
            for(let i=0;i<his_doctor_advice.length;i++){    
               his_doctor_advice[i].count_one = 0
               if(his_doctor_advice[i].prescribing_number_unit == his_doctor_advice[i].drug.max_unit){
                  his_doctor_advice[i].prescribing_number = parseInt(his_doctor_advice[i].prescribing_number) * his_doctor_advice[i].drug.min_number
                  his_doctor_advice[i].count_one =  parseInt(his_doctor_advice[i].prescribing_number) * his_doctor_advice[i].drug.min_number
               }
            }

            for(let i=0;i<his_doctor_advice.length;i++){
              for(let j=0;j<druglist.length;j++){
                if(his_doctor_advice[i].drug_id == druglist[j].id){
                   newHisArr.push(his_doctor_advice[i])
                }
              }
            }
            console.log("下盘子3223323223",newHisArr)
            
             if(config.is_open == 1){
               if(newHisArr!=null && newHisArr.length > 0){
                  let dataInfoOne = {}
                  newHisArr.forEach((item, index) => {
                  let { advice_name } = item
                  if (!dataInfoOne[advice_name]) {
                    dataInfoOne[advice_name] = {
                      advice_name:item.advice_name,
                      child: [],
                      count:0,
                      specification_name:this.getSpecification(item.drug_id),
                      drug_id:item.drug_id,
                      patient_id:item.patient_id,
                      min_number:item.drug.min_number,
                      max_unit:item.drug.max_unit,
                      min_unit:item.drug.min_unit
                    }
                  }
                })
                let hisarr = Object.values(dataInfoOne)
                console.log("hisarr233232233232",hisarr)
                if(hisarr.length > 0){
                  for(let i=0;i<newHisArr.length;i++){
                    for(let j=0;j<hisarr.length;j++){
                      if(newHisArr[i].drug_id == hisarr[j].drug_id){
                          hisarr[j].child.push(newHisArr[i])
                      }
                    }
                  }
                }
                console.log("hisarr",hisarr)
                this.tableList = []
                this.tableList = hisarr
              }

              console.log("中国人民解放军",this.tableList)
             }
          }
        })
      },
     getTimeOne(val) {
        if(val == ""){
        return ""
        }else {
        return uParseTime(val, '{y}-{m}-{d}')
        }
     },
     getCount(val){
       var count = 0
       if(val!=null && val.length > 0){
        for(let i=0;i<val.length;i++){
          count += parseInt(val[i].prescribing_number)
        }
       }
       if(count > 0){
        return count
       }else{
        return 0
       }
     },
    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
     })
    },
    getSpecification(id){
        var specification_name = ""
        for(let i=0;i<this.druglist.length;i++){
          if(id == this.druglist[i].id){
            specification_name = this.druglist[i].dose+this.druglist[i].dose_unit +"*"+this.druglist[i].min_number+this.druglist[i].min_unit +"/"+this.druglist[i].max_unit
          }
        }
        return specification_name
      },
     getHisCount(val,max_unit,min_unit,min_number){
        var total = 0
        var max_str = "";
        var min_str = "";
        for(let i=0;i<val.length;i++){
          total+=parseInt(val[i].prescribing_number)
        }
        if (total < min_number) {
        min_str = total + min_unit;
      }
      if (total == 0) {
        min_str = "";
        max_str = "";
      }
      if (total >= min_number) {
        if (parseInt(total / min_number) != 0) {
          max_str = parseInt(total / min_number) + max_unit;
        }
        if (total % min_number != 0) {
          min_str = (total % min_number) + min_unit;
        }
      }
      return max_str + min_str;
        
      },
   }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .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;
  }

  }
  }
  }
  }
  }
  }
  }
  }
</style>

