<template>
  <div class="page_scheduling">
      <van-sticky>
          <div class="staffManagementTitle">
              <i class="iconfont icon-zuojiantou jiantou" @click="toReturn"></i>
              <span class="titleName">患者排班</span>
              <span @click="toClick">临时排班</span>
            
          </div>
      </van-sticky>
      <div class="searchBox">
          <van-search
              class="searchInput"
              v-model="schedulingVal"
              placeholder="搜索患者姓名/透析号"
              @search="searchPatient"
          />
      </div>
      <div class="optionsBox">
          <div class="toolOne" @click="dateShow = true">
              <div style="display: flex;align-items: center;">
              {{ nowDate }}
              <van-icon name="arrow-down" />
              </div>
          </div>
          <div class="optionsOne" @click="typeShow = true">
              <div style="display: flex;align-items: center;">
              {{ type }}
              <van-icon name="arrow-down" />
              </div>
          </div>
          <div class="optionsOne" @click="areaShow = true">
              <div style="display: flex;align-items: center;">
              {{ area }}
              <van-icon name="arrow-down" />
              </div>
          </div>
          <van-switch :value="isOpen" @input="changeIsOpen" />
      </div>
      <div class="schedulingList">
          <div class="schedulingOne"  @touchstart="gotouchstart($event,index,item)" @touchmove="gotouchmove" @touchend="gotouchend" @click="item.patient.id > 0 ? toEditPatient(item.id,index,item.order.stage) : openPatientList(item,index)" :id="spanIndex.indexOf(index)>-1 ?  'tavActive' : '' " :class="modeColor(item.mode_name)" v-for="(item,index) in this.schedule" :key="index">
              <div >
                {{item.patient.name ? item.patient.name : '未排班'}}
                （<span v-if="item.schedule_type == 1">上午</span><span v-if="item.schedule_type == 2">下午</span><span v-if="item.schedule_type == 3">晚上</span>
                {{item.zone.name}}-{{item.number}}/{{item.mode_name}}）{{getPatientSolution(item.patient_id)}} {{getDiazer(item.patient_id,item.mode_id)}}
              </div>
              <div>
                <span v-if="item.order.stage == 0">待上机</span>
                <span v-if="item.order.stage == 1">已上机</span>
                <span v-if="item.order.stage == 2">已下机</span>
              </div>
              <span  v-show="show && item.order.stage == 0 && item.patient.id > 0" class="cancel" @click.stop="DeleteSchedule(item.id,item.order.stage,item.patient.name)">取消排班</span>
              <span v-show="(showOne || item.order.stage != 0) && item.patient.id > 0" @click.stop="DeleteSchedule(item.patient.id,item.order.stage,item.patient.name)"  class="canceltwo">取消排班</span>
              <span v-show="item.patient.id == 0" class="canceltwo" style="visibility: hidden;">取消排班</span>
          </div>
      </div>

      <div>
        <van-popup v-model="scheduleShow" position="bottom" :style="{ height: '100%' }">
          <div class="patientPopup">
            <div style="display:flex;align-items: center;">
              <van-icon name="arrow-left" size="1.5rem" @click="closePopup" />
              <van-search
                style="width:100%"
                v-model="patientVal"
                placeholder="搜索患者姓名/透析号"
                @input="searchPatientVal"
              />
            </div>
            <div class="patientContent">
              <div class="patientList">
                <div :class="activePatient == index ? 'activePatient patientCell' : 'patientCell'" @click="choosePatient(item,index)" v-for="(item,index) in patientList" :key="index + 'no'">
                  <span>{{ item.name }}</span>
                  <span>（{{ item.dialysis_no }}）</span>
                </div>
              </div>
              <div @click="createSchedule">确定排班</div>
            </div>
          </div>
        </van-popup>
        <van-popup v-model="changeTypeShow" get-container="#app" position="bottom" :style="{ height: '60%' }">
          <van-dropdown-menu>
            <div>
              <van-dropdown-item class="maxheight" v-model="modeValue" :options="modeOptions" @change="changeMode" />
              <van-dropdown-item class="maxheight" v-model="dialyzerValue" :options="DialyzerOptions" @change="changeDialyzer" />
              <van-dropdown-item class="maxheight" v-model="irrigationValue" :options="irrigationOptions" @change="changeIrrigation" />
              <van-dropdown-item class="maxheight" v-model="classValue" :options="classOptions" @change="changeClass" />
            </div>
          </van-dropdown-menu>
        </van-popup>
          <van-popup v-model="typeShow" position="bottom" :style="{ height: '40%' }">
              <van-picker
              show-toolbar
              :columns="typeColumns"
              @confirm="chooseType"
              @cancel="typeShow = false"
              />
          </van-popup>
          <van-popup v-model="areaShow" position="bottom" :style="{ height: '40%' }">
              <van-picker
              show-toolbar
              :columns="areaColumns"
              @confirm="chooseArea"
              @cancel="areaShow = false"
              />
          </van-popup>
          <van-popup v-model="dateShow" position="bottom" :style="{ height: '40%' }">
              <van-datetime-picker
              v-model="currentDate"
              type="date"
              :min-date="minDate"
              :max-date="maxDate"
              @confirm="getNowDate"
              @cancel="dateShow = false"
              />
          </van-popup>
      </div>
  </div>
</template>

