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

Page({

  /**
   * 页面的初始数据
   */
  data: {
    circleId: 0,
    isOwner: false,
    calenderStart:"",

    title:'',
    comment:'',
    startDate:'',
    startTime:'',
    finishDate: '',
    finishTime:'',
    enrollDate: '',
    enrollTime:'',
    frontImage:'',
    position: '',
    includeSocialUser:false,
    maxMember:0,
    timeLength:'',//活动时长
    leaderName:'',//联系人
    leaderContact: '',//联系方式
    venue: '',//集合地点
    venueDate: '',//集合日期
    venueTime: '',//集合时间


    fileList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var timestamp = Date.parse(new Date()); 
    var date = new Date(timestamp);//获取年份  
    var yyyy =date.getFullYear();//获取月份  
    var mm = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);//获取当日日期
    var dd = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); 
    this.setData({
      calenderStart: yyyy + '-' + mm + '-' + dd,
      circleId: options.circleId
    })

  },

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

  },

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

  //图片处理
  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),
      frontImage: file[0].path
    });
  },
  //获取活动标题输入
  inputTitle(e) {
    this.setData({
      title: e.detail.value
    })
  },

  //获取输入的活动描述
  inputComment(e) {
    this.setData({
      comment: e.detail.value
    })
  },

  //选择开始时间日期
  selectStartDate(e) {
    this.setData({
      startDate: e.detail.value
    })
  },
  //选择开始时间时间
  selectStartTime(e) {
    this.setData({
      startTime: e.detail.value
    })
  },

  //选择结束时间日期
  selectFinishDate(e) {
    this.setData({
      finishDate: e.detail.value
    })
  },
  //选择结束时间时间
  selectFinishTime(e) {
    this.setData({
      finishTime: e.detail.value
    })
  },

  //选择报名截止时间日期
  selectEnrollDate(e) {
    this.setData({
      enrollDate: e.detail.value
    })
  },
  //选择报名截止时间时间
  selectEnrollTime(e) {
    this.setData({
      enrollTime: e.detail.value
    })
  },

  //活动时长
  inputTimeLength(e) {
    this.setData({
      timeLength: e.detail.value
    })
  },

  //选择活动地点
  inputPosition(e) {
    this.setData({
      position: e.detail.value
    })
  },

  //获取是否允许社会人士参加输入
  onChangeIncludeSocialUser({ detail }) {
    // 需要手动对 checked 状态进行更新
    this.setData({ includeSocialUser: detail });
  },

  //最大参加人数
  inputMaxMember(e) {
    this.setData({
      maxMember: e.detail.value
    })
  },

  //联系人输入
  inputLeaderName(e) {
    this.setData({
      leaderName: e.detail.value
    })
  },

  //联系人联系方式输入
  inputLeaderContact(e) {
    this.setData({
      leaderContact: e.detail.value
    })
  },

  //选择集合时间日期
  selectVenueDate(e) {
    this.setData({
      venueDate: e.detail.value
    })
  },
  //选择集合时间时间
  selectVenueTime(e) {
    this.setData({
      venueTime: e.detail.value
    })
  },

  //集合地点
  inputVenue(e) {
    this.setData({
      venue: e.detail.value
    })
  },
  
  //提交审核
  applyEvent() {
    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
    }
    //活动开始时间必须输入
    if (that.data.startDate == null || that.data.startDate.trim() == "" ||
      that.data.startTime == null || that.data.startTime.trim() == "") {
      Notify('请输入活动开始时间')
      return
    }
    //活动结束时间必须输入
    if (that.data.finishDate == null || that.data.finishDate.trim() == "" ||
      that.data.finishTime == null || that.data.finishTime.trim() == "") {
      Notify('请输入活动结束时间')
      return
    }
    //活动报名截止必须输入
    if (that.data.enrollDate == null || that.data.enrollDate.trim() == "" ||
      that.data.enrollTime == null || that.data.enrollTime.trim() == "") {
      Notify('请输入报名截止时间')
      return
    }

    //活动时长
    if (that.data.timeLength == null || that.data.timeLength.trim() == "") {
      Notify('请输入活动时长')
      return
    }

    //活动地点
    if (that.data.position == null || that.data.position.trim() == "") {
      Notify('请输入活动地点')
      return
    }
    //活动人数必须为数字
    if (that.data.maxMember != null && that.data.maxMember.length > 0) {
      var tmp = parseInt(that.data.maxMember);
      if (isNaN(tmp) || tmp == null ||tmp < 0) {
        Notify('活动人数必须为数字')
        return
      }
    }

    //负责人姓名
    if (that.data.leaderName == null || that.data.leaderName.trim() == "") {
      Notify('请输入联系人姓名')
      return
    }

    //负责人联系方式
    if (that.data.leaderContact == null || that.data.leaderContact.trim() == "") {
      Notify('请输入负责人联系方式')
      return
    }

    //集合时间必须输入
    if (that.data.venueDate == null || that.data.venueDate.trim() == "" ||
      that.data.venueTime == null || that.data.venueTime.trim() == "") {
      Notify('请输入集合时间')
      return
    }

    //集合地点
    if (that.data.venue == null || that.data.venue.trim() == "") {
      Notify('请输入集合地点')
      return
    }

    var config = wx.getStorageSync('config');
    wx.request({
      url: app.url + 'ajax_set_circle_event_new.php',
      data: {
        unionId: config.unionId,
        circleId: that.data.circleId,
        title: that.data.title,
        comment: that.data.comment,
        startTime: that.data.startDate + " " + that.data.startTime,
        finishTime: that.data.finishDate + " " + that.data.finishTime,
        enrollTime: that.data.enrollDate + " " + that.data.enrollTime,
        position: that.data.position,
        includeSocialUser: that.data.includeSocialUser,
        maxMember: that.data.maxMember,
        timeLength: that.data.timeLength,
        leaderName: that.data.leaderName,
        leaderContact: that.data.leaderContact,
        venue: that.data.venue,
        venueTime: that.data.venueDate + " " + that.data.venueTime
      },
      header: { 'content-type': 'application/json' },
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        if(res.data.status == "OK") {
          if (that.data.frontImage != null && that.data.frontImage != undefined) {
            wx.uploadFile({
              url: app.url + 'ajax_set_circle_event_new.php?unionId=' + config.unionId + "&circleId=" + that.data.circleId,
              filePath: that.data.frontImage,
              name: 'frontImage',
              header: { 'content-type': 'multipart/form-data' },
              success: function (res) {
                Notify('提交成功！请等待审核！')
                //循环执行等待全部结束
                var a = setInterval(function () {
                  //跳转到圈子活动首页
                  wx.navigateTo({
                    url: '../circleEvents/circleEvents?circleId=' + that.data.circleId,
                  });
                  //循环执行代码 
                  clearInterval(a)
                }, 1000) //循环时间 这里是1秒

              },
              fail: function (res) {
                console.log(res, '上传失败')
              },
              complete: function (res) {
              },
            })
          }
        }
        if (res.data.status == "NG") {
          Notify(res.data.result.message)
          return
        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

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

  },
})