// packageI/parkCouponFree/index/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showTab: false,
    confinOption: {}, //后台设置的基础设置
    active: 0,
    disabledInquire: true, //车牌输入未完成，禁用
    InputEd: '', //获取已输入的车牌信息

    //  S 车牌组件
    customKeyboard: null,
    customCarNumInput: null,
    customCarNumInputIndex: 0,
    //  E 车牌组件
    list: [],
    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0, //总页数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.tab) {
      this.setData({
        active: 1
      })
    }
    this.setData({
      showTab: true
    })
    this.getConfigData();
    this.getCouponList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      customKeyboard: this.selectComponent('#customKeyboard'),
      customCarNumInput: this.selectComponent('#customCarNumInput')
    });

    // this.changeKerboard(1); // 显示车牌输入键盘
  },

  getConfigData() {
    let urlStr = app.getNetAddresss("plugin.parking-pay.frontend.coupon.index");
    app._postNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            confinOption: res.data
          })
        } else {
          app.tips(res.msg)
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },

  getCouponList(){
    // 兑换记录
    let urlStr = app.getNetAddresss("plugin.parking-pay.frontend.coupon.memberCouponList");
    app._postNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            list: res.data.data,
            total_page: res.data.data.last_page,
            isLoadMore: true
          })
        } else {
          app.tips(res.msg)
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },

  changeKerboard(type) {
    this.data.customKeyboard.change(type);
    this.data.customKeyboard.show();
  },

  navigateToMin(){
    // 跳转小程序
    wx.navigateToMiniProgram({
      appId: this.data.confinOption.mini_app_id,
      path: this.data.confinOption.mini_route,
      extraData: {},
      success(res) {
        // 打开成功
      },
      fail(res) {
        wx.showToast({
          title: '小程序跳转失败',
          icon: 'none',
          duration: 2000
        });
      }
    });
  },

  goUrl(e){
    let link = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: link,
    });
  },

  onInputFocus(e) {
    const index = e.detail;
    this.data.InputEd = this.data.customCarNumInput.data.numbers.map(item => item.value).join(''); //获取已输入的
    if (index == 0) {
      this.changeKerboard(1);
    } else if (index == 7) {
      this.changeKerboard(3);
    } else {
      this.changeKerboard(2);
    }
    this.setData({
      customCarNumInputIndex: index,
      disabledInquire: this.data.InputEd.length > 6 ? false : true
    });

    wx.pageScrollTo({
      selector: '#car-box',
      duration: 0
     })
  },

  onKeyboardClick(e) {
    this.data.customCarNumInput.change({
      index: this.data.customCarNumInputIndex,
      value: e.detail,
      remove: false
    })
  },

  onKeyboardDelete(e) {
    this.data.customCarNumInput.change({
      index: this.data.customCarNumInputIndex,
      value: '',
      remove: true
    })
  },

  toExchangeCoupon(e) {
    wx.navigateTo({
      url: '/packageI/parkCouponFree/exchangeCarCoupon/exchangeCarCoupon?num=' + this.data.InputEd
    })
  },

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

  },

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

  },

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

  },

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

  _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.memberCouponList");
      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.data.data;
            let list = this.data.list.concat(nextPageData);
            this.setData({
              list
            });
          } else {
            this.data.page = this.data.page - 1;
            this.data.isLoadMore = false;
          }
        }
      });
    }
  },

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

  },

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

  }
})