<template>
  <el-row class="movie-list">
    <el-row v-for="(item, index) in [1, 2, 3, 4]" :key="index">
      <el-col v-for="(item, index) in [1, 2, 3]" :key="index" :md="8">
        <el-row v-if="showCalender" style="padding-right: 5px; padding-left: 5px; padding-bottom: 5px">
          <el-card class="box-card">
            <div class="text item">
              <el-calendar v-model="calendarDate">
                <div
                  slot="dateCell"
                  slot-scope="{ date, data }"
                  @click="handleCellClick(date, data)"
                >
                  <p>{{ data.day.split("-").slice(2).join() }}</p>
                  <p v-if="dealMyDate(data.day)" class="blue budge" />
                </div>
              </el-calendar>
            </div>
          </el-card>
        </el-row>
      </el-col>
    </el-row>
  </el-row>
</template>

<script>
import {
  getCamDetail,
  getCamList,
  getCamRecord,
  getCamRecordByDay,
  getCamRecordByMonth,
  getLatestRecord
} from '@/api/cam'

export default {
  name: 'MyCalendar',
  data() {
    return {
      user: {
        userId: 1,
        screenName: '浩',
        avatarUrl: '//pic1.zhimg.com/v2-c755e7ec5510b0638c2d1bead712544a_r.jpg',
        following: 1024,
        follower: 1024
      },
      camId: null,
      calendarDate: new Date(),
      dateMap: new Map(),
      showCalender: true,
      showRecordList: false,
      camList: [],
      cam: null,
      camDetail: null,
      form: {},
      recordList: [],
      camRecord: null
    }
  },
  watch: {
    $route() {
      this.$router.go()
    },
    calendarDate: {
      handler(newValue, oldValue) {
        const oldMonth = this.getYearMonth(oldValue)
        const newMonth = this.getYearMonth(newValue)
        if (oldMonth !== newMonth) {
          this.showCalender = false
          this.form.yearMonth = this.getYearMonth(newValue)
          getCamRecordByMonth(this.form).then(resp => {
            if (resp.code === 0) {
              this.dateMap.clear()
              for (const item of resp.data) {
                const date1 = new Date(item)
                const dayStr = this.getYearMonthDay(date1)
                this.dateMap.set(dayStr, date1)
              }
            }
            this.showCalender = true
          })
        }
      }
    }
  },
  created() {
    document.title = '我的日历'
    const camId = this.$route.params.camId
    this.camId = camId
    getCamDetail(camId).then(resp => {
      if (resp.code === 0) {
        var camDetail = resp.data
        this.camDetail = camDetail
      }
    })

    this.form.camId = camId
    this.form.yearMonth = this.getYearMonth(new Date())
    getCamRecordByMonth(this.form).then(resp => {
      if (resp.code === 0) {
        this.dateMap.clear()
        for (const item of resp.data) {
          const date1 = new Date(item)
          const dayStr = this.getYearMonthDay(date1)
          this.dateMap.set(dayStr, date1)
        }
        this.showCalender = true
      }
    })

    getCamList().then(resp => {
      if (resp.code === 0) {
        this.camList = resp.data
      }
    })

    // const today = new Date()
    getLatestRecord(camId).then(resp => {
      if (resp.code === 0) {
        const respData = resp.data
        this.initMp4Player(respData.coverUrl, respData.videoUrl)
      }
    }).catch(error => {
      console.log(error)
      var ele = document.querySelector('#dplayer')
      ele.innerHTML = '<h5>没有今天的录像记录</h5>'
    })
  },
  methods: {
    onSelect(value) {
      const path = '/cam/record/' + value
      if (this.$route.path === path) {
        this.$router.go(0)
        return
      }
      this.$router.push(path)
    },
    handleCellClick(date, data) {
      this.form.yearMonthDay = this.getYearMonthDay(date)
    },
    dealMyDate(val) {
      let res = ''
      if (this.dateMap.get(val) != null) {
        res = true
      }
      return res
    },
    getYearMonth(date) {
      const year = date.getFullYear().toString().padStart(4, '0')
      const month = (date.getMonth() + 1).toString().padStart(2, '0')
      // const day = date.getDate().toString().padStart(2, '0')
      // const hour = date.getHours().toString().padStart(2, '0')
      // const minute = date.getMinutes().toString().padStart(2, '0')
      // const second = date.getSeconds().toString().padStart(2, '0')
      // 2023-02-16 08:25:05
      // console.log(`${year}-${month}-${day} ${hour}:${minute}:${second}`)
      return year + '-' + month
    },
    getYearMonthDay(date) {
      const year = date.getFullYear().toString().padStart(4, '0')
      const month = (date.getMonth() + 1).toString().padStart(2, '0')
      const day = date.getDate().toString().padStart(2, '0')
      return year + '-' + month + '-' + day
    },
    onClickRow(row) {
    }
  }
}
</script>

<style scoped>
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

   .is-selected {
     color: #1989FA;
   }

::v-deep.el-calendar{
  height: calc(100% - 35px);
}
::v-deep.el-calendar .el-calendar__body{
  padding-bottom: 16px;
  height: calc(100% - 80px);
}
::v-deep.el-calendar .el-calendar-table{
  height: 100%;
}
.el-backtop, ::v-deep.el-calendar .el-calendar-table td.is-today .date{
  background: #DCE9FF;
  color: rgba(0,0,0,0.45);
}
.el-backtop, ::v-deep.el-calendar .el-calendar-table .date:hover{
  background: #DCE9FF;
  color: rgba(0,0,0,0.45);
}
::v-deep.el-calendar .el-calendar-table .el-calendar-day:hover{
  background: none;
}
::v-deep.el-calendar .el-calendar-table td.is-selected{
  background: none;
}
::v-deep.el-calendar .el-calendar-table td.is-selected .date{
  background: #5E97F9;
  box-shadow: 0px 4px 7px 0px rgba(113,208,255,0.5);
  color: #fff;
}
::v-deep.el-calendar .el-calendar__button-group{
  display: none;
}
::v-deep.el-calendar .el-calendar__title{
  margin: 0 auto;
}
::v-deep.el-calendar .el-calendar-table .el-calendar-day{
  position: relative;
}
</style>
