<!--
 * @Descripttion: 当前组件信息-闯关奖励设置
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-11-21 16:37:02
 * @LastEditors: 无尘
 * @LastEditTime: 2020-12-21 17:16:18
 -->
<!--
<entry-award></entry-award>

import entryAward from '@/components/active-config/configs/entry-award.vue';
-->

<template>
  <div class="entry-award border-box">
    <el-form
      label-width="154px"
      :model="passAward"
      ref="rewardForm"
      :rules="rules"
    >
      <el-form-item
        label="闯关指标（多选）："
        prop=""
      >
        <ul class="flex ">
          <template v-for="(item, index) in modeData">
            <li
              v-if="index == 0"
              :class="['li-cell li-second-cell flex-1 ', passAward.dealNumberFlag == 1 ? '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="index == 1 && basePath.map(ele => ele.transName).includes('消费') && passAward.stepType == 6"
              :class="['li-cell li-second-cell flex-1 ', passAward.dealAmountFlag == 1 ? '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>
      </el-form-item>
      <el-form-item
        v-show="passAward.dealNumberFlag == 1"
        class="m-t-20"
        label="完成人数闯关奖项："
        prop=""
      >
        <el-table
          :data="passAward.dealNumberSettingParam"
          style="width: 100%"
        >
          <el-table-column label="条件">
            <template slot-scope="scope">
              <div class="cell">
                <div>
                  <span class="font-14 color-606266 p-r-10">满</span>
                  <el-input-number
                    class="w-116"
                    :disabled="$route.query.editFlag || scope.row.oldData"
                    v-model="scope.row.achieve"
                    controls-position="right"
                    :step="1"
                    :step-strictly="true"
                    :min="1"
                    :max="999999"
                  > </el-input-number>
                  <span class="font-14 color-606266 p-l-10">人</span>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="奖品">
            <template slot-scope="scope">
              <div class="cell flex flex-align-center">
                <div>
                  <div>
                    <el-checkbox
                      v-model="scope.row.pointCheck"
                      :disabled="$route.query.editFlag || scope.row.oldData"
                    >积分</el-checkbox>
                    <el-input-number
                      class="w-146"
                      :disabled="$route.query.editFlag || scope.row.oldData"
                      v-model="scope.row.point"
                      controls-position="right"
                      :step="1"
                      :step-strictly="true"
                      :min="1"
                      :max="999999"
                    > </el-input-number>
                  </div>
                  <div class="m-t-10">
                    <el-checkbox
                      v-model="scope.row.cardCheck"
                      :disabled="$route.query.editFlag || scope.row.oldData"
                    >卡券</el-checkbox>
                    <disable-card-show v-if="$route.query.editFlag || scope.row.oldData" :cardName.sync="scope.row.cardName"></disable-card-show>
                    <select-card v-else v-model="scope.row.cardId" :cardName.sync="scope.row.cardName"></select-card>
                  </div>
                </div>
                <el-button
                  v-if="scope.$index != 0"
                  class="m-l-16"
                  :disabled="$route.query.editFlag || scope.row.oldData"
                  @click.stop="deleteRow(scope.$index, passAward.dealNumberSettingParam)"
                  type="text"
                  size="small"
                >
                  删除
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div
          class="add-line text-center cursor-pointer"
          @click="addPeopleItem"
        ><i class="el-icon-plus color-2F54EB"></i><span class="color-2F54EB font-14 p-l-10">添加奖项</span></div>
      </el-form-item>
      <el-form-item
        v-show="passAward.dealAmountFlag == 1"
        class="m-t-20"
        label="成交额闯关奖项: "
        prop=""
      >
        <el-table
          :data="passAward.dealAmountSettingParam"
          style="width: 100%"
        >
          <el-table-column label="条件">
            <template slot-scope="scope">
              <div class="cell">
                <div>
                  <span class="font-14 color-606266 p-r-10">满</span>
                  <el-input-number
                    class="w-116"
                    :disabled="$route.query.editFlag || scope.row.oldData"
                    v-model="scope.row.achieve"
                    controls-position="right"
                    :step="1"
                    :step-strictly="true"
                    :min="1"
                    :max="999999"
                  > </el-input-number>
                  <span class="font-14 color-606266 p-l-10">元</span>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="奖品">
            <template slot-scope="scope">
              <div class="cell flex flex-align-center">
                <div>
                  <div>
                    <el-checkbox
                      v-model="scope.row.pointCheck"
                      :disabled="$route.query.editFlag || scope.row.oldData"
                    >积分</el-checkbox>
                    <el-input-number
                      class="w-146"
                      :disabled="$route.query.editFlag || scope.row.oldData"
                      v-model="scope.row.point"
                      controls-position="right"
                      :step="1"
                      :step-strictly="true"
                      :min="1"
                      :max="999999"
                    > </el-input-number>
                  </div>
                  <div class="m-t-10">
                    <el-checkbox
                      v-model="scope.row.cardCheck"
                      :disabled="$route.query.editFlag || scope.row.oldData"
                    >卡券</el-checkbox>

                    <disable-card-show v-if="$route.query.editFlag || scope.row.oldData" :cardName.sync="scope.row.cardName"></disable-card-show>
                    <select-card v-else v-model="scope.row.cardId" :cardName.sync="scope.row.cardName"></select-card>
                  </div>
                </div>
                <el-button
                  v-if="scope.$index != 0"
                  class="m-l-16"
                  :disabled="$route.query.editFlag || scope.row.oldData"
                  @click.stop="deleteRow(scope.$index, passAward.dealAmountSettingParam)"
                  type="text"
                  size="small"
                >
                  删除
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div
          class="add-line text-center cursor-pointer"
          @click="addMoneyItem"
        ><i class="el-icon-plus color-2F54EB"></i><span class="color-2F54EB font-14 p-l-10">添加奖项</span></div>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
