<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-11-07 17:30:04
 * @LastEditors  : 无尘
 * @LastEditTime : 2019-12-26 10:08:25
 -->

<!--
  <people-day-class :classData="classData" :cycleData="cycleData"></people-day-class>
  import peopleDayClass from '@/components/app/dailyAttendance/people-day-class.vue'
-->
<template>
  <div class="day-tabs">
    <div class="tablist">
      <div class="right-content-top border-box">
        <div class="right-top-wrap_left flex flex-pack-center">
          <ul class="flex flex-row flex-pack-center ">
            <div class="el-tabs__active-bar is-top" :style="translate"></div>
            <li :class="['flex flex-align-center flex-pack-center p-lr-24', activeTab == 1 ? 'active-li' : '']" @click="chooseTab($event, '1')"><span class="flex flex-align-center flex-pack-center right-top-title">按天排班</span></li>
            <li :class="['flex flex-align-center flex-pack-center p-lr-24', activeTab == 2 ? 'active-li' : '']" @click="chooseTab($event, '2')"><span class="flex flex-align-center flex-pack-center right-top-title">按周期排班</span></li>
          </ul>
        </div>
      </div>
    </div>
    <ul v-if="activeTab == 1">
      <li>
        <div class="day-item-title" title="修改该员工当天班次">修改该员工当天班次</div>
        <ul>
          <template v-for="(item, index) in classData">
            <li class="day-class-item p-lr-20" :key="index + 'class'" @click.stop="daySet(item)">
              <p :class="['class-item', item.classColor]">{{ item.classesName }}</p>
            </li>
          </template>
          <template v-for="(item, index) in defaultData">
            <li class="day-class-item p-lr-20" :key="index + 'defaultClass'" @click.stop="daySet(item)">
              <p :class="['class-item', item.classesId == '-1' ? 'class-rest' : 'class-clear']">{{ item.classesName }}</p>
            </li>
          </template>
        </ul>
      </li>
    </ul>
    <ul v-if="activeTab == 2">
      <li>
        <div v-if="cycleData.length" class="day-item-title" title="从该天开始周期排班至月底">从该天开始周期排班至月底</div>
        <div v-else class="day-item-title" title="还没有排班周期">还没有排班周期<br />请去后台考勤组规则里设置</div>
        <ul>
          <li v-if="cycleData.length" class="day-class-item p-lr-20" @click.stop="dayWeekSet">
            <p class="class-item day-tag-blue">{{ cycleData[0].rangeName }} {{ cycleData[0].classesList.map(item => item.classesName).join('-') }}</p>
          </li>
          <li v-else class="day-class-item p-lr-20" @click.stop="toCycleSet">
            <p class="class-item day-tag-blue">请设置排班周期</p>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</template>
