<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;" v-show="show">
            <div id="dialysisTable">
                <div v-for="(item, index) in list" :key="index">
                    <div class="signPrint" style="page-break-after: always;" v-for="(it,i) in item.his_advice_list">
                       
                        <div>分区/床位号:{{item.device_number.zone.name}}/{{item.device_number.number}}</div>  
                        <div>姓名:{{item.patient.name}}</div>  

                        <div>
                            <div>医嘱内容:{{ it.advice_name }} 
                            </div>  
                            <div>
                            规格:{{getSpecialName(it.drug_id)}} 
                            </div>  
                            <div>单次用量:   {{it.single_dose}}{{ it.single_dose_unit }}</div>  
                            <div>执行频率:{{it.execution_frequency}}</div>  
                            <div>给药途径:{{it.delivery_way}}</div>  
                        </div>
                        <div>打印时间:{{$route.query.selected_date}}</div> 
                    </div>  
                </div>
            </div>
        </div>

    </div>
</template>

<script>
import BreadCrumb from '@/xt_pages/components/bread-crumb'
import { getDialysisAdviceTemplateList } from '@/api/dialysis'
import { parseTime } from '@/utils'
import print from 'print-js'
const moment = require('moment')
export default {
    components:{
        BreadCrumb
    },
    data(){
        return{
            crumbs: [
                { path: false, name: '耗材药品' },
                { path: false, name: '打印' }
            ],
            tableData:[],
            start_time:"",
            end_time:"",
            print_time:moment(new Date()).add('year', 0).format('YYYY-MM-DD'),

            zone_selected: 0,
            scheduleMap:[],
            time:'',
            show:true,
            showOne:false,
            delivery_way:"",
            list:[],
            execution_frequency:"",
            org_id:0,
            adviceList:[],
            drugList:[],
        }
    },
    methods:{
        printAction: function() {
            const style = '@media print { .dialysisTable{width:960px;margin: 0 auto;text-align: center;border-collapse: collapse;}.order_title_panl {text-align: center;.main_title {font-size: 18px;line-height: 40px;font-weight: 500;}}.dialysisTable tr{padding: 10px 0;}.dialysisTable th {color: #000;padding: 0;margin: 0;height: 30px;}.dialysisTable tr td {padding: 12px 0;}.subadvice_content {text-align: left;padding-left: 25px !important;padding-right: 5px !important;}.advice_content {text-align: left;padding-left: 5px !important;padding-right: 5px !important;padding: 15px 5px !important;} }'

            printJS({
            printable: 'dialysisTable',
            type: 'html',
            documentTitle: '  ',
            style: style,
            scanStyles: false
            })
        },
     
        compare (property) {
            return function (a, b) {
                var value1 = a[property]
                var value2 = b[property]
                return value1 - value2
            }
        },
        parseTime: function (time, layout) {
            if (time == 0) {
                return ''
            }
            return parseTime(time, layout)
        },
        getName (val) {
            for (let i = 0; i < this.admin_user.length; i++) {
                if (this.admin_user[i].id == val) {
                    return this.admin_user[i].name
                }
            }
        },
        getAdvice: function(doctor_advice) {
          if (doctor_advice.length > 0) {
            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";

           
        }
        return name;
      }else{
        return ""
      }
    },
    getSpecialName(drug_id){
        console.log("drug_id woowow",drug_id)
        var name = ""
        for(let i=0;i<this.drugList.length;i++){
            if(drug_id == this.drugList[i].id){
            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 name
     },

     getweekday(date){
        var weekArray = new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六");

        var week = weekArray[new Date(date).getDay()];//注意此处必须是先new一个Date

        return week;
      },
    getlist(){
      
      var params = {
          total:0,
          schedule_date:this.$route.query.selected_date,
          schedule_type:this.$route.query.schedule_type,
          partition_id:this.$route.query.partition_id,
          page:this.$route.query.page,
          limit:1000,
          keywords:"",
          good_type:0,
          ids:"",
          

      }
     getDialysisAdviceTemplateList(params).then(response=>{
      if(response.data.state == 1){
      
        var list = response.data.data.list
        if(list!=null){
          for(let i=0;i<list.length;i++){
             list[i].his_advice_list=[]

          }
        }
       
        this.list = list
        this.total = response.data.data.total
        var adviceList = response.data.data.adviceList
        this.adviceList = adviceList

        for(let i=0;i<list.length;i++){
          for(let j=0;j<adviceList.length;j++){
            if(list[i].patient_id == adviceList[j].patient_id){
                list[i].his_advice_list.push(adviceList[j])
            }
          }
        }
      
        var druglist = response.data.data.drugList
        this.drugList =druglist
       
        this.config = response.data.data.config
         
     
      }
    })
    },
    getZoneName(patient_id){
      var zone_name = ""
       for(let i=0;i<this.list.length;i++){
          if(patient_id == this.list[i].patient_id){
              zone_name = this.list[i].device_number.zone.name
          }
       }

       return zone_name
    },
    getBedName(patient_id){
       var number = ""
       for(let i=0;i<this.list.length;i++){
          if(patient_id == this.list[i].patient_id){
            number = this.list[i].device_number.number
          }
       }

       return number
    }



    },
    computed: {
    filtedScheduals: function () {
        var scheduleMap = new Object()
        if (this.zone_selected == 0) {
            for (const key in this.scheduleMap) {
            scheduleMap[key] = this.scheduleMap[key]
            }
        } else {
            var zone_name = this.zones[this.zone_selected].text
            scheduleMap[zone_name] = this.scheduleMap[zone_name]
        }

        if (this.schedule_type_selected != 0) {
            var _scheduleMap = {}
            for (const key in scheduleMap) {
            var origin_schedules = scheduleMap[key]
            var schedules = []
            for (let index = 0; index < origin_schedules.length; index++) {
                const schedule = origin_schedules[index]
                if (schedule.schedule_type == this.schedule_type_selected) {
                schedules.push(schedule)
                }
            }
            if (schedules.length > 0) {
                _scheduleMap[key] = schedules
            }
            }

            scheduleMap = _scheduleMap
        }
        for (var key in scheduleMap) {
            let mapArr = scheduleMap[key]
            for (let i = 0; i < mapArr.length; i++) {
            mapArr[i]['new_advice'] = []
            }
        }

        for (var key in scheduleMap) {
            let mapArr = scheduleMap[key]
            for (let i = 0; i < mapArr.length; i++) {
            var maps = mapArr[i]
            var resp_advices = maps.new_doctor_advice
            if (resp_advices.length > 0) {
                var newGroupObject = function () {
                return Object.assign(
                    {},
                    {
                    group_no: 0,
                    advices: []
                    }
                )
                }
                var initGroupBlock = function (group, advice) {
                group.group_no = advice.groupno
                }

                var advice_groups = []
                var group = newGroupObject()
                for (let index = 0; index < resp_advices.length; index++) {
                const advice = resp_advices[index]
                if (advice.groupno == 0) {
                    // 老版本的医嘱
                    if (advice.parent_id > 0) {
                    if (advice_groups.length > 0) {
                        var parent_group = advice_groups[advice_groups.length - 1]
                        if (parent_group.advices.length > 0) {
                        if (parent_group.advices[0].id == advice.parent_id) {
                            parent_group.advices.push(advice)
                        }
                        }
                    }
                    continue
                    } else {
                    if (group.group_no > 0) {
                        advice_groups.push(group)
                        group = newGroupObject()
                    }

                    initGroupBlock(group, advice)
                    group.advices.push(advice)
                    advice_groups.push(group)
                    group = newGroupObject()
                    continue
                    }
                } else {
                    if (group.group_no > 0 && group.group_no != advice.groupno) {
                    advice_groups.push(group)
                    group = newGroupObject()
                    }
                    if (group.group_no == 0) {
                    initGroupBlock(group, advice)
                    }
                    if (group.group_no == advice.groupno) {
                    group.advices.push(advice)
                    }
                }
                }
                if (group.group_no > 0) {
                // 上述的算法会导致最后一组没有加到advice_groups，这里要手动加
                advice_groups.push(group)
                }
                advice_groups = advice_groups
            } else {
                advice_groups = []
            }
            maps.new_advice = advice_groups
            }
        }
        console.log("222222",scheduleMap)
        return scheduleMap
        },
       
     
      
    
    },
    created(){
        
      var time = this.$route.query.time
      var selected_date = this.$route.query.selected_date
      var schedule_type = this.$route.query.schedule_type
      var partition_id = this.$route.query.partition_id
      this.org_id = this.$store.getters.xt_user.org.id
      this.getlist()
     
    }
}
</script>


<style rel="stylesheet/scss" lang="scss" scoped>
.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;
}
.dialysisTable{
    width:960px;
    margin: 0 auto;
    text-align: center;
    border-collapse: collapse;
}
.order_title_panl {
    text-align: center;

    .main_title {
      font-size: 18px;
      line-height: 40px;
      font-weight: 500;
    }
}
.dialysisTable tr{
    padding: 10px 0;
}
.dialysisTable th {
    color: #000;
    padding: 0;
    margin: 0;
    height: 30px;
}
.dialysisTable tr td {
    padding: 12px 0;
}
.subadvice_content {
    text-align: left;
    padding-left: 25px !important;
    padding-right: 5px !important;
}
.advice_content {
    text-align: left;
    padding-left: 5px !important;
    padding-right: 5px !important;
    padding: 15px 5px !important;
}
</style>
