<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;">
                      打印时间：
                    </div>
                    <div class="table_panel">
                        <table class="table">
                            <thead>
                                <tr>
                                    <td width="60">序号</td>
                                    <td width="80">透析号</td>
                                    <td width="80">姓名</td>
                                    <td>药品名称</td>
                                    <td>规格名称</td>
                                    <td width="70">单位</td>
                                    <td width="70">自备量</td>
                                    <td width="70">已使用</td>
                                    <td width="70">剩余</td>
                                </tr>
                            </thead>
                            <tbody>
                                <tr v-for='(item,index) in 8' :key="index">
                                    <td>张三</td>
                                    <td>张三</td>
                                    <td>张三</td>
                                    <td>张三</td>
                                    <td>张三</td>
                                    <td>张三</td>
                                    <td>张三</td>
                                    <td>张三</td>
                                    <td>张三</td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import BreadCrumb from '@/xt_pages/components/bread-crumb'
import print from 'print-js'
import { getDoctorList,getScheduleByDoctorId } from '@/api/doctorSchedule'
export default {
    components:{
        BreadCrumb
    },
    data(){
        return{
          crumbs: [
            { path: false, name: '库存管理' },
            { path: false, name: '自备药查询' },
            { path: false, name: '自备药打印' },
          ],
          start_time:"",
          end_time:"",

          doctorlist:[],
          tableData:[],
          arrZero:[],
          arrOne:[],
          arrTwo:[],
          arrThree:[],
          arrFour:[],
          arrFive:[],
          arrSix:[],
          arrSeven:[],
          typevalue:0,
          
          doctorTableData:[],
          nurseTableData:[]
        }
    },
    methods:{
      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
          })
      },
      getDoctorList(){
        getDoctorList().then(response=>{
          var list =  response.data.data.list
          this.doctorlist = list
          this.getScheduleByDoctorId()

          //
          var doctorlist =  response.data.data.doctorlist
          let arr = []
          for(let i=0;i<doctorlist.length;i++){
              if(doctorlist[i].sort == 0){
                doctorlist[i].sort = ""
              }
          }
          console.log('doctorlist',doctorlist)
          this.doctorTableData = doctorlist
          var nurselist = response.data.data.nurselist
          for(let i=0;i<nurselist.length;i++){
              if(nurselist[i].sort == 0){
                  nurselist[i].sort = ""
              }
          }
          this.nurseTableData = nurselist
        })
      },
      //获取本周的所有排班列表
      getScheduleByDoctorId(){
        const params = {
          start_time:this.start_time,
          end_time:this.end_time,
          doctor_id:this.typevalue,
        }
        console.log("parasm",params)
        getScheduleByDoctorId(params).then(response=>{
          if(response.data.state == 1){
              var staffList = response.data.data.staffList
              console.log("stafflist",staffList)

              var sevenStr = ""
              var oneStr = ""
              var twoStr = ""
              var threeStr = ""
              var fourStr = ""
              var fiveStr = ""
              var sixStr = ""
              for(let i=0;i<staffList.length;i++){
              if(staffList[i].schedule_week == 0){
                  staffList[i].class_index = 0
                    var arr = []
                  this.arrZero.push(staffList[i].class_name)
                  for(let i=0;i<this.arrZero.length;i++){
                      if(this.arrZero[i]!=''){
                        arr.push(this.arrZero[i])
                      }
                  }
                  var countedNames = arr.reduce(function (allNames, name) { 
                      if (name in allNames) {
                          allNames[name]++;
                      }
                      else {
                          allNames[name] = 1;
                      }
                      return allNames;
                  }, {});
                  let objKey = Object.keys(countedNames)
                  let objVal = Object.values(countedNames)
                  var zeroStr = ''
                  objKey.map((item,index) => {
                  let objVal = Object.values(countedNames)
                      zeroStr += item + objVal[index] + '\n'
                  })
                  sevenStr = zeroStr
              }
              if(staffList[i].schedule_week == 1){
                  staffList[i].class_index = 1
                  var arr = []
                  this.arrOne.push(staffList[i].class_name)
                  for(let i=0;i<this.arrOne.length;i++){
                      if(this.arrOne[i]!=''){
                        arr.push(this.arrOne[i])
                      }
                  }
                  var countedNames = arr.reduce(function (allNames, name) { 
                      if (name in allNames) {
                          allNames[name]++;
                      }
                      else {
                          allNames[name] = 1;
                      }
                      return allNames;
                  }, {});
                  let objKey = Object.keys(countedNames)
                  let objVal = Object.values(countedNames)
                  let strOne = ''
                  objKey.map((item,index) => {
                  let objVal = Object.values(countedNames)
                      strOne += item + objVal[index] + '\n'
                  })
                  oneStr  = strOne
              }
              if(staffList[i].schedule_week == 2){
                  staffList[i].class_index = 2
                    var arr = []
                  this.arrTwo.push(staffList[i].class_name)
                  for(let i=0;i<this.arrTwo.length;i++){
                      if(this.arrTwo[i]!=''){
                        arr.push(this.arrTwo[i])
                      }
                  }
                  var countedNames = arr.reduce(function (allNames, name) { 
                      if (name in allNames) {
                          allNames[name]++;
                      }
                      else {
                          allNames[name] = 1;
                      }
                      return allNames;
                  }, {});
                  let objKey = Object.keys(countedNames)
                  let objVal = Object.values(countedNames)
                  let strTwo = ''
                  objKey.map((item,index) => {
                  let objVal = Object.values(countedNames)
                      strTwo += item + objVal[index] + '\n'
                  })
                    twoStr = strTwo
              }
              if(staffList[i].schedule_week == 3){
                  staffList[i].class_index = 3
                    var arr = []
                  this.arrThree.push(staffList[i].class_name)
                  for(let i=0;i<this.arrThree.length;i++){
                      if(this.arrThree[i]!=''){
                        arr.push(this.arrThree[i])
                      }
                  }
                  var countedNames = arr.reduce(function (allNames, name) { 
                      if (name in allNames) {
                          allNames[name]++;
                      }
                      else {
                          allNames[name] = 1;
                      }
                      return allNames;
                  }, {});
                  let objKey = Object.keys(countedNames)
                  let objVal = Object.values(countedNames)
                  let strThree = ''
                  objKey.map((item,index) => {
                  let objVal = Object.values(countedNames)
                      strThree += item + objVal[index] + '\n'
                  })
                  threeStr = strThree
              }
              if(staffList[i].schedule_week == 4){
                  staffList[i].class_index = 4
                  var arr = []
                  this.arrFour.push(staffList[i].class_name)
                  for(let i=0;i<this.arrFour.length;i++){
                      if(this.arrFour[i]!=''){
                        arr.push(this.arrFour[i])
                      }
                  }
                  var countedNames = arr.reduce(function (allNames, name) { 
                      if (name in allNames) {
                          allNames[name]++;
                      }
                      else {
                          allNames[name] = 1;
                      }
                      return allNames;
                  }, {});
                  let objKey = Object.keys(countedNames)
                  let objVal = Object.values(countedNames)
                  let strFour = ''
                  objKey.map((item,index) => {
                  let objVal = Object.values(countedNames)
                      strFour += item + objVal[index] + '\n'
                  })
                  fourStr = strFour
              }
              if(staffList[i].schedule_week == 5){
                  staffList[i].class_index = 5

                    var arr = []
                  this.arrFive.push(staffList[i].class_name)
                  for(let i=0;i<this.arrFive.length;i++){
                      if(this.arrFive[i]!=''){
                        arr.push(this.arrFive[i])
                      }
                  }
                  var countedNames = arr.reduce(function (allNames, name) { 
                      if (name in allNames) {
                          allNames[name]++;
                      }
                      else {
                          allNames[name] = 1;
                      }
                      return allNames;
                  }, {});
                  let objKey = Object.keys(countedNames)
                  let objVal = Object.values(countedNames)
                  let strFive = ''
                  objKey.map((item,index) => {
                  let objVal = Object.values(countedNames)
                      strFive += item + objVal[index] + '\n'
                  })
                    fiveStr = strFive
              }
              if(staffList[i].schedule_week == 6){
                  staffList[i].class_index = 6
                    var arr = []
                  this.arrSix.push(staffList[i].class_name)
                  for(let i=0;i<this.arrSix.length;i++){
                      if(this.arrSix[i]!=''){
                        arr.push(this.arrSix[i])
                      }
                  }
                  var countedNames = arr.reduce(function (allNames, name) { 
                      if (name in allNames) {
                          allNames[name]++;
                      }
                      else {
                          allNames[name] = 1;
                      }
                      return allNames;
                  }, {});
                  let objKey = Object.keys(countedNames)
                  let objVal = Object.values(countedNames)
                  let strSix = ''
                  objKey.map((item,index) => {
                  let objVal = Object.values(countedNames)
                      strSix += item + objVal[index] + '\n'
                  })
                  sixStr = strSix
                }
              }
              let tempArr = [], newArr = []
              for (let i = 0; i < staffList.length; i++) {
                if (tempArr.indexOf(staffList[i].user_name) === -1) {
                  newArr.push({
                    user_name: staffList[i].user_name,
                    admin_user_id:staffList[i].admin_user_id,
                    user_type:staffList[i].doctor_type,
                    is_sort:staffList[i].is_sort,
                    sort:staffList[i].sort,
                    list: [{class_name:staffList[i].class_name,admin_user_id:staffList[i].admin_user_id,schedule_week:staffList[i].schedule_week,user_type:staffList[i].doctor_type,class_attributes:staffList[i].class_attributes}]
                  })
                  tempArr.push(staffList[i].user_name);
                } else {
                  for (let j = 0; j < newArr.length; j++) {
                    if (newArr[j].user_name == staffList[i].user_name) {
                      newArr[j].list.push({class_name:staffList[i].class_name,admin_user_id:staffList[i].admin_user_id,schedule_week:staffList[i].schedule_week,user_type:staffList[i].doctor_type,class_attributes:staffList[i].class_attributes})
                    }
                  }
                }
              }

              let arr = [...newArr]
              let newNurse = []
              let newDoctor = []
              arr.map(item => {
                if(item.user_type == 2){
                  newDoctor.push(item)
                }
                if(item.user_type == 3){
                  newNurse.push(item)
                }
              })

              if(this.typevalue == 0){
                //护士
                newNurse.sort(this.compare('admin_user_id'))
                let newNurseTableData = []
                this.nurseTableData.map(item => {
                    if(item.is_sort == 1){
                        newNurseTableData.push(item)
                    }
                })
                newNurseTableData.sort(this.compare('admin_user_id'))
                newNurseTableData.map((item,index) => {
                    if (newNurse[index] && item.admin_user_id == newNurse[index].admin_user_id) {
                
                    }else{
                        newNurse.splice(index, 0, {user_name: item.user_name, admin_user_id: item.admin_user_id, list: [],user_type:item.user_type,is_sort:item.is_sort,sort:item.sort})
                    }
                })
                newNurse.sort(this.compare('sort'))

                //医生
                newDoctor.sort(this.compare('admin_user_id'))
                let newDoctorTableData = []
                this.doctorTableData.map(item => {
                    if(item.is_sort == 1){
                        newDoctorTableData.push(item)
                    }
                })
                newDoctorTableData.sort(this.compare('admin_user_id'))
                newDoctorTableData.map((item,index) => {
                    if (newDoctor[index] && item.admin_user_id == newDoctor[index].admin_user_id) {
                
                    }else{
                        newDoctor.splice(index, 0, {user_name: item.user_name, admin_user_id: item.admin_user_id, list: [],user_type:item.user_type,is_sort:item.is_sort,sort:item.sort})
                    }
                })
                newDoctor.sort(this.compare('sort'))
                
                

                let arr2 = []
                arr2.push({class_name:oneStr,schedule_week:1})
                arr2.push({class_name:twoStr,schedule_week:2})
                arr2.push({class_name:threeStr,schedule_week:3})
                arr2.push({class_name:fourStr,schedule_week:4})
                arr2.push({class_name:fiveStr,schedule_week:5})
                arr2.push({class_name:sixStr,schedule_week:6})
                arr2.push({class_name:zeroStr,schedule_week:0})
                var obj = {}
                obj.admin_user_id = "1000000"
                obj.user_name = "合计"
                obj.user_type = 10
                obj.list = arr2
                arr.push(obj)

                let data = []
                data.push(...newNurse)
                data.push(...newDoctor)
                data.push(obj)
                this.tableData = data
              }
              if(this.typevalue == 2){
                newDoctor.sort(this.compare('admin_user_id'))
                let newDoctorTableData = []
                this.doctorTableData.map(item => {
                    if(item.is_sort == 1){
                        newDoctorTableData.push(item)
                    }
                })
                newDoctorTableData.sort(this.compare('admin_user_id'))
                newDoctorTableData.map((item,index) => {
                    if (newDoctor[index] && item.admin_user_id == newDoctor[index].admin_user_id) {
                
                    }else{
                        newDoctor.splice(index, 0, {user_name: item.user_name, admin_user_id: item.admin_user_id, list: [],user_type:item.user_type,is_sort:item.is_sort,sort:item.sort})
                    }
                })
                newDoctor.sort(this.compare('sort'))
                
                let arr2 = []
                arr2.push({class_name:oneStr,schedule_week:1})
                arr2.push({class_name:twoStr,schedule_week:2})
                arr2.push({class_name:threeStr,schedule_week:3})
                arr2.push({class_name:fourStr,schedule_week:4})
                arr2.push({class_name:fiveStr,schedule_week:5})
                arr2.push({class_name:sixStr,schedule_week:6})
                arr2.push({class_name:zeroStr,schedule_week:0})
                var obj = {}
                obj.admin_user_id = "1000000"
                obj.user_name = "合计"
                obj.user_type = 10
                obj.list = arr2
                newDoctor.push(obj)
                this.tableData = newDoctor
              }
              if(this.typevalue == 3){
                newNurse.sort(this.compare('admin_user_id'))
                let newNurseTableData = []
                this.nurseTableData.map(item => {
                    if(item.is_sort == 1){
                        newNurseTableData.push(item)
                    }
                })
                newNurseTableData.sort(this.compare('admin_user_id'))
                newNurseTableData.map((item,index) => {
                    if (newNurse[index] && item.admin_user_id == newNurse[index].admin_user_id) {
                
                    }else{
                        newNurse.splice(index, 0, {user_name: item.user_name, admin_user_id: item.admin_user_id, list: [],user_type:item.user_type,is_sort:item.is_sort,sort:item.sort})
                    }
                })
                newNurse.sort(this.compare('sort'))
                let arr2 = []
                arr2.push({class_name:oneStr,schedule_week:1})
                arr2.push({class_name:twoStr,schedule_week:2})
                arr2.push({class_name:threeStr,schedule_week:3})
                arr2.push({class_name:fourStr,schedule_week:4})
                arr2.push({class_name:fiveStr,schedule_week:5})
                arr2.push({class_name:sixStr,schedule_week:6})
                arr2.push({class_name:zeroStr,schedule_week:0})
                var obj = {}
                obj.admin_user_id = "1000000"
                obj.user_name = "合计"
                obj.user_type = 10
                obj.list = arr2
                newNurse.push(obj)
                this.tableData = newNurse
              }
            }
        })
      },
      getClass(name,index){
        if(name != undefined){
          let newClass = '';
          this.tableData.map(item => {
            if(item.user_name == name){
              if(item.list){
                item.list.map(it => {
                  if(it.schedule_week == index){
                    newClass = it.class_name
                  }
                })
              } 
            }
          })
          if(newClass && newClass.indexOf("(") > -1){
            let num = newClass.indexOf("(")
            let a = newClass.slice(0, num) + '\n' + newClass.slice(num)
            return a
          }else if(newClass && newClass.indexOf("（") > -1) {
            let num = newClass.indexOf("（")
            let a = newClass.slice(0, num) + '\n' + newClass.slice(num)
            return a
          }else{
            return newClass
          }
          
        }
      },
      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;
      },
      compare(property){
        return function(a,b){
            var value1 = a[property];
            var value2 = b[property];
            return value2 - value1;
        }
      },
    },
    created(){
      var starttime =  this.$route.query.starttime
      this.start_time = starttime
      var endtime =  this.$route.query.endtime
      this.end_time = endtime
      this.weekArr = this.$route.query.weekArr.split(',')
      this.typevalue = parseInt(this.$route.query.typeValue)
      
      //获取该机构所有医护人员
      this.getDoctorList()
    }
}
</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>
