<template>
  <div>
    <el-drawer title="明日调令"
               :modal="false"
               :visible.sync="drawer"
               direction="rtl"
               size="76%">
      <div class="container">
        <div class="header">
          <p>明日调令</p>

        </div>

        <el-scrollbar ref="elScrollbar" class="scrollbar-wrapper">
          <template v-if="tomorrows && tomorrows.length > 0">
            <div class="list">
              <div v-for="(item, index) in tomorrows" :key="item.id" ref="listItem" class="list-item">
                <div class="list-item__name">
                  {{ item.equipment_id_equipments_id_relation ? item.equipment_id_equipments_id_relation.name : '' }}
                </div>
                <div class="list-item__content">
                  <el-tooltip class="item" effect="dark" :content="item.content" placement="top-start">
                    <span>{{ item.content }}</span>
                  </el-tooltip>
                </div>
                <div class="list-item__time">
                  执行时间：{{ timeFormat(item.start_time) }}至{{ timeFormat(item.end_time,'HH:mm') }}
                </div>
                <div class="list-item__last">预计时长 {{ $moment(item.end_time).diff($moment(item.start_time),'hours', true).toFixed(2) }}小时</div>
                <div class="list-item__operate">
                  <div v-for="(s, si) in status" :title="operateTitle(item, s)" class="step" @click="stepClick(item,s)">
                    <div class="step-icon">
                      <div class="step-icon__cir" :class="`step-icon__cir-${stepFormat(item.status,s.value)}`"></div>
                      <div class="step-icon__line" :class="`step-icon__line-${stepFormat(item.status,s.value)}`" v-if="si < status.length-1"></div>
                    </div>
                    <div class="step-label">{{ s.label }}</div>
                  </div>
                </div>
              </div>
            </div>
          </template>
          <template v-else>
            <el-empty description="暂无明日调令"></el-empty>
          </template>
        </el-scrollbar>
      </div>
    </el-drawer>
  </div>
</template>

<script>
export default {
  inject: ["nowTime"],
  props: {
    tomorrows: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      status: [
        {
          value: 2,
          label: "接收"
        },
        {
          value: 3,
          label: "通知开机"
        },
        {
          value: 4,
          label: "开机"
        }
      ],
      statusType: [
        {
          label: "存在运行中",
          color: "linear-gradient(to top left,#527ded, #527ded43)",
        },
        {
          label: "超时",
          color: "linear-gradient(to top left,#d15d52, #d15d5243)"
        },
        {
          label: "即将运行",
          color: "linear-gradient(to top left,#ecc069, #ecc06943)"
        }
      ],
      drawer: false,
    }
  },
  methods: {
    show () {
      this.drawer = true;
    },
    hide () {
      this.drawer = false;
    },
    stepClick (item,s,isDetail=false) {
      if (s.value !== 2) return
      if (isDetail) {
        this.$emit('step-click', {
          data: item,
          isDetail
        })
        return
      }
      const status = this.stepFormat(item.status,s.value)

      if (status === 'doing') {
        this.$emit('step-click', {
          data: item,
          isDetail
        })
      }
    },
  },
  computed: {
    time () {
      return this.nowTime()
    },

    timeFormat () {
      return function (time,format='YYYY-MM-DD HH:mm') {
        return this.$moment(time).format(format)
      }
    },

    timeStatusFormat () {
      return function (item) {
        //1 普通 2 进行中 3 倒计时 4 超时
        if (item.status === 3) {
          return 2
        }
        const diffMins = this.$moment(item.start_time).diff(this.time,"minutes");
        if (diffMins <= 5 && diffMins >= 0) {
          return 3
        }
        if (diffMins < 0) {
          return 4
        }
        return 1
      }
    },

    stepFormat () {
      return function (statue,s) {
        const doneStatus = Array.from({ length: statue },((_,i) => (i + 1)))
        if (statue === s) {
          return "doing"
        }
        if (doneStatus.find(i => i === s)) {
          return "done"
        }
        return false
      }
    },

    operateTitle () {
      return function (item, s) {
        switch (s.auth) {
          case 'receive':
            return item.receive_time
          case 'noticeOpen':
            return item.notice_open_time
          case 'open':
            return item.open_time
          default:
            return ''
        }
      }
    }
  },
}
</script>

