<template>
    <div class="newDoctorAdvice">
            <van-sticky>
                <div class="newDoctorAdviceTitle">
                    <span @click="backAction" style="color:#323233;font-size: .875rem;margin-right: 2.25rem;"><i class="iconfont icon-zuojiantou jiantou" style="color:#323233;"></i>返回</span>
                    <span style="font-weight:bold;">上一方/下一方</span>
                    <span @click="submitAction" style="color:#323233;font-size: .875rem;">设为本次临嘱</span>
                </div>
                <div class="adviceTitle">
                    <div>开始时间：<span class="timeSpan" @click="selectStartTimeAction">{{ start_time_str }}</span></div>
                    <div>开嘱医生: {{ $store.getters.user.user.user_name }}</div>
                </div>
            </van-sticky>
            <div style="padding:0 .875rem;flex:1;display:flex;flex-direction: column;overflow-y: auto;">
                <div style="flex:1;overflow-y: auto;">
                    <div style="margin-bottom:.625rem;">{{getContent()}}</div>
                    <div v-for="item in other_doctor_advices">
                        <div class="lastAdviceOne">
                            医嘱内容：<span>{{item.advice_name }}</span>
                            <span v-if="item.advice_desc">({{ item.advice_desc }}{{item.drug_spec_unit}})</span>
                            <span v-if="item.prescribing_number">&nbsp;&nbsp;{{item.prescribing_number}}{{item.prescribing_number_unit}}</span>
                            <span v-if="item.single_dose && template_id == 6">{{item.single_dose}}{{item.single_dose_unit}}</span>
                            <span v-if="item.single_dose && template_id != 6">单次用量{{item.single_dose}}{{item.single_dose_unit}}</span>

                            <span v-if="item.parent_id == 0">{{item.delivery_way}}</span>
                            <span v-if="item.parent_id == 0">{{item.execution_frequency}}</span>
                            <span v-if="item.parent_id == 0 && item.remark.length > 0">({{item.remark}})</span>
                        </div>
                        <div class="lastAdviceOne" v-for="subdrug in item.children">
                            {{ subdrug.advice_name }}{{ subdrug.advice_desc }} {{ subdrug.drug_spec_unit }}
                            <span v-if="subdrug.prescribing_number">
                            {{ subdrug.prescribing_number
                            }}{{ subdrug.prescribing_number_unit }}
                            </span>
                            <span v-if="subdrug.single_dose">
                            {{ subdrug.single_dose }}
                            {{ subdrug.single_dose_unit }}
                            </span>
                        </div>
                    </div>

                </div>
                <div class="lastOrNextBox">
                    <div @click="openLast">上一方</div>
                    <div @click="openNext">下一方</div>
                </div>
            </div>
            <mt-datetime-picker
            ref="start_time_picker"
            type="datetime"
            @confirm="didSelectStartTime"
            v-model="start_time"
            class="startTimePicker datetimePicker"
            ></mt-datetime-picker>
    </div>
</template>

