<template>
  <el-dialog title="首次获奖策略管理" :visible.sync="show" width="40%" :before-close="close" v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="90px">
      <el-form-item label="策略名称" prop="strategyName">
        <el-input v-model="form.strategyName"></el-input>
      </el-form-item>
      <el-form-item label="会员等级">
        <el-select v-model="form.gradeLevel" multiple placeholder="请选择">
          <el-option v-for="v in gradeList" :key="v.gradeId" :label="v.gradeName" :value="v.gradeId" :disabled="!v.isShow"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="最近消费">
        <el-date-picker :pickerOptions="pickerOptions" v-model="form.lastCostTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="消费总额">
        <el-input-number controls-position="right" class="w150" :min="0" size="medium" v-model="form.costAllBegin"></el-input-number> 至
        <el-input-number controls-position="right" class="w150" :min="parseInt(form.costAllBegin)" size="medium" v-model="form.costAllEnd"></el-input-number> 元
      </el-form-item>
      <el-form-item label="消费次数">
        <el-input-number controls-position="right" class="w150" :min="0" size="medium" v-model="form.costTimesBegin"></el-input-number> 至
        <el-input-number controls-position="right" class="w150" :min="parseInt(form.costTimesBegin)" size="medium" v-model="form.costTimesEnd"></el-input-number> 次
      </el-form-item>
      <el-form-item label="客单价">
        <el-input-number controls-position="right" class="w150" :min="0" size="medium" v-model="form.avgCostBegin"></el-input-number> 至
        <el-input-number controls-position="right" class="w150" :min="parseInt(form.avgCostBegin)" size="medium" v-model="form.avgCostEnd"></el-input-number> 元
      </el-form-item>
      <el-form-item label="连带率">
        <el-input-number controls-position="right" class="w150" :min="0" size="medium" v-model="form.avgNumberBegin"></el-input-number> 至
        <el-input-number controls-position="right" class="w150" :min="parseInt(form.avgNumberBegin)" size="medium" v-model="form.avgNumberEnd"></el-input-number> 元
      </el-form-item>
      <el-form-item label="奖券设置">
        <el-button type="primary" @click="showCard">{{cardObj.cardName ||'选择卡券'}}</el-button>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button @click="close">关 闭</el-button>
        <el-button type="primary" @click="submit('form')">保 存</el-button>
    </span>
  </el-dialog>
