<template>
  <div class="right-wrap">
    <topNavNew :navpath="navpath"></topNavNew>
    <div class="right-content" @click="selectGoodsCateHide">
      <div class="right-box">
        <div class="attention-wrap">
          <div class="form-wrap">
            <el-form
              :model="ruleForm"
              :rules="rules"
              ref="ruleForm"
              label-width="100px"
              class="demo-ruleForm"
            >
            <dm-sub-title type="fill" text="基本信息" style="margin-bottom: 20px;"/>
              <el-form-item label="策略名称" prop="strategyName" class="padding-0 padding-r-45">
                <el-input
                  v-model="ruleForm.strategyName"
                  class="w-340 padding-0"
                  @keyup.native="value => toInput(value)"
                ></el-input>
                <label class="input-label">{{ cateNameLength }}/20</label>
              </el-form-item>

              <el-form-item label="适用对象" prop class="margin-20">
                <el-select @change="changePeople" v-model="ruleForm.useCrowd" placeholder="请选择">
                  <el-option
                    v-for="item in applyObjOption"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="人群定向" v-if="ruleForm.useCrowd == 2" prop class="padding-0">
                <el-button @click="showPeople">已设置{{ selectCount }}项筛选</el-button>
              </el-form-item>

              <el-form-item label="适用条件" class="padding-0">
                <div class="title-tip">该设置只对微信商城中的积分抵现起作用。</div>
              </el-form-item>
              <dm-sub-title type="fill" text="渠道配置" style="margin-bottom: 20px; margin-top:30px;"/>
              <!-- 只获取微信的 -->
              <template v-for="(item, index) in ruleForm.channelList">
                <template v-if="item.channelType == 1">
                  <el-form-item label="达摩微商城" prop="" :key="index">
                    <el-switch v-model="item.openStatus" :active-value="1" :inactive-value="0"> </el-switch>
                    <span style="color: #909399;font-size:12px; margin-left:16px;">客户在微盟微商城产生的订单</span>
                  <div class="form-child margin-20" v-if="item.openStatus" :key="index">
                    <el-form-item label="选择商户">
                      <el-select
                        v-model="item.enterpriseRelationIdArr"
                        placeholder="请选择"
                        multiple
                        size="small"
                        class="w240"
                      >
                        <el-option
                          v-for="item in mechantOption"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value"
                        ></el-option>
                      </el-select>
                    </el-form-item>

                    <el-form-item label="适用商品" class="margin-20">
                      <el-select
                        class="fl w240"
                        style="margin-right: 10px;"
                        v-model="item.channelRelationProType"
                        placeholder="请选择"
                      >
                        <el-option
                          v-for="item in applyGoods"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value"
                        ></el-option>
                      </el-select>
                      <!-- 二级 -->

                      <!-- 商品品牌 -->
                      <el-select
                        size="small"
                        class="fl w240"
                        @change="selectGoodsStoreWx"
                        v-if="item.channelRelationProType == 2"
                        v-model="valueWx"
                        multiple
                        placeholder="请选择"
                      >
                        <el-option
                          v-for="item in goodsTreeOptionsWx"
                          :key="item.brandId"
                          :label="item.brandName"
                          :value="item.brandId"
                        ></el-option>
                      </el-select>

                      <!-- 商品品类 -->
                      <div v-if="item.channelRelationProType == 3" class="fl goods-cate">
                        <goodsCate
                          ref="selectGoodsCate"
                          :selectCateType="selectCateTypeWx"
                          :selectCateData="selectCateDataWx"
                          @getCateIds="getCateIds"
                        ></goodsCate>
                      </div>

                      <el-button
                        style="width:202px"
                        class="fl"
                        v-if="item.channelRelationProType == 4"
                        @click="showGoodsDialog('wx')"
                      >已选{{ goodsListWx.length }}件商品</el-button>
                    </el-form-item>

                    <el-form-item label="折扣控制:" prop="region" class="margin-20">
                      <div class="radio-cell">
                        <el-radio v-model="item.integralUseDiscountControl" label="1">所有折扣商品都适用</el-radio>
                      </div>
                      <div class="radio-cell margin-20 padding-bottom-20">
                        <el-radio v-model="item.integralUseDiscountControl" label="2">折扣</el-radio>
                        <!-- <el-input @focus="inputFocus(item,'integralUseDiscountControlLower')"  @change="formatInputChange($event)" v-model="item.integralUseDiscountControlLower"></el-input> -->
                        <el-input-number
                          v-model="item.integralUseDiscountControlLower"
                          :precision="2"
                          :controls="false"
                          :max="10.0"
                          :min="1"
                        ></el-input-number>
                        <label>至</label>
                        <!-- <el-input @focus="inputFocus(item,'integralUseDiscountControlUpper')"  @change="formatInputChange($event)" v-model="item.integralUseDiscountControlUpper"></el-input> -->
                        <el-input-number
                          v-model="item.integralUseDiscountControlUpper"
                          :precision="2"
                          :controls="false"
                          :max="10.0"
                          :min="1"
                        ></el-input-number>
                        <label>折商品都适用</label>
                        <span style="font-size: 13px;color: #828282;line-height: 1;padding-left: 10px;">折扣范围1-10，填写8.8，则表示“打8.8折”，也即商品原价*8.8/10=商品折后价</span>
                      </div>
                    </el-form-item>
                  </div>
                  </el-form-item>
                </template>

                <!-- 微盟渠道-->
                <template v-if="item.channelType == 9">
                  <el-form-item label="微盟渠道" prop="" :key="index">
                    <el-switch v-model="item.openStatus"> </el-switch>
                    <span style="color: #909399;font-size:12px; margin-left:16px;">客户在微盟微商城产生的订单</span>
                  <div class="form-child margin-20" v-if="item.openStatus" :key="index">
                    <el-form-item label="选择商户">
                      <el-select size="small" v-model="item.enterpriseRelationIdArr" placeholder="请选择" multiple class="w240">
                        <el-option v-for="(item, ind) in mechantOption" :key="ind" :label="item.label" :value="item.value"> </el-option>
                      </el-select>
                    </el-form-item>
                    <el-form-item label="选择店铺" class="margin-20">
                      <el-select v-model="storeValue" v-if="!isJinHua" class="w240">
                        <el-option label="所有门店" value=""></el-option>
                      </el-select>
                      <vue-gic-store-new v-else :scenesVal="scencs" :options="options" :isAdd="isAdd" :uuid.sync="wmuuid" ref="wmstoreGroup"></vue-gic-store-new>
                    </el-form-item>
                    <el-form-item label="微盟商品" class="margin-20">
                      <el-select v-model="item.channelRelationProType" placeholder="请选择" class="w240">
                        <el-option v-for="item in wMGoodOption" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                      </el-select>
                    </el-form-item>
                    <el-form-item label="折扣控制:" prop="region" class="input-margin-b margin-20 padding-bottom-20">
                      <div class="radio-cell">
                        <el-radio v-model="item.integralUseDiscountControl" label="1">所有折扣商品都适用</el-radio>
                      </div>
                      <div class="radio-cell margin-20">
                        <el-radio v-model="item.integralUseDiscountControl" label="2">折扣</el-radio>
                        <el-input-number v-model="item.integralUseDiscountControlLower" :precision="2" :controls="false" :max="10.0" :min="1"></el-input-number>
                        <label>至</label>
                        <el-input-number v-model="item.integralUseDiscountControlUpper" :precision="2" :controls="false" :max="10.0" :min="1"></el-input-number>
                        <label>折商品都适用</label>
                        <span style="font-size: 13px;color: #828282;line-height: 1;padding-left: 10px;">折扣范围1-10，填写8.8，则表示“打8.8折”，也即商品原价*8.8/10=商品折后价</span>
                      </div>
                    </el-form-item>
                  </div>
                  </el-form-item>
                </template>
              </template>
               <dm-sub-title type="fill" text="规则配置" style="margin-bottom: 20px; margin-top:40px;"/>
              <el-form-item label="抵现规则:" prop="integralUseStatus" class="margin-20" label-width="110px">
                <div>
                  <el-radio v-model="ruleForm.integralUseStatus" label="0">不支持抵现</el-radio>
                </div>
                <div class="margin-20">
                  <el-radio v-model="ruleForm.integralUseStatus" label="1">每使用</el-radio>
                  <!-- <el-input @keyup.native="formatInputNum($event)" v-model="ruleForm.integralUse.integral"></el-input> -->
                  <el-input-number v-model="ruleForm.integralUse.integral" :controls="false" step-strictly></el-input-number>
                  <label>积分,抵 </label>
                  <!-- <el-input @blur="formatInputNumBlur($event)" v-model="ruleForm.integralUse.fee"></el-input> -->
                  <el-input-number v-model="ruleForm.integralUse.fee" :precision="1" :controls="false" :max="10000.0" :min="0.1"></el-input-number>
                  <label>元</label>
                </div>
              </el-form-item>
              <el-form-item label="比例限制:" label-width="110px" prop="" :rules="{ required: true, message: '请选择抵现规则', trigger: 'blur' }">
                <label style="color: #606266">最多可抵用商品金额的 </label>
                <el-input @keyup.native="formatInputNum($event)" v-model="ruleForm.channelList[0].useProportionalRestriction"></el-input>%
              </el-form-item>
              <!-- 新增策略有效期配置 -->
              <el-form-item label="策略有效期:" prop="perpetualFlag" label-width="110px">
                <!-- 1 永久 0 非永久 -->
                <el-radio v-model="ruleForm.perpetualFlag" :label="1">永久有效</el-radio>
                <el-radio v-model="ruleForm.perpetualFlag" :label="0">
                  在该时段内有效
                </el-radio>
                <div style="margin-top:10px;">
                  <el-date-picker
                    v-if="ruleForm.perpetualFlag === 0"
                    v-model="ruleForm.perpetualDate"
                    type="datetimerange"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    format="yyyy-MM-dd HH:mm:ss"
                    value-format="timestamp"
                    :default-time="['00:00:00', '23:59:59']"
                  ></el-date-picker>
                </div>
                </el-form-item>
              <el-form-item>
                <el-row>
                  <el-button :disabled="tpnEditBool" type="primary" @click="submitForm('ruleForm')">保 存</el-button>
                  <el-button @click="goBack">返 回</el-button>
                </el-row>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>
    <!-- <vue-gic-footer></vue-gic-footer> -->
    <!-- 添加商品 -->
    <add-goods :goodsListId="goodsListId" :goodsType="goodsType" :goodShow.sync="goodShow" :projectName="repProjectName" @selectGoods="selectGoods"></add-goods>

    <!-- 人群筛选器的弹框 -->
    <el-dialog title="自定义人群" :visible.sync="dialogVisiblePeople" width="980px">
      <div class="select-people">
        <vue-gic-group-people :projectName="repProjectName" :useId="useId" :hasSearchData="hasSearchData" :sceneValue="sceneValue" ref="peopleFilter" @findFilter="findFilter" @getBackData="getBackData" @editHide="editHide" @editShow="editShow" @hideBtn="hideBtn"></vue-gic-group-people>
        <div v-show="toggleTag" class="people-btn-wrap">
          <el-row>
            <el-button @click="getData" type="primary" size="small">确定</el-button>
            <el-button @click="cancelFilter" size="small">取消</el-button>
          </el-row>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-row>
          <el-button @click="dialogVisiblePeople = false">取 消</el-button>
          <el-button type="primary" @click="getPeopleData">确 定</el-button>
        </el-row>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import topNavNew from 'components/nav/navNew';
