<template>
  <div class="page_medicalScheduling">
    <div class="searchBox">
      <div style="width:70%">
        <van-search
          v-model="schedulingVal"
          placeholder="请输入医护人员姓名"
          @search="searchPatient"
          @input="searchPatient"
        />
      </div>
      <div class="searchOne" @click="typeShow = true">
        <div style="display: flex;align-items: center;">
          {{ type }}
          <van-icon name="arrow-down" />
        </div>
      </div>

    </div>
    <div class="operateTitle">
      <div class="operatePrevious" @click="lastclick">&lt;上一周({{ weekNum - 1 }})</div>
      <div class="operateTime">{{ weekDayArr[0] }}-{{ weekDayArr[6] }}({{ weekNum }})</div>
      <div class="operateNext" @click="nextclick">下一周({{ weekNum + 1 }})&gt;</div>
    </div>
    <div>
      <el-table style="width:768px;" :data="tableData" :height="tableHeight" border :header-cell-style="{backgroundColor: 'rgb(245, 247, 250)', color: '#606266'}" :row-style="{ color: '#303133' }">
        <el-table-column prop="name" fixed :label="'医护姓名'" width="110px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.user_name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="name" :label="'周一\n(' + weekDayArr[0].slice(5) + ')'" width="120px" align="center">
          <template slot-scope="scope">
            <span v-if="getClassAttributes(scope.row.user_name,1) == 2" style="color:red;">{{ getClass(scope.row.user_name,1) }}</span>
            <span v-else>{{ getClass(scope.row.user_name,1) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="name" :label="'周二\n(' + weekDayArr[1].slice(5) + ')'" width="120px" align="center">
          <template slot-scope="scope">
            <span v-if="getClassAttributes(scope.row.user_name,2) == 2" style="color:red;">{{ getClass(scope.row.user_name,2) }}</span>
            <span v-else style="color:red;">{{ getClass(scope.row.user_name,2) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="name" :label="'周三\n(' + weekDayArr[2].slice(5) + ')'" width="120px" align="center">
          <template slot-scope="scope">
            <span v-if="getClassAttributes(scope.row.user_name,3) == 2" style="color:red;">{{ getClass(scope.row.user_name,3) }}</span>
            <span v-else>{{ getClass(scope.row.user_name,3) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="name" :label="'周四\n(' + weekDayArr[3].slice(5) + ')'" width="120px" align="center">
          <template slot-scope="scope">
            <span v-if="getClassAttributes(scope.row.user_name,4) == 2" style="color:red;">{{ getClass(scope.row.user_name,4) }}</span>
            <span v-else>{{ getClass(scope.row.user_name,4) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="name" :label="'周五\n(' + weekDayArr[4].slice(5) + ')'" width="120px" align="center">
          <template slot-scope="scope">
            <span v-if="getClassAttributes(scope.row.user_name,5) == 2" style="color:red;">{{ getClass(scope.row.user_name,5) }}</span>
            <span v-else>{{ getClass(scope.row.user_name,5) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="name" :label="'周六\n(' + weekDayArr[5].slice(5) + ')'" width="120px" align="center">
          <template slot-scope="scope">
            <span v-if="getClassAttributes(scope.row.user_name,6) == 2" style="color:red;">{{ getClass(scope.row.user_name,6) }}</span>
            <span v-else>{{ getClass(scope.row.user_name,6) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="name" :label="'周日\n(' + weekDayArr[6].slice(5) + ')'" width="120px" align="center">
          <template slot-scope="scope">
            <span v-if="getClassAttributes(scope.row.user_name,0) == 2" style="color:red;">{{ getClass(scope.row.user_name,0) }}</span>
            <span v-else>{{ getClass(scope.row.user_name,0) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="备注" width="120px" align="center">
          <template slot-scope="scope">
              {{ scope.row.remark }}
          </template>
        </el-table-column>

      </el-table>

      <div style="margin-top: 20px;margin-left: 10px;">
         {{ remark }}
      </div>
    </div>

    <div>
      <van-popup v-model="typeShow" position="bottom" :style="{ height: '40%' }">
        <van-picker
          show-toolbar
          :columns="typeColumns"
          @confirm="chooseType"
          @cancel="typeShow = false"
        />
      </van-popup>
    </div>
  </div>
</template>

<script>
  import { getMobileDoctorList, getMobileStaffScheduleList, getMobileNextWeekScheduleList, getScheduleByUserType, GetSearchPatientByDoctorId } from '@/api/staff_schedule'
import { HTML5_FMT } from 'moment'
  export default {
    data () {
      return {
        tableHeight: '',
        typeShow: false,
        type: '请选择',
        typeColumns: ['全部', '医生', '护士'],
        schedulingVal: '',
        tableData: [],
        weekNum: '',
        weekDayArr: [],
        todayDate: '',
        clen: 7,
        nowYear: new Date().getFullYear(),
        arr: [],
        currentFirstDate1: '',
        doctorlist: [],
        arrZero: [],
        arrOne: [],
        arrTwo: [],
        arrThree: [],
        arrFour: [],
        arrFive: [],
        arrSix: [],
        arrSeven: [],
        doctorTableData:[],
        nurseTableData:[],
        remark:""
      }
    },
    methods: {
      chooseType (value) {
        this.type = value
        this.typeShow = false
        this.getScheduleByUserType()
      },
      searchPatient () {
        const params = {
          user_name: this.schedulingVal,
          start_time: this.getTimestamp(this.weekDayArr[0]),
          end_time: this.getTimestamp(this.weekDayArr[6]),
          start_time_one:this.weekDayArr[0],
          end_time_one:this.weekDayArr[6]
        }
        GetSearchPatientByDoctorId(params).then(response => {
          if (response.data.state == 1) {
            var staffList = response.data.data.staffList
            // console.log('搜索数据', staffList)

            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,
                  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,remark:staffList[i].remark}]
                })
                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,remark:staffList[i].remark})
                  }
                }
              }
            }

            if (this.schedulingVal != '') {
              this.tableData = newArr
            } else {
              let arr = [...newArr]
              arr.sort(this.compare('admin_user_id'))
              this.doctorlist.sort(this.compare('admin_user_id'))
              this.doctorlist.forEach((item, index) => {
                if (arr[index] && item.admin_user_id == arr[index].admin_user_id) {

                } else {
                  arr.splice(index, 0, {user_name: item.user_name, admin_user_id: item.admin_user_id, list: []})
                  // console.log("arr",arr)
                }
              })
              this.tableData = arr
            }
          }
        })
      },
      formatDate (date) {
        var year = date.getFullYear() + '-'
        
        if((date.getMonth() + 1)<10){
          var month = "0"+(date.getMonth() + 1) + '-'
        }else{
          var month = (date.getMonth() + 1) + '-'
        }
       
        if(date.getDate() <10){
          var day = "0" + date.getDate()
        }else{
          var day = date.getDate()
        }
        
        
       
        return year + month + day
      },
      addDate (date, n) {
        date.setDate(date.getDate() + n)
        return date
      },
      //
      setDate (date) {
        var week = date.getDay() - 1
        date = this.addDate(date, week * -1)
        this.currentFirstDate = new Date(date)
        this.currentDate = new Date(date)
        for (var i = 0; i < this.clen; i++) {
          this.weekDayArr.push(this.formatDate(i == 0 ? date : this.addDate(date, 1)))
        }
      
      },
      // 上一周
      lastclick () {
        this.weekDayArr = []
        this.setDate(this.addDate(this.currentFirstDate, -7))
        if (this.weekNum == 1) {
          let year = this.nowYear - 1
          let month = 12
          let date = 31
          this.weekNum = this.getYearWeek(year, month, date)
          this.nowYear = this.nowYear - 1
        }
        this.weekNum = this.weekNum - 1
        this.getMobileNextWeekScheduleList()
      },
      // 下一周
      nextclick () {
        this.weekDayArr = []
        this.setDate(this.addDate(this.currentFirstDate, 7))
        this.weekNum = this.weekNum + 1
        if (this.weekNum == 53) {
          let year = this.nowYear + 1
          let month = 1
          let date = 1
          this.weekNum = this.getYearWeek(year, month, date)
          this.nowYear = this.nowYear + 1
        }
        this.getMobileNextWeekScheduleList()
      },

      getYearWeek (year, month, date) {
        /*
                  dateNow是当前日期
                  dateFirst是当年第一天
                  dataNumber是当前日期是今年第多少天
                  用dataNumber + 当前年的第一天的周差距的和在除以7就是本年第几周
              */
        let dateNow = new Date(year, parseInt(month) - 1, date)
        let dateFirst = new Date(year, 0, 1)
        let dataNumber = Math.round((dateNow.valueOf() - dateFirst.valueOf()) / 86400000)
        return Math.ceil((dataNumber + ((dateFirst.getDay() + 1) - 1)) / 7)
      },
      setDate1 (date) {
        var week = date.getDay() - 1
        date = this.addDate(date, week * -1)
        this.currentDate = new Date(date)
        let arr1 = []
        for (var i = 0; i < this.clen; i++) {
          arr1.push(this.formatDate(i == 0 ? date : this.addDate(date, 1)))
        }
        this.arr.push(arr1)
        let num = this.weekNum
        this.currentFirstDate1 = num + 1
      },
      getTimestamp (time) { // 把时间日期转成时间戳
        return (new Date(time)).getTime() / 1000
      },
      // 获取该机构下的所有医生和护士
      getMobileDoctorList () {
        getMobileDoctorList().then(response => {
          if (response.data.state == 1) {
            var staffList = response.data.data.staffList
            // console.log('医生护士', staffList)
            this.doctorlist = staffList
            this.doctorTableData = response.data.data.doctorList
            this.nurseTableData = response.data.data.nurseList
            // 获取一周内的所有医护排班数据
            this.getMobileStaffScheduleList()
          }
        })
      },

      // 获取一周内的所有医护排班数据
      getMobileStaffScheduleList () {
        this.tableData = []

        const params = {
          start_time: this.getTimestamp(this.weekDayArr[0]),
          end_time: this.getTimestamp(this.weekDayArr[6]),
          start_time_one:this.weekDayArr[0],
          end_time_one:this.weekDayArr[6],
        }
        getMobileStaffScheduleList(params).then(response => {
          if (response.data.state == 1) {
            var staffList = response.data.data.staffList
            // console.log('排班数据', staffList)
            var sevenStr = ''
            var oneStr = ''
            var twoStr = ''
            var threeStr = ''
            var fourStr = ''
            var fiveStr = ''
            var sixStr = ''
            this.arrZero = []
            this.arrOne = []
            this.arrTwo = []
            this.arrThree = []
            this.arrFour = []
            this.arrFive = []
            this.arrSix = []
            this.arrSeven = []
            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,
                  remark:staffList[i].remark,
                  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,remark:staffList[i].remark}]
                })
                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,remark:staffList[i].remark})
                  }
                }
              }
            }

            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)
              }
            })
            //护士
            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.remark = response.data.data.scheduleRemark.remark
            this.tableData = data
          }
        })
      },

      getClass (name, index) {
        if (name != undefined) {
          let newClass = ''
          this.tableData.map(item => {
            if (item.user_name == name) {
              item.list.map(it => {
                if (it.schedule_week == index) {
                  newClass = it.class_name
                }
              })
            }
          })
          return newClass
        }
      },
      getClassAttributes (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_attributes
                  }
                })
              }
            }
          })
          return newClass
        }
      },

      // 获取上一周下一周的数据
      getMobileNextWeekScheduleList () {
        const params = {
          start_time: this.getTimestamp(this.weekDayArr[0]),
          end_time: this.getTimestamp(this.weekDayArr[6]),
          start_time_one:this.weekDayArr[0],
          end_time_one:this.weekDayArr[6],
        }
        getMobileNextWeekScheduleList(params).then(response => {
          if (response.data.state == 1) {
            var staffList = response.data.data.staffList
            // console.log('下一周数据', staffList)
            var sevenStr = ''
            var oneStr = ''
            var twoStr = ''
            var threeStr = ''
            var fourStr = ''
            var fiveStr = ''
            var sixStr = ''
            this.arrZero = []
            this.arrOne = []
            this.arrTwo = []
            this.arrThree = []
            this.arrFour = []
            this.arrFive = []
            this.arrSix = []
            this.arrSeven = []
            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,
                  remark:staffList[i].remark,
                  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,remark:staffList[i].remark}]
                })
                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,remark:staffList[i].remark})
                  }
                }
              }
            }

            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)
              }
            })
            //护士
            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.remark = response.data.data.scheduleRemark.remark
            this.tableData = data
          }
        })
      },
      getScheduleByUserType () {
        var doctor_id = 0
        if (this.type == '全部') {
          doctor_id = 0
        }
        if (this.type == '医生') {
          doctor_id = 2
        }
        if (this.type == '护士') {
          doctor_id = 3
        }
        const params = {
          doctor_id: doctor_id,
          start_time: this.getTimestamp(this.weekDayArr[0]),
          end_time: this.getTimestamp(this.weekDayArr[6]),
          start_time_one:this.weekDayArr[0],
          end_time_one:this.weekDayArr[6],
        }
        getScheduleByUserType(params).then(response => {
          if (response.data.state == 1) {
            var staffList = response.data.data.staffList
            // console.log('stafflist', staffList)

            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,
                  remark:staffList[i].remark,
                  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)
              }
            })

            // console.log(1111111111111, newArr)
            if (this.type == '全部') {
              //护士
              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 data = []
              data.push(...newNurse)
              data.push(...newDoctor)
              this.tableData = data
            }
            if (this.type == '医生') {
              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'))
              this.tableData = newDoctor
            }
            if (this.type == '护士') {
              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'))
              this.tableData = newNurse
            }
          }
        })
      },
      compare (property) {
        return function (a, b) {
          var value1 = a[property]
          var value2 = b[property]
          return value2 - value1
        }
      }
    },
    created () {
      this.tableHeight = (document.documentElement.clientHeight - (Number(document.documentElement.style.fontSize.slice(0, -2)) * 3.125)) + 'px'
      let year = new Date().getFullYear()
      let month = new Date().getMonth() + 1
      let date = new Date().getDate()
      this.weekNum = this.getYearWeek(year, month, date)

      this.todayDate = this.formatDate(new Date())
      this.setDate(new Date())
      if (this.currentFirstDate1 <= 53) {
        for (var i = 0; i <= 9; i++) {
          this.setDate1(this.addDate(this.currentDate, 7))
        }
      }
      // console.log('this.arr', this.arr)

      // 获取该机构下的所有医生和护士
      this.getMobileDoctorList()
    }
  }
