var WxParse = require('../../wxParse/wxParse.js');
const app = getApp();
const baseUrl = getApp();
const api = require('../../config/api.js');
const util = require('../../config/util.js');
Page({
  startX: 0,
  startY: 0,
  isClear: false,
  /**
   * 页面的初始数据
   */
  data: {
    title: '我的收藏',  
    singdouble: 2, //1单选，2多选
    paiduanactive:1,//1不是判断，2是判断题
    activeopen:true,
    itemdata: [],
    itemdatacurrent:[],
    exam_id:1,
    baseurl:baseUrl.globalData.baseimgurl,
    content:'',
    examtypename:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: this.data.title
    })
    console.log(options);
    this.setData({
      exam_id:options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
        this.getexamdata(this.data.exam_id);
        this.wrodtypelist(this.data.exam_id);
  },  

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },  
  openactive:function(e){
    console.log(e);
    let opendata=this.data.openactive;
    opendata=!opendata;
    console.log(opendata);
    this.setData({
      activeopen:opendata,      
      openactive:opendata
    })
  },
  getexamdata:function(params) {
        //得到练习题目
      const that = this;
      let url = baseUrl.globalData.baseUrl + '/exam/myexamexp';   
      var param = {
        exam_id:params
      };
      //此处为使用封装的post请求
      util.post(url, param).then((res) => {
        console.log(res.data);
        console.log(res.data.examtypename);
        that.setData({
          itemdataall: res.data,
          itemdata:res.data.examitem,
          examtypename:res.data.examtypename,
          content:res.data.name.replace(/\<img/gi, '<img style="width:80%;height:auto"'),
          singdouble:res.data.examtype,
          paiduanactive:res.data.examtype    
        });

      }).catch((errMsg) => {
        console.log(errMsg);
      });      
  },
  wrodtypelist:function(params){

    //获取课程留言
    const that = this;
    let url = baseUrl.globalData.baseUrl + '/exam/wordsexam';
    var param = {
      exam_id: params
    };
    //此处为使用封装的post请求
    util.get(url, param).then((res) => {
       console.log(res);
      that.setData({
        replylist: res.data
      });

    }).catch((errMsg) => {
      console.log(errMsg);
    });
  },
  formSubmit:function(e) {

    if(wx.getStorageSync('wxuserid')==''){
       wx.navigateTo({
         url: '/pages/userlogin/userlogin',
       })
    }
  
    let content = this.data.wordstextname;
    console.log(content)
    if (content == '') {
      wx.showToast({
        title: '留言内容为空,请填写内容',
        icon: 'none',
        duration: 1500
      })
      return false;     
    }
    // console.log(this.data.imagestr);
    this.wordsadd(content,this.data.imagestr);
   
  },
  bindTextAreaBlur: function(e) {
    this.setData({
     wordstextname:e.detail.value
    })  
 }, 
 uploadpicsimage:function(e){

  const that = this;
  let url = baseUrl.globalData.baseUrl + '/wordslist/uploadpics';
  var param = {
    url: e
  };
  //此处为使用封装的post请求
  util.post(url, param).then((res) => {
     console.log(res);   

  }).catch((errMsg) => {
    console.log(errMsg);
  });
 },
 wordsadd:function(content,imagestr){

        const that = this;
        let url = baseUrl.globalData.baseUrl + '/exam/examexplainadd';
        var param = {
          content: content,
          imagestr:imagestr,
          userid:wx.getStorageSync('wxuserid')==''?3:wx.getStorageSync('wxuserid'),
          exam_id:that.data.courseid
        };
        //此处为使用封装的post请求
        util.post(url, param).then((res) => {
          console.log(res.data);   
          if(res.data>0){
            wx.showToast({
              title: '留言内容成功',
              icon: 'none',
              duration: 1500
            })
            
            that.wrodtypelist(that.data.exam_id); //试题留言   
            that.setData({
              wordstextname:''
            })
            
          }

        }).catch((errMsg) => {
          console.log(errMsg);
        });
 },contentclick:function(params) {

     console.log(params);
       let id=params.currentTarget.id;
       let xid=params.currentTarget.dataset.xid;
       console.log(id);
       this.setData({
        wordstextname:'回复@'+id,
        showview:true
       });

      this.addinccomment(xid);

       wx.showToast({
        title: '@成功，留言请移底部留言',
        icon: 'none',
        duration: 1500
      })
 },rdclick:function(params) {
    wx.navigateTo({
      url: '/pages/communityinfo/communityinfo?id='+params.currentTarget.id,
    })
 },addinccomment:function(params) {
     //增加评论数
      console.log(params);
      let userid=wx.getStorageSync('wxuserid');
      const that = this;
      let url = baseUrl.globalData.baseUrl + '/exam/addcommentcount';
      var param = {
        id:params
      };
      //此处为使用封装的post请求
      util.get(url, param).then((res) => {
        console.log(res);   
          // this.wrodtypelist(this.data.courseid)
          this.wrodtypelist(that.data.courseid);
          this.getexamexplantypelist(that.data.courseid);
      }).catch((errMsg) => {
        console.log(errMsg);
      });
 },
 preview:function (params) {
      let that=this;
       console.log(params)
       let currentUrl = params.currentTarget.dataset.src;
       let arrindex=params.currentTarget.dataset.id;
      
       let rearr=that.data.replylist[arrindex].pics;
       console.log(rearr);
       let arr=[];
       rearr.forEach(function (item,index) {       
             arr[index]=that.data.baseurl+item
       });
       console.log(arr);
       wx.previewImage({
         current: currentUrl, // 当前显示图片的http链接
         urls: arr // 需要预览的图片http链接列表
       })
     },
     uploadpic:function(){
      var that = this;
      let imagesList = [];
      let maxSize = 1024 * 1024;
      let maxLength = 6;
      let flag = true;
      wx.chooseImage({
        count: 9, //最多可以选择的图片总数
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          wx.showToast({
            title: '正在上传...',
            icon: 'loading',
            mask: true,
            duration: 500
          })
          for (let i = 0; i < res.tempFiles.length; i++) {
            if (res.tempFiles[i].size > maxSize) {
              flag = false;
              console.log(111)
              wx.showModal({
                content: '图片太大，不允许上传',
                showCancel: false,
                success: function (res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                  }
                }
              });
            }
          }
          if (res.tempFiles.length > maxLength) {
            console.log('222');
            wx.showModal({
              content: '最多能上传' + maxLength + '张图片',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  console.log('确定');
                }
              }
            })
          }
          if (flag == true && res.tempFiles.length <= maxLength) {
            that.setData({
              imagesListp: res.tempFilePaths
            })
            that.data.imagestr=[];
            var arr='';
            for (let i = 0; i < that.data.imagesListp.length; i++) {       
              wx.uploadFile({ 
                url: baseUrl.globalData.baseUrl + '/wordslist/uploadpics', 
                filePath: that.data.imagesListp[i], 
                name: 'file', 
                success:function(res){ 
                  //打印
                  console.log("------------响应数据：");
                  console.log(res.data);
                
                  var message = JSON.parse(res.data);
                  that.data.imagestr.push(message);
                  that.setData({
                     imagestr:that.data.imagestr
                  });
                },
              })
          
            }
          
  
          }
          console.log(res);
        },
        fail: function (res) {
          console.log(res);
        }
      })
   
    },    
    clickbtn:function(params) {
      console.log(params);
      let userid=wx.getStorageSync('wxuserid');
      const that = this;
      let url = baseUrl.globalData.baseUrl + '/exam/wordscourseclickcount';
      var param = {
        words_id:params.currentTarget.id,
        user_id: userid
      };
      //此处为使用封装的post请求
      util.get(url, param).then((res) => {
        console.log(res);   
          // this.wrodtypelist(this.data.courseid)
          this.wrodtypelist(this.data.exam_id);
          this.getexamdata(this.data.exam_id);
      }).catch((errMsg) => {
        console.log(errMsg);
      });
},

})