<template>
  <div class="newDoctorManagement">
    <div style="height:100%;display:flex;flex-direction: column;">
      <div class="toolBox">
        <div class="toolOne" @click="typeShow = true">
          <div style="display: flex;align-items: center;">
            {{ type }}
            <van-icon name="arrow-down" />
          </div>
        </div>
        <div class="toolOne" @click="startShow = true">
          <div style="display: flex;align-items: center;">
            {{ startTime }}
            <van-icon name="arrow-down" />
          </div>
        </div>
        <div class="toolOne" @click="endShow = true">
          <div style="display: flex;align-items: center;">
            {{ endTime }}
            <van-icon name="arrow-down" />
          </div>
        </div>
      </div>

      <div class="adviceBox" v-if="doctorAdvice.length > 0">
        <van-list
         v-model="loading"
         :finished="finished"
         @load="onLoad"
         finished-text="没有更多了"
         :offset="offset"
         >

          <div class="adviceOne" v-for="(item,index) in this.doctorAdvice" :key="index" v-show="showOne">
            <div class="adviceTitle">
              <p>{{getTime(item.start_time)}}</p>
              <van-icon class="ellipsis" name="ellipsis" @click="Delete(item.start_time, index, item.child[0].execution_staff)" />
            </div>

         <div v-for="(it,i) in item.child.slice(0,3)" :key="i" @click="toDoctorAdviceDetail(item.start_time)">
            <div class="statOrder"  v-if="it.advice_type == 1 && it.parent_id == 0">
              <div class="statOrderTitle">
                <span v-if="i<1">长期医嘱</span>
                <span v-if="i<1" style="margin-left:1rem">{{getTimes(it.start_time)}}</span>
              </div>
              <div class="orderContent">
                <p> {{ it.advice_name }} {{ it.advice_desc
                     }}{{ it.drug_spec_unit }} {{ it.prescribing_number
                    }}{{ it.prescribing_number_unit }} {{ it.single_dose
                    }}{{ it.single_dose_unit }} {{ it.delivery_way }}
                    {{ it.execution_frequency }}
                     <span v-if="it.frequency_type == 1" style="color:red">每次必推</span>
                     <span v-if="it.frequency_type == 2" style="color:red">{{it.day_count}}天/1次</span>
                     <span v-if="it.frequency_type == 3" style="color:red">每周{{it.week_day}}</span>
                    </p>

                <div v-for="(i, index) in childList" :key="index">
                 <p v-if="i.parent_id == it.id">▲
                   {{ i.advice_name }}{{ i.advice_desc
                      }}{{ i.drug_spec_unit }} {{ i.prescribing_number
                      }}{{ i.prescribing_number_unit }} {{ i.single_dose
                      }}{{ i.single_dose_unit }}
                     {{ i.delivery_way }}
                    {{ i.execution_frequency }}
                 </p>
                </div>
                <div class="doctorBox">
                  <p>开嘱医生：{{ it.user_name }}</p>
                  <p>执行护士：{{ getDoctor(it.execution_staff) }}</p>
                  <p>核对护士：{{ getDoctor(it.checker) }}</p>
                </div>
              </div>
            </div>

            <div class="statOrder" v-if="(it.advice_type ==2 || it.advice_type == 3)  && it.parent_id == 0">
              <div v-if="i<1">
               <span style="color:#313234;font-size:0.875rem">透析器/灌流器:{{it.dialyzer_perfusion_apparatus}}
                 {{it.dialysis_dialyszers}}  <span v-if="it.dialysis_irrigation!=''">/</span>{{it.dialysis_irrigation}}
                </span><br>
               <span style="color:#313234;font-size:0.875rem">抗凝剂:
                <span v-if="it.anticoagulant == 1">无肝素</span>
                <span v-if="it.anticoagulant == 2">普通肝素</span>
                <span v-if="it.anticoagulant == 3">低分子肝素</span>
                <span v-if="it.anticoagulant == 4">阿加曲班</span>
                <span v-if="it.anticoagulant == 5">枸橼酸钠</span>
                <span v-if="it.anticoagulant == 6">低分子肝素钙</span>
                <span v-if="it.anticoagulant == 7">低分子肝素钠</span>
                <span v-if="it.anticoagulant == 8">依诺肝素</span>
                <span v-if="it.anticoagulant == 9">达肝素</span>
                <span v-if="it.anticoagulant == 10">体外抗凝</span>
                <span v-if="it.anticoagulant == 11">那屈肝素</span>
                <span v-if="it.anticoagulant == 12">无抗凝剂</span>
                 (
                 首剂：<span>{{ it.anticoagulant_shouji }}</span>
                      <span v-if="it.anticoagulant == 1">mg</span>
                      <span v-if="it.anticoagulant == 2">iu</span>
                      <span v-if="it.anticoagulant == 3">iu</span>
                      <span v-if="it.anticoagulant == 4">mg</span>
                      <span v-if="it.anticoagulant == 5">mg</span>
                      <span v-if="it.anticoagulant == 6">iu</span>
                      <span v-if="it.anticoagulant == 7">iu</span>
                      <span v-if="it.anticoagulant == 8">iu</span>
                      <span v-if="it.anticoagulant == 9">iu</span>
                      <span v-if="it.anticoagulant == 10">mg</span>
                 维持：
                    <span>{{ it.anticoagulant_weichi }}</span>
                    <span v-if="it.anticoagulant == 1">mg/h</span>
                    <span v-if="it.anticoagulant == 2">iu/h</span>
                    <span v-if="it.anticoagulant == 3">iu/h</span>
                    <span v-if="it.anticoagulant == 4">mg/h</span>
                    <span v-if="it.anticoagulant == 5">ml/h</span>
                    <span v-if="it.anticoagulant == 6">iu/h</span>
                    <span v-if="it.anticoagulant == 7">iu/h</span>
                    <span v-if="it.anticoagulant == 8">iu/h</span>
                    <span v-if="it.anticoagulant == 9">iu/h</span>
                    <span v-if="it.anticoagulant == 10">mg/h</span>
                  总量：
                    <span>{{ it.anticoagulant_zongliang }}</span>
                    <span v-if="it.anticoagulant == 1">mg</span>
                    <span v-if="it.anticoagulant == 2">iu</span>
                    <span v-if="it.anticoagulant == 3">iu</span>
                    <span v-if="it.anticoagulant == 4">mg</span>
                    <span v-if="it.anticoagulant == 5">mg</span>
                    <span v-if="it.anticoagulant == 6">iu</span>
                    <span v-if="it.anticoagulant == 7">iu</span>
                    <span v-if="it.anticoagulant == 8">iu</span>
                    <span v-if="it.anticoagulant == 9">iu</span>
                    <span v-if="it.anticoagulant == 10">mg</span>
                   )</span>
              </div>
              <div class="longOrderTitle">
                <span v-if="i<1">临时医嘱</span>
                <span v-if="i<1" style="margin-left:1rem">{{getTimes(it.start_time)}}</span>
              </div>
              <div class="orderContent" v-if="i<4">
                <p v-if="it.parent_id == 0">{{ it.advice_name }} {{ it.advice_desc
                  }}{{ it.drug_spec_unit }} {{ it.prescribing_number
                    }}{{ it.prescribing_number_unit }} {{ it.single_dose
                    }}{{ it.single_dose_unit }} {{ it.delivery_way }}
                   {{ it.execution_frequency }}</p>
                <div v-for="(i, index) in childList" :key="index">
                  <p v-if="i.parent_id == it.id">▲ {{ i.advice_name }} {{ i.advice_desc
                      }}{{ i.drug_spec_unit }} {{ i.prescribing_number
                      }}{{ i.prescribing_number_unit }} {{ i.single_dose
                      }}{{ i.single_dose_unit }}
                     {{ i.delivery_way }}
                    {{ i.execution_frequency }}</p>
                </div>
              </div>
               <div class="doctorBox">
                  <p>开嘱医生：{{ it.user_name }}</p>
                  <p>执行护士：{{ getDoctor(it.execution_staff) }}</p>
                  <p>核对护士：{{ getDoctor(it.checker) }}</p>
                </div>
            </div>

            <div class="statOrder" v-if="it.advice_type ==4 && it.parent_id == 0">
              <div class="longOrderTitle">
                <span style="color:#313234" v-if="i<1">普通医嘱</span>
                <span v-if="i<1" style="margin-left:1rem;color:#313234">{{getTimes(it.start_time)}}</span>
              </div>
              <div class="orderContent" v-if="i<4">
                <p v-if="it.parent_id == 0">{{ it.advice_name }} {{ it.advice_desc
                  }}{{ it.drug_spec_unit }} {{ it.prescribing_number
                    }}{{ it.prescribing_number_unit }} {{ it.single_dose
                    }}{{ it.single_dose_unit }} {{ it.delivery_way }}
                   {{ it.execution_frequency }}</p>
                <div v-for="(i, index) in childList" :key="index">
                  <p v-if="i.parent_id == it.id">▲ {{ i.advice_name }} {{ i.advice_desc
                      }}{{ i.drug_spec_unit }} {{ i.prescribing_number
                      }}{{ i.prescribing_number_unit }} {{ i.single_dose
                      }}{{ i.single_dose_unit }}
                     {{ i.delivery_way }}
                    {{ i.execution_frequency }}</p>
                </div>
              </div>
               <div class="doctorBox">
                  <p>开嘱医生：{{ it.user_name }}</p>
                  <p>执行护士：{{ getDoctor(it.execution_staff) }}</p>
                  <p>核对护士：{{ getDoctor(it.checker) }}</p>
                </div>
            </div>
         </div>

            <div class="all" @click="toDoctorAdviceDetail(item.start_time)">全部</div>
          </div>
        </van-list>
      </div>

      <div class="noimgBox" v-else>
        <img src="../../../assets/images/none.png" alt />
      </div>
    </div>

    <!-- 弹出层 -->
    <div>
      <van-popup
        class="newPopup"
        get-container="#app"
        v-model="typeShow"
        position="bottom"
        :style="{ height: '40%' }"
      >
        <van-picker
          show-toolbar
          :columns="columns"
          @cancel="onCancel"
          @confirm="onConfirm"
        />
      </van-popup>

      <van-popup
        class="newPopup"
        get-container="#app"
        v-model="startShow"
        position="bottom"
        :style="{ height: '40%' }"
      >
        <van-datetime-picker
          v-model="currentDate"
          type="date"
          :min-date="minDate"
          :max-date="maxDate"
          @confirm="getstartTime"
          @cancel="startShow = false"
        />
      </van-popup>
      <van-popup v-model="endShow" class="newPopup" get-container="#app" position="bottom" :style="{ height: '40%' }">
        <van-datetime-picker
          v-model="currentDate"
          type="date"
          :min-date="minDate"
          :max-date="maxDate"
          @confirm="getstartTime2"
          @cancel="endShow = false"
        />
      </van-popup>
      <van-action-sheet
        class="newPopup"
        get-container="#app"
        v-model="newShow"
        :actions="actions"
        cancel-text="取消"
        @cancel="onCancel"
        @select="toDelete"
      />
      <van-action-sheet
        class="newPopup"
        get-container="#app"
        v-model="newChildShow"
        :actions="actions1"
        cancel-text="取消"
        @cancel="onCancel"
        @select="toChildDelete"
      />
      <van-action-sheet
        class="newPopup"
        get-container="#app"
        v-model="newChildShow"
        :actions="actions2"
        cancel-text="取消"
        @cancel="onCancel"
        @select="toChildDelete"
      />
    </div>
  </div>