<script>
import { GetLastOrNextDoctorAdvice } from '@/api/advice'
import {parseTime} from '@/utils'
import {Toast} from 'vant'
import {uParseTime} from '@/utils/tools'
export default {
  props: {
    schedulePatient: {
      type: Object
    }
  },
  data () {
    return {
      start_time: new Date(),
      start_time_str: '',
      record_date: '',
      request_record_date: '',
      other_doctor_advices: [],
      template_id: 0
    }
  },
  created () {
    this.template_id = this.$store.getters.user.template_info.template_id
    this.record_date = parseTime(new Date(), '{y}-{m}-{d}')
    if (this.$store.getters.user.org.id == 9671 || this.$store.getters.user.org.id == 9675 || this.$store.getters.user.org.id == 10340) {
      var date = new Date()
      var year = date.getFullYear() // 获取完整的年份(4位)
      var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1).toString() : date.getMonth() + 1 // 获取当前月份(0-11,0代表1月)
      var day = date.getDate() < 10 ? '0' + date.getDate().toString() : date.getDate() // 获取当前日(1-31)

      if (this.schedulePatient.schedule_type == 1) {
        this.start_time_str = year + '-' + month + '-' + day + ' ' + '07:00' + ':00'
        this.start_time = year + '-' + month + '-' + day + ' ' + '07:00' + ':00'
      } else if (this.schedulePatient.schedule_type == 2) {
        this.start_time_str = year + '-' + month + '-' + day + ' ' + '9:00' + ':00'
        this.start_time = year + '-' + month + '-' + day + ' ' + '9:00' + ':00'
      } else if (this.schedulePatient.schedule_type == 3) {
        this.start_time_str = year + '-' + month + '-' + day + ' ' + '18:00' + ':00'
        this.start_time = year + '-' + month + '-' + day + ' ' + '18:00' + ':00'
      }
    }else if ( this.$store.getters.user.org.id == 9829) {
      var date = new Date()
      var year = date.getFullYear() // 获取完整的年份(4位)
      var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1).toString() : date.getMonth() + 1 // 获取当前月份(0-11,0代表1月)
      var day = date.getDate() < 10 ? '0' + date.getDate().toString() : date.getDate() // 获取当前日(1-31)

      if (this.schedulePatient.schedule_type == 1) {
        this.start_time_str = year + '-' + month + '-' + day + ' ' + '06:30' + ':00'
        this.start_time = year + '-' + month + '-' + day + ' ' + '06:30' + ':00'
      } else if (this.schedulePatient.schedule_type == 2) {
        this.start_time_str = year + '-' + month + '-' + day + ' ' + '11:00' + ':00'
        this.start_time = year + '-' + month + '-' + day + ' ' + '11:00' + ':00'
      } else if (this.schedulePatient.schedule_type == 3) {
        this.start_time_str = year + '-' + month + '-' + day + ' ' + '15:30' + ':00'
        this.start_time = year + '-' + month + '-' + day + ' ' + '15:30' + ':00'
      }
    }else if ( this.$store.getters.user.org.id == 10353) {
           var date = new Date()
           var year = date.getFullYear() // 获取完整的年份(4位)
           var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1).toString() : date.getMonth() + 1 // 获取当前月份(0-11,0代表1月)
           var day = date.getDate() < 10 ? '0' + date.getDate().toString() : date.getDate() // 获取当前日(1-31)

           if (this.schedulePatient.schedule_type == 1) {
             this.start_time_str = year + '-' + month + '-' + day + ' ' + '07:00' + ':00'
             this.start_time = year + '-' + month + '-' + day + ' ' + '07:00' + ':00'
           } else if (this.schedulePatient.schedule_type == 2) {
             this.start_time_str = year + '-' + month + '-' + day + ' ' + '12:00' + ':00'
             this.start_time = year + '-' + month + '-' + day + ' ' + '12:00' + ':00'
           } else if (this.schedulePatient.schedule_type == 3) {
             this.start_time_str = year + '-' + month + '-' + day + ' ' + '17:30' + ':00'
             this.start_time = year + '-' + month + '-' + day + ' ' + '17:30' + ':00'
           }
      } else {
      this.start_time_str = parseTime(this.start_time, '{y}-{m}-{d} {h}:{i}') + ':00'
    }

    this.request_record_date = this.record_date
    this.isLastOrNextVisible = true
    let params = {
      patient_id: this.$route.query.patient_id,
      record_time: this.request_record_date,
      type: 1
    }

    this.GetLastOrNextDoctorAdvice(params)
  },
  methods: {
    backAction: function () {
      this.$emit('back')
    },
    didSelectStartTime: function (time) {
      this.start_time_str = parseTime(time, '{y}-{m}-{d} {h}:{i}') + ':00'
    },
    selectStartTimeAction: function () {
      this.$refs.start_time_picker.open()
    },
    GetLastOrNextDoctorAdvice (params) {
      GetLastOrNextDoctorAdvice(params).then(response => {
        if (response.data.state == 1) {
          this.other_doctor_advices = []
          var doctor_advices = response.data.data.advices
          this.other_doctor_advices = doctor_advices
          this.request_record_date = uParseTime(this.other_doctor_advices[0].record_date, '{y}-{m}-{d}')
          this.sch = response.data.data.schedule
        } else {
          Toast.fail(response.data.msg)
        }
      })
    },
    submitAction () {
      console.log("hhhhhhhhhhhhh",this.other_doctor_advices)
      this.$emit('finish', this.other_doctor_advices, this.start_time_str)
    },
    getContent: function () {
      if (this.sch != null) {
        var date = uParseTime(this.sch.schedule_date, '{y}-{m}-{d}')
        var week = ''
        var sch_type = ''

        switch (this.sch.schedule_week) {
          case 1:
            week = '周一'
            break
          case 2:
            week = '周二'

            break
          case 3:
            week = '周三'

            break
          case 4:
            week = '周四'

            break
          case 5:
            week = '周五'

            break
          case 6:
            week = '周六'

            break
          case 7:
            week = '周日'
            break
        }
        switch (this.sch.schedule_type) {
          case 1:
            sch_type = '上午'
            break
          case 2:
            sch_type = '下午'

            break
          case 3:
            sch_type = '晚上'

            break
        }
        return date + '(' + week + '/' + sch_type + ')'
      } else {
        return ''
      }
    },
    openLast () {
      let params = {
        patient_id: this.$route.query.patient_id,
        record_time: this.request_record_date,
        type: 1
      }

      this.GetLastOrNextDoctorAdvice(params)
    },
    openNext () {
      let params = {
        patient_id: this.$route.query.patient_id,
        record_time: this.request_record_date,
        type: 2
      }
      this.GetLastOrNextDoctorAdvice(params)
    }
  }
}
</script>

