// index.js
let myPro = require("../../utils/wxRequest.js");
let util = require("../../utils/util.js");
Page({
    data: {
		page: 1,
		size: 10,
		dataList: [],
		loading: false,
		finished: false,
		content: "",
		userThumb: "", // 用户头像
		kfThumb: "", // 客服头像
		latestId: '', // 最新的一条
		openToView: true,
		toView: ''
    },
    onLoad: function (options) {
        // Do some initialize when page load.
		let that = this;
		that.setData({
			userThumb: getApp().globalData.userInfo.thumb,
			kfThumb: getApp().globalData.logoTu
		});
		// console.log("用户头像",getApp().globalData.userInfo.thumb)
		
    },
    onShow: function () {
        // Do something when page show.
		let that = this;
		// 滚动区域高度计算
		wx.getSystemInfo({
			success: function(res){
				let height = res.windowHeight;
				// that.setData({
				// 	scrollH: height + 160,
				// });
				console.log('总height',height)
				const query = wx.createSelectorQuery();
				// query.select('.tips').boundingClientRect();
				query.select('.wrap_pub').boundingClientRect();
				query.exec((res) =>{
					// 分别取出节点的高度
					let tipsH = res[0].height; // 发布框的高度 px
					let pubH = res[0].height; // 发布框的高度 px
					let scrollH = height - pubH;
					that.setData({
						scrollH: scrollH,
						initScrollH: scrollH
					});
					// wx.pageScrollTo({
					// 	scrollTop: scrollH,
					// 	duration: 100
					// })
					console.log('setBgHeight',getApp().globalData.setBgHeight)
					// console.log('pubH',pubH)
					console.log('scrollH',scrollH)
				});
			}
		});
		// 验证登录
		getApp().verifyLogin(function(loginStatus){
			if(loginStatus){
				that.setData({
					page: 1,
					openToView: true,
					dataList: [],
					init: true,
					finished: false
				});
				that.getDataList();
			}
		});
    },
    onReady: function () {
        // Do something when page ready.
    },
    onHide: function () {
        // Do something when page hide.
    },
    onUnload: function () {
        // Do something when page close.
    },
    onPullDownRefresh: function () {
        // Do something when pull down.
		// let that = this;
		// that.getDataList();
		// //停止当前页面下拉刷新
		// wx.stopPullDownRefresh();
    },
    onReachBottom: function () {
        // Do something when page reach bottom.
    },
    onPageScroll: function () {
        // Do something when page scroll
		
    },
    onResize: function () {
        // Do something when page resize
    },
	// 获取数据列表
	getDataList(){
		let that = this;
		let params = {
			page: that.data.page,
			size: that.data.size
		};
		myPro.wxRequest("user/v2/suggestionList","GET",params).then(res=>{
			let list = res.result;
			if(list.length != 0){
				if(that.data.page == 1){
					that.setData({
						dataList: list,
						page: that.data.page+1,
						init: false
					});
					console.log('openToView',that.data.openToView);
					setTimeout(function(){
						if(that.data.openToView){
							that.setData({
								toView: 'item'+ list[list.length -1].id
							});
						};
					},100)
				}else{
					list = list.concat(that.data.dataList);
					that.setData({
						dataList: list,
						page: that.data.page+1,
						init: false
					});
				}
			}else{
				wx.showToast({
					title: "暂无更多数据",
					icon: "none"
				});
			};
		}).catch(err=>{
			console.log("报错信息",err);
			wx.showToast({
				title: err,
				icon: "none"
			});
		})
	},
	// 获取输入内容
	getContent(event){
		let that = this;
		that.setData({
			content: event.detail
		});
	},
	// 实现得不好, 还是使用自带的
	// 聚焦
	bindFocus(event){
		let that = this;
		// console.log('focus',event);
		that.setData({
			content: event.detail.value,
			keybordH: event.detail.height, // 键盘高度
			scrollH: that.data.scrollH - event.detail.height
		});
	},
	// 失去焦点
	bindBlur(event){
		let that = this;
		that.setData({
			content: event.detail.value,
			keybordH: 0,	// 键盘高度
			scrollH: that.data.initScrollH
		});
	},
	// 发布消息
	bindSendContent(){
		let that = this;
		if(!that.data.content){
			wx.showToast({
				title: "请输入内容",
				icon: "none"
			});
			return;
		};
		let params = {
			content: that.data.content
		};
		myPro.wxRequest("user/v2/suggestionApply","POST",params).then(res=>{
			that.setData({
				content: "",
				init: true
			});
			wx.showToast({
				title: res.msg,
				icon: "none"
			});
			// 重新拉取
			that.setData({
				page: 1,
				dataList: [],
				openToView: true,
				finished: false,
			});
			that.getDataList();
		}).catch(err=>{
			console.log("报错信息",err);
			wx.showToast({
				title: err,
				icon: "none"
			})
		})
	},
	// 滚动到顶部
	bindUpper(){
		let that = this;
		// console.log('触发了吗=====')
		if(!that.data.init){
			that.setData({
				openToView: false
			});
			that.getDataList();
		};
	}
});
