<template>
  <div class="courseManagement">
    <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>
      <div class="adviceBox" v-if="courseManagement.length > 0">
        <van-list
          v-model="loading"
          finished-text="没有更多了"
          @load="onLoad"
          :finished="finished"
        >
          <div class="adviceMain">
            <div
              class="adviceOne"
              v-for="(item, index) in this.courseManagement"
              :key="item.id"
            >
              <div class="adviceTitle">
                <p>{{ getTime(item.record_time) }}</p>
                <van-icon
                  class="ellipsis"
                  name="ellipsis"
                  @click="toDeleteCourseManagement(item.id, index)"
                />
              </div>
              <div class="statOrder" @click="toCourseDetail(item.id)">
                <div class="statOrderTitle">
                  <span>{{ item.title }}</span>
                </div>
                <div class="orderContent">
                  <p>
                    <span v-html="item.content"></span>
                  </p>
                </div>
                <div class="doctorBox">
                  <p>记录医生：{{ item.user_name }}</p>
                </div>
              </div>
              <div class="all"  @click="toCourseDetail(item.id)">全部</div>
            </div>
          </div>
        </van-list>
      </div>
      <div class="noimgBox" v-else>
        <img src="../../../assets/images/none.png" alt />
      </div>
    </div>

    <!-- 弹出层 -->
    <div>
      <van-popup
       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
        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"
        position="bottom" :style="{ height: '40%' }"
        get-container="#app">
        <van-datetime-picker
          v-model="currentDate"
          type="date"
          :min-date="minDate"
          :max-date="maxDate"
          @confirm="getstartTime2"
          @cancel="endShow = false"
        />
      </van-popup>

      <van-action-sheet
        get-container="#app"
        v-model="newShow"
        :actions="actions"
        cancel-text="取消"
        @cancel="onCancel"
        @select="onDeleteConfirm"
      />
    </div>
  </div>
</template>

<script>
import {
  getCourseManagement,
  DeleteCouseManage,
  getPatientDetail
} from '@/api/patient/patient'
import { uParseTime } from '@/utils/tools'
import { clearStyle } from '@/libs/clearStyle'
import Vue from 'vue'
import { Dialog } from 'vant'
const moment = require('moment')
export default {
  props: {
    active: Number
  },
  data () {
    return {
      newShow: false,
      startShow: false,
      endShow: false,
      startTime: '请选择',
      endTime: '请选择',
      endTimes: '',
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date(),
      // actions: [{ name: '查看全部' }, {name: '新增病程'}, { name: '全部删除' }],
      actions: [{ name: '查看全部' }, { name: '全部删除' }],
      limit: 10,
      page: 1,
      total: 0,
      loading: false,
      finished: false,
      typeShow: false,
      columns: [],
      courseManagement: [],
      patientid: 0,
      list: [],
      patient_id: 0,
      patient_index: 0,
      showOne: true,
      showTwo: false,
      seacherActive: 0
    }
  },
  methods: {
    onCancel () {
      this.typeShow = false
    },
    onConfirm (value) {
      this.type = value
      this.typeShow = false
    },
    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.courseManagement = []
      this.getCourseManagement(
        this.patientid,
        this.limit,
        this.page,
        this.startTime,
        this.endTime
      )
      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.courseManagement = []
      this.getCourseManagement(
        this.patientid,
        this.limit,
        this.page,
        this.startTime,
        this.endTime
      )
      this.endShow = false
    },
    getTime (time) {
      // return uParseTime(time, "{y}-{m}-{d} {h}:{i}:{s}");
      return uParseTime(time, '{y}-{m}-{d}')
    },
    getCourseManagement (patientid) {
      getCourseManagement(
        patientid,
        this.limit,
        this.page,
        this.startTime,
        this.endTime
      ).then(response => {
        if (response.data.state == 1) {
          var coursemanagement = response.data.data.coursemanagement
          this.courseManagement = clearStyle(coursemanagement)
          var total = response.data.data.total
          this.total = total
          this.loading = false
        }
      })
    },
    onLoad () {
        this.loading = false
        this.finished = true
    },
    toDeleteCourseManagement (id, index) {
      this.patient_id = id
      this.patient_index = index
      this.newShow = true
    },
    onDeleteConfirm (value) {
      if (value.name == '查看全部') {
        var patientid = this.$route.query.patientid
        this.$router.push(
          '/coursedetail?id=' +
            this.patient_id +
            '&patientid=' +
            patientid +
            '&active=' +
            this.active
        )
      }
      if (value.name == '全部删除') {
        this.DeleteCouseManage(this.patient_id)
      }

      // if (value.name == '新增病程') {
      //   var patientid = this.$route.query.patientid
      //   this.$router.push('/add/addCouseManagement?&patientid=' + patientid + '&seacherActive=' + this.seacherActive)
      // }
    },
    DeleteCouseManage (id, index) {
      Dialog.confirm({
        title: '删除提示!',
        message: '确认删除该条信息吗?,删除后将无法恢复!'
      }).then(() => {
        DeleteCouseManage(id).then(response => {
          if (response.data.state === 1) {
            var msg = response.data.data.msg
            // console.log('msg', msg)
            this.courseManagement.splice(index, 1)
            this.newShow = false
          }
        })
      })
    },
    toCourseDetail (id) {
      var patientid = this.$route.query.patientid
      this.$router.push(
        '/coursedetail?id=' +
          id +
          '&patientid=' +
          patientid +
          '&active=' +
          this.active
      )
    },
    getPatientDetail (patientid) {
      getPatientDetail(patientid).then(response => {
        if (response.data.state === 1) {
          var patientDetail = response.data.data.patientDetail
          if (patientDetail.blood_patients == 0) {
            this.courseManagement = []
            this.showOne = false
            this.showTwo = true
          }
          if (patientDetail.blood_patients == 1) {
            this.showOne = true
            this.showTwo = false
          }
          this.patientName = patientDetail.name
        }
      })
    },
    addCourse(){
      var patientid = this.$route.query.patientid
      this.$router.push('/add/addCouseManagement?&patientid=' + patientid + '&seacherActive=' + this.seacherActive)
    }
  },
  created () {
    var patientid = this.$route.query.patientid
    this.patientid = patientid
    this.seacherActive = parseInt(this.$route.query.seacherActive)
    // console.log("干体重id", patientid);
    this.getCourseManagement(patientid)
    this.getPatientDetail(patientid)
  }
}
</script>

<style lang="scss" scoped>
.courseManagement {
  height: 100%;
  overflow-y: auto;
  .toolBox {
    display: flex;
    height: 3.125rem;
    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;
      margin-left: 0.8125rem;
    }
  }
  .adviceBox {
    padding: 0 1.125rem;
    margin-bottom: 3.125rem;
  }
  .adviceMain {
    > div:last-child {
      border: none;
    }
  }
  .adviceOne {
    // box-shadow: 0px 1px 0px 0px rgba(0, 0, 0, 0.1);
    border-bottom: 1px solid #e5e5e5;
    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;
    }
  }
  .add {
    z-index: 1000;
    position: fixed;
    bottom: 0;
    left: 0;
    right:0;
    width: 100%;
    height: 2.75rem;
    line-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;
    }
  }
}
::-webkit-scrollbar {
  width: 0;
}
</style>
