<template>
  <div class="page_allDoctorManagement">
    <van-sticky>
      <div class="allDoctorManagementTitle">
        <i class="iconfont icon-zuojiantou jiantou" @click="$router.go(-1)"></i>
        <span class="titleName">{{ patient_name }}的医嘱</span>
      </div>
    </van-sticky>
    <div class="orderBox">
      <div
        class="orderBoxOne"
        v-for="(item, index) in doctorAdvicedetail"
        :key="index"
      >
        <div v-for="(it, i) in item.child" :key="i">
          <div style="display:flex;justify-content:space-between;align-items:center">
            <p class="time" v-if="i<1">
              {{ it.advice_date ? getTime(it.advice_date) : "" }}
            </p>
            <span v-if="i<1"><van-icon class="ellipsis" name="ellipsis" @click="DeleteFive(item.start_time,item.child[0].execution_staff,index)" /></span>
          </div>
          <div class="statOrder">
            <div v-if="it.advice_type == 1 && it.parent_id == 0">
              <div class="statOrderTitle">
                <span v-if="i<1">长期医嘱</span>
                <span style="margin-left:1rem" v-if="i<1">
                  {{ getTimes(it.start_time) }}
                </span>
              </div>
              <div class="orderContent">
                <div style="display:flex;justify-content:space-between">
                  <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>
                  <span><van-icon
                    class="ellipsis"
                    name="ellipsis"
                    @click="toDelete(it.id,it.execution_state,i)"
                  />
                  </span>
                </div>

                <div v-for="(i, index) in childList" :key="index" >
                  <div style="display:flex;justify-content:space-between" >
                    <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>
                    <span v-if="i.parent_id == it.id"><van-icon
                      class="ellipsis"
                      name="ellipsis"
                      @click="Delete(i.id,i.execution_state,index)"
                    />
                    </span>
                  </div>
                </div>

                <div class="doctorBox">
                  <p>开嘱医生：{{ getDoctor(it.advice_doctor)}}</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 class="longOrderTitle">
                <span v-if="i<1">临时医嘱</span>
                <span style="margin-left:1rem" v-if="i<1">
                  {{ getTimes(item.child[0].start_time) }}
                </span>
              </div>
              <div class="orderContent">
                <div style="display:flex;justify-content:space-between">
                  <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 }}
                  </p>
                  <van-icon
                    class="ellipsis"
                    name="ellipsis"
                    @click="DeleteTwo(it.id,it.execution_state,index)"
                  />
                </div>
                <div v-for="(i, index) in childList" :key="index">
                  <div style="display:flex;justify-content:space-between">
                    <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>
                    <van-icon
                      class="ellipsis"
                      name="ellipsis"
                      @click="DeleteThree(i.id,i.execution_state,index)"
                    />
                  </div>
                </div>
              </div>
              <div
                class="doctorBox"
              >
                <p>开嘱医生：{{ getDoctor(it.advice_doctor) }}</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 v-if="i<1">普通医嘱</span>
                <span style="margin-left:1rem" v-if="i<1">
                  {{ getTimes(item.child[0].start_time) }}
                </span>
              </div>
              <div class="orderContent">
                <div style="display:flex;justify-content:space-between">
                  <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 }}
                  </p>
                  <van-icon
                    class="ellipsis"
                    name="ellipsis"
                    @click="DeleteTwo(it.id,it.execution_state,index)"
                  />
                </div>
                <div v-for="(i, index) in childList" :key="index">
                  <div style="display:flex;justify-content:space-between">
                    <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>
                    <van-icon
                      class="ellipsis"
                      name="ellipsis"
                      @click="DeleteThree(i.id,i.execution_state,index)"
                    />
                  </div>
                </div>
              </div>
              <div
                class="doctorBox"
              >
                <p>开嘱医生：{{ getDoctor(it.advice_doctor) }}</p>
                <p>执行护士：{{ getDoctor(it.execution_staff) }}</p>
                <p>核对护士：{{ getDoctor(it.checker) }}</p>
              </div>
            </div>
          </div>
        </div>

      </div>

      <van-action-sheet
        v-model="newShow"
        :actions="actions1"
        cancel-text="取消"
        @cancel="onCancel"
        @select="onSelect"
      />

      <van-action-sheet
        v-model="newShowOne"
        :actions="actions2"
        cancel-text="取消"
        @cancel="onCancelOne"
        @select="onSelectOne"
      />

      <van-action-sheet
        v-model="newShowTwo"
        :actions="actions3"
        cancel-text="取消"
        @cancel="onCancelTwo"
        @select="onSelectTwo"
      />
      <van-action-sheet
        v-model="newShowThree"
        :actions="actions3"
        cancel-text="取消"
        @cancel="onCancelThree"
        @select="onSelectThree"
      />

      <van-action-sheet
        v-model="newShowFour"
        :actions="actions4"
        cancel-text="取消"
        @cancel="onCancelFour"
        @select="onSelectFour"
      />
    </div>
  </div>
