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

<!--
  <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 day-tag-red">{{ item.serial }}</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.id == '0' ? 'class-rest' : 'class-clear']">{{ item.serial }}</p>
            </li>
          </template>
        </ul>
      </li>
    </ul>
    <ul v-if="activeTab == 2">
      <li>
        <div class="day-item-title" title="从该天开始周期排班至月底">从该天开始周期排班至月底</div>
        <ul>
          <li class="day-class-item p-lr-20" @click.stop="dayWeekSet">
            <p class="class-item day-tag-blue">{{ cycleData[0].cycleName }} {{ cycleData[0].itemList.map(item => item.className).join('-') }}</p>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</template>
<script>
export default {
  name: 'people-day-class',
  props: {
    classData: {
      type: [Array, Object],
      default() {
        return [];
      }
    },
    cycleData: {
      type: [Array, Object],
      default() {
        return [];
      }
    },
    itemSchedule: {
      type: [Array, Object],
      default() {
        return [];
      }
    },
    dates: {
      type: [Array, Object],
      default() {
        return [];
      }
    },
    workDate: {
      type: Number,
      default() {
        return new Date().getTime();
      }
    }
  },
  data() {
    return {
      activeTab: '1',
      translate: {
        width: '56px',
        transform: `translateX(30px)`
      },
      defaultData: [
        {
          id: '0',
          serial: '休息'
        },
        {
          id: '-2',
          serial: '清空'
        }
      ]
    };
  },
  methods: {
    /**
     * 选择tab
     *
     */
    chooseTab(e, tag) {
      const that = this;
      that.activeTab = tag;
      that.translate = {
        width: e.target.offsetWidth + 'px',
        transform: `translateX(${e.target.offsetLeft}px)`
      };
    },
    /**
     * 选择班次
     */
    daySet(item) {
      const that = this;
      // console.log(item);
      if (that.itemSchedule.schedules && that.itemSchedule.schedules.length) {
        let arr = that.itemSchedule.schedules.map(ele => ele.workDate);
        if (arr.includes(that.workDate)) {
          that.itemSchedule.schedules.forEach(ele => {
            if (ele.workDate == that.workDate) {
              ele.serial = item.serial;
              ele.classes = {
                id: item.id,
                serial: item.serial
              };
              ele.isRest = true;
            }
          });
          that.$forceUpdate();
        } else {
          let newData = JSON.parse(JSON.stringify(that.itemSchedule.schedules));
          newData.push({
            classes: {
              id: item.id,
              serial: item.serial
            },
            serial: item.serial,
            isRest: true,
            workDate: that.workDate
          });
          that.itemSchedule.schedules = JSON.parse(JSON.stringify(newData));
          // console.log('false:', that.itemSchedule);
        }
      } else {
        // console.log('null', that.itemSchedule);
        that.$set(that.itemSchedule, 'schedules', []);
        that.itemSchedule.schedules.push({
          classes: {
            id: item.id,
            serial: item.serial
          },
          serial: item.serial,
          isRest: true,
          workDate: that.workDate
        });
      }
      // 根据时间戳排序
      that.itemSchedule.schedules.sort(function(a, b) {
        return a.workDate - b.workDate;
      });
      // console.log('all:[]', that.itemSchedule);
      that.$forceUpdate();
    },
    /**
     * 周期
     */
    dayWeekSet() {
      const that = this;
      // 当前选择的几号,还有轮询排班周期
      let today = new Date(that.workDate).getDate();
      let newDates = []; // 当前成员 新的需要重新设置的日期数据
      for (let index = today - 1; index < that.dates.length; index++) {
        newDates.push(that.dates[index]);
      }
      let cycleLength = that.cycleData[0].itemList.length; // 排班有几个班次，相当于一个周期是几天
      // 然后在这个日期区间内按照排班周期设置
      newDates.forEach((date, index) => {
        date.serial = that.cycleData[0].itemList[index % cycleLength].className;
        date.classes = {
          id: that.cycleData[0].itemList[index % cycleLength].classId,
          serial: that.cycleData[0].itemList[index % cycleLength].className
        };
        date.isRest = true;
        date.workDate = date.timeStamp;
      });
      // 把小于当前日期已经设置的排班全部添加到周期设置的数据中（newDates）
      if (!that.itemSchedule.hasOwnProperty('schedules')) {
        that.$set(that.itemSchedule, 'schedules', []);
      }
      that.itemSchedule.schedules.forEach((ele, key) => {
        if (ele.workDate < that.workDate) {
          newDates.push(ele);
        }
      });
      // 重新根据时间戳排序
      newDates.sort(function(a, b) {
        return a.workDate - b.workDate;
      });
      that.itemSchedule.schedules = JSON.parse(JSON.stringify(newDates));
      that.$set(that.itemSchedule, 'schedules', JSON.parse(JSON.stringify(newDates)));
      // console.log(that.itemSchedule, newDates);
      that.$forceUpdate();
    }
  }
};
</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>
