<template>
<div class='attence'>
<div class='list-item' v-for="(v,i) in list" :key="i">
  <div class='schedule'>
    <div class='time'>{{v.schedule_Date}}</div>
    <div class='locate'><img src='/static/imgs/community.png'>{{v.comm_Name}}</div>
  </div>
  <div class='on'>上班
    <div class='sign'>
      <img v-if='v.first_tsid>0' class='yes' src='/static/imgs/yes.png'>
      <img v-else-if='i>0 || beforeCheckIn===false' class='no' src='/static/imgs/no.png'>
    </div>
  </div>
  <div class='off'>下班
    <div class='sign'>
      <img v-if='v.second_tsid>0' class='yes' src='/static/imgs/yes.png'>
      <img v-else-if='i>0 || beforeCheckOff===false' class='no' src='/static/imgs/no.png'>
    </div>
  </div>
</div>
<NoData v-if='loaded && list.length==0' paddingTop=0 />
</div>
</template>

<script>
import NoData from '@/components/NoData'
const week = ['日','一','二','三','四','五','六']
export default {
  components:{NoData},
  data() {
    return {
      list: [],
      beforeCheckIn: 'unknown',
      beforeCheckOff: 'unknown',
      loaded: false
    }
  },
  methods: {
    getList() {
      this.loaded = false
      wx.showLoading({ title: '正在加载' })
      wx.request({
        url: this.rootUrl + '/TSheet/attence_records',
        data: { sessionID: wx.getStorageSync('sessionID') },
        success: res => {
          if (res.statusCode == '500') {
            this.service.getUnionId(this.rootAvatar, this.rootUrl).then(res => {
              this.getList()
            })
          } else {
            let dateStr, dateObj
            const todayStr = this.service.formatDate(new Date())

            const attenceRecords = res.data.response
            attenceRecords.forEach(v => {
              dateStr = this.service.correctTime(v.schedule_Date)
              dateObj = this.service.correctTime(v.schedule_Date,'DateObj')
              v.schedule_Date = dateStr + ' (星期' + week[dateObj.getDay()] + ')' + ( dateStr === todayStr ? ' (今天)' : '' )
            })

            this.list = []
            this.list = attenceRecords

            if(attenceRecords.length > 0) this.checkDuty()
            
            this.loaded = true
            wx.hideLoading()
          }
        },
        fail: res => {
          wx.hideLoading()
          wx.showToast({ title: '加载失败', icon: 'none' })
        }
      })
    },
    checkDuty(){
      const tArr = this.list[0].schedule_Date.substring(0,10).split(/-/).map(Number)
      const now = new Date()
      if (now.getDate() !== tArr[2] || now.getMonth()+1 !== tArr[1] || now.getFullYear() !== tArr[0]){
        this.beforeCheckIn = false
        this.beforeCheckOff = false
      }else{
        const CheckFunc = obj=>{
          obj.knockOff = this.service.correctTime(obj.knockOff,'DateObj')
          const hourEnd = obj.knockOff.getHours()
          const minuteEnd = obj.knockOff.getMinutes()
          const hour = new Date().getHours()
          const minute = new Date().getMinutes()
          return hour < hourEnd || hour === hourEnd && minute < minuteEnd
        }
        wx.request({
          url: this.rootUrl + '/ptime/all',
          success: res => {
            this.beforeCheckIn = CheckFunc(res.data[0])
            this.beforeCheckOff = CheckFunc(res.data[1])
          }
        })
      }
    }
  },
  onLoad() {
    this.getList()
  }
}
</script>
<style lang="stylus" scoped>
.attence
  position absolute
  Height_Width(100%)
  Background()
.list-item
  padding 20rpx 30rpx
  background white
  Border(0, 0, 1rpx, 0)
  Flex(flex,flex-end)
  .schedule
    margin 0 auto 0 0
    .time
      Font(36rpx)
    .locate
      margin 25rpx 20rpx 0 0
      Flex(flex,,center)
      Font(30rpx)
      color #666
      img
        Height_Width(32rpx)
        margin-right 10rpx
  .on, .off
    padding 0 20rpx
    Font(32rpx,32rpx,bold)
    Flex(flex,space-between,center,column)
    .sign
      Flex(flex,flex-end,center,column)
      .yes
        Height_Width(40rpx)
        position relative
        top 4rpx
      .no
        Height_Width(32rpx)
  .off
    padding-right 0
</style>
