// pages/chatRelease/chatRelease.js
//获取应用实例
const app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {

		classify: 1, //分类
		imgs: null, //本地图片地址
		items: [

			{
				name: '1',
				value: '闲聊',
				checked: 'true'
			},
			{
				name: '2',
				value: '吐槽'
			},
			{
				name: '3',
				value: '表白'
			},
			{
				name: '4',
				value: '捞人'
			},

		]
	},

	//-----------------------分类选择-------------------------
	radioChange: function (e) {
		this.setData({
			'classify': e.detail.value
		})
		console.log(this.data.classify)
	},

	//-----------------------图片上传-------------------------
	//添加上传图片
	chooseImageTap: function () {
		var that = this;
		wx.showActionSheet({
			itemList: ['从相册中选择', '拍照'],
			itemColor: "#00000",
			success: function (res) {
				if (!res.cancel) {
					if (res.tapIndex == 0) {
						that.chooseWxImage('album')
					} else if (res.tapIndex == 1) {
						that.chooseWxImage('camera')
					}
				}
			}
		})
	},
	// 图片本地路径
	chooseWxImage: function (type) {
		var that = this;
		var imgsPaths = that.data.imgs;
		wx.chooseImage({
			count: 1,
			sourceType: ['album', 'camera'],
			sizeType: ['original', 'compressed'],
			success: function (res) {
				// console.log(res.tempFilePaths[0]);
				that.setData({
					imgs: res.tempFilePaths[0]
				})
			}
		})
	},

	//---------------------删除选中图片------------------------
	deleteImage() {
		this.setData({
			imgs: null
		})
	},

	//-------------------------表单---------------------------
	formSubmit(e) {
		var that = this;
		if (app.globalData.hasLogin && e.detail.value.head !== "" && e.detail.value.body !== "") {
			if (that.data.imgs != null) {
				// console.log(that.data.imgs);
				// //调用上传方法
				wx.uploadFile({
					url: app.globalData.myUrl + '/tree/img', //
					filePath: that.data.imgs,
					method: 'POST',
					name: 'file',
					header: {
						'content-type': 'multipart/form-data'
					},
					formData: {
						method: 'POST'
					},
					success: function (res) {
						var data = JSON.parse(res.data) //接口返回网络路径
						//上传表单
						wx.request({
							url: app.globalData.myUrl + '/tree/save',
							method: 'GET',
							header: {
								'content-type': 'application/json' // 默认值
							},

							data: {
								token: app.globalData.token, //用户标识
								head: e.detail.value.head, //树洞标题
								body: e.detail.value.body, //树洞主体
								imgPath: app.globalData.httpUrl + '/' + data['serverFileName'], //图片路径
								classify: that.data.classify //树洞分类
							},
							success: function (res) {
								if (res.data.success) {
									that.sucShowToast(res.data.msg);
									wx.navigateBack({
										delta: 1
									})
									that.setData({
										imgs: null
									})
								} else {
									that.errShowToast(res.data.msg);
								}
							}
						})
					}
				})
			} else {
				//上传表单
				wx.request({
					url: app.globalData.myUrl + '/tree/save',
					method: 'GET',
					header: {
						'content-type': 'application/json' // 默认值
					},
					data: {
						token: app.globalData.token, //用户标识
						head: e.detail.value.head, //树洞标题
						body: e.detail.value.body, //树洞主体
						classify: that.data.classify //树洞分类
					},
					success: function (res) {
						if (res.data.success) {
							wx.navigateBack({
								delta: 1
							})
							that.sucShowToast(res.data.msg);
							that.setData({
								imgs: null
							})
						} else {
							that.errShowToast(res.data.msg);
						}
					}
				})
			}
		} else {
			if (!app.globalData.hasLogin) {
				that.errShowToast('你还没有登录喔');
			} else {
				that.errShowToast('内容不能为空');
			}
		}
	},

	//---------------------------函数封装------------------------------
	errShowToast: function (toastText) {
		wx.showToast({
			title: toastText,
			image: '/images/password.png',
			duration: 2000
		});
	},
	sucShowToast: function (toastText) {
		wx.showToast({
			title: toastText,
			duration: 2000
		});
	},




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

	},

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

	},

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

	},

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

	},

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

	},

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

	},

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

	},

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

	}
})