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

const COUNT_MAX=10;
const app = getApp();

Page({
  behaviors: [commonBehavior],
  data: {
    IsShowAlert:false,
    Count:0,
    CountMax:COUNT_MAX,
    Words:[],
    IsShowExample:false,
  },
  onLoad: function (options) {
    let that = this;
    that.setData({
      Containnerheight: main.getWindowHeight(),
    });
    main.checkGenerating();
  },

  onShow:function(e){
    let that = this;
    let words=app.globalData.OCRWords;
    debugger;
    that.data.Words=[];
    let list=app.globalData.SelectedWords;
    let count=0;
    for(let i=0;i<words.length;i++){
      let obj={};
      obj.Name=words[i];
      obj.CSS="";
      for(let j=0;j<list.length;j++){
        if (list[j]==obj.Name){
          obj.CSS="Selected";
          count++;
          break;
        }
      }
      that.data.Words.push(obj);
    }
    that.setData({
      Words:that.data.Words,
      CountMax:COUNT_MAX-list.length+count,
      Count:count,
    });
  },
  selectWord:function(e){
    
    let word=e.currentTarget.dataset.word;
    let list=this.data.Words;
    let count=0,index=-1;
    for(let i=0;i<list.length;i++){
        if(word==list[i].Name){
          if (list[i].CSS)
            list[i].CSS="";
          else
            list[i].CSS="Selected";
          index=i;
        }
        if(list[i].CSS=="Selected"){
          count++;
        }
    }
    //debugger;
    if (this.data.Count==this.data.CountMax && count>this.data.CountMax){
      wx.showToast({
        title: '本次最多'+this.data.CountMax+"个",
        icon:"none"
      });
      list[index].CSS="";
    }
    else{
      this.setData({
        Words:list,
        Count:count,
        IsShowAlert:false,
      });
    }
  },
  goto: function (e) {
    let that=this;
    var url=e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    });
  },
  clearInput: function (e) {
    let that=this;
    let list=this.data.Words;
    for(let i=0;i<list.length;i++){
        list[i].CSS="";
    }

    this.setData({
      Words:list,
      Count:0,
    });
  },
  gotoRedirectTo:function(e){
    let that=this;
    var url=e.currentTarget.dataset.url;
    wx.redirectTo({
      url: url,
    })
  },
  submit:function(){
    if (this.data.Count==0){
      this.setData({
        IsShowAlert:true,
      });
    }
    else{
      let list=this.data.Words;
      for(let i=0;i<list.length;i++){
        if(list[i].CSS=="Selected"){
          app.globalData.SelectedWords.push(list[i].Name);
        }
      }
      wx.navigateBack({
        delta: 1,
      });
    }
  },
  catchTouchMove: main.catchTouchMove,
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})