<!--
 * @Descripttion: 当前组件信息-即时奖励设置
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-11-21 16:37:02
 * @LastEditors: 无尘
 * @LastEditTime: 2019-12-15 14:28:02
 -->
<!--
<spot-award></spot-award>

import spotAward from '@/components/active-config/configs/spot-award.vue';
-->

<template>
  <div class="spot-award border-box">
    <el-form
      label-width="154px"
      :model="instanceAward"
      ref="rewardForm"
      :rules="rules"
    >
      <el-form-item
        label="即时奖励模式："
        prop=""
      >
        <ul class="flex ">
          <template v-for="(item, index) in modeData">
            <li
              v-if="index != 2"
              :class="['li-cell li-second-cell flex-1 ', instanceAward.instanceMade == item.goalId ? 'li-second-active' : '']"
              :key="index + 'first'"
              @click="selectStore(item)"
            >
              <div class="li-cell-inner">
                <div class="li-cell-bottom border-box">
                  <div class="li-cell-scene">
                    <div class="li-cell-scene_title font-14 color-303133">{{ item.goalName }}</div>
                    <div class="li-cell-scene_text font-14 color-606266">{{ item.describe }}</div>
                  </div>
                </div>
              </div>
              <div class="common-cell-check">
                <div class="common-cell-triangle">
                  <div class="common-cell-outer">
                    <i class="el-icon-upload-success el-icon-check"></i>
                  </div>
                </div>
              </div>
            </li>
            <li
              v-if="basePath.map(ele => ele.transName).includes('消费') && index == 2 && instanceAward.stepType == 6"
              :class="['li-cell li-second-cell flex-1 ', instanceAward.instanceMade == item.goalId ? 'li-second-active' : '']"
              :key="index + 'first'"
              @click="selectStore(item)"
            >
              <div class="li-cell-inner">
                <div class="li-cell-bottom border-box">
                  <div class="li-cell-scene">
                    <div class="li-cell-scene_title font-14 color-303133">{{ item.goalName }}</div>
                    <div class="li-cell-scene_text font-14 color-606266">{{ item.describe }}</div>
                  </div>
                </div>
              </div>
              <div class="common-cell-check">
                <div class="common-cell-triangle">
                  <div class="common-cell-outer">
                    <i class="el-icon-upload-success el-icon-check"></i>
                  </div>
                </div>
              </div>
            </li>
          </template>
        </ul>
        <div
          v-if="instanceAward.instanceMade == 1"
          class="store-select-wrap"
        >
          <div class="store-select-title">
            <span class="font-14 color-606266">定额奖励额度</span>
            <el-input-number
              :disabled="$route.query.editFlag"
              v-model="instanceAward.instancePoint"
              controls-position="right"
              :step="1"
              :step-strictly="true"
              :min="1"
              :max="999999"
            > </el-input-number>
            <span class="font-14 color-606266">积分</span>
          </div>
        </div>
        <div
          v-if="instanceAward.instanceMade == 2"
          class="store-select-wrap w-845"
        >
          <div class="store-select-title">
            <span class="font-14 color-606266">阶梯奖励内容:</span>
            <el-table
              :data="instanceAward.instanceContent"
              style="width: 100%"
              :row-class-name="tableRowClassName"
            >
              <el-table-column
                label="区间"
                width="455"
              >
                <template slot-scope="scope">
                  <div class="cell">
                    <div v-if="scope.$index == 0">
                      <span class="font-14 color-606266 p-r-10">前</span>
                      <el-input-number
                        :disabled="$route.query.editFlag"
                        v-model="scope.row.end"
                        controls-position="right"
                        :step="1"
                        :step-strictly="true"
                        :min="1"
                        :max="999999"
                        @change="changeEndNum($event,scope.$index,scope.row)"
                      > </el-input-number>
                      <span class="font-14 color-606266 p-l-10">人</span>
                    </div>
                    <div v-if="scope.$index != 0 && scope.$index != instanceAward.instanceContent.length - 1">
                      <span class="font-14 color-606266 p-r-10">第</span>
                      <el-input-number
                        :disabled="true"
                        v-model="scope.row.start"
                        controls-position="right"
                        :step="1"
                        :step-strictly="true"
                        :min="1"
                        :max="999999"
                      > </el-input-number>
                      <span class="font-14 color-606266 p-l-10 p-r-10">人 至</span>
                      <el-input-number
                        :disabled="$route.query.editFlag"
                        v-model="scope.row.end"
                        controls-position="right"
                        :step="1"
                        :step-strictly="true"
                        :min="1"
                        :max="999999"
                        @change="changeEndNum($event,scope.$index,scope.row)"
                      > </el-input-number>
                      <span class="font-14 color-606266 p-l-10">人</span>
                    </div>
                    <div v-if="scope.$index != 0 && scope.$index == instanceAward.instanceContent.length - 1">
                      <span class="font-14 color-606266 p-r-10">第</span>
                      <el-input-number
                        :disabled="true"
                        v-model="scope.row.start"
                        controls-position="right"
                        :step="1"
                        :step-strictly="true"
                        :min="1"
                        :max="999999"
                      > </el-input-number>
                      <span
                        v-if="!scope.row.infinity"
                        class="font-14 color-606266 p-l-10 p-r-10"
                      >人 至</span>
                      <el-input-number
                        :disabled="$route.query.editFlag"
                        v-if="!scope.row.infinity"
                        v-model="scope.row.end"
                        controls-position="right"
                        :step="1"
                        :step-strictly="true"
                        :min="1"
                        :max="999999"
                      > </el-input-number>
                      <el-checkbox
                        :disabled="$route.query.editFlag"
                        v-if="scope.$index == instanceAward.instanceContent.length - 1"
                        class="m-l-10"
                        v-model="scope.row.infinity"
                        @change="changeEnd($event, scope.row)"
                      >无穷大</el-checkbox>
                    </div>
                  </div>
                </template>
              </el-table-column>
              <el-table-column label="奖励积分">
                <template slot-scope="scope">
                  <div class="cell">
                    <el-input-number
                      :disabled="$route.query.editFlag"
                      v-model="scope.row.point"
                      controls-position="right"
                      :step="1"
                      :step-strictly="true"
                      :min="1"
                      :max="999999"
                    > </el-input-number>
                    <el-button
                      :disabled="$route.query.editFlag"
                      v-if="scope.$index != 0"
                      class="m-l-16"
                      @click.stop="deleteRow(scope.$index, instanceAward.instanceContent)"
                      type="text"
                      size="small"
                    >
                      删除
                    </el-button>
                  </div>
                </template>
              </el-table-column>
            </el-table>
            <div
              class="add-line text-center cursor-pointer"
              @click="toAddItem"
            ><i class="el-icon-plus color-1890ff"></i><span class="color-1890ff font-14 p-l-10">添加奖项</span></div>
          </div>
        </div>
        <div
          v-if="instanceAward.instanceMade == 3"
          class="store-select-wrap"
        >
          <div class="store-select-title">
            <span class="font-14 color-606266">成交额等比奖励额度：</span>
            <span class="font-14 color-606266">每消费</span>
            <el-input-number
              :disabled="$route.query.editFlag"
              v-model="instanceAward.instanceNumber"
              controls-position="right"
              :step="1"
              :step-strictly="true"
              :min="1"
              :max="999"
            > </el-input-number>
            <span class="font-14 color-606266 p-l-3 p-r-10">元 - </span>
            <el-input-number
              :disabled="$route.query.editFlag"
              v-model="instanceAward.instancePoint"
              controls-position="right"
              :step="1"
              :step-strictly="true"
              :min="1"
              :max="999"
            > </el-input-number>
            <span class="font-14 color-606266">积分</span>
          </div>
        </div>
      </el-form-item>
      <el-form-item
        v-if="(instanceAward.stepType == 6 && awardMode == 2) || awardMode == 1"
        class="m-t-20"
        label="即时奖励内容："
        prop=""
      >
        <ul class="flex  ">
          <template v-for="(item, index) in contentData">
            <li
              :class="['li-cell li-second-cell flex-1 ', instanceAward.awardLimitType == item.goalId ? 'li-second-active' : '']"
              :key="index + 'first'"
              @click="selectMoney(item)"
            >
              <div class="li-cell-inner">
                <div class="li-cell-bottom border-box">
                  <div class="li-cell-scene">
                    <div class="li-cell-scene_title font-14 color-303133">{{ item.goalName }}</div>
                    <div class="li-cell-scene_text font-14 color-606266">{{ item.describe }}</div>
                  </div>
                </div>
              </div>
              <div class="common-cell-check">
                <div class="common-cell-triangle">
                  <div class="common-cell-outer">
                    <i class="el-icon-upload-success el-icon-check"></i>
                  </div>
                </div>
              </div>
            </li>
          </template>
        </ul>
        <div
          v-if="instanceAward.awardLimitType == 2"
          class="store-select-wrap child-select-wrap"
        >
          <div class="store-select-title">
            <span class="font-14 color-606266">限制次数：</span>
            <el-input-number
              :disabled="$route.query.editFlag"
              v-model="instanceAward.awardLimitNumber"
              controls-position="right"
              :step="1"
              :step-strictly="true"
              :min="1"
              :max="999999"
            > </el-input-number>
            <span class="font-14 color-606266 p-l-3 p-r-10">次</span>
          </div>
        </div>
        <div
          v-if="instanceAward.awardLimitType == 3"
          class="store-select-wrap child-select-wrap"
        >
          <div class="store-select-title">
            <span class="font-14 color-606266">限制额度：</span>
            <el-input-number
              class="w-140"
              :disabled="$route.query.editFlag"
              v-model="instanceAward.awardLimitPoint"
              controls-position="right"
              :step="1"
              :step-strictly="true"
              :min="1"
              :max="9999999999"
            > </el-input-number>
            <span class="font-14 color-606266 p-l-3 p-r-10">积分</span>
          </div>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  name: 'spot-award',
  props: {
    value: {
      type: [Object, Array],
      default() {
        return {
          instanceMade: 1, //即时奖励模式 1、定额奖励，2阶梯奖励，3成交额等比奖励
          instancePoint: 1, //奖励积分（定额中的积分，等比中的积分）
          instanceNumber: 1, //等比奖励种的每多少元
          instanceContent: [
            {
              start: 0,
              end: 5,
              point: 1,
              infinity: false
            },
            {
              start: 10,
              end: 20,
              point: 1,
              infinity: false
            }
          ], //即时奖励内容（阶梯内容） 奖励内容json字符串
          awardLimitType: 1, //奖励限制类型 1、不限制奖励（推荐），2、限制奖励次数，3、限制奖励额度
          awardLimitNumber: 1, //限制奖励次数
          awardLimitPoint: 1, //限制奖励额度
          "stepType": 1 //步骤类型 类型：1服务号关注，2活动营销页访问奖励，3会员认证奖励，4游戏参与奖励，5卡券领取奖励6，消费奖励
        };
      }
    },
    awardMode: {
      type: [String, Number],
      default() {
        return '1'
      }
    }
  },
  components: {},
  data() {
    return {
      basePath: [],
      instanceAward: {
        instanceMade: 1, //即时奖励模式 1、定额奖励，2阶梯奖励，3成交额等比奖励
        instancePoint: 1, //奖励积分（定额中的积分，等比中的积分）
        instanceNumber: 1, //等比奖励种的每多少元
        instanceContent: [
          {
            start: 0,
            end: 5,
            point: 1,
            infinity: false
          },
          {
            start: 10,
            end: 20,
            point: 1,
            infinity: false
          }
        ], //即时奖励内容（阶梯内容） 奖励内容json字符串
        awardLimitType: 1, //奖励限制类型 1、不限制奖励（推荐），2、限制奖励次数，3、限制奖励额度
        awardLimitNumber: 1, //限制奖励次数
        awardLimitPoint: 1, //限制奖励额度
        stepType: 1 //步骤类型 类型：1服务号关注，2活动营销页访问奖励，3会员认证奖励，4游戏参与奖励，5卡券领取奖励6，消费奖励
      },
      modeData: [
        {
          goalId: 1,
          goalName: '定额奖励（推荐）',
          describe: '固定额度奖励'
        },
        {
          goalId: 2,
          goalName: '阶梯奖励',
          describe: '累计邀请人数越多，奖励也越高'
        },
        {
          goalId: 3,
          goalName: '成交额等比奖励',
          describe: '按受邀人成交额的一定比例来奖励'
        }
      ],
      contentData: [
        {
          goalId: 1,
          goalName: '不限制奖励（推荐）',
          describe: '同一个受邀人无论消费多少次，邀请人都能获得相应额度的奖励。'
        },
        {
          goalId: 2,
          goalName: '限制奖励次数',
          describe: '限制单个受邀人的消费订单数，超过该订单额度之后，邀请人将无法获得该受邀人的奖励。'
        },
        {
          goalId: 3,
          goalName: '限制奖励额度',
          describe: '限制单个受邀人能给邀请人触发的奖励，超过该额度之后，邀请人将无法获得该受邀人的奖励。'
        }
      ]
    };
  },
  methods: {
    /**
     * 表单提交
     */
    async submitForm() {
      const that = this;
      return new Promise(function(resolve, reject) {
        that.$refs['rewardForm'].validate(async valid => {
          if (valid) {
            that.$emit('input', that.instanceAward);
            if(that.instanceAward.instanceMade == 1 && !that.instanceAward.instancePoint){
              resolve(false);
            }
            if(that.instanceAward.instanceMade == 3 && (!that.instanceAward.instancePoint || !that.instanceAward.instanceNumber)){
              resolve(false);
            }
            if(that.instanceAward.instanceMade == 2 && !that.instanceAward.instanceContent.length){
              resolve(false);
            }
            // 阶梯奖励没有勾选无穷大判断
            if(that.instanceAward.instanceMade == 2 && that.instanceAward.instanceContent.length>1&&!that.instanceAward.instanceContent[that.instanceAward.instanceContent.length-1].infinity) {
              resolve(false);
            }
            if(that.instanceAward.instanceMade == 2 && that.instanceAward.instanceContent.length){
              let flag = true;
              that.instanceAward.instanceContent.forEach(ele => {
                if ((!ele.start && ele.start!=0)  || (!ele.end && ele.end != -1) || !ele.point) {
                  flag = false;
                }
              })
              console.log(flag)
              if (!flag) {
                resolve(false);
              }
            }
            // 阶梯奖励中顺序值判断
            if(that.instanceAward.instanceMade == 2 && that.instanceAward.instanceContent.length>1){
              let flag = true;
              const arr = [];
              that.instanceAward.instanceContent.forEach(ele => {
                if ((!ele.start&&ele.start!=0) || (!ele.end && ele.end != -1) || !ele.point) {
                  flag = false;
                }
                arr.push(ele.start);
                if(ele.end != -1) {
                  arr.push(ele.end);
                }
              });

              for(let i = 0; i<arr.length-1; i++) {
                for(let j = i+1; j<arr.length; j++){
                  if(arr[i]>=arr[j]){
                    flag = false;
                    break;
                  }
                }
                if (!flag) {
                  break;
                }
              }
              if (!flag) {
                resolve(false);
              }
            }
            if(that.instanceAward.awardLimitType == 2 && !that.instanceAward.awardLimitNumber){
              resolve(false);
            }
            if(that.instanceAward.awardLimitType == 3 && !that.instanceAward.awardLimitPoint){
              resolve(false);
            }
            resolve(that.instanceAward);
          } else {
            resolve(false);
          }
        });
      });
    },
    /**
     * 选择即时奖励模式：
     */
    selectStore(item) {
      const that = this;
      if (that.$route.query.hasOwnProperty('editFlag')) {
        return false;
      }
      that.instanceAward.instanceMade = item.goalId;
    },
    /**
     * 选择即时奖励内容：
     */
    selectMoney(item) {
      const that = this;
      if (that.$route.query.hasOwnProperty('editFlag')) {
        return false;
      }
      that.instanceAward.awardLimitType = item.goalId;
    },
    /**
     * 阶梯奖励无穷大奖项：
     */
    changeEnd(e, row) {
      row.end = e ? -1 : '';
    },
    /**
     * 阶梯奖励end奖项：
     */
    changeEndNum(e, index, row) {
      const that = this;
      if (index < that.instanceAward.instanceContent.length-1) {
        that.instanceAward.instanceContent[index +1].start = e + 1;
      }
    },
    /**
     * 阶梯奖励添加奖项：
     */
    toAddItem() {
      const that = this;
      if(that.instanceAward.instanceContent.length < 2 || !that.instanceAward.instanceContent.map(ele=>ele.infinity).includes(true)) {
        that.instanceAward.instanceContent.push({
          start: that.instanceAward.instanceContent.length == 0? 0 : that.instanceAward.instanceContent[that.instanceAward.instanceContent.length-1].end +1,
          end: 20,
          point: 1,
          infinity: false
        });
        return false;
      }
      if(that.instanceAward.instanceContent.length >= 2 && that.instanceAward.instanceContent.map(ele=>ele.infinity).includes(true)) {
        that.instanceAward.instanceContent.splice(-1, 0, {
          start: that.instanceAward.instanceContent[that.instanceAward.instanceContent.length-2].end + 1,
          end: 20,
          point: 1,
          infinity: false
        });
      }

    },
    /**
     * 阶梯奖励删除奖项：
     */
    deleteRow(index, arr) {
      arr.splice(index, 1);
    }
  },
  watch: {
    value(newData) {
      const that = this;
      that.instanceAward = JSON.parse(JSON.stringify(newData));
    }
  },
  mounted() {
    const that = this;
    that.instanceAward = JSON.parse(JSON.stringify(that.value));
    if (Object.keys(that.$store.state.baseInfo).length) {
      that.basePath = JSON.parse(JSON.stringify(that.$store.state.baseInfo)).router;
    }
  }
};
</script>
<style lang="less" scoped>
.p-l-3 {
  padding-left: 3px;
}
.p-l-10 {
  padding-left: 10px;
}
.p-r-10 {
  padding-right: 10px;
}
.m-l-10 {
  margin-left: 10px;
}
.m-l-16 {
  margin-left: 16px;
}
.w-140 {
  width: 140px;
}
.spot-award {
  width: 100%;
  padding: 20px;
  background: #fff;
  .spot-award-title {
    margin: 0 auto;
    text-align: center;
    font-weight: 600;
    line-height: 30px;
    padding-top: 27px;
    margin-bottom: 48px;
  }
  .m-b-0 {
    margin-bottom: 0;
  }
  .m-t-48 {
    margin-top: 48px;
  }
  .m-t-50 {
    margin-top: 50px;
  }
  .spot-award-subtitle {
    width: 800px;
    margin: 14px auto;
    line-height: 20px;
    font-weight: 500;
    text-align: center;
  }
  ul {
    .li-cell {
      position: relative;
      width: 427px;
      // height: 131px;
      background: rgba(255, 255, 255, 1);
      border-radius: 4px;
      border: 1px solid rgba(220, 223, 230, 1);
      margin-left: 10px;
      cursor: pointer;
      max-width: 30%;
      transition: all 0.3s;
      &:hover {
        -webkit-transform: translateY(-10px);
        -moz-transform: translateY(-10px);
        -o-transform: translateY(-10px);
        transform: translateY(-10px);
        box-shadow: 0px 2px 10px 0 rgba(174, 181, 189, 0.5);
      }
      &:first-child {
        margin-left: 0;
      }
      .li-cell-inner {
        border-radius: 4px;
        width: 100%;
        height: 100%;
        .li-cell-left {
          height: 100%;
          padding-left: 30px;
          img {
            max-width: 66px;
            max-height: 66px;
          }
        }
        .li-cell-right {
          height: 100%;
          padding: 16px 36px;
          .li-cell-active_name {
            font-weight: 600;
            line-height: 25px;
          }
          .li-cell-active_describe {
            margin-top: 13px;
            font-weight: 500;
            line-height: 20px;
          }
        }
      }
      .common-cell-check {
        display: none;
        position: absolute;
        right: 0;
        bottom: 0;
        width: 22px;
        height: 22px;
        z-index: 1;
        .common-cell-triangle {
          position: relative;
          width: 100%;
          height: 100%;
          overflow: hidden;
          .common-cell-outer {
            position: absolute;
            right: -16px;
            bottom: -6px;
            width: 32px;
            height: 20px;
            background: #1890ff;
            text-align: center;
            -webkit-transform: rotate(-45deg);
            -moz-transform: rotate(-45deg);
            transform: rotate(-45deg);

            i {
              position: absolute;
              top: 0px;
              left: 7px;
              font-size: 12px;
              color: #fff;
              -webkit-transform: rotate(45deg);
              -moz-transform: rotate(45deg);
              transform: rotate(45deg);
            }
          }
        }
      }
      /* 二级 */
      &.li-second-cell {
        max-width: 234px;
        // height: 141px;
        padding: 20px;
        .li-cell-inner {
          .li-cell-bottom {
            // height: 60px;
            background: #fff;
            border-radius: 0 0 4px 4px;
            .li-cell-scene {
              .li-cell-scene_title {
                font-weight: 600;
                line-height: 20px;
              }
              .li-cell-scene_text {
                margin-top: 10px;
                font-weight: 500;
                line-height: 20px;
                /* text-overflow: ellipsis;
                display: -webkit-box;
                -webkit-line-clamp: 3; //规定几行显示省略号
                -webkit-box-orient: vertical;
                overflow: hidden; */
              }
            }
          }
        }
        .common-cell-check {
          .common-cell-triangle {
            .common-cell-outer {
              background: #1890ff;
              i {
                color: #fff;
              }
            }
          }
        }
      }
      &.li-second-active {
        border-radius: 4px;
        border: 2px solid rgba(24, 144, 255, 1);
        box-shadow: 0px 6px 8px 0px rgba(166, 181, 199, 0.34);
        .common-cell-check {
          display: block;
          .common-cell-triangle {
            border-bottom-right-radius: 1px;
          }
        }
      }
    }
  }
  .store-select-wrap {
    width: 580px;
    // height: 470px;
    margin-top: 10px;
    padding: 20px;
    background: rgba(245, 247, 250, 1);
    &.w-845 {
      width: 845px;
    }
    .add-line {
      height: 50px;
      line-height: 50px;
      background: #fff;
    }
    .el-input-number {
      width: 123px;
    }
    .store-select-body {
      margin-top: 20px;
    }
    &.child-select-wrap {
      padding-left: 20px;
      .el-input-number {
        width: 123px;
        &.w-140 {
          width: 140px;
        }
      }
    }
  }
}
</style>