</template>

<script>
import {
  getNewPatientId,
  getDoctorAdvices,
  getPatientDetail,
  DeleteManagementTwo
} from '@/api/patient/patient'
import { uParseTime } from '@/utils/tools'
import { Dialog } from 'vant'
import { GetAllStaff} from '@/api/admin_user'
const moment = require('moment')
export default {
  props: {
    active: Number
  },
  data () {
    return {
      docShow: false,
      loading: false,
      finished: false,
      newShow: false,
      newChildShow: false,
      typeShow: false,
      startShow: false,
      endShow: false,
      type: '全部',
      columns: ['全部', '长期医嘱', '临时医嘱', '普通医嘱'],
      startTime: '请选择',
      endTime: '请选择',
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(),
      actions: [{ name: '查看全部' }, { name: '全部删除' }],
      actions1: [{ name: '删除' }],
      actions2: [{ name: '删除' }],
      form: {
        type: 0,
        limit: 5,
        page: 1
      },
      total: '',
      showOne: true,
      showTwo: false,
      doctorAdvice: [],
      childList: [],
      patient_id: '',
      doctor: [],
      id: 0,
      index: 0,
      staff: 0,
      childId: 0,
      state: 0,
      list: [],
      date: 0,
      patient_name: '',
      offset: 20
    }
  },
  methods: {
    onCancel () {
      this.typeShow = false
    },
    onConfirm (value) {
      this.form.page = 1
      this.type = value
      this.typeShow = false
      this.doctorAdvice = []
      this.getDoctorAdvices(
        this.patient_id,
        this.type,
        this.startTime,
        this.endTime,
        this.form.limit,
        this.form.page
      )
      // this.onLoad();
    },
    getstartTime (value) {
      this.form.page = 1
      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.startTime = `${year}-${month}-${day}`
      this.doctorAdvice = []
      this.getDoctorAdvices(
        this.patient_id,
        this.type,
        this.startTime,
        this.endTime,
        this.form.limit,
        this.form.page
      )
      this.startShow = false
      // this.onLoad();
    },
    getstartTime2 (value) {
      this.form.page = 1
      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.endTime = `${year}-${month}-${day}`

      this.doctorAdvice = []
      this.getDoctorAdvices(
        this.patient_id,
        this.type,
        this.startTime,
        this.endTime,
        this.form.limit,
        this.form.page
      )
      this.endShow = false
      // this.onLoad();
    },
    getDoctorAdvices (patientid) {
      this.loading = true
      if (this.type === '全部') {
        this.form.type = 0
      }
      if (this.type === '长期医嘱') {
        this.form.type = 1
      }
      if (this.type === '临时医嘱') {
        this.form.type = 3
      }
      if (this.type === '普通医嘱') {
        this.form.type = 4
      }
      getDoctorAdvices(
        patientid,
        this.form.type,
        this.startTime,
        this.endTime,
        this.form.limit,
        this.form.page
      ).then(response => {
        if (response.data.state === 1) {
          var advice = response.data.data.advice
          // console.log("advice", advice);
          var one = response.data.data.one
          //  console.log("one", one);
          var total = response.data.data.total
          this.total = total
          // console.log("total", total);
          let dataInfo = {}
          one.forEach((item, index) => {
            let { start_time } = item
            if (!dataInfo[start_time]) {
              dataInfo[start_time] = {
                start_time,
                child: []
              }
            }
          })
          let list = Object.values(dataInfo)
          // console.log("list", list);
          list.map(item => {
            for (let i = 0; i < advice.length; i++) {
              if (
                item.start_time === advice[i].start_time &&
                advice[i].parent_id === 0
              ) {
                item.child.push(advice[i])
              }
            }
          })

          let objarr = []
          for (let i = 0; i < advice.length; i++) {
            if (advice[i].parent_id != 0) {
              objarr.push(advice[i])
            }
          }
          this.childList = objarr
          let arr = this.doctorAdvice
          arr.push(...list.reverse())

          this.doctorAdvice = arr

          this.loading = false
        }
      })
    },
    getPatientDetail (patientid) {
      getPatientDetail(patientid).then(response => {
        if (response.data.state === 1) {
          var patientDetail = response.data.data.patientDetail
          console.log('patientDetail', patientDetail)
          if (patientDetail.blood_patients == 0) {
            this.doctorAdvice = []
            this.showOne = false
            this.showTwo = true
          }
          if (patientDetail.blood_patients == 1) {
            this.showOne = true
            this.showTwo = false
          }
          this.patientName = patientDetail.name
        }
      })
    },
    getTime (time) {
      // return uParseTime(time, "{y}-{m}-{d} {h}:{i}:{s}");
      return uParseTime(time, '{y}-{m}-{d}')
    },
    getTimes (time) {
      return uParseTime(time, '{h}:{i}')
    },
    onLoad () {
      // setTimeout(() => {
      this.form.page++
      // console.log("page", this.form.page);
      if (this.form.page <= Math.ceil(this.total / 5)) {
        this.getDoctorAdvices(
          this.patient_id,
          this.form.type,
          this.startTime,
          this.endTime,
          this.form.limit,
          this.form.page
        )
      } else {
        this.loading = false
        this.finished = true
      }
      // }, 5000);
    },

    toDoctorAdviceDetail (id) {
      var patientid = this.$route.query.patient_id
      this.$router.push(
        '/doctoradvice?id=' +
          id +
          '&patientid=' +
          this.patient_id +
          '&date=' + this.date + '&patient_name=' + this.patient_name + '&patient_id=' + patientid
      )
    },
    getDoctor (id) {
      for (let i = 0; i < this.doctor.length; i++) {
        if ((this.doctor[i].user_id == id)) {
          return this.doctor[i].user_name
        }
      }
    },
    Delete (id, index, staff) {
      this.id = id
      this.index = index
      this.staff = staff
      this.newShow = true
    },
    // 长期
    DeleteOne (id) {
      alert(id)
    },
    DeleteChild (id, index, state) {
      this.id = id
      this.index = index
      this.state = state
      this.newChildShow = true
    },
    // 临时
    DeleteTemporary (id) {
      alert(id)
    },
    toDelete (val) {
      if (val.name == '查看全部') {
        var patientid = this.$route.query.patient_id
        this.$router.push(
          '/doctoradvice?id=' +
            this.id +
            '&patientid=' +
            this.patient_id +
            '&date=' + this.date + '&patient_name=' + this.patient_name + '&patient_id=' + patientid
        )
      }
      if (val.name == '全部删除') {
        var patient_id = this.$route.query.patient_id
        this.DeleteManagement(this.id, patientid, this.index, this.staff)
      }
    },
    toChildDelete (val) {
      if (val.name == '删除') {
        this.DeleteChild(this.id, this.index, this.state)
      }
    },
    // 删除整组医嘱
    DeleteManagement (id, patientid, index, staff) {
      if (staff > 0) {
        this.$toast('医嘱已执行,无法删除')
        return false
      }
      Dialog.confirm({
        title: '删除提示!',
        message: '确认删除该条信息吗?,删除后将无法恢复!'
      }).then(() => {
        DeleteManagementTwo(id, patientid).then(response => {
          if (response.data.state === 1) {
            var msg = response.data.data.msg
            this.doctorAdvice.splice(index, 1)
            this.newShow = false
          }
        })
      })
    },
    DeleteChild (id, index, state) {
      if (state == 1) {
        this.$toast('医嘱已执行,无法删除')
        return false
      }
      Dialog.confirm({
        title: '删除提示!',
        message: '确认删除该条信息吗?,删除后将无法恢复!'
      }).then(() => {
        DeleteChild(id).then(response => {
          if (response.data.state === 1) {
            var msg = response.data.data.msg

            this.childList.splice(index, 1)
            this.newShow = false
          }
        })
      })
    },
    getNewPatientId (id) {
      getNewPatientId(id).then(response => {
        if (response.data.state == 1) {
          var patient = response.data.data.patient
          this.getDoctorAdvices(patient.id)
          this.getPatientDetail(patient.id)
          this.patient_id = patient.id
        }
      })
    },
    GetAllStaff () {
      GetAllStaff().then(response => {
        if (response.data.state === 1) {
          var admins = response.data.data.admins
          console.log('admins', admins)
          let obj = {}
          let newArr = []
          newArr = admins.reduce((item, next) => {
            obj[next.user_id] ? ' ' : obj[next.user_id] = true && item.push(next)
            return item
          }, [])
          console.log('newArr', newArr)
          this.doctor = newArr
        }
      })
    }

  },
  created () {
    var patientid = this.$route.query.patient_id
    this.getNewPatientId(patientid)
    localStorage.setItem('docActive', this.active)
    this.date = this.$route.query.date
    this.patient_name = this.$route.query.patient_name
    this.GetAllStaff()
  },
  watch: {
    $route (to, from) {
      var patientid = this.$route.query.patient_id
    }
  }
}
</script>