import addGoods from 'components/memberShip/add-goods';
import goodsCate from 'components/memberShip/goodsCate';
// import the styles
import '@riophae/vue-treeselect/dist/vue-treeselect.css';

import showMsg from '@/common/js/showmsg';
import errMsg from '@/common/js/error';
import strLength from '@/common/js/strlen';
import { getRequest, postRequest, postJson } from '@/api/api';
import { log } from '@/utils/index.js';
import wmMixins from './strategy.js';

const timeStramp = {
  year: new Date().getFullYear(),
  month: new Date().getMonth(),
  day: new Date().getDate()
};

export default {
  name: 'addIntegralSytem',
  mixins: [wmMixins],
  data() {
    return {
      repProjectName: 'gic-clique',
      navpath: [
        {
          name: '集团管理',
          path: ''
        },
        {
          name: '会员制度',
          path: ''
        },
        {
          name: '积分制度',
          path: '/pointSystem'
        },
        {
          name: '编辑积分抵现策略',
          path: ''
        }
      ],
      rules: {
        strategyName: [{ required: true, message: '请填写策略名称', trigger: 'blur' }],
        integralUseStatus: [{ required: true, message: '请输入积分抵现比例限制', trigger: 'blur' }],
        perpetualFlag: [{ required: true, message: '请选择策略有效期', trigger: 'blur' }],
      },
      // 添加商品弹窗
      goodShow: false,

      // 长度
      cateNameLength: 0,
      // 输入字限制
      inputFlagNum: 20,

      // 商户
      mechantOption: [],

      applyGoods: [
        {
          label: '所有商品',
          value: '1'
        }
        // {
        //   label: '按商品品牌',
        //   value: '2',
        // },
        // {
        //   label: '按商品品类',
        //   value: '3',
        // },
        // {
        //   label: '按单件商品',
        //   value: '4',
        // },
      ],
      // 微盟渠道
      wMShopOption: [
        {
          label: '所有店铺', // 就是所有门店
          value: '1'
        }
      ],
      wMGoodOption: [
        {
          label: '所有商品',
          value: '1'
        }
      ],
      // 门店  新
      uuid: '', // 新增传空，编辑必传,
      options: [0, 1, 2, 3, 4, 5], // 0 全部 1 门店类型 2门店标签 3 门店区域 4 门店分组 5 部分门店
      isAdd: true,
      scencs: 'integral',
      wmuuid: '', // 微盟
      ruleForm: {
        // 积分获取规则
        integralStrategyId: 'ff80808163a7a09b0163b01171e202db', //策略id
        enterpriseId: 'ff8080815dacd3a2015dacd3ef5c0000',
        strategyName: 1, //策略名称
        strategyType: 1, // 策略类型 1消费积分 2积分抵现
        useCrowd: '1', // 策略类型 1所有 2人群筛选
        crowdJson: '', //人群筛选器内容
        selectorParams: '',
        integralGetStatus: '1', // 积分获取开启 1是0否
        integralGet: { fee: 11, integral: 12 },
        integralGetStrategy: '1', // 取整策略 1四舍五入 2抹零 3向上取整
        integralGetFreezeDays: 1, // 获取积分冻结天数
        integralGetEffectiveDays: 1, // 获取积分有效天
        integralUseStatus: '0', // 积分抵现开启 1是0否
        integralUse: { fee: '', integral: '' }, // 积分抵现 {fee:5,integral:5}
        perpetualFlag: 1,
        channelList: [
          {
            integralStrategyChannelId: 'ff808081649321d20164a1fafbdd01df', //id
            enterpriseId: 'ff8080815dacd3a2015dacd3ef5c0000',
            integralStrategyId: 'ff80808163a7a09b0163b01171e202db', //策略id
            channelType: '1', //渠道类型 1微信 3:线下 5淘宝 6京东 7唯品会 8天猫
            openStatus: true, //渠道配置是否启用 1启用 0停用
            channelRelationEnterpriseType: '1', // 策略关联企业 1所有企业 2部分企业
            channelRelationStoreType: '1', //策略关联门店类型 1所有门店 2门店分组 3部分门店
            channelStoreType: '-1', // 策略选择门店类型 （-1:所有 0:自营 1:联营 2:加盟,3代销4托管）
            channelRelationProType: '1', // 策略关联商品类型 1所有商品 2商品品牌 3商品类目 4部分商品
            integralUseDiscountControl: '1', // 折扣控制 1所有折扣商品适用 2折扣范围
            integralUseDiscountControlUpper: '0.0',
            integralUseDiscountControlLower: '0.0',
            useProportionalRestriction: '0.0', // 抵现比例限制
            strategyGoodsRelationIdArr: [], // 商品
            storeRelationIdArr: [], //门店
            enterpriseRelationIdArr: [] //商户
          }
        ],
        perpetualDate: [new Date(timeStramp.year, timeStramp.month, timeStramp.day, 0, 0, 0).getTime(), new Date(timeStramp.year, timeStramp.month, timeStramp.day, 23, 59, 59).getTime()]
      },

      // 适用对象
      applyObjOption: [
        {
          label: '所有会员',
          value: '1'
        },
        {
          label: '自定义人群',
          value: '2'
        }
      ],
      dialogVisiblePeople: false,

      /* 人群筛选器 */
      sceneValue: 'member', // 场景值
      useId: '',
      searchParams: '',
      hasSearchData: '', // 当前页回显的数据(接口返回)
      toggleTag: false,
      selectCount: 0,

      // 单个商品类型
      goodsType: 'wx', // 单个商品类型  wx：微信商城，line：线下渠道
      goodsListId: [],
      goodsListWx: [],
      goodsListLine: [],

      // 商品品类
      selectCateDataWx: [], // 用于回显
      selectCateDataLine: [],
      selectCateTypeWx: 'wx',
      selectCateTypeLine: 'line',
      cateDataWx: [],
      cateDataLine: [],
      storeGroupList: [],

      // 商品品牌
      goodsTreeOptionsWx: [],
      valueWx: [],
      goodsTreeOptionsLine: [],
      valueLine: [],
      // 太平鸟标识
      tpnEditBool: false,
      tpnEnterpriseId: 'ff808081671d3ceb01672adebf4e6149'
    };
  },
  created() {
    // this.selectReplayStyle()
  },
  mounted() {
    let that = this;
    log(that.$route.query.integralStrategyId);
    // 获取策略 id
    let id = that.$route.query.integralStrategyId;
    that.ruleForm.integralStrategyId = id;
    that.getInfo();

    // 商品品牌
    that.getGoodsBrand();
    that.getLoginInfo();
  },
  methods: {
    // 返回
    goBack() {
      this.$router.go(-1);
    },
    async checkListValue(data) {
      let obj = {
        flag: true,
        text: ''
      };
      if (!!data.channelList.length) {
        for (let i = 0; i < data.channelList.length; i++) {
          if (!!data.channelList[i].openStatus) {
            if (data.channelList[i].integralUseDiscountControl == 2) {
              if (!data.channelList[i].integralUseDiscountControlUpper || !data.channelList[i].integralUseDiscountControlLower) {
                obj.flag = false;
                obj.text = '请填写折扣';
              }
              if (parseInt(data.channelList[i].integralUseDiscountControlUpper) > 10 || parseInt(data.channelList[i].integralUseDiscountControlLower) > 10 || Number(data.channelList[i].integralUseDiscountControlUpper) <= Number(data.channelList[i].integralUseDiscountControlLower)) {
                obj.flag = false;
                obj.text = '折扣格式有误';
              }
            }
            if (data.channelList[i].channelType == 1 && !data.channelList[i].enterpriseRelationIdArr.length) {
              obj.flag = false;
              obj.text = '请选择商户';
            }
            if (data.channelList[i].channelType == 9 && !data.channelList[i].enterpriseRelationIdArr.length) {
              obj.flag = false;
              obj.text = '请选择商户';
            }
            if (data.channelList[i].channelType == 9 && this.isJinHua) {
              const storeGroupDom = this.$refs.wmstoreGroup;
              let flag = null;
              if (storeGroupDom instanceof Array) {
                flag = await this.$refs.wmstoreGroup[0].isStoreSave();
              } else {
                flag = await this.$refs.wmstoreGroup.isStoreSave();
              }
              if (!flag) {
                obj.flag = false;
                obj.text = '请选择门店';
              }
            }
          }
        }
      }
      return obj;
    },
    // 保存
    submitForm(formName) {
      let that = this;
      if (!that.tpnEditBool) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            // log(that.ruleForm);
            let data = JSON.parse(JSON.stringify(that.ruleForm));
            // 非空验证
            if (!data.strategyName) {
              showMsg.showmsg('请填写策略名称', 'error');
              return;
            }
            // if (!data.channelList[0].enterpriseRelationIdArr.length) {
            //   showMsg.showmsg('请选择商户', 'error');
            //   return;
            // }
            // if (data.channelList[0].integralUseDiscountControl == 2) {
            //   let flagA = Number(data.channelList[0].integralUseDiscountControlLower) >= Number(data.channelList[0].integralUseDiscountControlUpper);
            //   if (!data.channelList[0].integralUseDiscountControlLower || !data.channelList[0].integralUseDiscountControlUpper) {
            //     that.$message.error('折扣控制中的折扣不能为空');
            //     return;
            //   }
            //   if (flagA) {
            //     that.$message.error('折扣控制中的折扣格式有误');
            //     return;
            //   }
            // }
            let flag = !data.integralUse.fee || String(data.integralUse.integral) == '';
            if (data.integralUseStatus == 1 && flag) {
              that.$message.error('请填写抵现规则');
              return;
            }
            if (data.integralUseStatus == 1) {
              if (!Number(data.integralUse.fee)) {
                showMsg.showmsg('抵现规则中的抵现金额不能为0', 'error');
                return;
              }
              if (!Number(data.integralUse.integral)) {
                showMsg.showmsg('抵现规则中的使用积分不能为0', 'error');
                return;
              }
            }

            // if (!data.channelList[0].useProportionalRestriction) {
            //   showMsg.showmsg('请填写比例限制', 'error');
            //   return;
            // } else {
            //   if (data.channelList[0].useProportionalRestriction > 100) {
            //     showMsg.showmsg('比例限制不能大于100', 'error');
            //     return;
            //   }
            // }
            that.checkListValue(data).then(res => {
              if (!res.flag) {
                showMsg.showmsg(res.text, 'error');
                return;
              } else {
                let useProportionalRestriction = undefined;
                // 设置开启状态 1-->true
                data.channelList.forEach(function(ele, index) {
                  ele.openStatus = ele.openStatus == true ? 1 : 0;
                  if (ele.channelType == 1) {
                    ele.strategyGoodsRelationIdArr = ele.goodsRelationIdArr;
                    delete ele.goodsRelationIdArr;
                  }
                  if (!ele.enterpriseRelationIdArr || !ele.enterpriseRelationIdArr.length) {
                    ele.enterpriseRelationIdArr = [];
                  }
                  if (!ele.storeRelationIdArr || !ele.storeRelationIdArr.length) {
                    ele.storeRelationIdArr = [];
                  }
                  if (!ele.strategyGoodsRelationIdArr || !ele.strategyGoodsRelationIdArr.length) {
                    ele.strategyGoodsRelationIdArr = [];
                  }
                  if (ele.channelType == '9') {
                    ele.storeControlId = that.wmuuid;
                  }
                  if(index == 0) {
                    useProportionalRestriction = ele.useProportionalRestriction;
                  } else {
                    ele.useProportionalRestriction = useProportionalRestriction;
                  }
                });
                let saveBool = true;
                data.channelList.forEach(item => {
                  if (item.channelType == 1 && item.openStatus == 1) {
                    // 微信商城
                    // 适用商品
                    if (item.channelRelationProType == 2 && this.valueWx.length < 1) {
                      // 商品品牌
                      showMsg.showmsg('请选择商品品牌', 'error');
                      return (saveBool = false);
                    }
                    if (item.channelRelationProType == 3 && this.cateDataWx.length < 1) {
                      // 商品品类
                      showMsg.showmsg('请选择商品品类', 'error');
                      return (saveBool = false);
                    }
                    if (item.channelRelationProType == 4 && this.goodsListWx.length < 1) {
                      // 单件商品
                      showMsg.showmsg('请选择单件商品', 'error');
                      return (saveBool = false);
                    }
                  }
                });
                // 自定义人群
                if (data.useCrowd == 2 && this.selectCount < 1) {
                  showMsg.showmsg('请选择人群定向', 'error');
                  return;
                }

                delete data.selectorParamsId;

                // 规则需要乘以 10 去掉---20180906
                // data.integralUse.fee = data.integralUse.fee;
                // data.integralUse.integral = data.integralUse.integral;
                data.integralUse = JSON.stringify(data.integralUse);

                // if (data.channelList) {
                //   data.channelList.forEach(function(ele, index) {
                //     ele.openStatus = 1;
                //   });
                // }
                // 3.0_特殊策略新增有效期配置
                if (data.perpetualFlag === 1) {
                  // 永久
                  // perpetualFlag 是否永久  0 非永久  1永久'
                  // startValidTime 有效期时间开始(毫秒时间戳)
                  // endValidTime  有效时间结束（毫秒时间戳）
                } else {
                  // 时间区间
                  data.startValidTime = data.perpetualDate[0];
                  data.endValidTime = data.perpetualDate[1];
                }
                delete data.perpetualDate;
                if (saveBool) {
                  that.saveForm(data);
                }
              }
            })
          } else {
            log('error submit!!');
            return false;
          }
        });
      }
    },

    // 保存数据
    saveForm(data) {
      let that = this;
      // log("编辑积分抵现策略:" ,data)
      let para = data;

      postJson('/api-admin/save-member-integral-strategy', para)
        .then(res => {
          log(res.data.result);
          let resData = res.data;
          if (resData.errorCode == 0) {
            // that.updateData()
            that.$message({
              message: '保存成功',
              type: 'success'
            });

            that.changeRoute('/pointSystem');

            return;
          }
          errMsg.errorMsg(resData);
          // that.updateData()
        })
        .catch(function(error) {
          log(error);
        });
    },

    // 输入
    toInput: function(value) {
      let that = this;
      let temp = strLength.getByteVal(value.target.value, that.inputFlagNum);
      that.ruleForm.strategyName = temp.trim();
      that.cateNameLength = strLength.getZhLen(that.ruleForm.strategyName);
    },

    inputFocus(obj, tag, flag) {
      let that = this;
      that.tempTag = tag;
      that.tempData = obj;
      if (!!flag) {
        that.tempFlag = flag;
      } else {
        that.tempFlag = '';
      }
    },
    // 折扣重新写的判断
    formatInputChange(e) {
      let that = this;
      log(e);
      if (that.tempTag == 'integralUseDiscountControlLower' || that.tempTag == 'integralUseDiscountControlUpper') {
        that.tempData[that.tempTag] = Number(String(e).replace(/[^\.\d]/g, '')).toFixed(2) == 'NaN' ? '0.00' : Number(String(e).replace(/[^\.\d]/g, '')).toFixed(2);
        if (Number(that.tempData[that.tempTag]) > 10) {
          that.tempData[that.tempTag] = Number(10).toFixed(2);
        }
        // if (Number(that.tempData.integralUseDiscountControlLower) >= Number(that.tempData.integralUseDiscountControlUpper)) {
        //   that.tempData.integralUseDiscountControlLower = String(Number(that.tempData.integralUseDiscountControlUpper - 1).toFixed(2)).indexOf('-') !='-1'? '0.00':Number(that.tempData.integralUseDiscountControlUpper - 1).toFixed(2)
        // }
        return;
      }
    },

    // 输入框输入校验数字
    formatInput(e) {
      let that = this;
      log(e, e.target.value, that.tempData.integralUseDiscountControlLower);
      that.tempData.integralUseDiscountControlLower = Number(String(that.tempData.integralUseDiscountControlLower).replace(/[^\.\d]/g, '')).toFixed(2) == 'NaN' ? '0.00' : Number(String(that.tempData.integralUseDiscountControlLower).replace(/[^\.\d]/g, '')).toFixed(2);
      that.tempData.integralUseDiscountControlUpper = Number(String(that.tempData.integralUseDiscountControlUpper).replace(/[^\.\d]/g, '')).toFixed(2) == 'NaN' ? '0.00' : Number(String(that.tempData.integralUseDiscountControlUpper).replace(/[^\.\d]/g, '')).toFixed(2);
    },
    formatInputNum(e) {
      let that = this;
      that.ruleForm.integralUse.fee = strLength.getCharVal(String(that.ruleForm.integralUse.fee).replace(/[^\.\d]/g, ''), 6);
      that.ruleForm.integralUse.integral = strLength.getCharVal(String(that.ruleForm.integralUse.integral).replace(/[^\.\d]/g, ''), 8);
      that.ruleForm.channelList[0].useProportionalRestriction = strLength.getCharVal(String(that.ruleForm.channelList[0].useProportionalRestriction).replace(/[^\d]/g, ''), 3);
    },
    formatInputNumBlur() {
      let that = this;
      that.ruleForm.integralUse.fee = Number(strLength.getCharVal(String(that.ruleForm.integralUse.fee).replace(/[^\.\d]/g, ''), 6)).toFixed(1);
      // that.ruleForm.integralGet.integral = Number(strLength.getCharVal(String(that.ruleForm.integralGet.integral).replace(/[^\.\d]/g,''),8)).toFixed(1)
    },
    // 路由跳转
    changeRoute(route) {
      this.$router.push(route);
    },

    // 获取策略信息
    getInfo() {
      let that = this;
      let para = {
        strategyId: that.ruleForm.integralStrategyId
      };

      postRequest('/api-admin/get-member-integral-strategy', para)
        .then(res => {
          log(res.data.result);
          let resData = res.data;
          if (resData.errorCode == 0) {
            // 商户列表
            let prise = res.data.result.enterpriseList;
            prise.forEach(function(ele, index) {
              ele.label = ele.brandName;
              ele.value = ele.enterpriseId;
            });
            that.mechantOption = prise;

            let item = res.data.result.strategy;

            if (item.channelList) {
              item.channelList.forEach(item => {
                if (item.channelType == '1') {
                  // 微信商城
                  if (item.channelRelationProType == 3) {
                    // 商品品类
                    if (item.goodsCategoryList) {
                      item.goodsCategoryList.forEach(item => {
                        that.selectCateDataWx.push(item);
                      });
                    }
                  }
                  if (item.channelRelationProType == 4) {
                    // 单件商品
                    if (item.strategyGoodsRelationIdArr) {
                      item.strategyGoodsRelationIdArr.forEach(item => {
                        that.goodsListWx.push(item);
                      });
                    }
                  }
                  if (item.channelRelationProType == 2) {
                    // 商品品牌
                    if (item.strategyGoodsRelationIdArr) {
                      item.strategyGoodsRelationIdArr.forEach(item => {
                        that.valueWx.push(item);
                      });
                    }
                  }
                }
              });
            }

            // 自定义人群
            if (item.useCrowd == 2) {
              let arr = JSON.parse(item.crowdJson);
              this.selectCount = 0;
              if (arr.list) {
                arr.list.forEach(item => {
                  if (item.list) {
                    this.selectCount += item.list.length;
                  }
                });
              }
              this.useId = item.selectorParamsId;
              this.hasSearchData = item.crowdJson;
            }

            item.integralUse = JSON.parse(item.integralUse);
            if (!item.integralUse) {
              item.integralUse = {};
              item.integralUse.fee = '';
              item.integralUse.integral = '';
            }
            // 规则需要先除以10去掉 -----20180906
            that.ruleForm = that.formatData(item);
            // 字数控制
            that.cateNameLength = strLength.getZhLen(that.ruleForm.strategyName);
            that.$set(that.ruleForm, 'perpetualDate', []);

            that.ruleForm.perpetualDate[1] = this.ruleForm.endValidTime;
            that.ruleForm.perpetualDate[0] = this.ruleForm.startValidTime;
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          log(error);

          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },

    // 去空值
    bouncer(arr) {
      return arr.filter(function(val) {
        return !(!val || val === '');
      });
    },

    formatData(item) {
      item.integralGetStatus = String(item.integralGetStatus);
      item.integralUseStatus = String(item.integralUseStatus);
      item.useCrowd = String(item.useCrowd);
      item.integralGet = JSON.parse(item.integralGet);
      item.channelList.forEach((ele, index) => {
        ele.openStatus = ele.openStatus == 0 ? false : true;
        ele.channelType = String(ele.channelType);
        ele.channelRelationStoreType = String(ele.channelRelationStoreType);
        ele.channelStoreType = String(ele.channelStoreType);
        ele.channelRelationProType = String(ele.channelRelationProType);
        ele.integralUseDiscountControl = String(ele.integralUseDiscountControl);
        ele.integralUseDiscountControlUpper = String(ele.integralUseDiscountControlUpper);
        ele.integralUseDiscountControlLower = String(ele.integralUseDiscountControlLower);
        ele.useProportionalRestriction = String(ele.useProportionalRestriction);
        if (!ele.strategyGoodsRelationIdArr || !ele.strategyGoodsRelationIdArr.length) {
          ele.strategyGoodsRelationIdArr = [];
        }
        if (!ele.storeRelationIdArr || !ele.storeRelationIdArr.length) {
          ele.storeRelationIdArr = [];
        }
        if (!ele.enterpriseRelationIdArr || !ele.enterpriseRelationIdArr.length) {
          ele.enterpriseRelationIdArr = [];
        }
        if (ele.channelType == 9) {
          this.wmuuid = ele.storeControlId;
          this.isAdd = false;
        }
      });
      let bool = item.channelList.some(item => item.channelType == 9);
      !bool && item.channelList.push({
        integralStrategyChannelId: '', //id
        enterpriseId: '',
        integralStrategyId: '', //策略id
        channelType: '9', //渠道类型 1微信 3:线下 5淘宝 6京东 7唯品会 8天猫
        openStatus: true, //渠道配置是否启用 1启用 0停用
        channelRelationEnterpriseType: '1', // 策略关联企业 1所有企业 2部分企业
        channelRelationStoreType: '1', //策略关联门店类型 1所有门店 2门店分组 3部分门店
        channelStoreType: '-1', // 策略选择门店类型 （-1:所有 0:自营 1:联营 2:加盟,3代销4托管）
        channelRelationProType: '1', // 策略关联商品类型 1所有商品 2商品品牌 3商品类目 4部分商品
        integralUseDiscountControl: '1', // 折扣控制 1所有折扣商品适用 2折扣范围
        integralUseDiscountControlUpper: '0.0',
        integralUseDiscountControlLower: '0.0',
        useProportionalRestriction: '0.0', // 抵现比例限制
        strategyGoodsRelationIdArr: [], // 商品
        storeRelationIdArr: [], //门店
        enterpriseRelationIdArr: [] //商户
      })
      return item;
    },

    // 获取商品品牌  1
    getGoodsBrand() {
      let that = this;
      let para = {};
      getRequest('/api-mall/list-enterprise-brand', para)
        .then(res => {
          // log(res.data.result)
          let resData = res.data;
          if (resData.errorCode == 0) {
            let data = resData.result;
            that.goodsTreeOptionsWx = data;
            that.goodsTreeOptionsLine = data;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },

    // 选择商品品牌  微信商城
    selectGoodsStoreWx(val) {
      this.valueWx = val;
      this.ruleForm.channelList.forEach((item, index) => {
        if (item.channelType == '1') {
          this.ruleForm.channelList[index].strategyGoodsRelationIdArr = [];
          val.forEach(ele => {
            this.ruleForm.channelList[index].strategyGoodsRelationIdArr.push(ele);
          });
        }
      });
    },
    // 单件商品 的 子组件显示方法
    showGoodsDialog(type) {
      let that = this;
      that.goodsType = type;
      that.goodShow = true;
      that.goodsListId = [];
      if (type == 'wx') {
        // that.goodsListId = ['1']
        that.goodsListId = that.goodsListWx;
      } else if (type == 'line') {
        // that.goodsListId = ['12121']
        that.goodsListId = that.goodsListLine;
      }
    },

    // 单件商品 的 子组件触发方法
    selectGoods(val) {
      let that = this;
      that.goodShow = false;
      that.goodsListId = [];
      if (val) {
        if (val.goodsType == 'wx') {
          that.ruleForm.channelList.forEach((item, index) => {
            if (item.channelType == '1') {
              that.ruleForm.channelList[index].strategyGoodsRelationIdArr = [];
              val.strategyGoodsRelationIds.forEach(ele => {
                that.ruleForm.channelList[index].strategyGoodsRelationIdArr.push(ele);
              });
            }
          });
          that.goodsListWx = val.strategyGoodsRelationIds;
        } else if (val.goodsType == 'line') {
          that.ruleForm.channelList.forEach((item, index) => {
            if (item.channelType == '3') {
              that.ruleForm.channelList[index].strategyGoodsRelationIdArr = [];
              val.strategyGoodsRelationIds.forEach(ele => {
                that.ruleForm.channelList[index].strategyGoodsRelationIdArr.push(ele);
              });
            }
          });
          that.goodsListLine = val.strategyGoodsRelationIds;
        }
      }
    },

    // 选择适用对象
    changePeople(val) {
      log(this.ruleForm.useCrowd);
    },
    showPeople() {
      this.dialogVisiblePeople = true;
    },
    /* 人群筛选器 start */
    getData() {
      // 父组件调用子组件方法,触发父组件事件
      this.$refs.peopleFilter.confirmSet();
    },
    findFilter(value) {
      // 子组件触发父组件事件,返回过滤条件数据
      log(value);
      this.ruleForm.crowdJson = value;
      this.searchParams = value;
      if (this.ruleForm.crowdJson) {
        let arr = JSON.parse(this.ruleForm.crowdJson);
        this.selectCount = 0;
        if (arr.list) {
          arr.list.forEach(item => {
            this.selectCount += item.list.length;
          });
        }
      }
    },
    cancelFilter() {
      this.$refs.peopleFilter.cancelSet();
    },
    getBackData(val) {
      log(val);
      this.ruleForm.selectorParams = val;
    },
    // 隐藏保存按钮和确认按钮  (子组件会调用)
    hideBtn() {
      let that = this;
      that.toggleTag = false;
    },
    // 显示编辑,保存按钮隐藏,确认按钮显示 (子组件会调用)
    editShow() {
      this.toggleTag = true;
    },
    editHide() {
      this.toggleTag = false;
    },
    getPeopleData() {
      // 筛选器 弹框的确认按钮
      let that = this;
      that.$refs.peopleFilter.confirmSet();
      setTimeout(function() {
        if (that.searchParams) {
          that.lsSaveBtn(that.searchParams);
        }
      }, 500);
    },
    lsSaveBtn(searchParams) {
      // 人群筛选器弹框的确定按钮  临时保存
      postRequest('/api-admin/get-screen-detail-param', {
        requestProject: 'gic-web',
        tempBusinessId: this.tempBusinessId ? this.tempBusinessId : '',
        detailParams: searchParams
      })
        .then(res => {
          if (res.data.errorCode == 0) {
            this.tempBusinessId = res.data.result;
            this.useId = res.data.result;
            this.dialogVisiblePeople = false;
            this.searchParams = '';
          } else {
            this.$message.error(res.data.message);
          }
        })
        .catch(err => {
          log(err);
        });
    },
    // 商品品类
    getCateIds(val) {
      // 获取数据
      // log(val);
      if (val.selectCateType == 'wx') {
        this.ruleForm.channelList.forEach((item, index) => {
          if (item.channelType == '1') {
            this.ruleForm.channelList[index].strategyGoodsRelationIdArr = [];
            val.cateIdsList.forEach(ele => {
              this.ruleForm.channelList[index].strategyGoodsRelationIdArr.push(ele);
              this.cateDataWx.push(ele);
            });
          }
        });
      } else if (val.selectCateType == 'line') {
        this.ruleForm.channelList.forEach((item, index) => {
          if (item.channelType == '3') {
            this.ruleForm.channelList[index].strategyGoodsRelationIdArr = [];
            val.cateIdsList.forEach(ele => {
              this.ruleForm.channelList[index].strategyGoodsRelationIdArr.push(ele);
              this.cateDataLine.push(ele);
            });
          }
        });
      }
    },
    selectGoodsCateHide() {
      // 调用子组件 商品品类组件的函数
      if (this.ruleForm.channelList.length > 0) {
        this.ruleForm.channelList.forEach((item, index) => {
          if (item.channelType == '1' && item.channelRelationProType == 3) {
            // 微信商城
            if (this.$refs.selectGoodsCate instanceof Array) {
              this.$refs.selectGoodsCate[0].hideDragClick();
            } else {
              this.$refs.selectGoodsCate.hideDragClick();
            }
          }
        });
      }
    },
    // 判断是否是太平鸟
    getLoginInfo() {
      let that = this;
      let param = {};
      postRequest('/api-auth/get-login-user-info', param)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 0) {
            if (resData.result.enterpriseId == that.tpnEnterpriseId) {
              that.tpnEditBool = true;
            }
          } else {
            errMsg.errorMsg(resData);
          }
        })
        .catch(function(error) {
          log(error);
        });
    }
  },
  components: {
    topNavNew,
    addGoods,
    goodsCate
  }
};
</script>
<style lang="less" scoped>
.right-box {
  padding: 20px;
}
.w340 {
  width: 340px;
}
.attention-wrap {
  .item-label {
    font-size: 14px;
    color: #606266;
    margin-bottom: 30px;
    span {
      display: inline-block;
      width: 80px;
    }
  }

  .vue-treeselect {
    display: inline-block;
    vertical-align: top;
    width: 300px;
  }

  .title-tip {
    font-size: 12px;
    color: #888;
  }
  // .attention-content-wrap {
  // }
}

.form-wrap {
  .el-radio {
    margin-right: 0;
  }
  .el-input,
  /deep/ .el-input-number {
    width: 115px;
    padding: 0 5px;
    .el-input__inner {
      text-align: left;
    }
    &.w-300 {
      width: 300px;

      /*/deep/ .el-input-group__append{
        background-color: #fff;
        border-left: 0px;
      }
      /deep/ .el-input__inner {
        border-right: 0px;
      }*/
    }
    &.w-340 {
      width: 340px;
    }
  }

  /deep/ .input-label {
    position: absolute;
    left: 303px;
    color: #ccc;
    font-size: 12px;
  }

  .el-select {
    vertical-align: top;
  }

  .span-txt {
    position: absolute;
    left: 222px;
    z-index: 2;
    width: 300px;
    text-align: center;
  }

  .padding-0 {
    padding: 0;
  }

  .form-child {
    width: 100%;
    padding: 20px 20px 0px 0px;
    background: #f5f6f9;
    margin-top: 20px;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
  }

  .margin-l-120 {
    margin-left: 120px;
  }

  .margin-20 {
    margin-top: 20px;
  }
   .padding-bottom-20 {
    padding-bottom: 20px;
  }

  .tinymce-wrap {
    width: 780px;
  }
}

/*人群筛选器*/
.select-people {
  max-height: 500px;
  overflow-y: auto;
  overflow-x: hidden;
}
.select-people /deep/ .marginLeft134 {
  margin-left: 80px;
}
.select-people /deep/ .filter-content .member-templates-condition-title {
  text-align: left;
  font-size: 14px;
  line-height: 24px;
}
.select-people /deep/ .filter-content {
  margin-top: 0;
}
.people-btn-wrap {
  width: 100%;
  background: #f2f3f4;
  box-sizing: border-box;
  padding: 0 0 24px 100px;
}
.select-people /deep/ .detail {
  margin-top: 0;
}

.margin-r-6 {
  margin-right: 6px;
}
.w240 {
    width: 240px;
  }
.right-box /deep/ .el-input__inner {
  padding-right: 12px;
}
.right-box .padding-r-45 /deep/ .el-input__inner {
  padding-right: 45px;
}
/* 商品品类 */
.goods-cate {
  display: inline-block;
}
.goods-cate .goods-cate-content {
  width: 217px;
}
</style>
