<template>
  <div>
    <div class="mine">
      <!-- 表头月份 -->
      <div class="month">
        <div class="title">{{ month }}月排班</div>
        <div @click="schedulingDetails" class="btn" v-if="level == 10">
          排班
        </div>
        <!-- v-if='level==10' -->
      </div>
      <!-- 日历 -->
      <el-calendar>
        <template slot="dateCell" slot-scope="{ date, data }">
          <div
            v-for="(item, index) in stateType(getDay(date), data)"
            :key="index"
            class="dateContent"
            :class="
              thisDay == getDay(date) ? 'today' : item.type == 4 ? 'color5' : ''
            "
          >
            <span
              class="date"
              :class="item.type == 4 && thisDay != getDay(date) ? 'color4' : ''"
              >{{ getDay(date) }}
            </span>
            <span
              class="type"
              :class="
                item.type == 1
                  ? 'color1'
                  : item.type == 2
                  ? 'color2'
                  : item.type == 4 && thisDay != getDay(date)
                  ? 'color4'
                  : item.type == 3
                  ? 'color3'
                  : 'color6'
              "
              >{{ typeList[item.type - 1] }}</span
            >
          </div>
        </template>
      </el-calendar>
    </div>
  </div>
</template>

<script>
import api from "../../server/home";
export default {
  data() {
    return {
      thisDay: "", //今天日期（日）
      stateList: [], //值班列表
      typeList: ["早班", "中班", "晚班", "休息", "离职"], //1是早班 2是中班 3是晚班 4是休息 5离职
      month: "",
      level: "", //等级权限
    };
  },
  methods: {
    // 标注今天日期
    getDay(date) {
      return date.getDate();
    },
    schedulingDetails() {
      this.$router.push("/scheduling/details");
    },
    // 获取排班列表
    getScheduling() {
      let now = new Date();
      let year = now.getFullYear();
      let month = now.getMonth() + 1;
      if (month < 10) {
        month = "0" + month;
      }
      let start_time = `${year}-${month}-01`;
      const enddate = new Date(year, month, 0);
      var end_time =
        enddate.getFullYear() +
        "-" +
        (enddate.getMonth() + 1) +
        "-" +
        enddate.getDate();
      let parems = {
        start_time: start_time,
        end_time: end_time,
      };
      api.getScheduling(parems).then((res) => {
        if (res.code == 200) {
          this.stateList = res.data;
        }
      });
    },
    // 筛选每天对应的排班数据
    stateType(date, data) {
      // 获取月份
      if (!data) {
        return;
      }
      let month = data.day.split("-")[1];
      return this.stateList.filter((item) => {
        if (date == item.day && month == this.month) {
          return item;
        }
      });
    },
  },
  created() {
    this.level = localStorage.getItem("level");
    let now = new Date();
    this.thisDay = now.getDate();
    this.month = now.getMonth() + 1;
    // 获取排班列表
    this.getScheduling();
    this.stateType();
  },
};
</script>

<style lang='less' scoped>
.mine {
  width: 857px;
  height: 504px;
  background-color: #ffffff;
  margin: auto auto;
  border-radius: 10px;
  // 表头月份样式
  .month {
    width: 857px;
    height: 80px;
    border-radius: 10px;
    border: 1px solid #fa7d22;
    font-size: 24px;
    font-family: PingFangSC-Semibold, PingFang SC;
    font-weight: 600;
    color: #fa7d22;
    text-align: center;
    line-height: 80px;
    display: flex;
    align-items: center;
    .title {
      margin-left: 385px;
    }
    .btn {
      font-weight: 400;
      width: 80px;
      line-height: 30px;
      height: 30px;
      background: #fa7d22;
      border-radius: 20px;
      color: #ffffff;
      font-size: 14px;
      margin-left: 285px;
    }
  }
  // 日历样式
  /deep/.el-calendar__header {
    display: none;
  }
  /deep/.el-calendar-day {
    text-align: center;
    color: #333333;
    border: none;
  }
  /deep/ thead {
    font-size: 20px;
    font-weight: 400;
    color: #333;
  }
  /deep/td.current {
    border: none;
  }
  /deep/td.next {
    pointer-events: none;
    border: none;
    .dateContent {
      color: #d4d4d4;
    }
  }
  /deep/td.prev {
    pointer-events: none;
    border: none;
    .dateContent {
      color: #d4d4d4 !important;
    }
  }
  .dateContent {
    display: flex;
    font-size: 20px;
    flex-direction: column;
    width: 60px;
    height: 60px;
    margin: auto auto;
    margin-top: 10px;
    border-radius: 8px;
    padding-top: 6px;
    box-sizing: border-box;
    .date {
      margin-top: 4px;
    }
    .type {
      font-size: 14px;
      margin-top: 4px;
    }
  }
  .today {
    border: 1px solid #fa7d22 !important;
  }
  /deep/tr.el-calendar-table__row:last-child {
    display: none;
  }
  .el-calendar {
    pointer-events: none;
  }
}
/deep/td.current {
  .color1 {
    color: #19d1ca; //早班
  }
  .color2 {
    color: #ffa5a5; //午班
  }
  .color3 {
    color: #4b5d8d; //晚班
  }
  .color4 {
    color: #ffffff; //休息
  }
  .color5 {
    background-color: #fa7d22;
  }
  .color6 {
    color: #999999; //离职
  }
}
</style>