// pages/myNews/myNews.js
var app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    keyword: '',
    fileList: [],
    imgPaths: [],
  },

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

  //
  delete(event) {
    const {
      index
    } = event.detail;
    const fList = this.data[`fileList`];
    const imgList = this.data[`imgPaths`];
    fList.splice(index, 1);
    imgList.splice(index, 1);
    this.setData({
      [`fileList`]: fList,
      [`imgPaths`]: imgList
    });
  },
  afterRead(event) {
    const {
      file
    } = event.detail;
    const fList = this.data[`fileList`];
    let images = this.data.imgPaths;
    file.forEach(item => {
      images.push(item.path)
    })
    this.setData({
      [`fileList`]: fList.concat(file),
      imgPaths: images
    });
  },
  beforeRead(event) {
    const {
      file,
      callback
    } = event.detail;
    if (file[0].path.indexOf('jpg') < 0 && file[0].path.indexOf('png') < 0) {
      wx.showToast({
        title: '请选择图片文件上传',
        icon: 'none'
      });
      callback(false);
      return;
    }
    callback(true);
  },

  //上传素材
  upload() {
    var that = this;
    console.log(that.data.imgPaths)
    //感想必须输入
    if (that.data.keyword == null || that.data.keyword.trim() == "") {
      Notify('请输入文字')
      return
    }
    var config = wx.getStorageSync('config');
    wx.request({
      url: app.url + 'ajax_set_faq_new.php',
      data: {
        unionId: config.unionId,
        content: that.data.keyword,
      },
      header: { 'content-type': 'application/json' },
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        //循环上传图片
        console.log(res)
        var completeCount = 0;
        for (var i = 0; i < that.data.imgPaths.length; i++) {
          //停顿1秒
          var imgPath = that.data.imgPaths[i];
          console.log(imgPath)
          setTimeout(function () {
            wx.uploadFile({
              url: app.url + 'ajax_set_faq_new.php?unionId=' + config.unionId + "&faqId=" + res.data.result.faqDat.id,
              filePath: imgPath,
              name: 'images',
              header: { 'content-type': 'multipart/form-data' },
              success: function (res) {
              },
              fail: function (res) {
                console.log(res, '上传失败')
              },
              complete: function (res) {
                completeCount++;
              },
            })
          }, 1000) //延迟时间 这里是1秒
        }

        //循环执行等待全部结束
        var a = setInterval(function () {
          //循环执行代码 
          if (that.data.imgPaths.length == completeCount) {
            //跳转到首页
            wx.showToast({
              title: '上传成功!',
              icon: 'success',
              duration: 2000
            });
            clearInterval(a)

            //返回上一页
            wx.navigateBack({
              delta: 1
            })
          }
        }, 1000) //循环时间 这里是1秒

      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },


  keywordInput(e) {
    this.setData({
      keyword: e.detail.value
    })
  },

  back: function () {
    wx.navigateBack({
      delta: 1
    })
  },
})