// pages/Input_content/input_content.js

const app = getApp();
const url = require('../../utils/const.js');
const APIClient = require('../../utils/APIClient.js');
const login = require('../../utils/loginSchedule.js');
const utils = require('../../utils/util.js');

const HOST = url.apiUrl;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tempFilePath: [],
    imgId: [],
    textValue: '',
    //type: '',
    //columnType: ''
  },

  /**
   * 获取输入框内容
   */
  bindKeyInput: function(e) {
    this.setData({
      textValue: e.detail.value
    })
  },

  /**
   * 图片上传
   */
  uploading: function () {
    var that = this;  
    wx.chooseImage({  
      count: 2,  //最多可以选择的图片总数  
      sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有  
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有  
      success: function (res) {  
        // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片  
        var tempFilePaths = res.tempFilePaths; 
        //启动上传等待中...  
        wx.showToast({  
          title: '正在上传...',  
          icon: 'loading',  
          mask: true,  
          duration: 1000 
        })  

        that.setData({
          tempFilePath: tempFilePaths
        })
        var uploadImgCount = 0;  
        for (var i = 0, h = tempFilePaths.length; i < h; i++) {  
          //上传文件
          wx.uploadFile({  
            url: HOST + '/cms/file/upload',  
            filePath: tempFilePaths[i],  
            name: 'uploadfile_ant',  
            header: {  
              "Content-Type": "multipart/form-data"  
            },  
            success: function (res) {  
              uploadImgCount++;  
              let data = JSON.parse(res.data);
              that.data.imgId.push(data.data.id)
              that.setData({
                imgId: that.data.imgId,
              })
              console.log(data);
              //如果是最后一张,则隐藏等待中  
              if (uploadImgCount == tempFilePaths.length) {  
                wx.hideToast();  
              }  
            },  
            fail: function (res) {  
              wx.hideToast();  
              wx.showModal({  
                title: '错误提示',  
                content: '上传图片失败',  
                showCancel: false,  
                success: function (res) { }  
              })  
            }  
          });  
        }  
      }  
    }); 
  },
  //点击发送
  send: function() {
    const type = utils.getUrl().type;
    const columnType = utils.getUrl().columnType;
    const columnId = utils.column(columnType).columnId;
    const columnName = utils.column(columnType).columnName;
    if(this.data.imgId.length == 0 && type == 2){
      wx.showModal({
        title: '提示',
        content: '请上传分享的作品',
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return false;
    } 
    let data = {
      "title": this.data.textValue,
      "type": type,
      "columnId": columnId,
      "columnType": columnType,
      "columnNames": columnName,
    }; 
    if(type == 2){
      data.imagesStrList = this.data.imgId
    }
    login.getOpenidSessionKey(function(res) {
      //console.log(res.data.data.uid);
      APIClient.getSendSchedule({
        uid: res.data.data.uid
      }, data).success(res => {
        console.log(res)
        if(res.data.success) {  
          wx.redirectTo({
            url: utils.url(columnType)
          })
        }
      })      
    }, function() {
      wx.showModal({
        title: '提示',
        content: '需要获取您的公开信息(昵称、头像等),请从小程序列表删除小学王者班后再次扫码进入,允许授权后可正常使用',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    });
    
  },
  cancel: function() {
    wx.navigateBack({ changed: true });
  },
  /**
   * 图片预览
   */
  listenerButtonPreviewImage: function(e) {
    let index = e.target.dataset.index;
    let that = this; 
    wx.previewImage({
        current: that.data.tempFilePath[index],
        urls: that.data.tempFilePath,
        //这根本就不走
        success: function(res) {
            //console.log(res);
        },
        //也根本不走
        fail: function() {
            //console.log('fail')
        }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.type == 2) {
      wx.setNavigationBarTitle({
        title: '作品分享'
      })
    }
    if(options.type == 1) {
      wx.setNavigationBarTitle({
        title: '答疑讨论'
      })
    }
    this.setData({
      type: options.type
    })
  },

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

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

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

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

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

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