// pages/payment/mjCoupon/mjCoupon.js
const config = require('../../../config/config.js')
const Http = require('../../../utils/HttpBasics.js')
const util = require('../../../utils/util.js')
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    optionsData:'',
    avaList:[],
    coListH:[],
    arr:[],
    orderId:'',
    END:'',
    TYPE:'',
    ID:'',
    coupon_id:'',
    composeOrderId:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      optionsData: options,
      flag: "coupons",
      payPrice: Number(options.val),
    });
    if (options.phone&&options.phone != 'undefined') {
      // options.phone = (options.phone != 'undefined' ? options.phone : '未绑定')
      this.setData({
        phone: this.data.optionsData.phone,
        coupon_id: this.data.optionsData.coupon_id,
        flag: "record",
      });
      let postData = {
        phone: this.data.phone
      }
      this.submitForm(postData)
    }
    if (options.END &&options.END != 'undefined') {
      this.setData({
        coupon_id: this.data.optionsData.coupon_id,
        flag: "coupons",
        checked: true
      });
      let postData1 = {
        cUserId: this.data.optionsData.ID
      }
      this.getCoupons1()
    }
  },
  showInfo(){
    wx.showToast({
      title: '抱歉，请输入正确的手机号',
      icon: 'none',
      duration: 2000,
      mask: false
    });
  },
  //切换扫会员码
  getCoupons() {
    this.setData({
      flag: "coupons",
    })
  },
  //切换手机号
  getRecord() {
    this.setData({
      flag: "record",
    })
  },
  getList(e) {
    console.log(e.detail.value)
    this.setData({
      phone: e.detail.value
    })
  },
  submitForm() {
    const reg = /^1[0|1|2|3|4|5|6|7|8|9]\d{9}$/;
    let _this = this;
    let postData = {
      phone: _this.data.phone,
      payPrice: _this.data.optionsData.val,
    }
    if (!reg.test(_this.data.phone)) {
      wx.showToast({
        title: '抱歉，请输入正确的手机号',
        icon: 'none',
        duration: 2000,
        mask: false
      });
      return;
    }
    Http.post({
      url: config.api.getManjian,
      data: postData
    })
      .then(res => {
        if (res && res.data.coList) {
          res.data.coList.map((item, index) => {
            item = Object.assign(item, { checked: false })
            if (this.data.coupon_id == item.coupon_id) {
              item.checked = true;
            }
          });//15313025161
          this.setData({
            avaList: res.data.coList,
            orderId: res.data.order.id,
            composeOrderId: res.data.order.composeOrderId
          })
        } else {
          wx.showToast({
            title: '抱歉,暂无可使用的优惠券!',
            icon: 'none',
            duration: 2000,
            mask: false
          });
          this.setData({
            avaList: '',
            orderId: res.data.order.id
          })
          //15669929956
        }
      })
      .catch(err => {
        wx.showToast({
          title: err.message,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      });
  },
  /**
   * 扫描会员码
   */
  gotoSaoyiSao: function () {
    let optionsData = this.data.optionsData
    let that = this
    wx.scanCode({
      success: function (val) {
        console.log(val.result);
        if (util.isJSON(val.result)) {
          let value = JSON.parse(val.result);
          let postData1 = {
            cUserId: value.ID,
            payPrice: optionsData.val,
          }
          that.setData({
            END: value.END,
            TYPE: value.TYPE,
            ID: value.ID
          })
          if (value.END == 'C' && value.TYPE == 'memberCode' && value.ID.length >= 18) {
            console.log(444444444444)
            Http.post({
              url: config.api.getManjian,
              data: postData1
            }).then (res => {
              console.log(res, 9999999999)
              if (res.code == 200) {
                if (res.data.coList) {
                  res.data.coList.map((item, index) => {
                    item = Object.assign(item, { checked: false })
                    if (that.data.coupon_id == item.coupon_id) {
                      item.checked = true;
                    }
                  });
                  that.setData({
                    avaList: res.data.coList,
                    orderId: res.data.order.id,
                    composeOrderId: res.data.order.composeOrderId
                  })
                }else{
                  wx.showToast({
                    title: '抱歉,暂无可使用的优惠券!',
                    icon: 'none',
                    duration: 2000,
                    mask: false
                  });
                  that.setData({
                    avaList: '',
                    orderId: res.data.order.id
                  })
                }
              }
            }).catch(err =>{
              wx.showModal({
                title: "抱歉",
                content: err.message,
                showCancel: false
              })
              that.setData({
                avaList: '',
                // orderId: res.data.order.id
              })
            })
            } else {
            wx.showToast({
              title: '请扫描正确的二维码',
              icon: 'none',
              duration: 1300
            })
          }
        } else {
          wx.showToast({
            title: '请扫描正确的二维码',
            icon: 'none',
            duration: 1300
          })
        }
      },
      fail: function () {
        wx.showToast({
          title: '请扫描正确的二维码',
          icon: 'none',
          duration: 1300
        })
      }
    })
  },

  getCoupons1() {
    this.setData({
      END: this.data.optionsData.ID,
      TYPE: this.data.optionsData.TYPE,
      ID: this.data.optionsData.ID,
    })
    let postData1 = {
      cUserId: this.data.optionsData.ID,
      payPrice: this.data.optionsData.val,
    }
      Http.post({
        url: config.api.getManjian,   
        data: postData1
      }).then(res => {
        if (res.code == 200) {
          res.data.coList.map((item, index) => {
            item = Object.assign(item, { checked: false })
            if (this.data.coupon_id == item.coupon_id) {
              item.checked = true;
            }
          });
          this.setData({
            avaList: res.data.coList,
            orderId: res.data.order.id
          })
        } else {
          wx.showModal({
            title: "抱歉",
            content: res.message,
            showCancel: false
          })
        }
      })
    
  },
  /**
   * 选择使用的优惠券
   */
  choicecoupon: function (e) {
    let that = this;
    let index = e.currentTarget.dataset.index;
    let item = this.data.avaList[index];
    let title = e.currentTarget.dataset.title;
    let id = e.currentTarget.dataset.id;
    let enable = e.currentTarget.dataset.enable;
    if (enable != 0){
      item.checked = !item.checked;
      this.setData({
        avaList: that.data.avaList,
      });
    }
    let pages = getCurrentPages(); //获取当前页面js里面的pages里的所有信息。
    let prevPage = pages[pages.length - 2]; 
    
    if (enable == 0) {
      wx.showToast({
        title: '当前金额不可使用此券',
        icon: 'none',
        duration: 2000,
        mask: false
      });
    }else{
      console.log(that.data.composeOrderId,"that.data.composeOrderId")
      prevPage.setData({  // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。
        coupon_id: e.currentTarget.dataset.coupon_id,
        title: e.currentTarget.dataset.title,
        price: e.currentTarget.dataset.price,
        orderId: that.data.orderId,
        composeOrderId: that.data.composeOrderId,
        phone: that.data.phone,
        ID: that.data.ID,
        TYPE: that.data.TYPE,
        END: that.data.END,
        
      })
      wx.navigateBack({
        delta: 1  // 返回上一级页面。
      })
    }
  },
})