</template>
<script>
import { setRem, setHeight } from '@/libs/functionRem'
import {
  GetDoctorAdviceDetail,
  getAllDoctor,
  DeleteChild,
  DeletePatient,
  DeleteManagement
} from '@/api/patient/patient'
import { Dialog } from 'vant'
import { uParseTime } from '@/utils/tools'
export default {
  data () {
    return {
      id: 0,
      active: 0,
      childList: [],
      doctorAdvicedetail: [],
      patient_name: '',
      patient_date: '',
      parentData: {},
      doctor: [],
      newShow: false,
      actions1: [{ name: '删除' }],
      actions2: [{ name: '删除' }],
      actions3: [{ name: '删除' }],
      actions4: [{ name: '全部删除' }],
      newShow: false,
      newShowOne: false,
      newShowTwo: false,
      newShowThree: false,
      newShowFour: false,
      index: 0,
      state: 0
    }
  },
  methods: {
    toReturn () {
      this.$router.push(
        '/patientdetail?patientid=' + this.id + '&active=' + this.active
      )
    },
    GetDoctorAdviceDetail (date, id) {
      GetDoctorAdviceDetail(date, id).then(response => {
        if (response.data.state === 1) {
          var doctorAdvicedetail = response.data.data.doctorAdvicedetail
          // console.log('doctorAdvicedetail', doctorAdvicedetail)
          this.patient_name = doctorAdvicedetail[0].name
          let objarr = []
          for (let i = 0; i < doctorAdvicedetail.length; i++) {
            if (doctorAdvicedetail[i].parent_id != 0) {
              objarr.push(doctorAdvicedetail[i])
            }
          }
          this.childList = objarr
          // console.log('childList', this.childList)
          let dataInfo = {}
          doctorAdvicedetail.forEach((item, index) => {
            let { start_time } = item
            if (!dataInfo[start_time]) {
              dataInfo[start_time] = {
                start_time,
                child: []
              }
            }
            dataInfo[start_time].child.push(item)
          })
          let list = Object.values(dataInfo) // list 转换成功的数据
          let arr = []

          // console.log('list是 ', list)
          this.doctorAdvicedetail = list
        }
      })
    },
    getTime (time) {
      // return uParseTime(time, "{y}-{m}-{d} {h}:{i}:{s}");
      return uParseTime(time, '{y}-{m}-{d}')
    },
    getTimes (time) {
      return uParseTime(time, '{h}:{i}')
    },
    getAllDoctor () {
      getAllDoctor().then(response => {
        if (response.data.state === 1) {
          var doctor = response.data.data.doctor
          // console.log('doctor', doctor)
          this.doctor = doctor
        }
      })
    },
    getDoctor (id) {
      for (let i = 0; i < this.doctor.length; i++) {
        if ((this.doctor[i].admin_user_id == id)) {
          return this.doctor[i].user_name
        }
      }
    },
    DeleteFive (id, state, index) {
      this.id = id
      this.state = state
      this.index = index
      this.newShowFour = true
    },
    toDelete (id, state, index) {
      this.id = id
      this.state = state
      this.index = index
      this.newShow = true
    },
    Delete (id, state, index) {
      this.id = id
      this.state = state
      this.index = index
      this.newShowOne = true
    },
    DeleteTwo (id, state, index) {
      this.id = id
      this.state = state
      this.index = index
      this.newShowTwo = true
    },
    DeleteThree (id, state, index) {
      this.id = id
      this.state = state
      this.index = index
      this.newShowThree = true
    },
    onCancel () {
      this.newShow = false
    },
    onSelect (val) {
      if (val.name === '删除') {
        this.DeletePatient(this.id, this.state, this.index)
      }
    },
    onCancelOne () {
      this.newShowOne = false
    },
    onSelectOne (val) {
      if (val.name === '删除') {
        this.DeleteChild(this.id, this.index, this.state)
      }
    },
    onSelectTwo (val) {
      if (val.name === '删除') {
        this.DeletePatient(this.id, this.state, this.index)
      }
      this.newShowTwo = false
    },
    onCancelTwo () {
      this.newShowTwo = false
    },
    onCancelThree () {
      this.newShowThree = false
    },
    onSelectThree () {
      this.DeleteChild(this.id, this.index, this.state)
    },
    onSelectFour (val) {
      var patientid = this.$route.query.patientid
      if (val.name === '全部删除') {
        this.DeleteManagement(this.id, patientid, this.staff, this.index)
      }
    },
    onCancelFour () {
      this.newShowFour = false
    },
    DeletePatient (id, index, state) {
      if (state == 1) {
        this.$toast('医嘱已执行,无法删除')
        return false
      }
      Dialog.confirm({
        title: '删除提示!',
        message: '确认删除该条信息吗?,删除后将无法恢复!'
      }).then(() => {
        DeletePatient(id).then(response => {
          if (response.data.state === 1) {
            var msg = response.data.data.msg
            this.GetDoctorAdviceDetail(this.id)
            this.doctorAdvicedetail.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.newShowOne = false
            this.newShowThree = false
          }
        })
      })
    },
    DeleteManagement (id, patientid, index, staff) {
      if (staff > 0) {
        this.$toast('医嘱已执行,无法删除')
        return false
      }
      Dialog.confirm({
        title: '删除提示!',
        message: '确认删除该条信息吗?,删除后将无法恢复!'
      }).then(() => {
        DeleteManagement(id, patientid).then(response => {
          if (response.data.state === 1) {
            var msg = response.data.data.msg
            this.doctorAdvicedetail.splice(index, 1)
            this.newShowFour = false
          }
        })
      })
    }
  },
  created () {
    setRem()
    setHeight()
    var id = this.$route.query.id
    var patientid = this.$route.query.patientid
    this.id = patientid
    this.GetDoctorAdviceDetail(id, patientid)
    this.active = parseInt(this.$route.query.active)
    this.getAllDoctor()
    var docActive = localStorage.getItem('docActive', this.active)
  }
}
</script>
<style lang="scss" scoped>
.page_allDoctorManagement {
  .allDoctorManagementTitle {
    background: #fff;
    padding: 0 1.125rem;
    height: 3.125rem;
    display: flex;
    align-items: center;
  }
  .jiantou {
    font-size: 1.5rem;
    font-weight: 600;
    margin-right: 6.5rem;
  }
  .titleName {
    font-size: 1.125rem;
    font-weight: 600;
  }
  .orderBox {
    padding: 0 1.125rem;
    margin-bottom: 3.125rem;
  }
  .orderBoxOne {
    box-shadow: 0px 1px 0px 0px rgba(0, 0, 0, 0.1);
    padding-bottom: 1.25rem;
    .time {
      font-size: 1.125rem;
      margin-top: 0.625rem;
      font-weight: bold;
    }
  }
  .adviceTitle {
    padding-top: 0.625rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    p {
      color: #000000;
      font-weight: bold;
      font-size: 0.9375rem;
    }
  }
  .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;
  }
}
::-webkit-scrollbar {
  width: 0;
}
</style>