<style lang="scss" scoped>
.newDoctorManagement {
  height: 100%;
  overflow-y: auto;
  background: #fff;
  padding-top:2.75rem;
  overflow-x: hidden;
  .toolBox {
    display: flex;
    height: 3.125rem;
    justify-content: space-around;
    align-items: center;
    .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;
    }
  }
  .adviceBox {
    padding: 0 1.125rem;
    flex: 1;
    overflow-y: auto;
  }
  .adviceOne {
    box-shadow: 0px 1px 0px 0px rgba(0, 0, 0, 0.1);
    padding-bottom: 0.625rem;
  }
  .adviceTitle {
    padding-top: 0.625rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    p {
      color: #000000;
      font-weight: bold;
      font-size: 0.9375rem;
    }
  }
  .ellipsis {
    font-size: 1.25rem;
    color: #cccccc;
  }
  .statOrderTitle {
    color: #5b98ff;
    font-size: 0.8125rem;
    font-weight: bold;
    margin: 0.625rem 0;
  }
  .longOrderTitle {
    color: #ff964a;
    font-size: 0.8125rem;
    font-weight: bold;
    margin: 0.625rem 0;
  }
  .orderContent {
    font-size: 0.875rem;
    color: rgba(49, 50, 52, 1);
    p {
      line-height: 1.125rem;
    }
  }
  .doctorBox {
    font-size: 0.75rem;
    color: rgba(152, 152, 152, 1);
    line-height: 1.125rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 0.625rem;
  }
  .all {
    font-size: 0.8125rem;
    color: #5b98ff;
    margin-top: 0.625rem;
  }
  .noimgBox {
    margin-top: 40%;
    img {
      width: 9.375rem;
      height: 9.375rem;
      margin: 0 auto;
      display: block;
    }
  }
  .van-popup {
    border-radius: 0;
    transform: translate3d(0, 0, 0);
    top: initial;
  }

}
::-webkit-scrollbar {
  width: 0;
}

</style>


