const DEFAULT_PAGE = 0;
const Apps = getApp();
const api = require('../../config/api.js');
const util = require('../../config/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        baseurl: Apps.globalData.baseimgurl,
        messagelist: [],
        startPageX: 0,
        currentView: DEFAULT_PAGE,
        imagecount: 4,
        indeximagecount: 4,
        active: 1,
        challengeactive: 1,
        toView: `card_${DEFAULT_PAGE}`,
        list: [],
        replylist: [],
        imageslists: [], //图片预览
        showview: false,
        upload_picture_list: [],
        imagesListp: [],
        imagestr: [],
        id: 1,
        accesstoken: '',
        typeid: wx.getStorageSync('jmparm') == null ? 1 : wx.getStorageSync('jmparm'),
        page: 1, //页面数,
        pagecount: 0,
        itemdata:[],
        userctive:1, //1退出，2加入
        showdiage:false,
        payid:0, //支付ID
        totalmoney:0.01, //多少费用
        showdduihuan:false,
        valueduihuanma:'' //后台兑换码

    },
    onCloseduihuan:function (params) {
        this.setData({ showduihuan: false });
    },
    duihuanmoney:function (params) {
        this.setData({ 
            showdduihuan: true, 
            showdiage: false,
        });        
    },
    onChangeduihuan:function (params) {
            console.log("用户已输入兑换码");
            console.log(params.detail);
            this.setData({
                valueduihuanma :params.detail,
            })

    },
    querybtn:function (params) {
        //确认兑换码
        console.log("确认数据");
        console.log(params);
        console.log(this.data.valueduihuanma);
        console.log(this.data.payid);
        this.duihuanmadui(this.data.valueduihuanma,this.data.payid);
    },

    duihuanmadui:function (valueduihuanma,indexclasslist_id) {
        //确认兑换码
        const that = this;
        let url = Apps.globalData.baseUrl + '/indexclasslist/userduihuanma';
        var param = {
            namenum:valueduihuanma,
            indexclasslist_id:indexclasslist_id,
            user_id:wx.getStorageSync('wxuserid')
        };
        //此处为使用封装的post请求
        util.get(url, param).then((res) => {
            console.log(res);
            wx.reLaunch({
              url: '/pages/indexsq/indexsq',
            })

        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        console.log(options.id);
        if (options.id > 0) {
            this.setData({
                active: options.id == 1 ? 1 : options.id,
            })
        }
    },    
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        Apps.islogin();
        console.log(this.data.active);
        this.haveindexclassuser(this.data.active);
        this.getpaper(this.data.active); //班级列表 1全部，2我的班级
      
      
        },

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

    },
    onshowdiageClose() {
        this.setData({ showdiage: false });
      },
      showdiagePopup(e) {
        console.log(e);
        let pathid=e.currentTarget.id;

            this.setData({ showdiage: true,pathid:pathid });
        
       
      },
    getacctoken: function () {
        const that = this;
        let url = Apps.globalData.baseUrl + '/user/acctoken';
        var param = {
            pareid: '1213'
        };
        //此处为使用封装的post请求
        util.get(url, param).then((res) => {

            wx.setStorageSync('accesstoen', res.access_token)
            that.setData({
                accesstoken: res.access_token
            });

        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

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

    },

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

    },

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

    },  
    exithaveclass:function (params) {

      const that = this;
      wx.showModal({
          title: '退出提示',
          content: '您将退出该班级',
          cancelText: '退出',
          confirmText: '确认',
          success: function (res) {
              if (res.cancel) {
                  //这个跳转是左边按钮的跳转链接
                  //这里取消操作
                  console.log(res);

              } else {
                that.proexitexam(params);
              }
            
            }
      })
        
    },
    proexitexam:function(params){

      console.log(params.currentTarget.id);
      const id=params.currentTarget.id;
      const that = this;
      let url = Apps.globalData.baseUrl + '/indexclass/exitindexclasslist';
      var param = {
          id:id,
          user_id:wx.getStorageSync('wxuserid')
      };
      //此处为使用封装的post请求
      util.get(url, param).then((res) => {
          console.log(res);
          wx.reLaunch({
            url: '/pages/indexsq/indexsq',
          })

      }).catch((errMsg) => {
          console.log(errMsg);
      });
    },
    haveindexclassuser: function (par) {

        const that = this;
        let url = Apps.globalData.baseUrl + '/indexclass/haveindexclasslist';
        var param = {
            id: par,
            user_id:wx.getStorageSync('wxuserid')
        };
        //此处为使用封装的post请求
        util.get(url, param).then((res) => {
            console.log("232222222222222");
            console.log(res);
            that.setData({
                userctive: res
            });

        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },
    getpaper:function(params) {
        const that = this;
        let url = Apps.globalData.baseUrl + '/indexclass/indexclassfind';
        var param = {
          id: params
        };
        //此处为使用封装的post请求
        util.get(url, param).then((res) => {
          console.log('res==================');
          console.log(res);
          that.setData({
            itemdata: res.data
          });

        }).catch((errMsg) => {
          console.log(errMsg);
        });
    },
    jonclass1:function(par){
        console.log("跳转");
        console.log(par.currentTarget.dataset.money);
        let money=par.currentTarget.dataset.money;
        console.log(par.currentTarget.id);
        let payid=par.currentTarget.id;
        this.setData({
            showdiage: true,
            payid:payid,
            totalmoney:money
          });
    },
    payclassmoney:function(e){
        //微信支付
            console.log("微信支付");
            console.log(this.data.payid);
            this.payorderclick(this.data.payid,this.data.totalmoney);
    },
    payorderclick: function (id,money) {
       
       
        const that = this;
        let url = Apps.globalData.baseUrl + '/wxpay/makerorder';
        var param = {
            id: id,
            openid: wx.getStorageSync('wxopenid'),
            paytypeid: 3, //加入班级
            user_id: wx.getStorageSync('wxuserid') == null ? 3 : wx.getStorageSync('wxuserid'),
            vipsmonth: money
        };
        //此处为使用封装的post请求
        util.post(url, param).then((res) => {                 
            console.log(res);
            var rest = res;
            // var orders = res.data.orders;
            // 调用支付接口开始
            wx.requestPayment({
                timeStamp: rest.timeStamp,
                nonceStr: rest.nonceStr,
                package: rest.package,
                signType: rest.signType,
                paySign: rest.paySign,
                success: function (res) {                    
                  
                  wx.setStorage("jmparm",2);
                     wx.reLaunch({
                        url: '/pages/indexsq/indexsq',
                     })
                },
                fail: function (res) {
                    console.log(res)
                },
                complete: function (res) {
                    console.log(res)
                },
            })

        }).catch((errMsg) => {
            console.log(errMsg);
        });
    },
    jonclass:function(params) {
            //加入学习
            console.log(params.currentTarget.id);
            const that = this;
            let url = Apps.globalData.baseUrl + '/indexclass/addindexclasslist';
            var param = {
              id: params.currentTarget.id,
              user_id:wx.getStorageSync('wxuserid')
            };
            //此处为使用封装的post请求
            util.get(url, param).then((res) => {
              console.log('res==================');
              console.log(res);
             
              if(res>0){
                wx.setStorageSync('jmparm', 2);
                  wx.reLaunch({
                    url: '/pages/indexsq/indexsq',
                  })
              }
    
            }).catch((errMsg) => {
              console.log(errMsg);
            });
    },
    examingbtnclick:function (params) {
        
        wx.navigateTo({
                 url: '/pages/indexclass/indexclass?id='+params.currentTarget.id
        })
    }
})