<style lang="scss" scoped>
.day-cell{
  height: 100%;
}
.dialog-style{
  div{
    text-align: center;
    margin: 5px;
    /deep/ .el-input{
      display: inline-block;
      width: 370px;
    }
  }
  .add-bt{
    cursor: pointer;
    font-size: 18px;
    float: right;
  }
}
</style>
<template>
<div>
  <el-calendar v-model="searchMonth">
    <template
      slot="dateCell"
      slot-scope="{date, data}">
      <div class="day-cell" @click="chooseDay(data)">
        <p>{{getDayNum(data.day)}}</p>
        <div v-if="new Date(data.day).getMonth() == searchMonth.getMonth()">
          <p v-for="item in planList[getDayNum(data.day)]" :key="item.id">
            {{item.name}}
          </p>
        </div>
      </div>
    </template>
  </el-calendar>
   <el-dialog
      class="dialog-style"
      title="修改计划"
      :visible.sync="visibleChange"
      width="500px">
      <div v-for="(item, index) in dayPlan" :key="index">
          <label>计划：</label>
          <el-input placeholder="" v-model="item.name"></el-input>
      </div>
      <i class="el-icon-circle-plus-outline add-bt" @click="addDayPlan()"></i>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visibleChange = false">取 消</el-button>
        <el-button type="primary" @click="editTask">确 定</el-button>
      </span>
    </el-dialog>
</div>
</template>

<script>
import { getMonthPlan,addPlan,updatePlan,deletePlan } from '@/api/month';
export default {
  name: 'Month',
  components: {
  },
  data () {
    return {
      searchMonth: new Date(),
      params:{
        startTime: ''
      },
      visibleChange: false,
      planList: {},
      dayPlan: []
	  }
  },
  created() {
    this.getMonthPlan();
  },
  watch: {
    searchMonth(val) {
      this.params.startTime = val.getTime();
      this.getMonthPlan();
    }
  },
  methods: {
    getMonthPlan(){
      getMonthPlan(this.params).then((data) => {
        this.planList = {};
        for(let key in data.data){
          this.$set(this.planList,key,data.data[key]);
        }
      })
    },
    editTask(){
      this.visibleChange = false;
    },
    chooseDay(data){
      if(new Date(data.day).getMonth() != this.searchMonth.getMonth()){
        return;
      }
      this.visibleChange = true;
      let day = this.getDayNum(data.day);
      this.dayPlan = JSON.parse(JSON.stringify(this.planList[day]));
    },
    addDayPlan(){
      this.dayPlan.push({
        "name": "",
        "content": "",
        "year": "",
        "userId": "",
        "startTime": "",
      })
    },
    getDayNum(day){
      return ~~day.split('-')[2];
    }
  }
}
</script>
