<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" v-for="it in item.doctor_advice" style="page-break-after: always;">
                        <div>分区/床位号:{{item.device_number.zone.name}}/{{item.device_number.number}}</div>  
                        <div>姓名:{{item.patient.name}}</div>  
                        <div>医嘱内容:{{ it.advice_name }}</div>  
                        <div>给药途径:{{it.execution_frequency}}</div>  
                        <div>执行频率:{{it.delivery_way}}</div>  
                        <div>打印时间:{{time}}</div>   
                    </div>  
                </div>
            </div>
        </div>

          <div class="app-container" style="background-color: white;" v-show="showOne">
            <div id="dialysisTable">
                <div v-for="(item, index) in list" :key="index">
                    <div class="signPrint" v-for="it in item.doctor_advice" style="page-break-after: always;">
                        <div>分区/床位号:{{item.device_number.zone.name}}/{{item.device_number.number}}</div>  
                        <div>姓名:{{item.patient.name}}</div>  
                        <div>医嘱内容:{{getAdvice(item.doctor_advice)}}</div>  
                        <div>给药途径:{{item.doctor_advice[0].execution_frequency}}</div>  
                        <div>执行频率:{{item.doctor_advice[0].delivery_way}}</div>  
                        <div>打印时间:{{time}}</div>   
                    </div>  
                  
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import BreadCrumb from '@/xt_pages/components/bread-crumb'
import { getSchedualDoctors } from '@/api/advice'
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:[]
        }
    },
    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
            })
        },
        requestSchedualDoctors (time) {
            let newTime =  moment(time).format('YYYY-MM-DD')
            getSchedualDoctors({
            date: newTime,
            patient_type: 0,
            advice_type: 2,
            delivery_way:this.delivery_way,
            }).then(rs => {
                var resp = rs.data
                if (resp.state == 1) {
                this.admin_user = resp.data.adminUser
                var config = resp.data.config
                if(config.is_open == 0 || config.is_open == 2){
                
                   var schedulesArr = resp.data.scheduals
                   let schedules = []
                   var ids = this.$store.getters.temp_params.advice_ids
                   console.log('ids',ids)
                   schedulesArr.map(item => {
                   if(ids.indexOf(item.patient_id) > -1){
                        schedules.push(item)
                    }
                    
                })
               
                for (let i = 0; i < schedules.length; i++) {
                    schedules[i].dialysis_no = schedules[i].patient.dialysis_no
                }
           

                var arr = []
                for (let j = 0; j < schedules.length; j++) {
                    arr.push(schedules[j].dialysis_no)
                }
                arr.sort(function (a, b) {
                    return a - b
                })
               

                var arrTwo = []
                for (let i = 0; i < arr.length; i++) {
                    for (let j = 0; j < schedules.length; j++) {
                    if (arr[i] == schedules[j].dialysis_no) {
                        arrTwo.push(schedules[j])
                    }
                    }
                }

                arrTwo.map((item, index) => {
                  
                    item.doctor_advice.sort(this.compare('start_time'))
                })
                

                const res = new Map()
                let a = arrTwo.filter(
                    a => !res.has(a.dialysis_no) && res.set(a.dialysis_no, 1)
                )

               

                schedules = a
                var zoneMap = {}
                var scheduleMap = {}
                for (let index = 0; index < schedules.length; index++) {
                    const schedule = schedules[index]
                    if (schedule.doctor_advice.length == 0) {
                    continue
                    }
                    if (scheduleMap[schedule.device_number.zone.name] == null) {
                    scheduleMap[schedule.device_number.zone.name] = []
                    }
                    scheduleMap[schedule.device_number.zone.name].push(schedule)
                    if (zoneMap[schedule.device_number.zone.name] == null) {
                    zoneMap[schedule.device_number.zone.name] =schedule.device_number.zone}
                }

                var zones = []
                zones.push({ value: 0, text: '全部分区' })
                for (var zoneName in zoneMap) {
                    zones.push({ value: zoneMap[zoneName].id, text: zoneName })
                }

                zones = zones.sort(function (a, b) {
                    return a.value > b.value
                })
                this.zones = zones
                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.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("scheduleMap999999999999",schedules)
                  this.list = schedules
                }
               
                 if(config.is_open == 1){
                   var schedulesArr = resp.data.hisAdvices
                   let schedules = []
                   var ids = this.$store.getters.temp_params.advice_ids
                   console.log('ids',ids)
                   schedulesArr.map(item => {
                    if(ids.indexOf(item.patient_id) > -1){
                        schedules.push(item)
                    }
                    
                })
               
                for (let i = 0; i < schedules.length; i++) {
                    schedules[i].dialysis_no = schedules[i].patient.dialysis_no
                }
           

                var arr = []
                for (let j = 0; j < schedules.length; j++) {
                    arr.push(schedules[j].dialysis_no)
                }
                arr.sort(function (a, b) {
                    return a - b
                })
               

                var arrTwo = []
                for (let i = 0; i < arr.length; i++) {
                    for (let j = 0; j < schedules.length; j++) {
                    if (arr[i] == schedules[j].dialysis_no) {
                        arrTwo.push(schedules[j])
                    }
                    }
                }

                arrTwo.map((item, index) => {
                  
                    item.doctor_advice.sort(this.compare('start_time'))
                })
                

                const res = new Map()
                let a = arrTwo.filter(
                    a => !res.has(a.dialysis_no) && res.set(a.dialysis_no, 1)
                )

               

                schedules = a
                var zoneMap = {}
                var scheduleMap = {}
                for (let index = 0; index < schedules.length; index++) {
                    const schedule = schedules[index]
                    if (schedule.doctor_advice.length == 0) {
                    continue
                    }
                    if (scheduleMap[schedule.device_number.zone.name] == null) {
                    scheduleMap[schedule.device_number.zone.name] = []
                    }
                    scheduleMap[schedule.device_number.zone.name].push(schedule)
                    if (zoneMap[schedule.device_number.zone.name] == null) {
                    zoneMap[schedule.device_number.zone.name] =
                        schedule.device_number.zone
                    }
                }
             
                var zones = []
                zones.push({ value: 0, text: '全部分区' })
                for (var zoneName in zoneMap) {
                    zones.push({ value: zoneMap[zoneName].id, text: zoneName })
                }

                zones = zones.sort(function (a, b) {
                    return a.value > b.value
                })
                this.zones = zones
                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.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
                    }
                 }
                
                     this.list = schedules
                     this.scheduleMap = scheduleMap  
                 }
                console.log("this.list",this.list)
                
                }
            })
        },
        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";

            // 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;
      }else{
        return ""
      }
    },
    },
    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.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 delivery_way = this.$route.query.delivery_way
      console.log("delivery_Way",delivery_way)
      this.delivery_way = delivery_way
      this.time = moment(time).format('YYYY-MM-DD')
      console.log(11,this.$route.query)
      this.requestSchedualDoctors(time)
    }
}
</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>
