/**
 * 逻辑太多
 */

import dmUploadImg from '@/components/upload/img'
import imgTextDrag from './partials/imgtext-drag'
import {formateDateTimeByType,resetParams} from '@/utils/index.js'
import {cardGetShelfs,getCardDetail,saveUpdateCard,copyCardDetailService} from '@/service/api/cardApi.js'
import dmGoodsInput from '@/components/goods-input/index.vue'
const initForm = {
  cardType:0, // 1.卡券类型: 抵金券: 0; 折扣券: 1; 兑换券: 2
  cardName:'', // 2.卡券名称
  subName:'', // 3.备注名
  subTitle:'', // 4.卡券副标题
  erpDemoCode:'', // demo券号
  cardColor:'#1890ff', //5.卡券颜色
  useCondition:null,  // 6. 抵金券 减免金额; 折扣券 折扣额度
  cardLimit:1,
  useCustomCode:0,
  cardCodeCount:100000,
  customCodeBegin:'',
  customCodeEnd:'',
  customCodePrefix:'',
  customCodeSuffix:'',
  useCodePrefix:0,
  useCodeSuffix:0,
  cardEffectiveMode:0,
  dateTime:[],
  costValue:0,  //成本费用
  startDay:0,
  limitDay:null,
  cardExplain:'',
  image:{
    imgUrl: '',
    code: ''
  },
  coverDescript:'',
  useDescript:'',
  shelfIds:[],
  verificationType:0,
  cardDenomination:0,
  proNoList:[]
}
const sale_limit = { fee:0,goods:'',flag:false,type:0 } //最低消费
const goods = {ok:'',no:'',flag:false}
export default {
  data() {
    return {
      loading:false,
      cardShelfsLoading:false,  //卡券展架loading
      testList:[],
      form:JSON.parse(JSON.stringify(initForm)),
      sale_limit:JSON.parse(JSON.stringify(sale_limit)),
      goods:JSON.parse(JSON.stringify(goods)),
      only:false,
      cardLimitRadio:1,
      rules:{
        cardName:{required:true,message:'请输入卡券名称',trigger:'blur'},
        subName:{required:true,message:'请输入备注名称',trigger:'blur'},
        subTitle:{required:true,message:'请输入卡券副标题',trigger:'blur'},
        costValue:{required:true,type:'number',min:0,message:'请输入成本费用',trigger:'blur'},
        cardDenomination:{required:true,type:'number',message:'此项不能为空',trigger:'blur'},
        cardExplain:{required:true,message:'适用门店说明不能为空',trigger:'blur'},
        useDescript:{required:true,message:'使用说明不能为空',trigger:'blur'},
      },
      startDayOptions:[],
      imgTextList:[],
      cardShelfsList:[],
      sendChildData:{
        storeType:0,
        storeGroupIds:'',
        storeIds:[],
      },
      pickerOptions: {
        disabledDate(val) {
          return Date.now() >= (val.getTime() + 24*60*60*1000)
        }
      },
      isEdit:this.$route.meta.type === 'edit',
      isInfo:this.$route.meta.type === 'info',
      isCopy:this.$route.meta.type === 'copy',
    }
  },
  watch:{
    'sale_limit.flag'(val) {
      if (!val) {
        this.sale_limit.type = 0;
        this.sale_limit.goods = null
        this.sale_limit.fee = null
      }
    }
  },
  components:{
    'dm-upload-img': dmUploadImg,
    'img-text-drag': imgTextDrag,
    'dm-goods-input': dmGoodsInput
  },
  created() {
    this.cardGetShelfs()
    this.getCardDetail()
    this.$store.commit('mutations_breadcrumb',[{name:'营销管理',path:''},{name:'卡券营销',path:'/card'},{name:'卡券库',path:'/card/list'},{name:'卡券详情',path:''}])
  },
  computed: {
    asideShow() {
      return this.$store.state.marketing.asideShow
    }
  },
  methods:{
    //切换卡券类型  表单置为默认值
    reset(val) {
      this.$refs.form.resetFields()
      this.form = JSON.parse(JSON.stringify(initForm))
      this.sale_limit = JSON.parse(JSON.stringify(sale_limit))
      this.goods = JSON.parse(JSON.stringify(goods))
      this.form.cardType = val
    },
    //卡券营销--卡券库--新建/修改卡券--放入卡券展架--所有展架集合
    cardGetShelfs() {
      this.cardShelfsLoading = true;
      cardGetShelfs({cardGetShelfs:''}).then(res => {
        let list = res.result
        list.map(v => {
          v.key = v.shelfId
          v.label = v.shelfName
        })
        this.cardShelfsList = list
        this.cardShelfsLoading = false;
      })
    },
    //卡券展架方法
    filterMethod(query, item) {
      return item.label.indexOf(query) > -1
    },
    //卡券营销--卡券库--新建/编辑卡券/卡券记录-领取记录-点击卡券查看详情
    getCardDetail() {
      this.loading = true
      getCardDetail({coupCardId:this.$route.params.id}).then(res => {
        if (res.errorCode === 0) {
          //处理有效期下拉选项
          const dicMap =  res.result.dicMap
          this.startDayOptions = Object.keys(dicMap).map(key => ({label:dicMap[key].dictName=='0'?'当天':'第'+dicMap[key].dictName+'天',value:parseInt(dicMap[key].dictName)}))
          if(res.result.card) {
            const card = res.result.card;
            this.form.coupCardId = this.isCopy?'':card.coupCardId;
            this.form.cardType = card.cardType;
            this.form.cardName = card.cardName || '';
            this.form.subName = card.subName || '';
            this.form.subTitle = card.subTitle || '';
            this.form.erpDemoCode = card.erpDemoCode || '';

            this.form.cardColor = card.cardColor;
            this.form.cardDenomination = card.cardDenomination || 0;
            if(card.useCondition) {
              const useCondition = this.form.useCondition = JSON.parse(card.useCondition);
              this.sale_limit.fee = useCondition.sale_limit? (useCondition.sale_limit.fee || 0):0;
              this.sale_limit.goods = useCondition.sale_limit? (useCondition.sale_limit.goods || ''):'';
              this.sale_limit.flag = (this.sale_limit.fee || this.sale_limit.goods);
              if (this.sale_limit.fee ) {
                this.sale_limit.type = 1;
                this.sale_limit.flag = true;
              } else if (this.sale_limit.goods) {
                this.sale_limit.type = 2;
              } else {
                this.sale_limit.type = 0;
              }

              this.goods.ok = useCondition.goods? (useCondition.goods.ok || ''):'';
              this.goods.no = useCondition.goods? (useCondition.goods.no || ''):'';

              if (this.goods.ok || this.goods.no) {
                this.goods.flag = true;
              }
              this.only = (!!useCondition.only) || false;
              this.form.proNoList = useCondition.proNo ? [{name:useCondition.proNo,id:useCondition.proNo}]:[];
            }

            this.form.cardLimit = card.cardLimit;
            if (this.form.cardLimit === 1) {
              this.cardLimitRadio = 1;
            } else if (this.form.cardLimit === 100) {
              this.cardLimitRadio = 100;
            } else {
              this.cardLimitRadio = 0;
            }

            this.form.useCustomCode = card.useCustomCode;

            if (this.isCopy && this.form.useCustomCode === 1) {
              this.form.customCodeBegin = '';
              this.form.customCodeEnd = '';
              this.form.customCodePrefix = '';
              this.form.customCodeSuffix = '';
              this.form.useCodePrefix =  0;
              this.form.useCodeSuffix =  0;
            } else {
              this.form.customCodeBegin = card.customCodeBegin;
              this.form.customCodeEnd = card.customCodeEnd;
              this.form.customCodePrefix = card.customCodePrefix;
              this.form.customCodeSuffix = card.customCodeSuffix;
              this.form.useCodePrefix = card.useCodePrefix || 0;
              this.form.useCodeSuffix = card.useCodeSuffix || 0;
            }
            this.form.cardCodeCount = card.cardCodeCount;
            this.form.cardEffectiveMode = card.cardEffectiveMode;
            this.form.dateTime = [card.beginDate || '',card.endDate || ''];
            this.form.costValue = card.costValue || 0;
            this.form.startDay = card.startDay || 0;
            this.form.limitDay = card.limitDay || 0;
            this.form.cardExplain = card.cardExplain || '';
            this.form.useDescript = card.useDescript || '';
            this.form.coverDescript = card.coverDescript || '';

            this.sendChildData = {
              storeType:card.storeMode || 0,
              storeGroupIds:card.groupIds?(card.groupIds.length?card.groupIds.join(','):''):'',
            };

            let list = [];

            if (card.storeIds && card.storeIds.length) {
              card.storeIds.map(v => {
                list.push(JSON.parse(v));
              })
            }

            this.sendChildData.storeIds =  list;

            this.form.image = {
              imgUrl:card.qcloudImageUrl || '',
              code:card.imageFieldCode || ''
            };
            this.imgTextList = [];
            if (card.teletexts.length>0) {
              card.teletexts.map(v => {
                this.imgTextList.push({
                  imgUrl:v.imageUrl,
                  imgCode:v.imageFieldCode,
                  text:v.teletextContent,
                  cardTeletextId:v.cardTeletextId,
                  flag:true,
                  maskShow:false,
                  copyImgUrl:'',
                  copyText:'',
                  copyImgCode:''
                })
              });
            }
            this.form.shelfIds = card.shelfIds || [];
            this.form.verificationType = card.verificationType || 0;
          }
        } else {
          this.$tips({type:'error',message:'初始化数据失败'});
        }
        this.loading = false;
      }).catch(err => {
        console.log(err)
        this.loading = false
      })
    },
    checkErpDemoCode() {
      this.form.erpDemoCode = this.form.erpDemoCode.replace(/[^\w\.\/]/ig,'')
    },
    //提交保存
    submitForm(formName) {
      //验证 减免金额 和 折扣额度
      if (this.form.cardType === 0) {
        if (Number(this.form.cardDenomination) <= 0) {
          this.$tips({type:'warning',message:'减免金额不能小于0'});
          return;
        }
      } else if (this.form.cardType === 1) {
        if (Number(this.form.cardDenomination) < 1) {
          this.$tips({type:'warning',message:'最低折扣不能小于1'});
          return;
        }
      }
      if (this.form.costValue <= 0 && this.form.cardType !== 1) {
        this.$tips({type:'warning',message:'成本必须大于0'});
        return;
      }

      //验证号段  不能为空 起始号段和结尾号段必须位数一致
      if (this.form.useCustomCode) {
        if (!this.form.customCodeBegin || !this.form.customCodeEnd) {
          this.$tips({type:'warning',message:'起始号段和结尾号段不能为空'});
          return;
        }
        if (this.form.customCodeBegin.toString().length !== this.form.customCodeEnd.toString().length ) {
          this.$tips({type:'warning',message:'code起始号段和结尾号段必须位数一致'});
          return;
        }
        if ((this.form.customCodeEnd - this.form.customCodeBegin + 1) > 100000) {
          this.$tips({type:'warning',message:'创建数量不能大于100,000张'});
          // this.form.customCodeEnd  = this.form.customCodeBegin  = 0;
          return;
        }
      }
      //验证有效期
      if(this.form.cardEffectiveMode) {
        if(!this.form.limitDay)  {
          this.$tips({type:'warning',message:'有效期必填'});
          return;
        }
      } else {
        if (!this.form.dateTime || !this.form.dateTime[0]) {
          this.$tips({type:'warning',message:'有效期必填'});
          return;
        }
      }






      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.saveUpdateCard();
        } else {
          this.$tips({type:'warning',message:'表单提交错误，请检查表单是否填写完整'});
          return false;
        }
      });
    },
    //卡券营销--卡券库--保存卡券
    saveUpdateCard () {
      let card = {}, params = {}
      card.coupCardId = this.form.coupCardId;
      card.cardType = this.form.cardType;
      card.cardName = this.form.cardName;
      card.subName = this.form.subName;
      card.subTitle = this.form.subTitle;
      card.erpDemoCode  = this.form.erpDemoCode;
      card.cardColor = this.form.cardColor;
      card.cardDenomination = this.form.cardDenomination;
      card.coverDescript = this.form.coverDescript;
      card.useCodePrefix = this.form.useCodePrefix;
      card.useCodeSuffix = this.form.useCodeSuffix;
      //卡券条件
      //抵金券 sale_limit.fee 最低消费;  goods.ok 适用商品, goods.no 不适应商品, only: 1 不与其他优惠共享(不勾选only不传)
      if(this.form.cardType === 0) {
        card.useCondition = {
          sale_limit:{
            fee:this.sale_limit.fee
          },
          goods:{
            ok:this.goods.ok,
            no:this.goods.no
          },
        }
        if(this.only) {
          card.useCondition.only = Number(this.only)
        }
      //[折扣券]:  goods.ok 适用商品, goods.no 不适应商品, only: 1 不与其他优惠共享(不勾选only不传)
      } else if (this.form.cardType === 1) {
        card.useCondition = {
          goods:{
            ok:this.goods.ok,
            no:this.goods.no
          },
          only:Number(this.only)
        }
        if(this.only) {
          card.useCondition.only = Number(this.only)
        }
      // sale_limit.fee 消费金额满,sale_limit.goods 消费指定商品;  goods.ok 适用商品, goods.no 不适应商品, only: 1 不与其他优惠共享(不勾选only不传)
      } else if (this.form.cardType === 2) {
        card.useCondition = {
          sale_limit:{
            fee:this.sale_limit.fee,
            goods:this.sale_limit.goods
          },
          goods:{
            ok:this.goods.ok,
            no:this.goods.no
          },
          only:Number(this.only),
          proNo:this.form.proNoList.length?this.form.proNoList[0].id:''
        }
        if(this.only) {
          card.useCondition.only = Number(this.only)
        }
      }

      //卡券限制
      if (this.cardLimitRadio === 1) {
        card.cardLimit = 1;
      } else if (this.cardLimitRadio === 100) {
        card.cardLimit = 100;
      } else {
        card.cardLimit = this.form.cardLimit;
      }

      card.useCustomCode = this.form.useCustomCode;
      card.costValue = this.form.costValue || 0;
      //自定义号段
      if (card.useCustomCode  === 1) {
        card.customCodeBegin = this.form.customCodeBegin;
        card.customCodeEnd = this.form.customCodeEnd;
        if (this.form.useCodePrefix) {
          card.customCodePrefix = this.form.customCodePrefix;
        }
        if (this.form.useCodeSuffix) {
          card.customCodeSuffix = this.form.customCodeSuffix;
        }
        card.cardCodeCount = this.form.customCodeEnd - this.form.customCodeBegin + 1;
      } else {
        card.cardCodeCount = this.form.cardCodeCount;
      }
      //时间判断
      card.cardEffectiveMode = this.form.cardEffectiveMode;
      if (card.cardEffectiveMode === 0) {
        if (!this.form.dateTime) {
          card.beginDate = card.endDate = ''
        } else {
          card.beginDate = formateDateTimeByType(this.form.dateTime[0],'yyyy-MM-dd')
          card.endDate = formateDateTimeByType(this.form.dateTime[1],'yyyy-MM-dd')
        }
      } else {
        card.startDay = this.form.startDay || 0;
        card.limitDay = this.form.limitDay || 0;
      }

      card.cardExplain = this.form.cardExplain;
      card.useDescript = this.form.useDescript;

      //门店类型
      card.storeMode = this.sendChildData.storeType;
      //封面
      card.qcloudImageUrl =  this.form.image.imgUrl;
      card.imageFieldCode = this.form.image.code;

      //图文提交
      let teletextList = [];
      if (this.imgTextList.length>0) {
        this.imgTextList.map((v,i) => {
          teletextList.push({
            imageUrl:v.imgUrl,
            imageFieldCode:v.imgCode,
            teletextContent:v.text,
            cardTeletextId:v.cardTeletextId,
            sort:i
          })
        })
      }

      card.teletexts = teletextList;
      if(card.cardType === 2) {
        card.verificationType = this.form.verificationType || 0;
      }
      card.isDisable = true;
      card.notApplicableMode = 0;
      card.notApplicableProMode = 0;
      card.notApplicableStoreProMode = 0;
      card.proMode = 0;
      card.customCodeSync = 0;
      card.changeStock = 0;
      card.channelType = 1;
      card.checkSource = 0;
      card.applicableMode = 0;
      card.storeProMode = 0;


      if (this.sendChildData.storeType === 1) {
        if (this.sendChildData.storeGroupIds) {
          params.groupIdList = JSON.stringify(this.sendChildData.storeGroupIds.split(','));
        }
      } else if (this.sendChildData.storeType === 2){
        if (this.sendChildData.storeIds) {
          params.storeIdList = JSON.stringify(this.sendChildData.storeIds.map(v => v.storeId));
        } else {
          params.storeIdList = JSON.stringify([]);
        }
      }
      params.shelfIds = JSON.stringify(this.form.shelfIds);
      params.card = JSON.stringify(card);


      saveUpdateCard(params).then(res => {
        console.log(res)
        if (res.errorCode === 0) {
          if (this.isEdit) {
            this.$confirm('保存成功, 是否返回列表?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.$router.go(-1);
            }).catch(err => {
              this.getCardDetail();
            });
          } else {
            this.$router.go(-1);
          }
        } else {
          this.$tips({type:'error',message:res.message || '保存卡券失败'});
        }
      }).catch(err => {
        console.log(err)
        this.$tips({type:'error',message:'保存卡券失败'});
      })
    },
    //门店分组回执方法
    getSelectGroupData(val) {
      this.sendChildData.storeType = val.storeType || 0
      this.sendChildData.storeGroupIds = val.storeGroupIds || ''
      this.sendChildData.storeIds = val.storeIds || []
    },
    //检查是否为数字
    checkInputNumber(val,name) {
      this.form[name]  = (val.replace(/[^0-9]*/g,"")+'').substr(0,16);
      if (parseInt(this.form.customCodeEnd) - parseInt(this.form.customCodeBegin) < 0) {
        this.form.customCodeEnd = this.form.customCodeBegin;
        this.$tips({type:'warning',message:'自定义结尾号段不能小于初始号段'})
      }
      if ((parseInt(this.form.customCodeEnd) - parseInt(this.form.customCodeBegin) + 1) > 100000) {
        this.$tips({type:'warning',message:'创建数量不能大于100,000张'});
        // this.form.customCodeEnd  = this.form.customCodeBegin  = 0;
        return;
      }
    },
     //检查是否为字符串
    checkInputString(val,name) {
      this.form[name]  = (val.replace(/[^a-zA-Z0-9]*/g,"")+'')
    }
  }
}
