<template>
  <div class="newRescueRecord">
    <div>
       <div class="toolBox">
        <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 class="toolOne" @click="doctorShow = true">
          <div style="display: flex;align-items: center;">
            {{ user_name }}
            <van-icon name="arrow-down" />
          </div>
        </div>
      </div>
      <div class="adviceBox" v-if="summaryList.length > 0">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <div class="adviceMain">
            <div
              class="adviceOne"
              v-for="(item, index) in summaryList"
              :key="index"
            >
              <div class="adviceTitle">
                <p>阶段小结时间:{{getTime(item.record_time)}}</p>
              </div>
              <div style="margin-top:10px;">阶段小结化验结果:</div>
              <div v-for="it in item.template_inspection_content ? item.template_inspection_content : []">
                <p style="font-weight:bold;">{{ it.project_name }}&nbsp;&nbsp;检查日期:{{getTime(it.arr[0].inspect_date)}}</p>
                <div style="display:flex;flex-wrap: wrap;">
                  <div v-for="i in it.arr" style="width:50%;">
                  {{i.item_name}}<span v-if="i.unit!=''"> [{{i.unit}}]: </span> {{i.inspect_value}}
                   <span v-if="parseInt(i.inspect_value) < parseInt(i.range_min)">
                    ↓
                   </span>
                    <span v-if="parseInt(i.inspect_value) > parseInt(i.range_max)">
                   ↑
                   </span>
                  </div>
                </div>
              </div>
              <div>阶段小结总结:</div>
              <div class="statOrder">
                <p style="color:#5b98ff;margin-bottom:10px;">{{item.template_summary_content}}</p>
              </div>
              <div style="margin-top:10px;">阶段个体化透析方案:</div>
              <div>
                <p style="color:#5b98ff;margin-bottom:10px;">{{item.template_plan_content}}</p>
              </div>

              <div class="doctorBox">
                <p>记录医生：{{ getDoctorName(item.admin_user_id)}}</p>
              </div>
            </div>
          </div>
        </van-list>
      </div>
      <div class="noimgBox" v-else>
        <img src="../../../assets/images/none.png" alt />
      </div>
    </div>
    <!-- <div class="add">
      <div style="display: flex;align-items: center;">
        <van-icon class="addIcon" name="add" />新增
      </div>
    </div>-->

    <!-- 弹出层 -->
    <div>
     <van-popup
        class='newPopup'
        get-container="#app"
        v-model="doctorShow"
        position="bottom"
        :style="{ height: '40%' }"
      >
        <van-picker
          show-toolbar
          :columns="columns"
          @cancel="onCancel"
          @confirm="onConfirmOne"
        />
      </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="取消"
        @select="Delete"
      />
    </div>
  </div>
</template>

