const app = getApp()
const util = require("../../../utils/util");
const api = require('../../../api/api.js');
const user = require('../../../utils/user.js');
// const update = require('../../../api/update.js');




Page({
  data: {
    max:5,
    uploadShow:true,
    isFinish:false,
    content:'',
    feedbackInfo:{
      noticeType:'',
      sendTime:'',
    },
    fileList: [],
    pictures:[],
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
    console.log(options);
    if(options.id){
      this.setData({id:options.id});
      this.getDetail();
    }
  },
  getDetail(){
     
      util.request(api.getDetail, {id:this.data.id
      }, 'GET').then(res=> {
        if (res.errno === 0) {
          this.setData({
            isFinish:res.data.feedbackComment==''?false:true,
            feedbackInfo:res.data,
            content:res.data.feedbackComment
          });
          if(this.data.isFinish){
            let imgs=res.data.feedbackImgs;
            if(imgs==''){
              this.setData({
                uploadShow:false,
              });
            }else{
              let list=imgs.split(',');
              let current=[];
              list.forEach(item=>{
                let obj={
                  url:item
                };
                current.push(obj);
              });
              this.setData({
                fileList:current,
                max:current.length,
              });
            }
          }
        }
  
      
      });

  },
  bindContent(e){
    this.setData({
      content:e.detail.value
    })
  },
  deleteImage (event) {
    const { fileList = [] } = this.data;
    fileList.splice(event.detail.index, 1)
    this.setData({
      fileList: fileList
    })
    let urls=[];
    fileList.forEach(function(e){
      urls.push(e.url);
    })
    this.setData({
      "pictures": urls
    })
  },
  afterRead(event) {
    console.log(event);
    const { file } = event.detail
    let that = this;
    const uploadTask = wx.uploadFile({
      url: api.StorageUpload(),
      filePath: file.url,
      name: 'file',
      success: function (res) {
        console.log(res);
        var _res = JSON.parse(res.data);
        if (_res.errno === 0) {
          var url = _res.data.fileUrl;
          that.data.pictures.push(url)
          const { fileList = [] } = that.data;
          fileList.push({ ...file, url: url });
          that.setData({
            fileList: fileList
          })
        }
      },
      fail: function (e) {
        wx.showModal({
          title: '错误',
          content: '上传失败',
          showCancel: false
        })
      },
    })
  },
  submit(){
    if(this.data.content==''){
      wx.showToast({
        title: '请填写您的反馈',
        icon:'none'
      })
    }else{
      let imgs=[];
      this.data.fileList.forEach(item=>{
        imgs.push(item.url)
      });
      util.request(api.commitFeedback, {
        id:this.data.id,
        comment:this.data.content,
        imgs:imgs.join(',')

      }, 'GET').then(res=> {
        if (res.errno === 0) {
          wx.showToast({
            title: '提交成功',
            icon:'none'
          });
          this.getDetail();

        }
  
      
        });


    }
  },
  onReachBottom() {
    // if (this.data.totalPages > this.data.page) {
    //   this.setData({
    //     page: this.data.page + 1
    //   });
    //   this.getGoodsList();
    // } else {
    //   wx.showToast({
    //     title: '没有更多商品了',
    //     icon: 'none',
    //     duration: 2000
    //   });
    //   return false;
    // }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

  onUnload: function () {

  },
  

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
   
  },
  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
})