<script>
import showMsg from '@/common/js/showmsg';
export default {
  name: 'people-day-class',
  props: {
    // 班次,由于背景色显示所以源头新增classColor字段
    classData: {
      type: [Array, Object],
      default() {
        return [];
      }
    },
    // 周期
    cycleData: {
      type: [Array, Object],
      default() {
        return [];
      }
    },
    itemSchedule: {
      type: [Array, Object],
      default() {
        return [];
      }
    },
    // 每月的日数据
    dates: {
      type: [Array, Object],
      default() {
        return [];
      }
    },
    weekDay: {
      type: [Number, String],
      default() {
        return '';
      }
    }
  },
  data() {
    return {
      activeTab: '1',
      translate: {
        width: '56px',
        transform: `translateX(30px)`
      },
      defaultData: [
        {
          classesId: '-1',
          classesName: '休息',
          classColor: 'day-tag-grey'
        },
        {
          classesId: '-2',
          classesName: '清空',
          classColor: ''
        }
      ],
      itemData: {},
      itemCycleData: []
    };
  },
  methods: {
    /**
     * 选择tab
     *
     */
    chooseTab(e, tag) {
      const that = this;
      that.activeTab = tag;
      that.translate = {
        width: e.target.offsetWidth + 'px',
        transform: `translateX(${e.target.offsetLeft}px)`
      };
    },
    /**
     * 隐藏
     */
    hideSet() {
      let div = document.createElement('button');
      document.body.appendChild(div);
      div.click();
      document.body.removeChild(div);
    },
    /**
     * 选择班次
     */
    daySet(item) {
      const that = this;
      const y = new Date().getFullYear();
      const m = new Date().getMonth() + 1 < 10 ? new Date().getMonth() + 1 : '0' + (new Date().getMonth() + 1);
      const d = new Date().getDate() < 10 ? new Date().getDate() : '0' + new Date().getDate();
      if (new Date(that.weekDay).getTime() < new Date(String(y) + '-' + String(m) + '-' + String(d)).getTime()) {
        showMsg.showmsg('不能设置今天以前的排班', 'warning');
        return false;
      }
      that.hideSet();
      let arr = that.itemData.setClassesList.map(ele => ele.weekDay);
      if (arr.includes(that.weekDay)) {
        that.itemData.setClassesList.forEach(ele => {
          if (ele.weekDay == that.weekDay) {
            ele.classesName = item.classesName;
            ele.classesId = item.classesId;
            ele.classColor = item.classColor;
            ele.isRest = true;
          }
        });
        that.$emit('refreshData', JSON.parse(JSON.stringify(that.itemData)));

        that.$forceUpdate();
      }
    },
    /**
     * 设置周期
     */
    toCycleSet() {
      const that = this;
      that.$router.push(`/workGroupSet?appIcon=${that.$route.query.appIcon}&attenceGroupId=${that.$route.query.attenceGroupId}`);
    },
    /**
     * 周期
     */
    dayWeekSet() {
      const that = this;
      const y = new Date().getFullYear();
      const m = new Date().getMonth() + 1 > 10 ? new Date().getMonth() + 1 : '0' + (new Date().getMonth() + 1);
      const d = new Date().getDate() > 10 ? new Date().getDate() : '0' + new Date().getDate();

      if (new Date(that.weekDay).getTime() < new Date(String(y) + '-' + String(m) + '-' + String(d)).getTime()) {
        showMsg.showmsg('不能设置今天以前的排班', 'warning');
        return false;
      }
      let startIndex = 0;
      that.itemData.setClassesList.forEach((ele, index) => {
        if (that.weekDay == ele.weekDay) {
          startIndex = index;
        }
      });
      that.hideSet();
      // 当前选择的几号,还有轮询排班周期
      // let today = new Date(that.weekDay).getTime();
      let cycleLength = that.cycleData[0].classesList.length; // 排班有几个班次，相当于一个周期是几天
      // 然后在这个日期区间内按照排班周期设置
      let i = startIndex;
      for (let index = 0; index < that.itemData.setClassesList.length - startIndex; index++) {
        // if (new Date(that.itemData.setClassesList[startIndex].weekDay).getTime() >= today) {
        that.itemData.setClassesList[i].classesName = that.cycleData[0].classesList[index % cycleLength].classesName;
        that.itemData.setClassesList[i].classesId = that.cycleData[0].classesList[index % cycleLength].classesId;
        that.itemData.setClassesList[i].classColor = that.cycleData[0].classesList[index % cycleLength].classColor;
        that.itemData.setClassesList[i].isRest = true;
        i++;
        // }
      }
      /* that.itemData.setClassesList.forEach((date, index) => {
        // 不能保证顺序
        if (new Date(date.weekDay).getTime() >= today) {
          console.log(index % cycleLength);
          date.classesName = that.cycleData[0].classesList[index % cycleLength].classesName;
          date.classesId = that.cycleData[0].classesList[index % cycleLength].classesId;
          date.classColor = that.cycleData[0].classesList[index % cycleLength].classColor;
          date.isRest = true;
        }
      }); */
      // console.log('周期设置:', that.itemData);
      that.$emit('refreshData', JSON.parse(JSON.stringify(that.itemData)));
      that.$forceUpdate();
    }
  },
  watch: {
    itemSchedule(newData) {
      const that = this;
      that.itemData = JSON.parse(JSON.stringify(newData));
    },
    cycleData(newData) {
      const that = this;
      that.itemCycleData = JSON.parse(JSON.stringify(newData));
    }
  },
  mounted() {
    const that = this;
    that.itemData = JSON.parse(JSON.stringify(that.itemSchedule));
    that.itemCycleData = JSON.parse(JSON.stringify(that.cycleData));
  }
};
</script>
<style lang="less" scoped>
.tablist {
  margin-bottom: 16px;
  &:focus {
    outline: none;
    border: none;
  }
}
.right-content-top {
  height: 40px;
  line-height: 40px;
  border-bottom: 1px solid #ebeef5;
  outline: none;
  .right-top-wrap_left {
    &:focus {
      outline: none;
      border: none;
    }
    ul {
      position: relative;
      width: 100%;
      .el-tabs__active-bar {
        display: block;
        position: absolute;
        bottom: 0;
        left: 0;
        height: 2px;
        background-color: #1890ff;
        z-index: 1;
        transition: transform 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
        list-style: none;
      }
    }
    li {
      width: 50%;
      cursor: pointer;
      .right-top-title {
        font-size: 14px;
        color: #606266;
        height: 100%;
      }

      &.active-li {
        .right-top-title {
          color: #1890ff;
        }
      }
    }
  }
  .right-top-wrap_right {
    padding: 9px 0;
  }
}

.day-tabs {
  .p-lr-20 {
    padding: 0 20px;
  }
  .day-item-title {
    color: rgba(0, 0, 0, 0.43);
    font-size: 12px;
    text-align: center;
    line-height: 1.5;
    padding: 8px 16px;
    transition: all 0.3s;
  }

  .day-class-item {
    font-size: 12px;
    line-height: 42px;
    height: 42px;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .class-item {
    color: #fff;
    height: 26px;
    text-align: center;
    line-height: 26px;
    background-color: #2db7f5;
    text-overflow: ellipsis;
    overflow: hidden;
    border-radius: 0;
    white-space: nowrap;
    &:hover {
      opacity: 0.7;
    }
    &.day-tag-blue {
      background: #5e90ff;
      border-color: #5e90ff;
      color: #fff;
    }
    &.day-tag-red {
      background: #fb6370;
      border-color: #fb6370;
      color: #fff;
    }
    &.day-tag-purple {
      background: #7e6bd4;
      border-color: #7e6bd4;
      color: #fff;
    }
    &.day-tag-orange {
      background: #ff943e;
      border-color: #ff943e;
      color: #fff;
    }
    &.day-tag-green {
      background: #15bc83;
      border-color: #15bc83;
      color: #fff;
    }
    &.day-tag-pink {
      background: #ed4dae;
      border-color: #ed4dae;
      color: #fff;
    }
    &.class-rest {
      background: #f5f5f5 !important;
      color: #00bc85 !important;
    }
    &.class-clear {
      background: #f5f5f5 !important;
      color: #fb5549 !important;
    }
  }
}
</style>