<script>
import {
  getNewPatientId,
  getPatientDetail,
  getAllDoctorList,
  getTreateSummary
} from '@/api/patient/patient'
import { uParseTime } from '@/utils/tools'
import Vue from 'vue'
import { Dialog } from 'vant'
const moment = require('moment')
export default {
  props: {
    active: Number
  },
  data () {
    return {
      newShow: false,
      doctorShow: false,
      startShow: false,
      endShow: false,
      startTime: '开始时间',
      endTime: '结束时间',
      user_name: '记录医生选择',
      endTimes: '',
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(),
      actions: [{ name: '查看全部' }, { name: '全部删除' }],
      showOne: true,
      showTwo: false,
      limit: 10,
      page: 1,
      total: 0,
      loading: false,
      finished: false,
      rescueRecords: [],
      patient_id: 0,
      list: [],
      id: 0,
      index: 0,
      date: 0,
      patient_name: '',
      columns: [],
      doctorList: [],
      summaryList: [],
      anticoagulantsConfitList: []

    }
  },
  methods: {
    onCancel () {
      this.typeShow = false
    },
    onConfirm (value) {
      this.type = value
      this.typeShow = false
    },
    onConfirmOne (value) {
      this.user_name = value
      this.doctorShow = false
      var patientid = this.$route.query.patient_id
      this.page = 1
      this.limit = 10
      this.getTreateSummary(patientid)
    },
    getstartTime (value) {
      this.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.summaryList = []
      var patientid = this.$route.query.patient_id
      this.getTreateSummary(patientid)
      this.startShow = false
    },
    getstartTime2 (value) {
      this.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.summaryList = []
      var patientid = this.$route.query.patient_id
      this.getTreateSummary(patientid)
      this.endShow = false
    },

    onLoad () {
      setTimeout(() => {
        this.page++

        if (this.page <= Math.ceil(this.total / 10)) {
          var patientid = this.$route.query.patient_id
          this.getTreateSummary(patient_id)
        } else {
          this.loading = false
          this.finished = true
        }
      }, 1000)
    },
    getTime (time) {
      return uParseTime(time, '{y}-{m}-{d}')
    },
    toRescueRecord (id) {
      var patientid = this.$route.query.patient_id
      this.$router.push(
        '/rescuerecordtwo?id=' +
          id +
          '&patientid=' +
          patientid +
          '&date=' + this.date + '&patient_name=' + this.patient_name
      )
    },
    toDeleteRescueRecord (id, index) {
      this.id = id
      this.index = index
      this.newShow = true
    },
    Delete (value) {
      if (value.name == '查看全部') {
        var patientid = this.$route.query.patient_id
        this.$router.push(
          '/rescuerecordtwo?id=' +
            this.id +
            '&patientid=' +
            patientid +
            '&date=' + this.date + '&patient_name=' + this.patient_name
        )
      }
      if (value.name == '全部删除') {
        this.DeleteRescueRecord(this.id, this.index)
      }
    },
    DeleteRescueRecord (id, index) {
      Dialog.confirm({
        title: '删除提示!',
        message: '确认删除该条信息吗?,删除后将无法恢复!'
      }).then(() => {
        DeleteRescueRecord(id).then(response => {
          if (response.data.state === 1) {
            var msg = response.data.data.msg

            this.rescueRecords.splice(index, 1)
            this.newShow = false
          }
        })
      })
    },
    getNewPatientId (id) {
      getNewPatientId(id).then(response => {
        if (response.data.state == 1) {
          var patient = response.data.data.patient
          this.patient_id = patient.id
          this.getTreateSummary(patient.id)
        }
      })
    },
    getTreateSummary (id) {
      var creater = 0
      for (let i = 0; i < this.doctorList.length; i++) {
        if (this.user_name == this.doctorList[i].user_name) {
          creater = this.doctorList[i].admin_user_id
        }
      }
      var params = {
        limit: this.limit,
        page: this.page,
        patient_id: id,
        start_time: this.startTime,
        end_time: this.endTime,
        creater: creater
      }
      console.log('params', params)
      getTreateSummary(params).then(response => {
        if (response.data.state == 1) {
          var list = response.data.data.list
          console.log('list232322332', list)
          for (let i = 0; i < list.length; i++) {
            list[i].project_id = list[i].project_id.split(',')
          }

          for (let j = 0; j < list.length; j++) {
            let arr = this.unique(list[j].children, 'project_name')
            list[j].template_inspection_content = arr
          }

          this.summaryList = list
          var total = response.data.data.total
          this.total = total
        }
      })
    },
    getAllDoctorList () {
      getAllDoctorList().then(response => {
        if (response.data.state == 1) {
          var list = response.data.data.list
          console.log('list', list)
          this.doctorList = list
          for (let i = 0; i < list.length; i++) {
            this.columns.push(list[i].user_name)
          }
        }
      })
    },
    unique (data, key) {
      let lastData = []
      console.log('data2332332', data)
      if (data != null) {
        data.forEach(item => {
          let status = lastData.some(project_name => project_name[key] == item[key])

          if (!status) {
            // item.value0 = item.value;
            let obj = {
              project_name: '',
              arr: []
            }
            obj.project_name = item.project_name
            obj.arr.push(item)
            lastData.push(obj)
          } else {
            lastData.forEach((project_name, index) => {
              if (project_name[key] === item[key]) {
                project_name.arr.push(item)
              }
            })
          }
        })
      }
      return lastData
    },
    getAnticoagulant (id) {
      var name = ''
      if (id == 1) {
        name = '无肝素'
      }
      if (id == 2) {
        name = '普通肝素'
      }
      if (id == 3) {
        name = '低分子肝素'
      }
      if (id == 4) {
        name = '阿加曲班'
      }
      if (id == 5) {
        name = '枸橼酸钠'
      }
      if (id == 6) {
        name = '低分子肝素钙'
      }
      if (id == 7) {
        name = '低分子肝素钠'
      }
      if (id == 11) {
        name = '那屈肝素'
      }
      if (id == 12) {
        name = '无抗凝剂'
      }
      return name
    },
    getDoctorName (id) {
      var name = ''
      for (let i = 0; i < this.doctorList.length; i++) {
        if (id == this.doctorList[i].admin_user_id) {
          name = this.doctorList[i].user_name
        }
      }
      return name
    }
  },

  created () {
    var now = new Date()
    this.date = [now.getTime() - (7 * 24 * 60 * 60 * 1000), now.getTime()]

    var nowDate = new Date()
    var nowYear = nowDate.getFullYear()
    var nowMonth = nowDate.getMonth() + 1

    var nowDay = nowDate.getDate()
    this.endTime =
        nowYear +
        '-' +
        (nowMonth < 10 ? '0' + nowMonth : nowMonth) +
        '-' +
        (nowDay < 10 ? '0' + nowDay : nowDay)
    nowDate.setMonth(nowDate.getMonth() - 1)
    nowYear = nowDate.getFullYear()
    nowMonth = nowDate.getMonth() + 1
    nowDay = nowDate.getDate()
    this.startTime =
        nowYear +
        '-' +
        (nowMonth < 10 ? '0' + nowMonth : nowMonth) +
        '-' +
        (nowDay < 10 ? '0' + nowDay : nowDay)
    var patientid = this.$route.query.patient_id
    // this.getNewPatientId(patientid)
    this.anticoagulantsConfitList = this.$store.getters.anticoagulants_confit
    console.log('抗凝2323', this.anticoagulantsConfitList)
    this.date = this.$route.query.date
    this.patient_name = this.$route.query.patient_name
    this.getAllDoctorList()
    this.getTreateSummary(patientid)
  }
}
</script>

<style lang="scss" scoped>
.newRescueRecord {
  height: 100%;
  overflow-y: auto;
  background: #fff;
  padding-top:2.75rem;
  .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;
    margin-bottom: 3.125rem;
  }
  .adviceOne {
    box-shadow: 0px 1px 0px 0px rgba(0, 0, 0, 0.1);
    padding-bottom: 0.625rem;
    font-size: 0.8125rem;
  }
  .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);
    margin-top: .625rem;
    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;
    }
  }
  // .add {
  //   position: fixed;
  //   bottom: 0;
  //   left: 0;
  //   width: 100%;
  //   height: 2.75rem;
  //   background: rgba(255, 255, 255, 1);
  //   box-shadow: 0px -1px 0px 0px rgba(0, 0, 0, 0.1);
  //   display: flex;
  //   align-items: center;
  //   justify-content: space-around;
  //   font-size: 0.9375rem;
  //   color: #979798;
  //   .addIcon {
  //     color: #5b98ff;
  //     font-size: 1.25rem;
  //     margin-right: 0.25rem;
  //   }
  // }
  .van-popup {
    border-radius: 0;
    transform: translate3d(0, 0, 0);
    top: initial;
  }

}
::-webkit-scrollbar {
  width: 0;
}
</style>
