var config = require("../../config/config.js");
var app = getApp();
const Http = require("../../utils/HttpBasics");
const QR = require("../../utils/memberqrcode.js");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    canIUse: wx.canIUse('official-account'),
    flag: 'hidden',
    score:'0',
    name:"",
    birthdate:"",
    sex:"",
    showEdit:false,
    ismember:false,
    showQrcode:false,
    canvasHidden: false,
    maskHidden: true,
    imagePath: '',
    showmemberqrcode: false,
    placeholder: ''//默认二维码生成文本
  },
  /**
   * 跳转到成长值的页面
   */
  gotograde:function(){
    wx.navigateTo({
      url: '/pages/grade/grade',
    })
  },
  // getrun:function(){
  //   let that = this;
  //   wx.getWeRunData({
  //     success: function (res) {
  //       Http.post({
  //         url: config.api.getWeRunData,
  //         data: {
  //           encryptedData: res.encryptedData,
  //           iv: res.iv
  //         }
  //       }).then(res => {
  //         that.setData({
  //           step: res.data.stepInfoList[30].step
  //         })
  //       })
  //     }
  //   })
  // },
  /**
   * 
   */
  // getxinghao:function(){
  //   wx.getSystemInfo({
  //     success:function(res){
  //       wx.showModal({
  //         content: res.brand,
  //       })
  //     }
  //   })
  // },
  navigateTo() {
    wx.navigateTo({
      url: `/pages/login/index`
    });
  },
  showVersion: function() {
    /**
     * 长按显示版本号
     */
    let that = this;
    if (that.data.flag == 'hidden') {
      that.setData({  
        flag: 'show'
      });
    }
  },
  gotoedit:function(){
    wx.navigateTo({
      url: '/pages/edit/edit',
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this;
    let num = wx.getStorageSync('couponNum');
    wx.hideTabBarRedDot({
      index: 2
    });
    if (num == 'couponNum1') {
      wx.setStorage({
        key: 'couponNum',
        data: "couponNum1",
      })
      that.setData({
        couponNum: "couponNum1"
      })
    } else if (num == 'couponNum') {
      that.setData({
        couponNum: "couponNum"
      })
    };
    Http.get({
      url: config.api.getScore,
      data: {}
    }).then(res=>{
      if (res.data.address && res.data.name && res.data.sex && res.data.birthdate){
        that.setData({
          showEdit:true
        })
      }
      that.setData({
        score: res.data.score,
        levelName: res.data.levelName,
      })
      if (res.data.nickName || res.data.phone){
        that.setData({
          ismember:true,
          memberId: res.data.id
        })
      var size = this.setCanvasSize();
      var initUrl = JSON.stringify({flagid:res.data.id});
      this.createQrCode(initUrl, "mycanvas1", size.w, size.h);
      }
    })
    .catch(err => {
      wx.showModal({
        title: '提示',
        content: err.errMsg,
        showCancel:false
      })
    })
    //暂时注释
    // that.getrun();
    // 判断是否显示特享礼遇
    Http.get({
      url: config.api.scoreLevelInfo,
      data: {}
    })
    .then(res=>{
      // if true 则开启特享礼遇
      if (res.data.discountEnable){
        that.setData({
          discountEnable:true
        })
      }
      else{
        that.setData({
          discountEnable: false
        })
      }
    })
  },
  hideQrcode:function(){
    let that = this;
    that.setData({
      showQrcode: false
    })
  },
  //适配不同屏幕大小的canvas
  setCanvasSize: function () {
    var size = {};
    try {
      var res = wx.getSystemInfoSync();
      var scale = 750 / 500;
      //不同屏幕下canvas的适配比例；设计稿是750宽
      var width = res.windowWidth / scale;
      var height = width;
      //canvas画布为正方形
      size.w = width;
      size.h = height;
    } catch (e) {
      // Do something when catch error
      console.log("获取设备信息失败" + e);
    }
    return size;
  },
  createQrCode: function (url, canvasId, cavW, cavH) {
    //调用插件中的draw方法，绘制二维码图片
    QR.api.draw(url, canvasId, cavW, cavH);
  },
  qrcode: function (e) {
    var that = this;
    that.setData({
      showQrcode:true
    })
    wx.showToast({
      title: '生成中...',
      icon: 'loading',
      duration: 2000
    });
    var st = setTimeout(function () {
      wx.hideToast()
      var size = that.setCanvasSize();
      var url = JSON.stringify({ flagid:that.data.memberId});
      that.createQrCode(url, "mycanvas1", size.w, size.h);
      that.setData({
        maskHidden: true
      });
      clearTimeout(st);
    }, 1000)

  },
  close: function () {
    this.setData({
      showmemberqrcode: false,
    })
  }
});  