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

const app = getApp();

Page({
  data: {
    ImagePath: app.globalData.uploadImageUrl,
  },
  onLoad: function (options) {
    var that = this;
    that.setData({
      Containnerheight: main.getWindowHeight(),
    });

    that.init(options.id);
  },
  init: function (id) {
    var that = this;
    var arr = main.getDetailColor(-1);


    common.getStorageValue(this, "ColorIndexArr", [1,2,3,4], function () {
      
      for (var i = 1; i < arr.length; i++) {
        for(var j=0;j<that.data.ColorIndexArr.length;j++){
          arr[i].SelectedCSS = "";
          if (that.data.ColorIndexArr[j] == i) {
            arr[i].SelectedCSS = "panelItemSelected";
            break;
          }
        }
      }
      
      that.setData({
        ColorList: arr,
      });
    });
  },
  selectColor: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    for (var i = 0; i < that.data.ColorList.length; i++) {
      //that.data.ColorList[i].SelectedCSS = "";
      if (i == index) {
        if (that.data.ColorList[i].SelectedCSS == "")
          that.data.ColorList[i].SelectedCSS = "panelItemSelected";
        else
          that.data.ColorList[i].SelectedCSS = "";
      }
    }

    var arr = [];
    for (var i = 0; i < that.data.ColorList.length; i++) {
      if (that.data.ColorList[i].SelectedCSS == "panelItemSelected")
        arr.push(i);
    }
    if (arr.length==0)
      arr.push(1);
    
    that.setData({
      ColorList: that.data.ColorList,
    });

    wx.setStorageSync("ColorIndexArr", arr);
    main.UploadUserConfig();
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})
