// pages/personalCenter/personalCenter.js

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myTicketArrows: false,
    historyTicketArrows: false,
    getUser:false,
    getUserArr: [],//用户信息
    userPhone:"",//用户手机号
    mydeg: -90,
    historydeg: -90,
    historyArr: [],//优惠券使用记录列表
    myTicketArr: [
      // { "coupon_img_small": "../images/smallTicket.jpg", "coupon_sell_id": 1, "coupon_name": "哈哈哈", "coupon_memo": "嘻嘻嘻","end_date":"2020/3/5"},
      // { "coupon_img_small": "../images/smallTicket.jpg", "coupon_sell_id": 1, "coupon_name": "哈哈哈", "coupon_memo": "嘻嘻嘻", "end_date": "2020/3/5" },
      // { "coupon_img_small": "../images/smallTicket.jpg", "coupon_sell_id": 1, "coupon_name": "哈哈哈", "coupon_memo": "嘻嘻嘻", "end_date": "2020/3/5" },
      // { "coupon_img_small": "../images/smallTicket.jpg", "coupon_sell_id": 1, "coupon_name": "哈哈哈", "coupon_memo": "嘻嘻嘻", "end_date": "2020/3/5" },
      // { "coupon_img_small": "../images/smallTicket.jpg", "coupon_sell_id": 1, "coupon_name": "哈哈哈", "coupon_memo": "嘻嘻嘻", "end_date": "2020/3/5" },
    ],//我的优惠券列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.globalData.openId) {
      this.myTicket();
      this.historyTicket();
    } else {
      app.globalData.openidSuccessFuc = this.myTicket;
      app.globalData.openidSuccessFuc = this.historyTicket;
    }
  },
  myTicket:function(){//获取我的优惠券列表
    app.wxRequest(app.globalData.httpUrl + 'couponbuy/validtype', {}, e => {
      console.log(e)
      if (e.code == 200) {
        this.setData({
          myTicketArr: e.data
        })
      } 
    }, this)
  },
  historyTicket: function () {//获取优惠券使用列表
    app.wxRequest(app.globalData.httpUrl + 'couponbuy/history', {}, e => {
      console.log(e)
      if (e.code == 200) {
        this.setData({
          historyArr: e.data
        })
      }
    }, this)
  },
  lower:function(){//检测上拉是否到底
    console.log("到底了")
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
  lookMyTicket:function(){
    this.setData({
      myTicketArrows: !this.data.myTicketArrows,
    })
    if (this.data.myTicketArrows){
      this.setData({
        mydeg: 0,
      })
    }else{
      this.setData({
        mydeg: -90,
      })
    }
  },
  lookHistory:function(){
    this.setData({
      historyTicketArrows: !this.data.historyTicketArrows,
    })
    if (this.data.historyTicketArrows) {
      this.setData({
        historydeg: 0,
      })
    } else {
      this.setData({
        historydeg: -90,
      })
    }
  },
  lookTicket: function (e) {//查看优惠券
    console.log(e)
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../TicketDetails/TicketDetails?id=' + id,
    })
  },
  giveTicket:function(e){//赠送优惠券
    var id = e.target.dataset.id;
    wx.navigateTo({
      url: '../givePage/givePage?id=' + id,
    })
  },
  getUser:function(e){//点击登陆获取用户信息
    console.log(e)
    console.log(app.globalData.userInfo)
    this.setData({
      getUserArr: e.detail.userInfo
    })
    if (this.data.getUserArr){
      this.setData({
        getUser: true,
        userPhone: app.globalData.userInfo
      })
      this.submitUser();
    }
    console.log(this.data.getUserArr)
  },
  submitUser:function(){//提交用户信息
    var params={
      "province": this.data.getUserArr.province,
      "language": this.data.getUserArr.language,
      "city": this.data.getUserArr.city,
      "gender": this.data.getUserArr.gender,
      "nickname": this.data.getUserArr.nickName,
      "headimgurl": this.data.getUserArr.avatarUrl,
    }
    app.wxRequest(app.globalData.httpUrl + 'submit', params, e => {
      console.log(e)
      if (e.code == 200) {
       
      }
    }, this,"POST")
  }
})