<template>
  <div class="defineTime">
    <p class="tips">自定义时段不少于4个小时</p>
    <el-form :model="form" ref="defineTime">
      <el-form-item class="mt10" v-for="(v, i) in form.timeRangeList" :key="i" :prop="'timeRangeList.' + i + '.timeRange'" :rules="[{ validator: validateTime, trigger: 'change' }]">
        <div style="display: flex;align-items: center;">
          <el-time-select
            placeholder="起始时间"
            v-model="v.startTime"
            :picker-options="{
              start: '09:00',
              step: '00:30',
              end: '20:00'
            }"
          >
          </el-time-select>
          <el-time-select
            placeholder="结束时间"
            v-model="v.endTime"
            :picker-options="{
              start: '09:00',
              step: '00:30',
              end: '20:00',
              minTime: startTime
            }"
          >
          </el-time-select>

          <div class="delIcon" type="text" @click="delTimeRange(i)">
            <i class="iconfont icon-Delete"></i>
          </div>
        </div>
      </el-form-item>
    </el-form>
    <el-button type="text" @click="addTimeRange"> <i class="iconfont icon-Plus"></i> 添加时间段 </el-button>
  </div>
</template>

<script>
export default {
  data() {
    const validateTime = (rule, value, callback) => {
      //   if (!value) {
      //     return callback(new Error('请输入时间'));
      //   } else {
      //     callback();
      //   }
      callback();
    };
    return {
      validateTime,
      form: {
        timeRangeList: [{}]
      }
    };
  },
  methods: {
    // 删除兑换时段
    delTimeRange(index) {
      this.form.timeRangeList.splice(index, 1);
    },
    // 添加兑换时段
    addTimeRange() {
      if (this.form.timeRangeList.length >= 5) {
        this.$message.error('最多五个时间段');
        return false;
      }
      this.form.timeRangeList.push({});
    },
    submit() {
      return new Promise(resolve => {
        this.$refs.defineTime.validate(val => {
          if (val) {
            resolve(this.form);
          } else {
            resolve(false);
          }
        });
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.defineTime {
  width: 400px;
  //   height: 164px;
  background: #f7f8fa;
  border-radius: 4px;
  padding: 16px;
  box-sizing: border-box;
}
.tips {
  font-size: 12px;
  font-weight: 400;
  color: #909399;
  line-height: 17px;
}
</style>