<script>
import { getPatientScheduleList, GetAllZone, DeleteSchedule, searchPatient, updatePatientSchedule, getAllPatient, createSchedule, updateScheduleInfo, updateScheduleMode, updateScheduleDialysisPrescription, getSolutionSchedule,toInspection } from '@/api/staff_schedule'
import { Dialog, Toast } from 'vant'
var timeOutEvent = 0
export default {
data () {
  return {
    schedulingVal: '',
    nowDate: new Date(),
    type: '全部班',
    area: '全部分区',
    dateShow: false,
    typeShow: false,
    areaShow: false,
    currentDate: new Date(),
    minDate: new Date(1970, 0, 1),
    maxDate: new Date(2025, 10, 1),
    typeColumns: ['全部班', '上午', '下午', '晚上'],
    areaColumns: [],
    start_time: 0,
    class_type: 0,
    area_type: 0,
    zones: [],
    schedule: [],
    todayDate: 0,
    show: true,
    showOne: false,
    zoneid: 0,
    scheArr: [],
    spanIndex: [],
    changeTypeShow: false,
    modeOptions: [
      { text: '透析模式', value: 0 }
    ],
    DialyzerOptions: [
      { text: '透析器', value: '' }
    ],
    irrigationOptions: [
      { text: '灌流器', value: '' }
    ],
    classOptions: [
      { text: '更改班次', value: 0 },
      { text: '上午', value: 1 },
      { text: '下午', value: 2 },
      { text: '晚上', value: 3 }
    ],
    modeValue: 0,
    dialyzerValue: '',
    irrigationValue: '',
    classValue: 0,
    scheduleShow: false,
    org_id: 0,
    isOpen: false,
    patientQuery: {
      keywords: '',
      schedule: 0,
      contagion: 0
    },
    patientList: [],
    patientListMap: [],
    currentData: {
      id: 0,
      schedule_date: '',
      schedule_type: 0,
      bed_id: 0,
      partition_id: 0,
      patient_id: 0,
      schedule_week: 0,
      mode_id: 0,
      type_name: '',
      bed_name: '',

      partition_type: 0,
      contagions: [],
      patient: ''
    },
    activePatient: -1,
    patientIndex: -1,
    patientVal: '',
    patientParams: {
      patient_id: 0,
      mode_id: 0,
      schedule_type: 1,
      start_time: 0,
      dialyzer_perfusion_apparatus: '',
      bed_id: 0
    },
    solutionList: []
  }
},
created () {
  let treatment_mode = this.$store.getters.treatment_mode
  let modeArr = Object.values(treatment_mode).map(item => {
    let obj = {
      text: item.name,
      value: item.id
    }
    return obj
  })
  this.modeOptions.push(...modeArr)
  this.org_id = this.$store.getters.user.template_info.org_id
  if (this.org_id == 10223) {
    this.isOpen = true
  }
  if (sessionStorage.getItem('isOpenSchedule')) {
    this.isOpen = sessionStorage.getItem('isOpenSchedule') == 1
  }
  let value = new Date()
  let year = value.getFullYear()
  let month = value.getMonth() + 1
  let day = value.getDate()
  if (month >= 1 && month <= 9) {
    month = `0${month}`
  }
  if (day >= 1 && day <= 9) {
    day = `0${day}`
  }

  this.nowDate = `${year}-${month}-${day}`
  this.start_time = `${year}-${month}-${day}`

  var now = this.formatDate(new Date())
  this.todayDate = this.getTimestamp(now)
  // 获取所有分区
  this.GetAllZone()

  // 获取患者排班列表
  this.getlist()
  this.getAllPatient()
  this.getSolutionSchedule()
},
methods: {
  gotouchstart (event, index, item) {
    let that = this
    clearTimeout(timeOutEvent)// 清除定时器
    timeOutEvent = 0
    timeOutEvent = setTimeout(function () {
      // 执行长按要执行的内容，
      // console.log('hhhhhhhhhhh',item)
      if (item.patient.id == 0) {
        Toast.fail('该床位未有患者!')
        return
      }
      that.modeValue = item.mode_id
      that.dialyzerValue = item.prescription.dialysis_dialyszers ? item.prescription.dialysis_dialyszers : ''
      that.irrigationValue = item.prescription.dialysis_irrigation ? item.prescription.dialysis_irrigation : ''
      that.classValue = item.schedule_type

      that.patientParams.index = index
      that.patientParams.id = item.id
      that.patientParams.patient_id = item.patient_id
      that.patientParams.mode_id = item.mode_id
      that.patientParams.schedule_type = item.schedule_type
      that.patientParams.start_time = item.schedule_date
      that.patientParams.bed_id = item.bed_id
      that.patientParams.dialyzer_perfusion_apparatus = item.prescription.dialysis_dialyszers ? item.prescription.dialysis_dialyszers : ''
      that.patientParams.dialysis_irrigation = item.prescription.dialysis_irrigation ? item.prescription.dialysis_irrigation : ''
      that.changeTypeShow = true
    }, 1000)// 这里设置定时
  },
  // 手释放，如果在500毫秒内就释放，则取消长按事件，此时可以执行onclick应该执行的事件
  gotouchend () {
    clearTimeout(timeOutEvent)
    if (timeOutEvent != 0) {
      // console.log('ooooooooooooooo')
      // 这里写要执行的内容（尤如onclick事件）
    }
  },
  // 如果手指有移动，则取消所有事件，此时说明用户只是要移动而不是长按
  gotouchmove () {
    clearTimeout(timeOutEvent)// 清除定时器
    timeOutEvent = 0
  },
  formatDate (date) {
    var year = date.getFullYear() + '.'
    var month = (date.getMonth() + 1) + '.'
    var day = date.getDate()
    return year + month + day
  },
  getTimestamp (time) { // 把时间日期转成时间戳
    return (new Date(time)).getTime() / 1000
  },
  searchPatient () {
    const params = {
      keyword: this.schedulingVal,
      start_time: this.start_time
    }
    searchPatient(params).then(response => {
      if (response.data.state == 1) {
        var zoneArr = response.data.data.schedule
        let schedule = []
        zoneArr.map(item => {
          let obj = {
            bed_id: item.bed_id,
            id: item.id,
            mode_id: item.mode_id,
            mode_name: this.getModeName(item.mode_id),
            number: item.number.number,
            order: item.order,
            partition_id: 1,
            patient: {name: item.patient, id: item.patient_id},
            patient_id: item.patient_id,
            prescription: item.dialysis_prescription,
            schedule_date: item.schedule_date,
            schedule_type: item.schedule_type,
            schedule_week: item.schedule_week,
            zone: item.number.zone
          }
          schedule.push(obj)
        })
        for (let i = 0; i < schedule.length; i++) {
          if (schedule[i].schedule_date < this.todayDate) {
            this.showOne = true
            this.show = false
          }
          if (schedule[i].schedule_date > this.todayDate) {
            this.show = true
            this.showOne = false
          }
        }
        this.schedule = schedule
        // console.log('排班列表2332233232', this.schedule)
      }
    })
  },
  chooseType (value) {
    this.type = value
    this.typeShow = false
    this.getlist()
    this.getSolutionSchedule()
  },
  chooseArea (value) {
    this.area = value
    this.areaShow = false
    this.getlist()
    this.getSolutionSchedule()
  },
  getNowDate (value) {
    let year = value.getFullYear()
    let month = value.getMonth() + 1
    let day = value.getDate()
    if (month >= 1 && month <= 9) {
      month = `0${month}`
    }
    if (day >= 1 && day <= 9) {
      day = `0${day}`
    }

    this.nowDate = `${year}-${month}-${day}`
    this.start_time = `${year}-${month}-${day}`
    this.dateShow = false
    this.getlist()
    this.getSolutionSchedule()
  },
  toReturn () {
    this.$router.push('/homeIndex')
  },
  toClick () {
    this.$router.push('add_urgent_schedule')
  },
  getlist () {
    if (this.type == '全部班') {
      this.class_type = 0
    }
    if (this.type == '上午') {
      this.class_type = 1
    }
    if (this.type == '下午') {
      this.class_type = 2
    }
    if (this.type == '晚上') {
      this.class_type = 3
    }
    var zoneid = 0
    if (this.area == '全部分区') {
      zoneid = 0
    }
    for (let i = 0; i < this.zones.length; i++) {
      if (this.area == this.zones[i].name) {
        zoneid = this.zones[i].id
      }
    }
    this.zoneid = zoneid
    const params = {
      start_time: this.start_time,
      zoneid: this.zoneid,
      classtype: this.class_type,
      is_open: 1
    }
    getPatientScheduleList(params).then(response => {
      if (response.data.state == 1) {
        var zoneArr = response.data.data.schedule
        let arr = []
        var arrTwo = []
        var dialyzer = []
        var irrigation = []
        let stockType = response.data.data.stockType
        for (let i = 0; i < stockType.length; i++) {
          if (stockType[i].stock_attribute == 2) {
            arr.push(stockType[i])
          }
          if (stockType[i].stock_attribute == 3) {
            arrTwo.push(stockType[i])
          }
        }

        for (let i = 0; i < arr.length; i++) {
          for (let j = 0; j < arr[i].GoodInfo.length; j++) {
            dialyzer.push(arr[i].GoodInfo[j])
          }
        }
        for (let i = 0; i < arrTwo.length; i++) {
          for (let j = 0; j < arrTwo[i].GoodInfo.length; j++) {
            irrigation.push(arrTwo[i].GoodInfo[j])
          }
        }
        let DialyzerOptions = dialyzer.map(item => {
          let obj = {
            text: item.specification_name,
            value: item.specification_name
          }
          return obj
        })
        let irrigationOptions = irrigation.map(item => {
          let obj = {
            text: item.specification_name,
            value: item.specification_name
          }
          return obj
        })
        this.DialyzerOptions = [{ text: '透析器', value: '' }],
        this.irrigationOptions = [{ text: '灌流器', value: '' }],
        this.DialyzerOptions.push(...DialyzerOptions)
        this.irrigationOptions.push(...irrigationOptions)
        let schedule = []
        if (this.isOpen == true) {
          zoneArr.map(item => {
            if (item.schedule.length > 0) {
              let it_one_arr = []
              let it_two_arr = []
              let it_three_arr = []
              item.schedule.find((v) => {
                if (v.schedule_type == 1) {
                  v.zone = item.zone
                  v.number = item.number
                  v.sort = item.sort
                  it_one_arr.push(v)
                }
                if (v.schedule_type == 2) {
                  v.zone = item.zone
                  v.number = item.number
                  v.sort = item.sort
                  it_two_arr.push(v)
                }
                if (v.schedule_type == 3) {
                  v.zone = item.zone
                  v.number = item.number
                  v.sort = item.sort
                  it_three_arr.push(v)
                }

                // return v.schedule_type ===  1;
              })

              if (this.class_type == 0 || this.class_type == 1) {
                if (it_one_arr.length > 0) {
                  schedule.push(...it_one_arr)
                } else {
                  let type = {
                    schedule_type: 1,
                    patient: {name: '', id: 0},
                    zone: item.zone,
                    prescription: {},
                    sort: item.sort,
                    mode_id: 0,
                    number: item.number,
                    order: {id: 0, dialysis_date: 0, patient_id: 0, stage: 0, bed_id: 0},
                    bed_id: item.id
                  }
                  schedule.push(type)
                }
              }
              // let it_two = item.schedule.find((v) => {
              //   return v.schedule_type ===  2;
              // });
              if (this.class_type == 0 || this.class_type == 2) {
                if (it_two_arr.length > 0) {
                  schedule.push(...it_two_arr)
                } else {
                  let type = {
                    schedule_type: 2,
                    patient: {name: '', id: 0},
                    zone: item.zone,
                    prescription: {},
                    sort: item.sort,
                    mode_id: 0,
                    number: item.number,
                    order: {id: 0, dialysis_date: 0, patient_id: 0, stage: 0, bed_id: 0},
                    bed_id: item.id
                  }
                  schedule.push(type)
                }
              }
              // let it_three = item.schedule.find((v) => {
              //   return v.schedule_type ===  3;
              // });
              if (this.class_type == 0 || this.class_type == 3) {
                if (it_three_arr.length > 0) {
                  schedule.push(...it_three_arr)
                } else {
                  let type = {
                    schedule_type: 3,
                    patient: {name: '', id: 0},
                    zone: item.zone,
                    prescription: {},
                    sort: item.sort,
                    mode_id: 0,
                    number: item.number,
                    order: {id: 0, dialysis_date: 0, patient_id: 0, stage: 0, bed_id: 0},
                    bed_id: item.id
                  }
                  schedule.push(type)
                }
              }
            } else {
              if (this.class_type == 0 || this.class_type == 1) {
                let type1 = {
                  schedule_type: 1,
                  patient: {name: '', id: 0},
                  zone: item.zone,
                  prescription: {},
                  sort: item.sort,
                  mode_id: 0,
                  number: item.number,
                  order: {id: 0, dialysis_date: 0, patient_id: 0, stage: 0, bed_id: 0},
                  bed_id: item.id
                }
                schedule.push(type1)
              }
              if (this.class_type == 0 || this.class_type == 2) {
                let type2 = {
                  schedule_type: 2,
                  patient: {name: '', id: 0},
                  zone: item.zone,
                  prescription: {},
                  sort: item.sort,
                  mode_id: 0,
                  number: item.number,
                  order: {id: 0, dialysis_date: 0, patient_id: 0, stage: 0, bed_id: 0},
                  bed_id: item.id
                }
                schedule.push(type2)
              }
              if (this.class_type == 0 || this.class_type == 3) {
                let type3 = {
                  schedule_type: 3,
                  patient: {name: '', id: 0},
                  zone: item.zone,
                  prescription: {},
                  sort: item.sort,
                  mode_id: 0,
                  number: item.number,
                  order: {id: 0, dialysis_date: 0, patient_id: 0, stage: 0, bed_id: 0},
                  bed_id: item.id
                }
                schedule.push(type3)
              }
            }
          })
        } else {
          zoneArr.map(item => {
            if (item.schedule.length > 0) {
              item.schedule.map(it => {
                it.zone = item.zone
                it.number = item.number
                it.sort = item.sort
                schedule.push(it)
              })
            }
          })
        }

        for (let i = 0; i < schedule.length; i++) {
          if (schedule[i].schedule_date < this.todayDate) {
            this.showOne = true
            this.show = false
          }
          if (schedule[i].schedule_date > this.todayDate) {
            this.show = true
            this.showOne = false
          }
          if (schedule[i].mode_id == 0) {
            schedule[i].mode_name = ''
          }
          if (schedule[i].mode_id == 1) {
            schedule[i].mode_name = 'HD'
          }
          if (schedule[i].mode_id == 2) {
            schedule[i].mode_name = 'HDF'
          }
          if (schedule[i].mode_id == 3) {
            schedule[i].mode_name = 'HD+HP'
          }
          if (schedule[i].mode_id == 4) {
            schedule[i].mode_name = 'HP'
          }
          if (schedule[i].mode_id == 5) {
            schedule[i].mode_name = 'HF'
          }
          if (schedule[i].mode_id == 6) {
            schedule[i].mode_name = 'SCUF'
          }
          if (schedule[i].mode_id == 7) {
            schedule[i].mode_name = 'IUF'
          }
          if (schedule[i].mode_id == 8) {
            schedule[i].mode_name = 'HFHD'
          }
          if (schedule[i].mode_id == 9) {
            schedule[i].mode_name = 'HFHD+HP'
          }
          if (schedule[i].mode_id == 10) {
            schedule[i].mode_name = 'PHF'
          }
          if (schedule[i].mode_id == 11) {
            schedule[i].mode_name = 'HFR'
          }
          if (schedule[i].mode_id == 12) {
            schedule[i].mode_name = 'HDF+HP'
          }
          if (schedule[i].mode_id == 13) {
            schedule[i].mode_name = 'CRRT'
          }
          if (schedule[i].mode_id == 14) {
            schedule[i].mode_name = '腹水回输'
          }
          if (schedule[i].mode_id == 19) {
            schedule[i].mode_name = 'IUF+HD'
          }
          if (schedule[i].mode_id == 20) {
            schedule[i].mode_name = 'UF'
          }
          if (schedule[i].mode_id == 21) {
            schedule[i].mode_name = 'HD+'
          }
          if (schedule[i].mode_id == 22) {
            schedule[i].mode_name = '血浆胆红素吸附+HDF'
          }
          if (schedule[i].mode_id == 24) {
            schedule[i].mode_name = 'I-HDF'
          }
          if (schedule[i].mode_id == 25) {
            schedule[i].mode_name = 'HD高通'
          }
          if (schedule[i].mode_id == 26) {
            schedule[i].mode_name = 'CVVH'
          }
          if (schedule[i].mode_id == 27) {
            schedule[i].mode_name = 'CVVHD'
          }
          if (schedule[i].mode_id == 28) {
            schedule[i].mode_name = 'CVVHDF'
          }
          if (schedule[i].mode_id == 29) {
            schedule[i].mode_name = 'PE'
          }
          if (schedule[i].mode_id == 30) {
            schedule[i].mode_name = '血浆胆红素吸附+HP'
          }
        }
        this.schedule = schedule.sort((a, b) => { return a.sort - b.sort })
      }
    })
  },
  GetAllZone () {
    GetAllZone().then(response => {
      if (response.data.state == 1) {
        var obj = {id: 0, name: '全部分区'}
        var zone = response.data.data.zone
        this.zones.push(obj)
        this.zones.push(...zone)
        var arr = []
        for (let i = 0; i < this.zones.length; i++) {
          arr.push(this.zones[i].name)
        }

        this.areaColumns = arr
      }
    })
  },
  DeleteSchedule (id, stage, name) {
    if (stage == 1) {
      this.$toast('患者已上机，不能取消排班')
      return false
    }
    if (stage == 2) {
      this.$toast('患者已下机，不能取消排班')
      return false
    }
    Dialog.confirm({
      title: '删除提示!',
      message: '是否确认要删除' + name + '的排班'
    }).then(() => {
      DeleteSchedule(id).then(response => {
        if (response.data.state == 1) {
          var msg = response.data.data.msg
          this.$toast('取消成功!')
          this.getlist()
          this.getSolutionSchedule()
        }else{
            console.log("hhhhhhhhhhhhhhhhhhhh")
            var msg = response.data.msg
            this.$toast(msg)
          }
      })
    })
  },
  modeColor (id) {
    if (id == 'HD') {

    } else if (id == 'HDF') {
      return 'modeRed'
    } else if (id == 'HD+HP') {
      return 'modePurple'
    }
  },
  toEditPatient (id, index, stage) {
    if (stage == 1 || stage == 2) {
      this.$toast('已上机的患者不能调换床位号!')
      return
    }
    this.scheArr.push(id)
    let arrIndex = this.spanIndex.indexOf(index)
    if (arrIndex > -1) {
      this.spanIndex.splice(arrIndex, 1)
    } else {
      this.spanIndex.push(index)
      if (this.spanIndex.length > 2) {
        this.spanIndex.shift()
      }
    }
    if (this.scheArr[0] == this.scheArr[1]) {
      this.$toast('请选择不同床位的互换')
      this.scheArr = []
      this.spanIndex = []
      return
    }
    if (this.scheArr.length == 2) {
      Dialog.confirm({
        title: '提示!',
        message: '是否确认要更换位置'
      }).then(() => {
        var ids = this.scheArr.join(',')
        var params = {
          ids: ids
        }
        updatePatientSchedule(params).then(response => {
          if (response.data.state === 1) {
            var msg = response.data.data.msg
            this.getlist()
            this.$toast('保存成功')
            this.scheArr = []
            this.spanIndex = []
          }
        })
      }).catch(() => {
        this.scheArr = []
        this.spanIndex = []
      })
    }
  },
  changeIsOpen (isOpen) {
    let isOpenNum = 1
    if (isOpen) {
      isOpenNum = 1
    } else {
      isOpenNum = 2
    }
    let message = isOpen ? '是否展示空床位？' : '是否隐藏空床位？'
    Dialog.confirm({
      title: '提醒',
      message: message
    }).then(() => {
      this.isOpen = isOpen
      this.getlist()
    }).catch(() => {
      // on cancel
    })
    sessionStorage.setItem('isOpenSchedule', isOpenNum)
  },
  getAllPatient () {
    getAllPatient(this.patientQuery).then(res => {
      this.patientList = res.data.data.patients
      this.patientListMap = res.data.data.patients
    }).catch(() => {})
  },
  openPatientList (row, index) {
    this.patientIndex = index
    this.currentData.schedule_date = this.nowDate
    this.currentData.schedule_type = row.schedule_type
    this.currentData.bed_id = row.bed_id
    this.currentData.partition_id = row.zone.id
    this.currentData.schedule_week = this.getWeekDay(new Date(this.nowDate))

    switch (row.schedule_type) {
      case 1:
        this.currentData.type_name = '上午'
        break
      case 2:
        this.currentData.type_name = '下午'
        break
      case 3:
        this.currentData.type_name = '晚上'
        break
    }

    this.currentData.zone_name = row.zone.name
    this.currentData.bed_name = row.number
    this.currentData.partition_type = row.zone.type

    this.currentData.id = 0
    this.scheduleShow = true
  },
  closePopup () {
    this.activePatient = -1
    this.currentData = {
      id: 0,
      schedule_date: '',
      schedule_type: 0,
      bed_id: 0,
      partition_id: 0,
      patient_id: 0,
      schedule_week: 0,
      mode_id: 0,
      type_name: '',
      bed_name: '',

      partition_type: 0,
      contagions: [],
      patient: ''
    },
    this.scheduleShow = false
  },
  choosePatient (row, index) {
    this.activePatient = index
    this.currentData.mode_id = 1
    this.currentData.patient_id = row.id
    this.currentData.patient = ''
    this.currentData.contagions = row.contagions
  },
  createSchedule () {
    if (this.currentData.patient_id == 0) {
      Toast.fail('请选择需要排班患者')
      return
    }
    createSchedule(this.currentData.patient_id, this.currentData).then(res => {
      if (res.data.state == 0) {
        Toast.fail(res.data.msg)
      } else {
        // console.log(this.schedule[this.patientIndex])
        this.schedule[this.patientIndex].schedule_type = res.data.data.schedule.schedule_type
        this.schedule[this.patientIndex].patient.name = res.data.data.schedule.patient
        this.schedule[this.patientIndex].patient.id = res.data.data.schedule.patient_id
        this.schedule[this.patientIndex].patient_id = res.data.data.schedule.patient_id
        this.schedule[this.patientIndex].id = res.data.data.schedule.id
        this.schedule[this.patientIndex].schedule_date = res.data.data.schedule.schedule_date
        this.schedule[this.patientIndex].mode_name = 'HD'
        this.schedule[this.patientIndex].mode_id = 1
        Toast.success('排班成功!')
        this.scheduleShow = false
        this.activePatient = -1
        this.currentData = {
          id: 0,
          schedule_date: '',
          schedule_type: 0,
          bed_id: 0,
          partition_id: 0,
          patient_id: 0,
          schedule_week: 0,
          mode_id: 0,
          type_name: '',
          bed_name: '',

          partition_type: 0,
          contagions: [],
          patient: ''
        }
      }
    }).catch(() => {})
  },
  getWeekDay (date) {
    var week
    if (date.getDay() == 0) week = 7
    if (date.getDay() == 1) week = 1
    if (date.getDay() == 2) week = 2
    if (date.getDay() == 3) week = 3
    if (date.getDay() == 4) week = 4
    if (date.getDay() == 5) week = 5
    if (date.getDay() == 6) week = 6
    return week
  },
  searchPatientVal (val) {
    if (val.length > 0) {
      var scheduals = []
      for (let index = 0; index < this.patientListMap.length; index++) {
        const schedual = this.patientListMap[index]
        if (schedual.name.indexOf(val) != -1 || schedual.dialysis_no.indexOf(val) != -1) {
          scheduals.push(schedual)
        }
      }
      this.patientList = scheduals
    } else {
      this.getAllPatient()
    }
  },
  changeClass (value) {
    if (value == 0) {
      Toast.fail('请选择正确的排班!')
      return
    }
    let params = {
      patient_id: this.patientParams.patient_id,
      mode_id: this.patientParams.mode_id,
      schedule_type: value,
      start_time: this.patientParams.start_time,
      bed_id: this.patientParams.bed_id
    }
    updateScheduleInfo(params).then(res => {
      if (res.data.data.msgerr == '排班已存在') {
        Toast.fail(res.data.data.msgerr)
      } else {
        this.getlist()
        setTimeout(() => {
          Toast.success('更新成功!')
        }, 100)
      }
    }).catch(() => {})
  },
  changeMode (value) {
    if (value == '') {
      Toast.fail('请选择正确的透析模式!')
      return
    }
    this.patientParams.mode_id = value
    let params = {
      id: this.patientParams.id,
      mode_id: this.patientParams.mode_id
    }
    updateScheduleMode(params).then(res => {
      if (res.data.state == 0) {
        Toast.fail('保存失败!')
      } else {
        Toast.success('保存成功!')
        this.schedule[this.patientParams.index].mode_id = value
        this.schedule[this.patientParams.index].mode_name = this.getModeName(value)
      }
    })
  },
  getModeName (mode_id) {
    let mode_name = ''
    if (mode_id == 0) {
      mode_name = ''
    }
    if (mode_id == 1) {
      mode_name = 'HD'
    }
    if (mode_id == 2) {
      mode_name = 'HDF'
    }
    if (mode_id == 3) {
      mode_name = 'HD+HP'
    }
    if (mode_id == 4) {
      mode_name = 'HP'
    }
    if (mode_id == 5) {
      mode_name = 'HF'
    }
    if (mode_id == 6) {
      mode_name = 'SCUF'
    }
    if (mode_id == 7) {
      mode_name = 'IUF'
    }
    if (mode_id == 8) {
      mode_name = 'HFHD'
    }
    if (mode_id == 9) {
      mode_name = 'HFHD+HP'
    }
    if (mode_id == 10) {
      mode_name = 'PHF'
    }
    if (mode_id == 11) {
      mode_name = 'HFR'
    }
    if (mode_id == 12) {
      mode_name = 'HDF+HP'
    }
    if (mode_id == 13) {
      mode_name = 'CRRT'
    }
    if (mode_id == 14) {
      mode_name = '腹水回输'
    }
    if (mode_id == 19) {
      mode_name = 'IUF+HD'
    }
    if (mode_id == 20) {
      mode_name = 'UF'
    }
    if (mode_id == 21) {
      mode_name = 'HD+'
    }
    if (mode_id == 22) {
      mode_name = '血浆胆红素吸附+HDF'
    }
    if (mode_id == 24) {
      mode_name = 'I-HDF'
    }
    if (mode_id == 25) {
      mode_name = 'HD高通'
    }
    if (mode_id == 26) {
      mode_name = 'CVVH'
    }
    if (mode_id == 27) {
      mode_name = 'CVVHD'
    }
    if (mode_id == 28) {
      mode_name = 'CVVHDF'
    }
    return mode_name
  },
  changeDialyzer (value) {
    let params = {
      mode_id: this.schedule[this.patientParams.index].mode_id,
      patient_id: this.patientParams.patient_id,
      start_time: this.patientParams.start_time,
      dialyzer_perfusion_apparatus: value,
      dialysis_irrigation: this.patientParams.dialysis_irrigation
    }

    updateScheduleDialysisPrescription(params).then(res => {
      if (res.data.state == 0) {
        Toast.fail('保存失败!')
      } else {
        this.schedule[this.patientParams.index].prescription.dialysis_dialyszers = value
        this.patientParams.dialyzer_perfusion_apparatus = value
        Toast.success('保存成功!')
      }
    })
  },
  changeIrrigation (value) {
    let params = {
      mode_id: this.schedule[this.patientParams.index].mode_id,
      patient_id: this.patientParams.patient_id,
      start_time: this.patientParams.start_time,
      dialyzer_perfusion_apparatus: this.patientParams.dialyzer_perfusion_apparatus,
      dialysis_irrigation: value
    }
    updateScheduleDialysisPrescription(params).then(res => {
      if (res.data.state == 0) {
        Toast.fail('保存失败!')
      } else {
        this.schedule[this.patientParams.index].prescription.dialysis_irrigation = value
        this.patientParams.dialysis_irrigation = value
        Toast.success('保存成功!')
      }
    })
  },
  getSolutionSchedule () {
    var params = {
      start_time: this.start_time,
      zoneid: this.zoneid,
      classtype: this.class_type
    }
    getSolutionSchedule(params).then(response => {
      if (response.data.state == 1) {
        var schedule = response.data.data.schedule

        for (let i = 0; i < schedule.length; i++) {
          if (schedule[i].vmsolution.mode_id == 1) {
            schedule[i].vmsolution.mode_name = 'HD'
          }
          if (schedule[i].vmsolution.mode_id == 2) {
            schedule[i].vmsolution.mode_name = 'HDF'
          }
          if (schedule[i].vmsolution.mode_id == 3) {
            schedule[i].vmsolution.mode_name = 'HD+HP'
          }
          if (schedule[i].vmsolution.mode_id == 4) {
            schedule[i].vmsolution.mode_name = 'HP'
          }
          if (schedule[i].vmsolution.mode_id == 5) {
            schedule[i].vmsolution.mode_name = 'HF'
          }
          if (schedule[i].vmsolution.mode_id == 6) {
            schedule[i].vmsolution.mode_name = 'SCUF'
          }
          if (schedule[i].vmsolution.mode_id == 7) {
            schedule[i].vmsolution.mode_name = 'IUF'
          }
          if (schedule[i].vmsolution.mode_id == 8) {
            schedule[i].vmsolution.mode_name = 'HFHD'
          }
          if (schedule[i].vmsolution.mode_id == 9) {
            schedule[i].vmsolution.mode_name = 'HFHD+HP'
          }
          if (schedule[i].vmsolution.mode_id == 10) {
            schedule[i].vmsolution.mode_name = 'PHF'
          }
          if (schedule[i].vmsolution.mode_id == 11) {
            schedule[i].vmsolution.mode_name = 'HFR'
          }
          if (schedule[i].vmsolution.mode_id == 12) {
            schedule[i].vmsolution.mode_name = 'HDF+HP'
          }
          if (schedule[i].vmsolution.mode_id == 13) {
            schedule[i].vmsolution.mode_name = 'CRRT'
          }
          if (schedule[i].vmsolution.mode_id == 14) {
            schedule[i].vmsolution.mode_name = '腹水回输'
          }
          if (schedule[i].vmsolution.mode_id == 19) {
            schedule[i].vmsolution.mode_name = 'IUF+HD'
          }
          if (schedule[i].vmsolution.mode_id == 20) {
            schedule[i].vmsolution.mode_name = 'UF'
          }
          if (schedule[i].vmsolution.mode_id == 21) {
            schedule[i].vmsolution.mode_name = 'HD+'
          }
          if (schedule[i].vmsolution.mode_id == 18) {
            schedule[i].vmsolution.mode_name = 'SCUF'
          }
          if (schedule[i].vmsolution.mode_id == 19) {
            schedule[i].vmsolution.mode_name = 'SCUF'
          }
          if (schedule[i].vmsolution.mode_id == 20) {
            schedule[i].vmsolution.mode_name = 'SCUF'
          }
          if (schedule[i].vmsolution.mode_id == 21) {
            schedule[i].vmsolution.mode_name = 'HD+'
          }
          if (schedule[i].vmsolution.mode_id == 22) {
            schedule[i].vmsolution.mode_name = '血浆胆红素吸附+HDF'
          }
          if (schedule[i].vmsolution.mode_id == 23) {
            schedule[i].vmsolution.mode_name = '血浆胆红素吸附'
          }
          if (schedule[i].vmsolution.mode_id == 24) {
            schedule[i].vmsolution.mode_name = 'I-HDF'
          }
          if (schedule[i].vmsolution.mode_id == 25) {
            schedule[i].vmsolution.mode_name = 'HD高通'
          }
          if (schedule[i].vmsolution.mode_id == 26) {
            schedule[i].vmsolution.mode_name = 'CVVH'
          }
          if (schedule[i].vmsolution.mode_id == 27) {
            schedule[i].vmsolution.mode_name = 'CVVH'
          }
          if (schedule[i].vmsolution.mode_id == 28) {
            schedule[i].vmsolution.mode_name = 'CVVHDF'
          }
          if (schedule[i].vmsolution.mode_id == 29) {
            schedule[i].vmsolution.mode_name = 'PE'
          }
          if (schedule[i].vmsolution.solution != undefined) {
            schedule[i].vmsolution.dialysis_dialyszers = schedule[i].vmsolution.solution.dialysis_dialyszers
            schedule[i].vmsolution.dialysis_irrigation = schedule[i].vmsolution.solution.dialysis_irrigation
            schedule[i].vmsolution.dialyzer_perfusion_apparatus = schedule[i].vmsolution.solution.dialyzer_perfusion_apparatus
          }
          if (schedule[i].vmsolution.anticoagulant == 0) {
            schedule[i].vmsolution.anticoagulant = ''
          }
          if (schedule[i].vmsolution.anticoagulant == 1) {
            schedule[i].vmsolution.anticoagulant = '无肝素'
          }
          if (schedule[i].vmsolution.anticoagulant == 2) {
            schedule[i].vmsolution.anticoagulant = '普通肝素'
          }
          if (schedule[i].vmsolution.anticoagulant == 3) {
            schedule[i].vmsolution.anticoagulant = '低分子肝素'
          }
          if (schedule[i].vmsolution.anticoagulant == 4) {
            schedule[i].vmsolution.anticoagulant = '阿加曲班'
          }
          if (schedule[i].vmsolution.anticoagulant == 5) {
            schedule[i].vmsolution.anticoagulant = '枸橼酸钠'
          }
          if (schedule[i].vmsolution.anticoagulant == 6) {
            schedule[i].vmsolution.anticoagulant = '低分子肝素钙'
          }
          if (schedule[i].vmsolution.anticoagulant == 7) {
            schedule[i].vmsolution.anticoagulant = '低分子肝素钠'
          }
          if (schedule[i].vmsolution.anticoagulant == 8) {
            schedule[i].vmsolution.anticoagulant = '依诺肝素'
          }
          if (schedule[i].vmsolution.anticoagulant == 9) {
            schedule[i].vmsolution.anticoagulant = '达肝素'
          }
          if (schedule[i].vmsolution.anticoagulant == 10) {
            schedule[i].vmsolution.anticoagulant = '体外抗凝'
          }
        }

        this.solutionList = schedule
      }
    })
  },
  getPatientSolution (patient_id) {
    var anticoagulant = ''
    for (let i = 0; i < this.solutionList.length; i++) {
      if (patient_id == this.solutionList[i].vmsolution.patient_id) {
        anticoagulant = this.solutionList[i].vmsolution.anticoagulant
      }
    }
    return anticoagulant
  },
  getDiazer (patient_id, mode_id) {
    var diazers = ''
    var irrigation = ''
    for (let i = 0; i < this.solutionList.length; i++) {
      if (patient_id == this.solutionList[i].vmsolution.patient_id && mode_id == this.solutionList[i].vmsolution.mode_id) {
        if (this.solutionList[i].vmsolution.dialyzer_perfusion_apparatus != '') {
          diazers = this.solutionList[i].vmsolution.dialyzer_perfusion_apparatus
          irrigation = this.solutionList[i].vmsolution.dialysis_irrigation
        } else {
          diazers = this.solutionList[i].vmsolution.dialysis_dialyszers
          irrigation = this.solutionList[i].vmsolution.dialysis_irrigation
        }
      }
    }
    if (irrigation != '') {
      return diazers + '/' + irrigation
    } else {
      return diazers
    }
  },
  toInspection(){
    toInspection().then(response=>{

    })
  }

},
watch: {
  schedulingVal (newVal) {
    if (newVal == '') {
      this.schedulingVal = newVal
      this.getlist()
    } else {
      this.schedulingVal = newVal
      this.searchPatient()
    }
  }
}
}
</script>

