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

const app = getApp();

Page({
  data: {
    IsShowRemind:false,
    IsNew:0,
    IsShowID:false,
    IsShowMore:true,
  },
  onLoad: function (options) {
    let that = this;
    that.setData({
      Containnerheight: main.getWindowHeight(),
      UserID:app.globalData.userInfo.UserID,
    });
    main.checkGenerating();
    that.getData(that.data.UserID);
  },
  onShow:function(e){
    let that=this;
    if (that.data.IsNew){
      setTimeout(function(){
        that.getData(that.data.UserID);
      },1000);
    }
  },
  getData:function(userID,pageID){
    wx.showLoading({
      title: '请稍候',
    });
    let that=this;
    let url='GetYJBDCArticleList?UserID=' + userID;
    if (pageID)
      url+="&PageID="+pageID;
    if (that.data.IsNew==1)
      url+="&IsNew=1";
    console.log(url);
    main.getData(url, function (data) {
      wx.hideLoading();
      if (data) {
        if (!pageID)
          that.data.List=[];
        for(let i=0;i<data.length;i++){
            let item=data[i];
            item.WordsStr=item.Words.split(",").join("　");
            that.data.List.push(item);
        }
        let isShowMore=true;
        if (that.data.List[0].RowsCount==that.data.List.length)
        isShowMore=false;
        that.setData({
          List:that.data.List,
          IsShowMore:isShowMore,
        });
      }
    });
  },
  getMore:function(e){
    this.getData(this.data.UserID,this.data.List[this.data.List.length-1].ID);
  },
  goto: function (e) {
    let that=this;
    if (e.currentTarget.dataset.readcount===0){
      that.setData({
        IsNew:1,
      });
    }
    else{
      that.setData({
        IsNew:0,
      });
    }
    var url=e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    });
  },
  setClipboard:function(e){
    let words=e.currentTarget.dataset.words;
    app.globalData.SelectedWords=words.split(",");
    wx.redirectTo({
      url: 'wordsinput',
    })
  },
  deleteArticle:function(e){
    animation.toggleRemindWithAnimation(this);
    this.setData({
      ID:e.currentTarget.dataset.id
    });
  },
  showRemind:function(e){
    let that=this;
    const confirm=e.currentTarget.dataset.confirm;
    animation.toggleRemindWithAnimation(this);
    if (confirm=="1") {
      main.getData('DeleteYJBDCArticleList?UserID=' + that.data.UserID+"&ID="+that.data.ID, function (data) {
         wx.showToast({
           title: '删除完成',
         });
         that.setData({
            IsNew:1,
         });
         for(let i=0;i<that.data.List.length;i++){
           if (that.data.List[i].ID==that.data.ID){
             that.data.List.splice(i,1);
             break;
           }
         }
         that.setData({
          List:that.data.List,
       });
      });
    }
  },
  setGotoID:function(e){
    let that=this;
    if (app.globalData.userInfo.UserID<4 || app.globalData.userInfo.UserID==11){
      wx.showModal({
        title: '输入用户ID，获得生成的文章',
        content: '',
        editable:true,
        complete: (res) => {
          if (res.confirm) {
            if (res.content){
              that.setData({
                UserID:res.content,
              });
              that.getData(res.content);
            }
          }
          else{
            that.setData({
              IsShowID:!that.data.IsShowID,
           });
          }
        }
      })
    }
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: app.globalData.ShareImage,
    }
  },
})