// packageI/parkCouponFree/exchange-car-coupon/exchangeCarCoupon.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    car_number: '',//当前车牌号
    integral: "积分",
    warnText: '兑换优惠券',
    point: '---', //积分
    list: [],
    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0, //总页数
    radioIndex: '',
    disabledInquire: true //没有选券和积分不够时，禁用
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!this.options.num) {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请先填写车牌号！',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/packageI/parkCouponFree/index/index'
            })
          }
        }
      })
      return
    }
    this.getData();
    const value = wx.getStorageSync("integral");
    if (value) {
      this.setData({
        integral: value,
      });
    }
  },

  getData() {
    let urlStr = app.getNetAddresss("plugin.parking-pay.frontend.coupon.couponList");
    app._postNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            car_number: this.options.num,
            point: res.data.point,
            list: res.data.list.data,
            total_page: res.data.list.last_page,
            isLoadMore: true
          })
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  onChange(event) {
    this.setData({
      radioIndex: event.detail,
      disabledInquire: false
    });
    if (Number(this.data.list[event.detail].point) > Number(this.data.point)) {
      this.setData({
        disabledInquire: true,
        warnText: '积分不足'
      })
    }
  },

  exchangeCoupon(){
    const that = this;
    wx.showModal({
      title: '提示',
      content: `是否确认车牌${this.options.num}，兑换【${this.data.list[this.data.radioIndex].name}】？`,
      success(res) {
        if (res.confirm) {
          that.confirmExchange()
        }
      }
    })
  },
  confirmExchange() {
    let urlStr = app.getNetAddresss("plugin.parking-pay.frontend.coupon.exchangeCoupon");
    app._postNetWork({
      url: urlStr,
      data: {
        id: this.data.list[this.data.radioIndex].id,
        car_number: this.options.num
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          wx.navigateTo({
            url: '/packageI/parkCouponFree/index/index?tab=1'
          })
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },

  _getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page += 1;
      let urlStr = app.getNetAddresss("plugin.parking-pay.frontend.coupon.couponList");
      urlStr += `&page=${this.data.page}`;
      app._postNetWork({
        url: urlStr,
        success: (resdata) => {
          let res = resdata.data;
          this.data.isLoadMore = true;
          if (res.result === 1) {
            let nextPageData = res.data.list.data;
            let list = this.data.list.concat(nextPageData);
            this.setData({
              list
            });
          } else {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false;
          }
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

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

  },

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

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    if (this.data.isLoadMore) this._getMoreData();
  },

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

  },

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

  }
})