<template>
  <page-content>
    <view class="content h100">
      <index-header :use-info="useInfo" />
      <view class="line" />
      <index-nav
        :user="vuexUserInfo"
        :use-info="useInfo"
        :total-be-confirm-count="totalBeConfirmCount"
      />
      <view class="line" />
      <view v-if="!isEmpty">
        <view v-for="(item,keys,index) in newbornList" :key="index">
          <view class="top">
            <view>{{ changeDate(keys) }}  </view>
            <view>{{ changeYeaas(keys) }}</view>
          </view>
          <IndexList :newbornlist="item" />
        </view>
      </view>
      <view v-else class="no-result u-flex u-flex-col u-row-center u-col-center">
        <image :src="NoResultImage" class="img" />
        <view class="text">暂无探视</view>
      </view>

    </view>
  </page-content>
</template>

<script>
const app = getApp()
import {
  fhGetPatientInfo
} from '../../../../mainWeixinMp/api/remote/index'
import {
  fhGetVisitList,
  fhGetExceed
} from '../../../../mainWeixinMp/api/newborn/index'

import IndexHeader from './components/header.vue'
import IndexNav from './components/nav.vue'
import IndexList from './components/list.vue'
import dayjs from 'dayjs'

import NoResultImage from './images/no-recordnew.png'
export default {
  components: {
    IndexHeader,
    IndexNav,
    IndexList
  },
  data() {
    return {
      useInfo: {}, // 个人信息
      newbornList: [],
      isEmpty: true, // 暂无数据
      NoResultImage // 暂无探视提示图片
    }
  },
  computed: {
  },
  onReachBottom() {
  },
  onShow() {
    Promise.all[(this.fhGetPatientInfo(), this.fhGetVisitList(),
    this.fhGetExceed())]
    console.log(getApp().globalData.newbornSetting, 'getApp().globalData.newbornSettings')
  },
  onPullDownRefresh() {
    this.getNew()
  },
  methods: {
    /**
     * 下拉刷新
     */
    getNew() {
      this.fhGetVisitList()
      setTimeout(() => {
        uni.stopPullDownRefresh()
      }, 800)
      // uni.stopPullDownRefresh()
    },
    /**
     * 获取患者信息
     */
    async fhGetExceed() {
      const hospitalInfo = uni.getStorageSync('hospital-info')
      const hospitalId = hospitalInfo.hospitalId
      const familyPhoneNumber = uni.getStorageSync('auth-phone')
      try {
        const { results } = await fhGetExceed(hospitalId, familyPhoneNumber)
        getApp().globalData.timeExceed = results
      } catch (e) {
        return Promise.reject(e)
      }
    },
    /**
     * 获取患者信息
     */
    async fhGetPatientInfo() {
      const form = []
      const memberId = app.globalData.baseInfo.memberId
      try {
        const { results } = await fhGetPatientInfo(form, memberId)
        this.useInfo = results
        return results
      } catch (e) {
        return Promise.reject(e)
      }
    },
    /**
     * 获取新生儿探视列表
     *
     */
    async fhGetVisitList() {
      uni.showLoading({ title: '正在加载中...' })
      const params = {}
      const hospitalInfo = uni.getStorageSync('hospital-info')
      const hospitalId = hospitalInfo.hospitalId
      const nowTime = dayjs().format('YYYY-MM-DD')
      params.visitStartTime = `${nowTime} 00:00:00`
      params.visitEndTime = `${dayjs(nowTime).add(6, 'day').format('YYYY-MM-DD')} 23:59:59`
      params.memberType = 'FAMILY_MEMBER'
      params.familyPhoneNumber = uni.getStorageSync('auth-phone')
      console.log(params.visitStartTime)
      try {
        const { results } = await fhGetVisitList(params, hospitalId)
        console.log(results)
        this.newbornList = results
        Object.keys(this.newbornList).length === 0 ? this.isEmpty = true : this.isEmpty = false
        // this.handleList(results)
      } catch (e) {
        return Promise.reject(e)
      }
      uni.hideLoading()
    },
    changeDate(date) {
      return dayjs(date).format('MM月DD日') === dayjs().format('MM月DD日') ? `${dayjs(date).format('MM月DD日')}  今天` : `${dayjs(date).format('MM月DD日')} `
    },
    changeYeaas(date) {
      return `${dayjs(date).format('YYYY年')}`
    }
    /**
     * 处理新生儿列表日期的方法
     * obj:列表数据
     */
    // handleList(obj) {
    //   let array = []
    //   array = Object.keys(obj).map(val => ({
    //     key: val,
    //     val: obj[val]
    //   }))
    //   console.log(array, 'this.array')
    //   this.newbornList = array.reverse()
    // }
  }
}
</script>

<style lang="scss" scoped>
.index-choose {
  height: 35px;
  display: flex;
  background: #ffffff;
  font-size: 12px;
  text-align: center;
  justify-content: flex-end;
  padding-right: 15px;
  .index-sig {
    width: 72px;
    height: 29px;
    background: #3c9cff;
    color: #ffffff;
    padding-top: 5px;
  }
  .index-remote {
    width: 72px;
    height: 29px;
    background: #ffffff;
    color: #3c9cff;
    padding-top: 5px;
    border: 1px solid #3c9cff;
  }
}
.line {
  height: 5px;
  background: #f2f2f2;
}
.top {
  background-color: #f2f2f2;
  font-size: 28rpx;
  font-weight: 400;
  padding: 12rpx 30rpx;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.no-result {
    width: 100%;
    padding-top: 193rpx;
    opacity: 0.5;
    .img {
      width: 200rpx;
      height: 185rpx;
    }
    .text {
      margin-top: 23rpx;
      color: #b5b5b5;
      font-size: 26rpx;
    }
  }
</style>