/* import errMsg from '@/common/js/error';
import { getRequest } from '@/api/api'; */

import selectCard from '@/components/active-config/configs/select-card.vue';
import disableCardShow from '@/components/active-config/configs/disable-card-show.vue';
export default {
  name: 'entry-award',
  props: {
    value: {
      type: [Object, Array],
      default() {
        return {
          dealNumberFlag: 1, //成交人数开关 1开启
          dealAmountFlag: 0, //成交额开关 1开启
          dealNumberSettingParam: [
            {
              achieve: 5,
              point: 1,
              pointCheck: false,
              cardId: '',
              cardName: '',
              cardCheck: false,
              oldData: false
            }
          ], //成交人数奖项 成交人数奖项json
          dealAmountSettingParam: [
            {
              achieve: 5,
              point: 1,
              pointCheck: false,
              cardId: '',
              cardName: '',
              cardCheck: false,
              oldData: false
            }
          ], //成交额奖项 成交额奖项json
          "stepType": 1 //步骤类型 类型：1服务号关注，2活动营销页访问奖励，3会员认证奖励，4游戏参与奖励，5卡券领取奖励6，消费奖励
        };
      }
    }
  },
  components: { selectCard, disableCardShow },
  data() {
    return {
      basePath: [],
      passAward: {
        dealNumberFlag: 1, //成交人数开关 1开启
        dealAmountFlag: 0, //成交额开关 1开启
        dealNumberSettingParam: [], //成交人数奖项 成交人数奖项json
        dealAmountSettingParam: [], //成交额奖项 成交额奖项json
        stepType: 1 //步骤类型 类型：1服务号关注，2活动营销页访问奖励，3会员认证奖励，4游戏参与奖励，5卡券领取奖励6，消费奖励
      },
      modeData: [
        {
          goalId: 1,
          goalName: '完成人数（推荐）',
          describe: '以完成活动所设置路径的受邀人数量作为计数指标',
          check: false
        },
        {
          goalId: 2,
          goalName: '成交额',
          describe: '以受邀人的总消费金额作为计数指标（单个受邀人多次消费作累计计数）',
          check: false
        }
      ],
      // cardList: [] //卡券数据
    };
  },
  methods: {
    /**
     * 表单提交
     */
    submitForm() {
      const that = this;
      return new Promise(function(resolve, reject) {
        that.$refs['rewardForm'].validate(valid => {
          if (valid) {
            /* if (that.passAward.dealNumberFlag == 1  && that.passAward.dealNumberSettingParam.length) {
              that.passAward.dealNumberSettingParam.forEach(ele => {
                if (!ele.cardCheck && ele.cardId){
                  ele.cardId = '';
                  ele.cardName = '';
                }

                if(!ele.pointCheck && ele.point) {
                  ele.point = '';
                }
              });
            }
            if (that.passAward.dealAmountFlag == 1  && that.passAward.dealAmountSettingParam.length) {
              that.passAward.dealAmountSettingParam.forEach(ele => {
                if (!ele.cardCheck && ele.cardId){
                  ele.cardId = '';
                  ele.cardName = '';
                }

                if(!ele.pointCheck && ele.point) {
                  ele.point = '';
                }
              });
            } */
            that.$emit('input', that.passAward);
            if (that.passAward.dealNumberFlag == 0 && that.passAward.dealAmountFlag == 0) {
              resolve({flag: false, msg: '请完善闯关奖励'});
            }
            if (that.passAward.dealNumberFlag == 1 && !that.passAward.dealNumberSettingParam.length) {
              resolve({flag: false, msg: '请完善闯关奖励中成交人数奖项'});
            }
            if (that.passAward.dealNumberFlag == 1  && that.passAward.dealNumberSettingParam.length) {
              let flag = true;
              that.passAward.dealNumberSettingParam.forEach(ele => {
                if ((ele.cardCheck && !ele.cardId) || (!ele.pointCheck && !ele.cardCheck) || !ele.achieve) {
                  flag = false;
                }
              });
              if (!flag) {
                resolve({flag: false, msg: '请完善闯关奖励中成交人数奖项'});
              }
            }
            if (that.passAward.dealNumberFlag == 1 && that.passAward.dealNumberSettingParam.length>1) {
              let flag = true
              const arr = [];
              that.passAward.dealNumberSettingParam.forEach(ele => {
                arr.push(ele.achieve);
                if (ele.cardCheck && !ele.cardId) {
                  flag = false;
                }
              });
              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({flag: false, msg: '请按顺序填写闯关奖励中成交人数奖项中条件值'});
              }
            }
            if (that.passAward.dealAmountFlag == 1 && !that.passAward.dealAmountSettingParam.length) {
              resolve({flag: false, msg: '请完善闯关奖励中成交额奖项'});
            }
            if (that.passAward.dealAmountFlag == 1  && that.passAward.dealAmountSettingParam.length) {
              let flag = true;
              that.passAward.dealAmountSettingParam.forEach(ele => {
                if ((ele.cardCheck && !ele.cardId) || (!ele.pointCheck && !ele.cardCheck) || !ele.achieve) {
                  flag = false;
                }
              });
              if (!flag) {
                resolve({flag: false, msg: '请完善闯关奖励中成交额奖项'});
              }
            }
            if (that.passAward.dealAmountFlag == 1 && that.passAward.dealAmountSettingParam.length>1) {
              let flag = true;
              const arr = [];
              that.passAward.dealAmountSettingParam.forEach(ele => {
                arr.push(ele.achieve);
                if (ele.cardCheck && !ele.cardId) {
                  flag = false;
                }
              });
              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({flag: false, msg: '请按顺序填写闯关奖励中成交额奖项中条件值'});
              }
            }
            resolve(that.passAward);
          } else {
            resolve(false);
          }
        });
      });
    },
    /**
     * 选择奖励模式：
     */
    selectStore(item) {
      const that = this;
      if (that.$route.query.hasOwnProperty('editFlag')) {
        return false;
      }
      if (item.goalId == 1) {
        that.passAward.dealNumberFlag = that.passAward.dealNumberFlag == 0 ? 1 : 0;
      } else {
        that.passAward.dealAmountFlag = that.passAward.dealAmountFlag == 0 ? 1 : 0;
      }
    },
    /**
     * 删除：
     */
    deleteRow(index, arr) {
      const that = this;
      if (that.$route.query.hasOwnProperty('editFlag')) {
        return false;
      }
      arr.splice(index, 1);
    },

    /**
     * 成交人添加奖项：
     */
    addPeopleItem() {
      const that = this;
      if (that.$route.query.hasOwnProperty('editFlag')) {
        return false;
      }
      that.passAward.dealNumberSettingParam.push({
        achieve: 5,
        point: 1,
        pointCheck: false,
        cardId: '',
        cardName: '',
        cardCheck: false,
        oldData: false
      });
    },
    addMoneyItem() {
      const that = this; //成交额奖项 成交额奖项json
      if (that.$route.query.hasOwnProperty('editFlag')) {
        return false;
      }
      that.passAward.dealAmountSettingParam.push({
        achieve: 5,
        point: 1,
        pointCheck: false,
        cardId: '',
        cardName: '',
        cardCheck: false,
        oldData: false
      });
    }
  },
  watch: {
    value(newData) {
      const that = this;
      that.passAward = JSON.parse(JSON.stringify(newData));
    }
  },
  mounted() {
    const that = this;
    that.passAward = 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;
}
.m-t-10 {
  margin-top: 10px;
}
.w-116 {
  width: 116px;
}
.w-146 {
  width: 146px;
}
.entry-award {
  width: 100%;
  padding: 20px;
  background: #fff;
  .entry-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;
  }
  .entry-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: 265px;
        // 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(47, 84, 235, 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;
          }
        }
      }
    }
  }
  .add-line {
    height: 50px;
    line-height: 50px;
    background: #fff;
    border-bottom: 1px solid #ebeef5;
  }
  .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;
      }
    }
  }
}
</style>
