<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">{{ $store.getters.xt_user.org.org_name }}医护排班统计表</span>
                    </div>
                    <div style="text-align:right;margin-bottom:20px;font-size: 18px;">
                      打印时间：{{ getNowFormatDate()}}
                    </div>
                    <div class="table_panel">
                        <table class="table">
                            <thead>
                                <tr>
                                  <td width="150">医护姓名</td>
                                  <td width="120" v-for="(modeItem, index) in scheduleList" :key="index">{{modeItem.class_name}}</td>
                                  <td width="120">总工时/小时</td>
                                  <td width="120">出勤/天</td>
                                  <td width="120">缺勤/天</td>
                                </tr>
                            </thead>
                            <tbody>
                                <tr v-for="(item,i) in tableData" :key="i">
                                  <td width="120">
                                    {{ item.user_name}}
                                   </td>
                                   <td width="120" v-for="(it,i) in scheduleList" :key="i">
                                    {{ getCount(item.doctor_id,it.class_name) }}
                                   </td>
                                   <td width="120">
                                    {{ item.totalminute/60}}
                                   </td>
                                    <td width="120">
                                    {{item.attendance}}
                                   </td>
                                   <td width="120">
                                    {{item.absence}}
                                   </td>
                                </tr>
                            </tbody>
                        </table>
                        
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import BreadCrumb from '@/xt_pages/components/bread-crumb'
import { getDoctorList,getScheduleListTotal,getScheduleList } from '@/api/doctorSchedule'
import { parseTime } from '@/utils'
import print from 'print-js'
export default {
    components:{
        BreadCrumb
    },
    data(){
        return{
            crumbs: [
                { path: false, name: '医护排班' },
                { path: false, name: '排班统计打印' }
            ],
            start_time:0,
            end_time:0,
            doctorlist:[],
            tableData:[],
            scheduleList:[],
        }
    },
    created(){
      
      var startime =  this.$route.query.start_time
      this.start_time = parseInt(startime)
       console.log("开始时间",this.start_time)
      var end_time = this.$route.query.end_time
      this.end_time = parseInt(end_time)
      console.log("结束时间",this.end_time)
       //获取该机构所有医护人员
      this.getDoctorList()
       // 获取统计表的数据
      this.getlist()
       //获取所有排班种
      this.getScheduleList()
    },
    methods:{
      getDoctorList(){
        getDoctorList().then(response=>{
          var list =  response.data.data.list
          var doctorlist =  response.data.data.doctorlist
          var nurselist =  response.data.data.nurselist
          this.doctorArr = doctorlist
          this.nurseArr = nurselist
          this.doctorlist.push(...nurselist)
          this.doctorlist.push(...doctorlist)
        })
      },

      //获取统计表数据
        getlist(){
            if(this.timeSlot == 1){
               this.start_time = this.getTimestamp(this.weekDayArr[0]),
               this.end_time = this.getTimestamp(this.weekDayArr[6])
            }
            const params = {
                start_time:this.start_time,
                end_time:this.end_time,
            }
            // console.log("params",params)
            getScheduleListTotal(params).then(response=>{
                if(response.data.state == 1){
                    //获取每个班次对应的次数
                    var scheudleTotal =  response.data.data.scheudletotal
                    // console.log("scheudletotal",scheudleTotal)
                    //获取总分钟数
                    var list = response.data.data.list
                    // console.log("list",list)
                    var workday = response.data.data.workDay
                    // console.log("workday",workday)
                    var noWorkDay = response.data.data.noWorkDay
                    // console.log("noWorkDay",noWorkDay)

                    let tempArr = [], newArr = []
                    for (let i = 0; i < scheudleTotal.length; i++) {
                        if (tempArr.indexOf(scheudleTotal[i].user_name) === -1) {
                            newArr.push({
                                user_name: scheudleTotal[i].user_name,
                                doctor_id:scheudleTotal[i].doctor_id,
                                list: [{class_name:scheudleTotal[i].class_name,doctor_id:scheudleTotal[i].doctor_id,Count:scheudleTotal[i].Count}]
                            })
                            tempArr.push(scheudleTotal[i].user_name);
                        } else {
                            for (let j = 0; j < newArr.length; j++) {
                                if (newArr[j].user_name == scheudleTotal[i].user_name) {
                                    newArr[j].list.push({class_name:scheudleTotal[i].class_name,doctor_id:scheudleTotal[i].doctor_id,Count:scheudleTotal[i].Count})
                                }
                            }
                        }
                    }

                    newArr.map(item => {
                        list.map(it => {
                            if(item.doctor_id == it.doctor_id){
                                item.totalminute = it.totalminute
                            }
                        })
                    })
                    newArr.map(item => {
                        workday.map(it => {
                            if(item.doctor_id == it.doctor_id){
                                item.attendance = it.Count
                            }
                        })
                    })
                    newArr.map(item => {
                        noWorkDay.map(it => {
                            if(item.doctor_id == it.doctor_id){
                                item.absence = it.Count
                            }
                        })
                    })

                    let arr = [...newArr]

                    arr.sort(this.compare('doctor_id'))
                    this.doctorlist.sort(this.compare('admin_user_id'))
                    this.doctorlist.forEach((item, index) => {
                      if (arr[index] && item.admin_user_id == arr[index].doctor_id) {

                      }else{
                        arr.splice(index, 0, {user_name: item.user_name, doctor_id: item.admin_user_id, list: []})
                      }
                    })


                    console.log('打印数据',arr)
                    this.tableData = arr
                    this.$nextTick(() => {
                      this.$refs.tab.doLayout()
                    })
                }
            })
        },
        compare(property){
            return function(a,b){
              var value1 = a[property];
              var value2 = b[property];
              return value2 - value1;
            }
        },
         //获取所有班种
        getScheduleList(){
          getScheduleList().then(response=>{
             if(response.data.state == 1){
                var schedulelist = response.data.data.scheduleList
                // console.log("schedulelist",schedulelist)
                this.scheduleList = schedulelist
             }
          })
        },

         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: 16px; padding: 15px 5px; } tbody tr td { border: 1px solid; text-align: center; font-size: 16px; padding: 10px 5px; white-space: pre-line;} .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
          })
      },
       getCount(id,name){
            if(id != undefined){
                let Count = '';
                this.tableData.map(item => {
                    if(item.doctor_id == id){
                        if(item.list){
                            item.list.map(it => {
                                if(it.class_name == name){
                                    Count = it.Count
                                }
                            })
                        }

                    }
                })
                return Count
            }
        },
       getTime(value, temp) {
        if (value != undefined) {
          return parseTime(value, temp)
        }
        return ''
      },

     getNowFormatDate() {
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
      },
    }
}
</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: 18px;
            padding: 15px 5px;
          }
        }
      }
      tbody {
        tr {
          td {
            border: 1px solid;
            text-align: center;
            font-size: 18px;
            padding: 10px 5px;
            white-space: pre-line;
            .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>