</script>

<style lang="scss" scoped>
  .page_medicalScheduling {
    height: 100%;
    background: #f6f6f6;
    overflow-y: auto;
  .medicalSchedulingTitle {
    height: 3.125rem;
    line-height: 3.125rem;
    display: flex;
    align-items: center;
    background: #fff;
  }
  .jiantou {
    margin-left: 1.25rem;
    font-size: 1.5rem;
    font-weight: 600;
    margin-right: 7rem;
  }
  .titleName {
    font-size: 1.125rem;
    font-weight: 600;
  }
  .searchBox{
    display: flex;
    justify-content: space-between;
    background: #fff;
    align-items: center;
  .searchOne{
    width: 25%;
    height: 2.125rem;
    background: rgba(246, 246, 246, 1);
    border-radius: 5px;
    text-align: center;
    font-size: .875rem;
    color: #8d8d8d;
    display: flex;
    align-items: center;
    justify-content: space-around;
    margin-right: 3%;
  }
  }
  .operateTitle{
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: .625rem;
  .operatePrevious{
    color: #3a8ee6;
  }
  .operateNext{
    color: #3a8ee6;
  }
  .operateTime{
    border: 1px solid #DCDFE6;
    height: 36px;
    line-height: 36px;
    text-align: center;
    padding: 0 0.625rem;
    border-radius: 4px;
    font-size: .75rem;
  }
  }
  }
  ::-webkit-scrollbar{
    width: 0 !important;
    height: 0 !important;
  }
  ::-webkit-scrollbar-thumb{
    background: #fff;
  }
</style>

<style lang="scss">
  .page_medicalScheduling{
  .searchBox {
  .van-search__content {
    border-radius: 1.875rem;
  }
  .van-cell__value {
    display: flex;
    align-items: center;
  }
  .van-field__body {
    width: 100%;
  }
  }
  ::-webkit-scrollbar{
    width:0;
    height: 0;
  }
  .el-table .cell {
    white-space: pre-line;/*保留换行符*/
  }
  }
</style>
