import common from '../../utils/util';
import server from '../../utils/main';

const app = getApp();

Page({
  data: {
    PayList: null,
    IsLogin:1,
  },
  onLoad: function (options) {
    this.setData({
      Containnerheight: common.getSystemHeight(),
    });

    if (app.globalData.userInfo)
      this.init();
    else{
      this.setData({
        IsLogin: -1,
      });
    }
  },
  init: function () {
    var that = this;

    server.getData('GetWXUsersAllPayInfo500?UserID=' + app.globalData.userInfo.UserID + "&ProductID=" + app.globalData.ProgramID, function (data) {
      if (data) {
        var list = server.getProgramList();
        for(var i=0;i<data.length;i++){
          for (var k = 0; k < data[i].List.length; k++) {
            var item = data[i].List[k];
            for(var j=0;j<list.length;j++){
              if (item.ProductID == list[j].ID) {
                item.ImageName = list[j].ImageUrl;
                item.ProductName = list[j].Name;
              }
            }
          }
        }
        that.setData({
          IsLogin: 1,
          PayList: data,
        });
      }
      else {
        that.setData({
          IsLogin: 1,
          PayList: [],
        });
      }
    });
  },
  showLogin:function(){
    this.setData({
      IsLogin: -2,
    });
  },
  hideLogin:function(){
    this.setData({
      IsLogin: -1,
    });
  },
  onKeyInput: function (e) {
    var that=this; 
    that.setData({
      UserName: e.detail.value
    });
  },
  onKeyInput2: function (e) {
    var that=this; 
    that.setData({
      UserPwd: e.detail.value
    });
  },
  login: function () {
    var that = this;

    server.getData('Login20221228?UserName=' + that.data.UserName + "&UserPwd="+that.data.UserPwd+"&ProductID=" + app.globalData.ProgramID, function (data) {
      if (data) {
        if (data>0){
            app.globalData.userInfo={};
            app.globalData.userInfo.UserID=data;
            that.init();
        }
        else {
          wx.showToast({
            title: '用户名或密码不正确',
          });
        }
      }
      else {
        wx.showToast({
          title: '登录出错',
        });
      }
    });
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
});