<style lang="scss" scoped>
.page_scheduling {
  height: 100%;
  overflow-y: auto;
  background: #fafafa;
.staffManagementTitle {
  background: #fff;
  padding: 0 1.125rem;
  height: 3.125rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  span{
    -webkit-touch-callout:none;
    -webkit-user-select:none;
    -khtml-user-select:none;
    -moz-user-select:none;
    -ms-user-select:none;
    user-select:none;
  }
}
.jiantou {
  font-size: 1.5rem;
  font-weight: 600;
  -webkit-touch-callout:none;
  -webkit-user-select:none;
  -khtml-user-select:none;
  -moz-user-select:none;
  -ms-user-select:none;
  user-select:none;
  // margin-right: 6.5rem;
}
.titleName {
  font-size: 1.125rem;
  font-weight: 600;
  -webkit-touch-callout:none;
  -webkit-user-select:none;
  -khtml-user-select:none;
  -moz-user-select:none;
  -ms-user-select:none;
  user-select:none;
}
.searchInput{
  -webkit-touch-callout:none;
  -webkit-user-select:none;
  -khtml-user-select:none;
  -moz-user-select:none;
  -ms-user-select:none;
  user-select:none;
}
.optionsBox {
  display: flex;
// height: 3.125rem;
  padding-bottom: 0.625rem;
  align-items: center;
  width: 100%;
  background: #fff;
  min-height: 0;
  .optionsOne {
    //   max-width: 8.75rem;
    //   min-width: 6.25rem;
    padding: 0 0.75rem;
    height: 1.875rem;
    background: rgba(246, 246, 246, 1);
    border-radius: 5px;
    text-align: center;
    font-size: 0.8125rem;
    color: #8d8d8d;
    display: flex;
    align-items: center;
    justify-content: space-around;
    margin-left: 0.8125rem;
    -webkit-touch-callout:none;
    -webkit-user-select:none;
    -khtml-user-select:none;
    -moz-user-select:none;
    -ms-user-select:none;
    user-select:none;
  }
  .toolOne {
    width: 6.25rem;
    height: 1.875rem;
    background: rgba(246, 246, 246, 1);
    border-radius: 5px;
    text-align: center;
    font-size: 0.8125rem;
    color: #8d8d8d;
    display: flex;
    align-items: center;
    justify-content: space-around;
    margin-left: 0.8125rem;
    -webkit-touch-callout:none;
    -webkit-user-select:none;
    -khtml-user-select:none;
    -moz-user-select:none;
    -ms-user-select:none;
    user-select:none;
  }
}
.schedulingList{
    font-size: 0.875rem;
    background: #fff;
  .schedulingOne{
      display: flex;
      align-items: center;
      height: 4.125rem;
      justify-content: space-between;
      padding: 0 .625rem;
      border-top: 1px solid #e5e5e5;
      -webkit-touch-callout:none;
      -webkit-user-select:none;
      -khtml-user-select:none;
      -moz-user-select:none;
      -ms-user-select:none;
      user-select:none;
      >div:first-child{
          width: 50%;
      }
      .cancel{
          padding: 0.3125rem 0.625rem;
          background: #258ffc;
          border-radius: .3125rem;
          color:#fff;
          font-size: 0.875rem;
      }
      .canceltwo{
          padding: 0.3125rem 0.625rem;
          background: #ccc;
          border-radius: .3125rem;
          color:#fff;
          font-size: 0.875rem;
      }
  }

}
}
.modeRed{
  color:#ed5555;
}
.modePurple{
  color: #53b86e;
}
.patientPopup{
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
}
.patientContent{
  flex: 1;
  overflow-y: auto;
  height: 100%;
  display: flex;
  flex-direction: column;
>:last-child{
  height: 3.125rem;
  text-align: center;
  line-height: 3.125rem;
  background: #258ffc;
  color: #fff;
}
}
.activePatient{
  background: #badcff !important;
}
.patientList{
  flex: 1;
  overflow-y: auto;
  height: 100%;
.patientCell{
  display: flex;
  align-items: center;
  height: 3.125rem;
// justify-content: space-between;
  padding: 0 .625rem;
  border-top: 1px solid #e5e5e5;
}
}
</style>

<style lang="scss">
.page_scheduling{
  .searchBox {
      .van-search__content {
      border-radius: 1.875rem;
      }
      .van-cell__value {
      display: flex;
      align-items: center;
      }
      .van-field__body {
      width: 100%;
      }
  }
  #tavActive{
    background:#b3d8ff;
  }

}
#app{
.van-dropdown-menu{
  display: flex;
  height: 100%;
  flex-direction: column;
  >div:last-child{
    flex: 1;
  }
}
.van-dropdown-menu__bar .van-dropdown-menu__bar--opened{
  width: 100%;
}
.van-dropdown-item{
  position: absolute !important;
  top: 3rem !important;
}
.maxheight{
  .van-dropdown-item{
    z-index: 6000 !important;
  }
  .van-dropdown-item__content{
    max-height: 100%;
    z-index: 7000 !important;
  }
}
}
</style>