<style scoped lang="scss">
$container-height: 60vh;
$list-height: calc(#{$container-height} - 5.33rem);
.container {
  background: #FFFFFF;
  box-shadow: 5px 0px 20px 0px #234382;
  border-radius: 0.67rem;

  position: relative;
  margin: 3rem 3.62rem 0 3.62rem;
}
.list-item {
  transition: all .4s;
}
.list-item-enter-from {
  opacity: 0;
}
.header {
  font-size: 1.15rem;
  height: 5.33rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: #F5F5F5;
  border-radius: .67rem .67rem 0 0 ;

  padding: 0 2rem;

  p {
    font-weight: 600;
  }
  .type {
    display: flex;

    &-item {
      display: flex;
      justify-content: center;
      align-items: center;

      margin-left: 2rem;
      &__color {
        width: 1.6rem;
        height: 1.6rem;
        border-radius: 3px;

        margin-right: .6rem;
      }
      &__label {
        font-weight: normal;
      }
    }
  }
}
.list {
  height: $list-height;

  &-item {
    font-size: 1.15rem;
    display: flex;
    align-items: center;
    border-bottom: 1px #fff solid;

    padding: 1.2rem 2rem;

    &:nth-last-child(1) {
      border-radius: 0 0 .67rem .67rem;
      border-bottom: none;
    }

    $bkg-colors: #fff,linear-gradient(to bottom,#527ded18, #527ded28),linear-gradient(to bottom,#ecc06918, #ecc06928),linear-gradient(to bottom,#d15d5218, #d15d5228);
    @for $i from 1 through length($bkg-colors) {
      &-#{$i} {
        background: nth($bkg-colors,$i);

        @if $i == 3 {
          border: 1px solid #d15d52aa;
          border-bottom: 1px solid #d15d52aa !important;

          & + & {
            border-top: none;
          }
        }
      }
    }
    &__status {
      display: flex;
      align-items: center;
      font-weight: 600;
      flex-basis: 12%;

      $font-colors: #333, #527ded, #ecc069,#d15d52;
      $colors: #fff,linear-gradient(to top left,#527ded, #527ded43),linear-gradient(to top left,#ecc069, #ecc06943),linear-gradient(to top left,#d15d52, #d15d5243);
      @for $i from 1 through length($colors) {
        &-#{$i} {
          background: nth($colors,$i);
          width: 1.28rem;
          height: 1.28rem;
          border-radius: 3px;
          & + p {
            color: nth($font-colors,$i);
          }
        }
      }
    }
    &__name {
      font-weight: 600;
      font-size: 1.34rem;
      flex: 1;
    }
    &__content {
      flex-basis: 14%;
      word-break: keep-all;
      text-overflow: ellipsis;
      overflow: hidden;
    }
    &__time {
      flex-basis: 26.6%;
      text-align: center;
    }
    &__last {
      flex-basis: 11%;
      text-align: center;
    }
    &__operate {
      flex-basis: 24%;
      display: flex;
      align-items: center;
      justify-content: center;

      .step {
        width: 5rem;

        &-icon {
          display: flex;
          align-items: center;
          justify-content: center;

          position: relative;

          $done-color: #ec6a5e;
          $doing-color: #7ccac8;
          &__cir {
            width: 2.2rem;
            height: 2.2rem;
            background: #dcdcdc;
            border-radius: 100%;

            z-index: 2;
            position: relative;
            &-doing {
              cursor: pointer;
              transform: scale(.9, .9);
              background: $doing-color;

              position: relative;

              &::before {
                content: "";
                width: 132%;
                height: 132%;
                border-radius: 100%;
                opacity: 0.4;
                background: $doing-color;
                transform: translate(-50%,-50%);
                animation: scale infinite ease-out 4s;

                position: absolute;
                left: 50%;
                top: 50%;
              }
              @keyframes scale {
                0%,100% {
                  transform: translate(-50%,-50%);
                }
                50% {
                  transform: translate(-50%,-50%) scale(1.1,1.1);
                }
              }
            }
            &-done {
              background: $done-color;
            }
          }
          &__line {
            width: 100%;
            height: 2px;
            background: #dcdcdc;

            position: absolute;
            left: 50%;

            &-done {
              background: $done-color;
            }
          }

        }
        &-label {
          text-align: center;
          font-size: 1.05rem;
          line-height: 1.5;

          margin-top: 4px;
        }
      }
    }
    &__btn {
      font-size: 1.3rem;
      height: 2.4rem;
      width: 5rem;
      background: #a4ddf0;
      color: #333;
      border-color: #a4ddf0;
    }
  }
}
</style>
