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

const app = getApp();
var productList = [];

Page({
  data: {
  },
  onLoad: function (options) {

    this.setData({
      Containnerheight: common.getSystemHeight(),
      ProgramList: server.getProgramList(),
    });

    this.init();
  },
  onUnload:function(){
    productList=[];
  },
  init: function (e, isRemind) {
    var that = this;
    wx.showLoading({
      title: '装载中',
    });
    server.getData('GetUserActivateListByUserID500?UserID=' + app.globalData.userInfo.UserID + "&ProductID=" + app.globalData.ProgramID, function (data) {
      if (data && data.length > 0) {
        var programList = server.getProgramList();
        for (var i = 0; i < programList.length; i++) {
          for (var j = 0; j < data.length; j++) {
            if (programList[i].ID == data[j].ProductID) {
              data[j].Name = programList[i].Name;
              data[j].ImageUrl = programList[i].ImageUrl;
              data[j].appId = programList[i].appId;
              data[j].path = programList[i].path;
              //不是当前小程序不解锁
              if (app.globalData.ProgramID == data[j].ProductID)
                app.globalData.userInfo.IsMember = 1;
            }
          }
        }

        if (productList.length > 0) {
          for (var i = 0; i < productList.length; i++) {
            var b = false;
            for (var j = 0; j < data.length; j++) {
              if (productList[i].ProductID == data[j].ProductID) {
                b = true;
                
                break;
              }
            }
            if (!b) {
              productList[i].IsActivate = 1;
              data.push(productList[i]);
            }
          }
        }

        that.setData({
          List: data,
        });

        productList = data;

        if (isRemind) {
          wx.showModal({
            title: '产品未激活',
            content: '您有产品未激活，请立即点击红色“去激活”按钮。',
            showCancel: false,
            confirmText: "知道了",
          });
        }

        wx.setStorageSync("HasActivate", 1);
        wx.hideLoading();
      }
      else {
        if (isRemind) {
          var arr = [];
          for (var i = 0; i < productList.length; i++) {
            arr.push(productList[i].ProductID);
          }
          wx.redirectTo({
            url: "./activateFinish?idarr="+arr.join(","),
          });
          productList = [];
          wx.removeStorageSync("HasActivate");
        }
        else{
          for (var i = 0; i < productList.length; i++) {
            productList[i].IsActivate = 1;
          }

          if (productList.length>0){
            that.setData({
              List: productList,
            });
          }
          else{
            wx.removeStorageSync("HasActivate");
            wx.showModal({
              title: '提醒',
              content: '产品已激活。',
              showCancel: false,
              confirmText: "去首页",
              success:function(){
                wx.reLaunch({
                  url: '../../pages/index/index',
                })
              }
            });
          }
        }
      }
      wx.hideLoading();

    });
  },
  gotoFeedback: function () {
    server.gotoFeedback();
  },
  gotoActive: function (e) {
    var that = this;
    var productid = e.currentTarget.dataset.id;

    if (productid == app.globalData.ProgramID) {
      server.getData('UpdateWXUsersAll500?ProductID=' + productid
        + "&ProductUserID=" + app.globalData.userInfo.UserID
        + "&CurrentProductID=" + productid
        + "&CurrentUserID=" + app.globalData.userInfo.UserID, function (data) {
          if (data) {

          }
        });
    }
    else {
      var appId = e.currentTarget.dataset.appid;
      var path = e.currentTarget.dataset.path;

      path += "&ProductID=" + app.globalData.ProgramID;
      path += "&ProductUserID=" + app.globalData.userInfo.UserID;

      wx.navigateToMiniProgram({
        appId: appId,
        path: path,
        success(res) {

        }
      });
    }
    var data = this.data.List;
    for (var i = 0; i < data.length; i++) {
      if (data[i].ProductID == productid) {
        data[i].IsActivate = 0;
      }
    }
    that.setData({
      List: data,
    });
  },
  gotoFinish: function () {
    this.init(null, true);
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})