// scanGroup/jyScan/record/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    records: [],
	scanCount:"",
	showCouponComMess:false,
    isFormFilled:false,
    submitForm:{
        name:null,
        phone:null,
        idCard: null,
    },
    isOneReward:true,
		receiveVis:false,
		isNoAward:false,
		submitData:null,
		showUnlockRed:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
		console.log(options);
    Object.assign(this, options)
		this.hasMore = true
		this.currentPage = 1
		this.getRecords()
		wx.removeStorageSync('isThis')
  },
  getRecords(){
    app.utils.doPost({
			url: '/my/doQueryPrizeWithPage.json',
			params: {
				currentPage: this.currentPage,
				pageSize: 20,
				isAllRecord: 1,
				groupId: this.groupId
			}
		},true).then(res => {
			this.setData({
        records: this.data.records.concat(res.data.datalist),
        scanCount:res.data.items
			}, () => {
				if (this.data.records.length === res.data.items) {
					this.hasMore = false
				}
      })
		}).catch(err=>{
			let data = [{
				winType:2,
				amount:100,
				voucherType:1,
				createTime:32543635465,
				redStatus:0,
				
			},{
				winType:2,
				amount:100,
				voucherType:1,
				createTime:32543635465,
				redStatus:5,
				
			},{
				winType:2,
				amount:100,
				voucherType:1,
				createTime:32543635465,
				redStatus:4,
				
			},{
				winType:9,
				amount:100,
				voucherType:1,
				createTime:32543635465,
				redStatus:0,
			}]
			this.setData({
			  records: this.data.records.concat(data),
			  scanCount:1
						}, () => {
							// if (this.data.records.length === res.data.items) {
							// 	this.hasMore = false
							// }
			})
		})
  },
  updateList() {
		if (this.hasMore) {
			this.currentPage++
			this.getRecords()
		}
  },
  getAward(e){
		const { groupId,brandId,voucherNo} =e.currentTarget.dataset.item
        const postData = {
            groupId,
            brandId,
            voucherNo
				}
				this.setData({
					submitData:postData
				})
        app.utils.doPost({
            url: '/my/getWinGoodsAndCompleteInfo.json',
            params: postData
          },true)
          .then(async res => {
						await this.getUserInfo()
						const data=res.data
						if(data.needComplete && (data.prizeWeChatPhone || this.data.submitForm.phone) && (data.prizeUserName || this.data.submitForm.name) && (data.prizeIdcard || this.data.submitForm.idCard)){
                this.setData({
                    showCouponComMess:true
                })
            }else if(!data.needComplete){
                if(data.needSplit){
									const { groupId,brandId,voucherNo} =e.currentTarget.dataset.item
                    this.doTransfer('/my/doTransferToSplit.json',{groupId,brandId,voucherNo})
                }else{
									const { groupId,brandId,voucherNo} =e.currentTarget.dataset.item
                    this.doTransfer('/my/doTransfer.json',{groupId,brandId,voucherNo})
                }
            }
        })
	},
	// 获取用户信息
	getUserInfo(){
		return app.utils.doPost({
				url: '/my/getUserInfo.json',
				params: {}
		}).then(res=>{
			this.setData({
				'submitForm.phone':res.data.phone || null,
				'submitForm.idCard':res.data.idCard || null,
				'submitForm.name':res.data.name || null
			},()=>{
				this.checkIsFilled()
			})
		})
	},
	doTransfer(url,data){
		app.utils.doPost({
				url,
				params:data
			}).then(res => {
				this.setData({
						receiveVis:true,
				})
				this.messBoxClose()
				// if(this.voucherNo==data.voucherNo){
				// 	wx.setStorageSync('isThis', true)
				// }
				this.currentPage = 1
				this.data.records=[]
				this.getRecords()
		})
	},
  containerScan(){
    wx.scanCode({
			success: res => {
					const url = res.result
					const qr = app.utils.GetUrlPara(url, 'r').replace(/[\n\r]/g, '')
					if (qr) {
							wx.removeStorageSync('isThis')
							wx.reLaunch({
									url: `/scanGroup/index/index?qr=${qr}`
							})
					} else {
							wx.showModal({
									title: '提示',
									content: '请扫描正确的二维码',
									showCancel: false,
									confirmText: '好的'
							})
					}
			}
		})
	},
	// 关闭完善信息弹窗
	messBoxClose(){
		this.setData({
			// isOneReward:false,
			showCouponComMess:false,
			submitForm:{
				name:null,
				phone:null,
				idCard: null,
			},
			submitData:null
		})
	},
	// 校验数据是否填写完整
	checkIsFilled(){
		if(this.data.isOneReward){
				if(this.data.submitForm.name && this.data.submitForm.phone && this.data.submitForm.idCard){
						this.setData({
								isFormFilled:true
						})
				}else{
						this.setData({
								isFormFilled:false
						})
				}
		}else{
				if(this.data.submitForm.phone){
						this.setData({
								isFormFilled:true
						})
				}else{
						this.setData({
								isFormFilled:false
						})
				}
		}
	},
	onBindInput(e){
        const { key } = e.currentTarget.dataset
          this.setData({
              [`submitForm.${key}`]: e.detail.value
          })
        this.checkIsFilled()
  },
  getPhoneNumber (e) {
        const {
            code
        } = e.detail
        if (!code) return
        app.utils.doPost({
            url: '/daNoneUtc/doWechatBindPhone.json',
            params: {
                code
            }
        },true).then(res => {
            this.setData({
               	'submitForm.phone': res.data
            })
            this.checkIsFilled()
        })
  },
	IdCard(id) {
		var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
		return reg.test(id)
	},
	formSubmit(){
			let params={...this.data.submitForm, ignorePhone:1 }
			if(this.data.isOneReward){
				if (!this.IdCard(params.idCard)) {
					wx.showToast({
						title: '请输入格式正确的身份证',
						icon: 'none'
					})
					return
				}
				app.utils.doPost({
					url: '/completeMemberInfoAndSession.json',
					params: params
				}).then(res => {
					if (res) {
							this.doTransfer('/my/doTransferToSplit.json',this.data.submitData)
					}
				})
			}else{
						delete params.name
						delete params.idCard
						app.utils.doPost({
						url: '/completeMemberInfoAndSession.json',
						params: params
					}).then(res => {
						if (res) {
							this.doTransfer('/my/doTransfer.json',this.data.submitData)
						}
					})
				}
	},
	closeReceiveModal(){
		this.setData({
				receiveVis:false
		})
	},
	closeUnlockRed(){
		this.setData({
			showUnlockRed:!this.data.showUnlockRed
	})
	},
	goUnlock(){
		this.closeUnlockRed()
	},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})