<style lang="scss" scoped>
.newDoctorAdvice{
    height: 100%;
    display: flex;
    flex-direction: column;
    >:last-child{
        flex: 1;
        overflow-y: auto;
        padding: .625rem;
        display: flex;
        flex-direction: column;
        font-size: .875rem;
        color: #323233;
    }
}
.newDoctorAdviceTitle{
    height: 3.125rem;
    display: flex;
    justify-content: space-between;
    padding: 0 .875rem;
    align-items: center;
    font-size: 1rem;
    background: #fff;
}
.adviceTitle{
    display: flex;
    padding: .5rem .875rem;
    font-size: .875rem;
    justify-content: space-between;
    border-top: 1px solid #e5e5e5;
    border-bottom: 1px solid #e5e5e5;
    color: #323233;
    >div:first-child{
        display: flex;
        align-items: center;
    }
    >div:last-child{
        display: flex;
        align-items: center;
    }
}
.timeSpan{
    padding: .3125rem;
    border: 1px solid #e5e5e5;
    border-radius: .25rem;
}
.lastAdviceOne{
    margin-bottom: .625rem;
    color: #323233;
}
.lastOrNextBox{
    width: 100%;
    display:flex;
    justify-content: space-between;
    margin:0 auto 10px;
    >div{
        text-align: center;
        height: 2.5rem;
        width: 48%;
        line-height: 2.5rem;
        background: #409eff;
        color:#fff;
        border-radius: 4px;
    }
}
</style>

<style lang="scss">
.newDoctorAdvice{
    .v-modal{
        z-index: 4009 !important;
    }
    .startTimePicker{
        z-index: 4100 !important;
    }
    .datetimePicker{
        .picker-item{
            font-size: .875rem !important;
            height: 44px;
            line-height: 44px;

        }
        .mint-datetime-action{
            font-size: .9375rem !important;
        }

    }
}
.el-autocomplete-suggestion{
    z-index: 4011 !important;
}
.inputCell{
    .el-input{
        font-size: 1rem !important;
    }
}
</style>
