var app = getApp()
var that=null
var sopenFrame=null
var WxParse = null
Page( {
  data: {
    body:"",
    ifshowemojicons:false,
    vertical:false,
    t:0,
    image:"",
    addpic:"../../images/add.png",
    savebutton:"发送",
    images1:[{"datas":[{"key":"[微笑]","image":"1.png"},{"key":"[憋嘴]","image":"2.png"},{"key":"[色]","image":"3.png"},{"key":"[发呆]","image":"4.png"},{"key":"[得意]","image":"5.png"},{"key":"[流泪]","image":"6.png"},{"key":"[害羞]","image":"7.png"},{"key":"[闭嘴]","image":"8.png"},{"key":"[睡]","image":"9.png"},{"key":"[大哭]","image":"10.png"},{"key":"[尴尬]","image":"11.png"},{"key":"[发怒]","image":"12.png"},{"key":"[调皮]","image":"13.png"},{"key":"[呲牙]","image":"14.png"},{"key":"[惊讶]","image":"15.png"},{"key":"[难过]","image":"16.png"},{"key":"[酷]","image":"17.png"},{"key":"[冷汗]","image":"18.png"},{"key":"[抓狂]","image":"19.png"},{"key":"[吐]","image":"20.png"},{"key":"[偷笑]","image":"21.png"},{"key":"[愉快]","image":"22.png"},{"key":"[白眼]","image":"23.png"},{"key":"[傲慢]","image":"24.png"},{"key":"[饥饿]","image":"25.png"},{"key":"[困]","image":"26.png"},{"key":"[惊恐]","image":"27.png"},{"key":"[流汗]","image":"28.png"},{"key":"[憨笑]","image":"29.png"},{"key":"[悠闲]","image":"30.png"},{"key":"[奋斗]","image":"31.png"},{"key":"[咒骂]","image":"32.png"},{"key":"[疑问]","image":"33.png"}]},{"datas":[{"key":"[嘘]","image":"34.png"},{"key":"[晕]","image":"35.png"},{"key":"[疯了]","image":"36.png"},{"key":"[哀]","image":"37.png"},{"key":"[骷髅]","image":"38.png"},{"key":"[敲打]","image":"39.png"},{"key":"再见]","image":"40.png"},{"key":"[擦汗]","image":"41.png"},{"key":"[抠鼻]","image":"42.png"},{"key":"[鼓掌]","image":"43.png"},{"key":"[糗大了]","image":"44.png"},{"key":"[坏笑]","image":"45.png"},{"key":"[左哼哼]","image":"46.png"},{"key":"[右哼哼]","image":"47.png"},{"key":"[哈欠]","image":"48.png"},{"key":"[鄙视]","image":"49.png"},{"key":"[委屈]","image":"50.png"},{"key":"[快哭了]","image":"51.png"},{"key":"[阴险]","image":"52.png"},{"key":"[亲亲]","image":"53.png"},{"key":"[吓]","image":"54.png"},{"key":"[可怜]","image":"55.png"},{"key":"[菜刀]","image":"56.png"},{"key":"[西瓜]","image":"57.png"},{"key":"[啤酒]","image":"58.png"},{"key":"[篮球]","image":"59.png"},{"key":"[乒乓]","image":"60.png"},{"key":"咖啡]","image":"61.png"},{"key":"[饭]","image":"62.png"},{"key":"[猪头]","image":"63.png"},{"key":"[玫瑰]","image":"64.png"},{"key":"[凋谢]","image":"65.png"}]},{"datas":[{"key":"[嘴唇]","image":"65.png"},{"key":"[爱心]","image":"67.png"},{"key":"[心碎]","image":"68.png"},{"key":"[蛋糕]","image":"69.png"},{"key":"[闪电]","image":"70.png"},{"key":"[炸弹]","image":"71.png"},{"key":"[刀]","image":"72.png"},{"key":"[足球]","image":"73.png"},{"key":"[瓢虫]","image":"74.png"},{"key":"[便便]","image":"75.png"},{"key":"[月亮]","image":"76.png"},{"key":"[太阳]","image":"77.png"},{"key":"[礼物]","image":"78.png"},{"key":"[拥抱]","image":"79.png"},{"key":"[强]","image":"80.png"},{"key":"[弱]","image":"81.png"},{"key":"[握手]","image":"82.png"},{"key":"[胜利]","image":"83.png"},{"key":"[抱拳]","image":"84.png"},{"key":"[勾引]","image":"85.png"},{"key":"[拳头]","image":"86.png"},{"key":"[差劲]","image":"87.png"},{"key":"[爱你]","image":"88.png"},{"key":"[NO]","image":"89.png"},{"key":"[OK]","image":"90.png"},{"key":"[爱情]","image":"91.png"},{"key":"[飞吻]","image":"92.png"},{"key":"[跳跳]","image":"93.png"},{"key":"[发抖]","image":"94.png"},{"key":"[怄火]","image":"95.png"},{"key":"[转圈]","image":"96.png"},{"key":"[磕头]","image":"97.png"},{"key":"[回头]","image":"98.png"},{"key":"[跳绳]","image":"99.png"},{"key":"[投降]","image":"100.png"},{"key":"[激动]","image":"101.png"},{"key":"[街舞]","image":"102.png"},{"key":"[献吻]","image":"103.png"},{"key":"[左太极]","image":"104.png"},{"key":"[右太极]","image":"105.png"}]}],
    loadingHidden:false,
    Start:0,
    datas: []
  },
  onLoad: function() {
      that = this
      WxParse = require('../../wxParse/wxParse.js');
      that.rdata(0)
    },onPullDownRefresh: function () {
      //在当前页面显示导航条加载动画
       wx.showNavigationBarLoading(); 
      //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
      wx.showLoading({
          title: '刷新中...',
      })
      that.clearCache();
      that.rdata(1);
  }, // 页面上拉触底事件（上拉加载更多）
  onReachBottom: function () {
    that.setData({
      loadingHidden:false
      }); 
      that.rdata(2);//后台获取新数据并追加渲染
  },// 清缓存
  clearCache:function(){
     that.setData({
          datas: [],
          Start: 0,
          loadingHidden:false
     }); 
 },rdata: function(t) {
  var userId = wx.getStorageSync("userId")
  var LoginId = wx.getStorageSync("LoginId")
      var userInfodata = wx.getStorageSync("userInfo")
      var userInfo = JSON.parse(userInfodata)
      if(userId&&LoginId){
        wx.request({
          url: app.globalData.apiurl + "/xapiajax.ashx", 
          data: {
            action:"getchatlist",
             userId:(!userId?"":userId),
             LoginId:(!LoginId?"":LoginId),
             ttype:0,
             Start:that.data.Start
          },
          method:"GET",
          dataType:"json", 
          header: { 
            "content-type": "application/json" // 默认值
          },
          success (res) {
          
                var tmpArr = null;
                tmpArr = that.data.datas;
                tmpArr.push.apply(tmpArr,res.data.data);
                that.setData({
                     datas: tmpArr,
                     Start: that.data.Start+res.data.data.length,
                     loadingHidden:true
                })
                for (var i = 0; i < that.data.datas.length; i++) {
                  WxParse.wxParse('content' + i, 'html', that.data.datas[i].content, that);
                  if (i == (that.data.datas.length - 1)) {
                    WxParse.wxParseTemArray("contentArr", 'content', that.data.datas.length, that)
                  }
                }
              }
         });
       } 
       if(t==1){
        //隐藏loading 提示框
        wx.hideLoading();
        //隐藏导航条加载动画
        wx.hideNavigationBarLoading();
        //停止下拉刷新
        wx.stopPullDownRefresh();
      }
    },onkeyupbody: function(e) {
      that.setData({
        body:e.detail.value
      })
    },inaddcart: function(e) {
       
    },showemojicons: function(e) {
      that.setData({
        ifshowemojicons:true
      })
    },closeshowemojicons: function(e) {
      that.setData({
        ifshowemojicons:false
      })
    },onemojicons: function(e) {
      var id = e.currentTarget.dataset.id
      that.setData({
        body:that.data.body+id,
        ifshowemojicons:false
      })
    },
    swiperchange0: function(e) {
        //console.log(e.detail.current)
    },bsubmit:function(e) {
      that.setData({
        t:0
      })
      that.sendmsg();
    },uploadpic:function(e) {
      if(that.data.addpic=="../../images/ajaxLoader.gif"){
        wx.showToast({
          title: "正在上传中...",
          icon: "none",
          duration: 3000
         })
      }else{
      wx.chooseImage({
        count: 1,
        sizeType: ['original'],
        sourceType: ['album', 'camera'],
        success (res) {
              const tempFilePaths = res.tempFilePaths;
              var userId = wx.getStorageSync("userId")
              var LoginId = wx.getStorageSync("LoginId")
              that.setData({
                  addpic:"../../images/ajaxLoader.gif"
              })
              wx.uploadFile({
                url: app.globalData.apiurl + "/xapiajax.ashx", //仅为示例，非真实的接口地址
                filePath: tempFilePaths[0],
                name: 'image',
                formData: {
                  action:"uploadtempImage",
                  userId:(!userId?"":userId),
                  LoginId:(!LoginId?"":LoginId) 
                },
                success (res0){
                    var data=JSON.parse(res0.data);
                    
                   
                    if(data.status==1){
                      wx.showToast({
                        title: data.msg,
                        icon: "success",
                        duration: 1000
                      })
                         that.setData({
                             image:data.pic,
                             t:2
                        })
                        that.sendmsg();
                    }else{
                      wx.showToast({
                        title: data.msg,
                        icon: "none",
                        duration: 2000
                    })
                    that.setData({
                      addpic:"../../images/add.png"
                    })
                    }
                    
                }
              });
        }
      })
    }

    },sendmsg:function() {
    
      var b=true
      var body = (that.data.t==0?that.data.body:that.data.image);
      if(b){
        if(body==""){
           b=false
           wx.showToast({
              title: "内容不能为空！",
              icon: "none",
              duration: 3000
           })
         }
      }
      if(b){
        if(that.data.t==0){
            if(that.data.savebutton!="发送"){
                b=false
            }
        }else{
          if(that.data.addpic!="../../images/ajaxLoader.gif"){
            b=false
           }
        }
      }
      if(b){
        if(that.data.ttype==0){
          that.setData({
             savebutton:"发送中"
         })
      }
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
           action:"sendmsg",
           userId:(!userId?"":userId),
           LoginId:(!LoginId?"":LoginId),
           body:body,
           ttype:0,
           t:that.data.t
        },
        method:"POST",
        dataType:"json", 
        header: { 
          "content-type": "application/x-www-form-urlencoded" // 默认值
        },
        success (res) { 
          
          if(res.data.status==1){
            wx.showToast({
              title: res.data.msg,
              icon: "success",
              duration: 3000
             })
             if(that.data.t==0){
               that.setData({
                body:""
               })
             }else{
              that.setData({
                image:"",
                addpic:"./../images/add.png"
               })
             }
             that.clearCache();
             that.rdata(1);
          }else{
             wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 3000
             })
          }
          if(that.data.t==0){
            that.setData({
              savebutton:"发送"
            })
          }
          }
      })
    }
    },onviewimage:function(e) {
      var src=e.currentTarget.dataset.id; 
      var imgList=new Array(); 
      imgList[0]=src;
      wx.previewImage({
          urls: imgList,
          current:imgList[0] 
      })
  }
})