<template>
  <div class="leftBox">
    <div class="topBox">
      <el-date-picker
        v-model="selectMonth"
        type="month"
        format="yyyy年MM月"
        :clearable="false"
        style="margin-bottom: 10px; width: 150px"
        @change="changeMonth"
        prefix-icon="false"
        value-format="yyyy-MM-dd"
      />
      <div>
        <i
          class="el-icon-arrow-left"
          style="font-size: 18px; font-weight: bold"
          @click="preMonth"
        ></i>
        <i
          class="el-icon-arrow-right"
          style="font-size: 18px; font-weight: bold"
          @click="nextMonth"
        ></i>
      </div>
    </div>

    <el-calendar v-model="selectMonthValue">
      <template #dateCell="{ data }">
        <div
          :key="data.day"
          :id="data.day"
          @click="searchLog(data, isOverDay(data.day))"
          :class="{
            dayBox: true,
            disabled: isOverDay(data.day),
            hasLog: calendarList.filter(
              (ele) => ele.date == data.day + ' 00:00:00' && ele.state != -1
            ).length,
          }"
        >
          {{ data.day.split("-")[2] }}
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
import { workLogApi, projectApi } from "@/utils/api";

export default {
  name: "LeftMonth",
  data() {
    return {
      selectMonth: this.moment().format("YYYY-MM"),
      nowDay: this.moment().format("YYYY-MM-DD 23:59:59"),
      userId: this.$store.state.user.id,
      calendarList: [],
      selectDay:
        this.moment().date() > 10
          ? "0" + this.moment().date()
          : this.moment().date(),
      selectMonthValue: this.moment().format("YYYY-MM-DD"),
    };
  },
  methods: {
    preMonth() {
      this.selectDay = "01";
      this.selectMonth = this.moment(this.selectMonth)
        .subtract(1, "months")
        .format(`YYYY-MM-${this.selectDay}`);
      this.changeMonth();
    },
    nextMonth() {
      this.selectDay = "01";
      this.selectMonth = this.moment(this.selectMonth)
        .add(1, "months")
        .format(`YYYY-MM-${this.selectDay}`);
      this.changeMonth();
    },
    searchLog(data, overDay) {
      let checkMonth = this.moment(data.day).format("YYYY-MM");
      let selectMonth = this.moment(this.selectMonth).format("YYYY-MM");

      this.$nextTick(() => {
        if (overDay) {
          this.$message({
            message: "不能超过当前时间",
            type: "warning",
          });
          this.$emit("setDisableTable", true);
        } else {
          this.selectDay = data.day.split("-")[2];
          if (selectMonth !== checkMonth) {
            this.selectMonth = data.day;
            this.getLogMonth();
          }
          this.$emit("searchDay", data.day + " 00:00:00");
          this.$emit("setDisableTable", false);
        }
      });
    },
    isOverDay(data) {
      return new Date(data).getTime() > new Date(this.nowDay).getTime();
    },
    getLogMonth() {
      if (this.$route.query.userId) {
        this.userId = this.$route.query.userId;
      }
      let start = this.moment(this.selectMonth)
        .startOf("month")
        .format(`YYYY-MM-DD 00:00:00`);
      let end = this.moment(this.selectMonth)
        .endOf("month")
        .format("YYYY-MM-DD 23:59:59");

      workLogApi
        .getLogData({
          startDate: start,
          endDate: end,
          userId: this.userId,
        })
        .then((res) => {
          this.calendarList = res.data;
        });
    },
    changeMonth() {
      this.$nextTick(() => {
        if (this.selectMonth == this.selectMonthValue) return;
        this.selectDay = "01";
        this.selectMonth = this.moment(this.selectMonth).format(
          `YYYY-MM-${this.selectDay}`
        );
        this.selectMonthValue = this.selectMonth;
        this.searchLog(
          { day: this.selectMonth },
          this.isOverDay(this.selectMonth)
        );
        this.searchLog(
          { day: this.selectMonth },
          this.isOverDay(this.selectMonth)
        );
        this.getLogMonth();
      });
    },
  },
  watch: {},
  mounted() {
    this.getLogMonth();
  },
};
</script>

<style scoped lang="scss">
.leftBox ::v-deep .el-calendar__header {
  display: none;
}
.leftBox ::v-deep .el-date-editor {
  .el-input__inner {
    border: none;
    padding-left: 0;
    font-size: 18px;
    color: #333333;
    font-weight: 600;
  }
}
.leftBox ::v-deep .el-calendar__body {
  padding: 0 !important;
  thead {
    th {
      font-family: PingFang SC;
      font-weight: 400;
      font-size: 16px;
      color: #999999;
    }
  }
  tbody {
    .hasLog {
      background: #71afff;
      border-radius: 50%;
      color: #fff;
    }
    td {
      border: none;
      padding: 10px 0;
      background-color: #fff;

      .el-calendar-day {
        text-align: center;
        padding: 0 !important;
        font-size: 16px;
        background-color: #fff;

        span {
          font-family: cursive !important;
        }
      }
      &.next,
      &.prev {
        color: #333 !important;
      }
      &.is-selected .dayBox:not(.disabled) {
        border-radius: 50% !important;
        background-color: #4096ff;
        color: #fff;
      }
      .el-calendar-day {
        height: 40px;
        width: 40px;
        line-height: 40px;
        border-radius: 50% !important;
        &:hover {
          background-color: #88bdfd;
          color: #fff;
        }
      }
    }
  }
}
.topBox {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  i {
    margin-right: 10px;
    cursor: pointer;
  }
}
.leftBox ::v-deep .disabled {
  background-color: #fff !important;
  color: #c0c4cc;
  cursor: not-allowed;
  border-radius: 50% !important;
  background-color: #fff;
}
</style>
