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

const app = getApp();
const WORD_COUNT=30;
Page({
  data: {
    ImagePath: app.globalData.uploadImageUrl,
    FolderNameCount:WORD_COUNT,
    IsNew:false,
    FolderColor:"#6B727C",
    FolderIcon:{ImageUrl:"cardlist_folder_white_66x60",Width:66,Height:60}
  },
  onLoad: function (options) {
    var that = this;

    var folderID=0;
    if (options.type=="edit")
      folderID=options.FolderID;

    that.setData({
      Containnerheight: main.getWindowHeight(),
      FolderID:folderID,
    });

    if (that.data.FolderID){
      main.getData("GetUserFolderInfo?UserID=" + app.globalData.userInfo.UserID + "&FolderID=" + that.data.FolderID, function (data) {
        if (data) {

          that.setData({
            FolderColor: data.ColorName,
            FolderName:data.Name,
            FolderNameCount:WORD_COUNT-data.Name.length,
          });
          that.getFolderIcon(data.IconName);
        }
      });
    }
    
  },
  onShow:function(){
    var that = this;
    
    if (app.globalData.TempStr){
      that.setData({
        FolderColor: app.globalData.TempStr,
      });
      app.globalData.TempStr="";
    }
    if (app.globalData.TempStr2){
      that.getFolderIcon(app.globalData.TempStr2);
      app.globalData.TempStr2="";
    }

    
  },
  getFolderIcon:function(icon){
    var that=this;
    var obj={};
    var str=icon;
    obj.ImageUrl=str;
    obj.Width=str.substring(str.lastIndexOf("_")+1,str.lastIndexOf("x"));
    obj.Height=str.substring(str.lastIndexOf("x")+1);
    
    that.setData({
      FolderIcon: obj,
    });
  },
  onKeyInput: function (e) {
    var foldername = e.detail.value;
    var that = this;
    if (foldername.length>WORD_COUNT){
      wx.showToast({
        title: '字数超出',
        image:'../images/universalpic_exclamation_white_120x120.png',
      });
      foldername=foldername.substr(0,WORD_COUNT);
    }
    that.setData({
      FolderName: foldername,
      FolderNameCount:WORD_COUNT-foldername.length,
    });
    
  },
  save:function(){
    var that = this;
    if (that.data.FolderNameCount==WORD_COUNT){
      wx.showToast({
        title: '填写标题',
        image:'../images/universalpic_exclamation_white_120x120.png',
      });
    }
    else{
      wx.showLoading({
        title: '保存中',
      });

      var name=that.data.FolderName;
      if (name.length>0){
        name=name.replace("\r","");
        name=name.replace("\n","");
      }
      var url="UpdateFolderInfo?UserID=" + app.globalData.userInfo.UserID;
      url+="&FolderID="+that.data.FolderID;
      url+="&Name="+name;
      if (!that.data.FolderColor)
        that.data.FolderColor="#6B727C";
      url+="&ColorName="+that.data.FolderColor.substr(1);
      url+="&IconName="+that.data.FolderIcon.ImageUrl;
      main.getData(url, function (data) {
        var obj={};
        obj.ColorName=that.data.FolderColor;
        obj.IconName=that.data.FolderIcon.ImageUrl;
        obj.Name=that.data.FolderName;
        app.globalData.TempStr=obj;
        setTimeout(function(){
          app.globalData.FolderList=[];
          wx.hideLoading();
          that.close();
        },2000);
        
      }); 
    }
  },
  changeIcon:function(e){
    var color=e.currentTarget.dataset.color;
    var icon=e.currentTarget.dataset.icon;
    
    wx.navigateTo({
      url: './folderIcon?FolderColor='+color+"&FolderIcon="+icon,
    })
  },
  deleteFolder:function(){
    var that = this;
    wx.showModal({
      title: '提醒',
      showCancel: true,
      content: "删除卡单不能恢复。删除后，原卡单里的题卡将不属于任何卡单。您确定删除吗？",
      cancelText:"确定",
      confirmText: "取消",
      confirmColor:"#000000",
      success(res) {
        if (!res.confirm) { 
          wx.showLoading({
            title: '删除中',
          });
          var url="UpdateFolderInfo?UserID=" + app.globalData.userInfo.UserID;
          url+="&FolderID="+that.data.FolderID;
          url+="&Flag=-1";
          main.getData(url, function (data) {
            setTimeout(function(){
              wx.hideLoading();
              app.globalData.FolderList=[];
              that.close(2);
            },2000);
          }); 
        }
      },
    });
  },
  close: function (num) {
    if (!num)
      num=1;
    wx.navigateBack({
      delta: num,
    });
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})
