// pages/welfareMall/feedback/feedback.js
import WelfareMall from '../../../api/welfareMall'
import Upload from '../../../api/upload'
import {getMobileCache, getPhoneNumber as getPhoneNumberSync, getColor} from '../../../utils/user'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    t_length: 0,
    avatarUrl: [],
    avatarUrlShow: [],
    chooseViewShowBanner: true,
    content: '',
    basePath: '',
    hideWindowValue: false,
    config: {},
  },

  bindText: function (e) {
    var t_text = e.detail.value.length;
    var text = e.detail.value;
    this.setData({
      t_length: t_text,
      content: text,
    }) 
  },

   /** 图片选择 */
   bindViewTap: function(){
    var that = this;
    if(this.data.avatarUrlShow.length < 4){
       wx.chooseImage({
        // 设置最多可以选择的图片张数，默认9,如果我们设置了多张,那么接收时//就不在是单个变量了,
         count: 1, 
         sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
         sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
         success: function(res){
          // 获取成功,将获取到的地址赋值给临时变量
          console.log(res);
          if (res.tempFilePaths.count == 0) {
            return;
          }
          //图片上传到金山云
          var tempFilePaths = res.tempFilePaths
          
          wx.uploadFile({
            url: Upload.uploadPic(),
            filePath: tempFilePaths[0],
            name: 'file',
            formData:{
              'basePath':'welfareMall',
            },
            success: function (res) {
              //提交路径
              var urlImg = JSON.parse(res.data).data.url;
              //展示路径
              var destPath = JSON.parse(res.data).data.destPath;

              var imgArrNow = that.data.avatarUrl;
              var imgArrNowShow = that.data.avatarUrlShow;
              imgArrNow.push(urlImg);
              imgArrNowShow.push(destPath);
              if(that.data.avatarUrlShow.length  > 4){
                wx.showToast({
                  title: '最多添加4张图片',
                  icon: 'none',
                  duration: 1500
                })
                return
              }
              that.setData({
              //将临时变量赋值给已经在data中定义好的变量
              avatarUrl:imgArrNow,
              avatarUrlShow:imgArrNowShow
              })
              that.chooseViewShowBanner();
            },
            fail: function(err) {
              console.log(err);
            },
          })
          
         },
         fail: function(res) {
          // fail
         },
         complete: function(res) {
          // complete
         }
        })
    } else {
      wx.showToast({
        title: '最多添加4张图片',
        icon: 'none',
        duration: 1500
      })
    }
   },

   
   /** 删除图片Banner */
  deleteImvBanner: function(e) {
    var avatarUrlShow = this.data.avatarUrlShow;
    var avatarUrl = this.data.avatarUrl;
    var itemIndex = e.currentTarget.dataset.id;
    avatarUrlShow.splice(itemIndex, 1);
    avatarUrl.splice(itemIndex, 1);
    this.setData({
      avatarUrlShow: avatarUrlShow
    })
    //判断是否隐藏选择图片
    this.chooseViewShowBanner();
  },

   /** 是否隐藏图片选择Banner*/
   chooseViewShowBanner: function() {
    if (this.data.avatarUrlShow.length > 3) {
      this.setData({
        chooseViewShowBanner: false
      })
    } else {
      this.setData({
        chooseViewShowBanner: true
      })
    }
  },

   /** 图片预览 */
  previewImage: function(e){
    var that = this,
    //获取当前图片的下标
      index = e.currentTarget.dataset.index,
      //数据源
      avatarUrlShow = this.data.avatarUrlShow;
    wx.previewImage({
    //当前显示下标
     current: avatarUrlShow[index],
     //数据源
     urls: avatarUrlShow
    })
   },

   /**
    * 提交
    */
   toSubmit: function() {
     if(this.data.content == null || this.data.content == '' || this.data.content == undefined){
      wx.showToast({
        title: '意见内容必填',
        icon: 'none',
        duration: 1500
      })
      return
     }
    WelfareMall.feedBackAdd({
      mobile: getMobileCache(),
      photoList: this.data.avatarUrl,
      content: this.data.content,
    }).then(res => {
        if (res.code == 200) {
          this.hideWindow()
        }
    }).catch(_ => {
        console.log(_)
    })
  },

  /**
   * 弹框
   */
  hideWindow(){
    this.setData({
      hideWindowValue:true
    })
  },

  goPerson(){
    wx.switchTab({
         url: '../personal/personal'
     })
  },

  async getIndexList(){
    let res =  await getColor();
    this.setData({
      config:res.config
    })
  },
  goToRefund (){
    wx.navigateTo({
      url: '/pages/welfareMall/refundInfo/refundInfo'
    })
  },
  goToBuy(){
    wx.navigateTo({
      url: '/pages/welfareMall/buyInfo/buyInfo'
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getIndexList()
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})