// pages/circleNoticeNew/circleNoticeNew.js
import Notify from '../../dist/notify/notify';
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    circleId: '',
    circleDat: null,

    title: '',
    comment:'',
    fileList: [],
    imagePath:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取参数
    this.setData({
      circleId: options.circleId
    })
  },

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

  },

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

  },

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

  },

  //第1张图片
  delete(event) {
    const { index, name } = event.detail;
    const fileList = this.data[`fileList`];
    fileList.splice(index, 1);
    this.setData({ [`fileList`]: fileList });
  },
  afterRead(event) {
    const { file, name } = event.detail;
    const fileList = this.data[`fileList`];
    this.setData({
      [`fileList`]: fileList.concat(file),
      imagePath: file[0].path
    });
  },
  beforeRead(event) {
    const { file, callback } = event.detail;
    if (file[0].path.indexOf('jpg') < 0) {
      wx.showToast({ title: '请选择jpg图片上传', icon: 'none' });
      callback(false);
      return;
    }
    callback(true);
  },

  //发布公告
  publish(){
    var that = this;
    //内容必须输入
    if (that.data.title == null || that.data.title.trim() == "") {
      Notify('请输入公告标题')
      return
    }
    //内容必须输入
    if (that.data.comment == null || that.data.comment.trim() == "") {
      Notify('请输入公告内容')
      return
    }
    var config = wx.getStorageSync('config');
    wx.request({
      url: app.url + 'ajax_set_circle_notice_new.php',
      data: {
        unionId: config.unionId,
        circleId: that.data.circleId,
        title: that.data.title,
        comment: that.data.comment,
      },
      header: { 'content-type': 'application/json' },
      method: 'GET',
      dataType: 'json',
      success: function (res) {

        if (that.data.imagePath != null && that.data.imagePath != undefined) {
        wx.uploadFile({
          url: app.url + 'ajax_set_circle_notice_new.php?unionId=' + config.unionId + "&circleId=" + that.data.circleId,
          filePath: that.data.imagePath,
          name: 'image',
          header: { 'content-type': 'multipart/form-data' },
          success: function (res) {
            Notify('发布成功！')
            //循环执行等待全部结束
            var a = setInterval(function () {
              //跳转到圈子首页
              wx.redirectTo({
                url: '../circleDetails/circleDetails?circleId=' + that.data.circleId,
              });
              //循环执行代码 
              clearInterval(a)
            }, 1000) //循环时间 这里是1秒

          },
          fail: function (res) {
            console.log(res, '上传失败')
          },
          complete: function (res) {
          },
        })
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  //获取输入的公告标题
  inputTitle(event){
    this.setData({
      title: event.detail.value
    })
  },

  //获取输入的公告内容
  inputComment(event) {
    this.setData({
      comment: event.detail.value
    })
  }
})