</template>
<script>
import {znmStrategy,znmStrategySave} from '@/service/api/gameApi.js';
import {formateDateTimeByType,resetParams} from '@/utils/index.js'
  export default {
      props:{
          show:{
              type:Boolean,
              default:false
          },
          dataId:{
              type:String,
              default:''
          },
          gameActivityId:{
              type:String,
              default:''
          },
          cardObj:{
              type:Object,
              default(){
                return{}
              }
          },
      },
      watch: {
        show(val) {
          if (val) {
            this.znmStrategy();
          }
        }
      },
    data() {
      return {
        loading:false,
        form: {
          strategyName:'',
          gradeLevel:[],
          avgNumberBegin:null,
          avgNumberEnd:null,
          avgCostBegin:null,
          avgCostEnd:null,
          costTimesBegin:null,
          costTimesEnd:null,
          costAllBegin:null,
          costAllEnd:null,
          lastCostTime:['',''],
          prizeReferId:'',
          cardStatus:-1,
        },
        rules:{
          strategyName:{required:true,message:'请填写策略名称',trigger:'blur'}
        },
        gradeList: [],
        pickerOptions: {
          disabledDate(time) {
            return time.getTime() > Date.now();
          }
        },
        showCardDialog:false
      };
    },
    methods: {
      async znmStrategy() {
        this.loading = true;
        let res = await znmStrategy({gamePrizeStrategyId:this.dataId || ''});
        this.loading = false;
        console.log(res)
        this.gradeList = res.result.gradeList;
        const strategy = res.result.strategy;
        if (res.result.strategy) {
          this.form.strategyName = strategy.strategyName;
          this.form.gamePrizeStrategyId =  strategy.gamePrizeStrategyId;
          this.form.gameActivityId  = strategy.gameActivityId;
          this.form.strategyName = strategy.strategyName;
          this.form.prizeReferId = strategy.prizeReferId;
          this.form.prizeReferType = strategy.prizeReferType;
          this.form.prizeStock = strategy.prizeStock;
          this.form.prizeCount = strategy.prizeCount;
          this.form.prizeIntegral = strategy.prizeIntegral;
          this.form.strategyType = strategy.strategyType;
          this.form.status = strategy.status;
          this.cardObj.cardName = strategy.proTitle;
          this.cardObj.coupCardId = strategy.prizeReferId;
          if (res.result.strategy.crowdFilter) {
            const obj = JSON.parse(res.result.strategy.crowdFilter);
            this.form.gradeLevel = obj.gradeLevel.split(',').filter(v => v != '');
            this.form.cardStatus = obj.cardStatus;
            this.form.avgNumberBegin = parseInt(obj.avgNumberBegin);
            this.form.avgNumberEnd = parseInt(obj.avgNumberEnd);
            this.form.avgCostBegin = parseInt(obj.avgCostBegin);
            this.form.avgCostEnd = parseInt(obj.avgCostEnd);
            this.form.costTimesBegin = parseInt(obj.costTimesBegin);
            this.form.costTimesEnd = parseInt(obj.costTimesEnd);
            this.form.costAllBegin = parseInt(obj.costAllBegin);
            this.form.costAllEnd = parseInt(obj.costAllEnd);
            this.form.lastCostTime = [obj.lastCostTimeBeginStr,obj.lastCostTimeEndStr]
            console.log(this.form)
          }
        } else {
          resetParams(this.form);

        }
      },
      close() {
          this.$emit('update:show',false);
      },
      // 显示卡券弹窗
      showCard(type) {
        // this.showCardDialog = true;
        this.$emit('get-data','dialogStrategy');
      },
      // 子组件触发方法
      selectCard(val) {
        // 模拟检查数据
        console.log(val)
        this.cardObj = val;
        this.showCardDialog = false;
      },
      submit(formName){
        if (!this.cardObj.coupCardId) {
          this.$tips({type:'warning',message:'请选择卡券'});
          return;
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let data = {
              gamePrizeStrategyId: this.form.gamePrizeStrategyId,
              gameActivityId: this.form.gameActivityId || this.$route.params.id || this.gameActivityId,
              strategyName: this.form.strategyName,
              prizeReferId: this.cardObj.coupCardId,
              prizeReferType: '1',
              prizeStock: 0,
              prizeCount : 0,
              prizeIntegral : 0,
              strategyType:'2',
              status:1,
            }
            let _data = {
              gradeLevel:this.form.gradeLevel.join(','),
              cardStatus:this.form.cardStatus,
              //连带率
              avgNumberBegin: this.form.avgNumberBegin,
              avgNumberEnd: this.form.avgNumberEnd,
              //客单价
              avgCostBegin: this.form.avgCostBegin,
              avgCostEnd: this.form.avgCostEnd,
              //消费次数
              costTimesBegin : this.form.costTimesBegin,
              costTimesEnd: this.form.costTimesEnd,
              //消费总额
              costAllBegin: this.form.costAllBegin,
              costAllEnd : this.form.costAllEnd,
              //最近消费
              lastCostTimeBeginStr: formateDateTimeByType(this.form.lastCostTime[0],'yyyy-MM-dd'),
              lastCostTimeEndStr: formateDateTimeByType(this.form.lastCostTime[1],'yyyy-MM-dd')
            }
            data.crowdFilter = JSON.stringify(_data);
            znmStrategySave(data).then(res => {
              this.$tips({type:'success',message:'操作成功'});
              this.$emit('update:show',false);
              this.$emit('refresh');
            })
          } else {
            this.$tips({type:'warning',message:'表单提交错误或未填写完整'});
            return false;
          }
        });
      }
    },
  };
</script>
