// pages/my/coupon/coupon.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        down: false,
        shopping: [],
        // shopping: [
        //     {
        //         list: [
        //             'sss',
        //             'sss',
        //             'sss',
        //         ],
        //         isExpire:false
        //     },
        //     {
        //         list: [
        //             'sss',
        //             'sss',
        //             'sss',
        //         ],
        //         isExpire:true
        //     },
        // ],
        isNum:false,
        code:'',
        pageindex: 1,
        currtime: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      
    },
    onShow: function(){
        this.getdata()
        this.setData({currtime: this.getcurrtime()})
    },
    getcurrtime(){
        let that = this
        let time = new Date()
        let year = time.getFullYear()
        let month = time.getMonth() + 1
        let date = time.getDate()
        let hours = time.getHours()
        let minute = time.getMinutes()
        let second = time.getSeconds()
  
        if (month < 10) { month = '0' + month }
        if (date < 10) { date = '0' + date }
        if (hours < 10) { hours = '0' + hours }
        if (minute < 10) { minute = '0' + minute }
        if (second < 10) { second = '0' + second }
        // console.log(year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second)
        return year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
    },
    getdata(){
        var that = this;
        wx.showLoading()
        app.api.useApi(app.globalData.baseAppUrl + "api/user/coupon", {}, "get").then(function (res) {
            console.log(res)
            wx.hideLoading()
            if (that.data.pageindex == 1) {
              that.setData({shopping: []})
            }
            if (res.message.data.data.length > 0) {
              that.setData({shopping: that.data.shopping.concat(res.message.data.data)})
            } else {
              that.setData({pageindex: that.data.pageindex -= 1})
            }
        }).catch(function (err) { console.log(err) })
    },
    changeBtn(e) {
        if (e.detail.value || e.detail.value != '') {
            this.setData({
                code: e.detail.value,
                isNum:true
            })
        }else{
            this.setData({
                code: e.detail.value,
                isNum:false
            })
        }
        
    },
    onReady: function () {
        this.toast = this.selectComponent("#toast");
    },
    listenerLogin: function () {
        let that = this
        // this.toast.showToast('优惠券无效');
        wx.showLoading()
        app.api.useApi(app.globalData.baseAppUrl + "api/coupon/code", {code:that.data.code}, "post").then(function (res) {
            console.log(res)
            if (res.code != 200) {
                wx.showLoading({
                    title: res.message.msg,
                })
                setTimeout(function () {
                    wx.hideLoading()
                }, 1000);
            } else {
                wx.showLoading({
                    title: res.message.msg,
                })
                setTimeout(() => {
                    that.getdata()
                }, 1000);
            }
        }).catch(function (err) { console.log(err) })
    },

    exchange(){
        if (!this.data.isNum) return
        this.listenerLogin()
    },
    clearValue(){
        this.setData({
            code:'',
            isNum:false
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage(res) {
        // res.target.dataset.id+
        return {
            title: '海星洗护',
            path: '/pages/my/share/share',
            imageUrl: '../../../image/my/01.png'
        }
    },
      /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({pageindex:this.data.pageindex+=1})
    this.getdata()
  },


})