<template>
  <div class="newDryWeight">
    <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="dryweight.length > 0">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <div class="adviceMain">
            <div
              class="adviceOne"
              v-for="(item, index) in dryweight"
              :key="index"
            >
              <div class="adviceTitle">
                <p>{{ getTime(item.ctime) }}</p>
                <van-icon
                  class="ellipsis"
                  name="ellipsis"
                  @click="DeleteDryWeightOne(item.id, index)"
                />
              </div>
              <div class="statOrder" @click="getDryWeightDetail(item.id,item.patient_id)">
                <div class="weightContent">
                  <p>干体重：{{ item.dry_weight }}kg</p>
                  <p>{{ item.adjusted_value }}kg</p>
                </div>
                <div class="doctorBox">
                  <p>记录医生：{{ item.user_name }}</p>
                </div>
                <div class="all" @click="getDryWeightDetail(item.id)">全部</div>
              </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-action-sheet
        class="newPopup"
        get-container="#app"
        v-model="newShow"
        :actions="actions"
        cancel-text="取消"
        @cancel="onCancel"
        @select="onDeleteConfirm"
      />
    </div>
    <van-popup v-model="startShow" 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="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>
  </div>
</template>

<script>
import {
  getNewPatientId,
  getDryWeight,
  DeleteDryWeight,
  getPatientDetail
} from '@/api/patient/patient'
import { uParseTime } from '@/utils/tools'
import { Dialog } from 'vant'
const moment = require('moment')
export default {
  props: {
    active: Number,
    dryId: String
  },
  data () {
    return {
      newShow: false,
      startShow: false,
      endShow: false,
      startTime: '请选择',
      endTime: '请选择',
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(2025, 10, 1),
      endTimes: '',
      currentDate: new Date(),
      actions: [{ name: '查看全部' }, { name: '全部删除' }],
      page: 1,
      limit: 10,
      dryweight: [],
      patient_id: '',
      total: 0,
      list: [],
      loading: false,
      finished: false,
      typeShow: false,
      dry_id: 0,
      dry_index: 0,
      showOne: true,
      showTwo: false,
      date: 0,
      patient_name: ''
    }
  },
  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.dryweight = []
      this.getDryWeight(
        this.patient_id,
        this.startTime,
        this.endTime,
        this.limit,
        this.page
      )

      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.dryweight = []
      this.getDryWeight(
        this.patient_id,
        this.startTime,
        this.endTime,
        this.limit,
        this.page
      )
      this.endShow = false
    },
    getDryWeight (patientid) {
      getDryWeight(
        patientid,
        this.startTime,
        this.endTime,
        this.limit,
        this.page
      ).then(response => {
        if (response.data.state === 1) {
          var dryweight = response.data.data.dryweight
          console.log("333333",dryweight)
          let arr = this.dryweight
          arr.push(...dryweight)
          this.dryweight = arr
          var total = response.data.data.total
          this.total = total
          this.loading = false
        }
      })
    },
    getDryWeight1 (patientid) {
      getDryWeight(
        patientid,
        this.startTime,
        this.endTimes,
        this.limit,
        this.page
      ).then(response => {
        if (response.data.state === 1) {
          var dryweight = response.data.data.dryweight
          this.dryweight = dryweight

          var total = response.data.data.total

          this.total = total
          this.loading = false
        }
      })
    },
    onLoad () {
      setTimeout(() => {
        this.page++
        if (this.page <= Math.ceil(this.total / 10)) {
          this.getDryWeight(
            this.patient_id,
            this.startTime,
            this.endTimes,
            this.limit,
            this.page
          )
        } else {
          this.loading = false
          this.finished = true
        }
      }, 1000)
    },
    getTime (time) {
      // return uParseTime(time, "{y}-{m}-{d} {h}:{i}:{s}");
      return uParseTime(time, '{y}-{m}-{d}')
    },
    DeleteDryWeightOne (id, index) {
      this.newShow = true
      this.dry_id = id
      this.dry_index = index
    },
    onDeleteConfirm (value) {
      if (value.name == '查看全部') {
        var patientid = this.$route.query.patient_id
        this.$router.push(
          '/dryweightwo?id=' +
            this.dry_id +
            '&patientid=' +
            patientid + '&date=' + this.date + '&patient_name=' + this.patient_name
        )
      }
      if (value.name == '全部删除') {
        this.DeleteDryWeight(this.dry_id, this.dry_index)
      }
    },
    DeleteDryWeight (id, index) {
      Dialog.confirm({
        title: '删除提示!',
        message: '确认删除该条信息吗?,删除后将无法恢复!'
      }).then(() => {
        DeleteDryWeight(id).then(response => {
          if (response.data.state === 1) {
            var msg = response.data.data.msg
            // console.log("msg", msg);
            this.dryweight.splice(index, 1)
            this.newShow = false
          }
        })
      })
    },
    getDryWeightDetail (id,patientid) {
      // var patientid = this.$route.query.patient_id
     
      console.log('进来', patientid)
      this.$router.push('/dryweightwo?id=' + id + '&patientid=' + patientid + '&date=' + this.date + '&patient_name=' + this.patient_name)
    },
    getPatientDetail (patientid) {
      getPatientDetail(patientid).then(response => {
        if (response.data.state === 1) {
          var patientDetail = response.data.data.patientDetail
          if (patientDetail.blood_patients == 0) {
            this.dryweight = []
            this.showOne = false
            this.showTwo = true
          }
          if (patientDetail.blood_patients == 1) {
            this.showOne = true
            this.showTwo = false
          }
          // console.log("病人详情", patientDetail);
          this.patientName = patientDetail.name
        }
      })
    },
    getNewPatientId (id) {
      getNewPatientId(id).then(response => {
        if (response.data.state == 1) {
          var patient = response.data.data.patient
          this.getDryWeight(patient.id)
          this.patient_id = patient.id
          this.getPatientDetail(patient.id)
        }
      })
    }
  },
  created () {
    var patientid = this.$route.query.patient_id
    this.getNewPatientId(patientid)
    console.log("干体重id", patientid);
    this.date = this.$route.query.date
    this.patient_name = this.$route.query.patient_name
  }
}
</script>

<style lang="scss" scoped>
.newDryWeight {
  height: 100%;
  overflow-y: auto;
  background: #fff;
  padding-top:2.75rem;
  .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;
  }
  .weightContent {
    font-size: 0.875rem;
    color: rgba(49, 50, 52, 1);
    display: flex;
    p {
      line-height: 1.125rem;
      margin-right: 0.625rem;
      margin-top: 0.375rem;
    }
  }
  .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>

