<template>
	<view class="page" style="background-color: #fff;">
		<!-- 建议输入框 -->
		<u-input v-model="content" type="textarea" height="200" placeholder="请输入您的意见或建议" border autoHeight></u-input>
		<u-gap></u-gap>
		<u-button type="primary" :disabled="submitDisable" @click="submit()">提交</u-button>
		<u-gap></u-gap>
		<u-divider v-if="dataList.length > 0">历史记录</u-divider>
		<u-gap></u-gap>
		<!-- 记录 -->
		<u-cell-group>
			<u-cell-item
				v-for="(item, index) in dataList"
				:key="index"
				:title="item.content"
				:label="vk.pubfn.timeFormat(item._add_time, 'yyyy-MM-dd')"
				:value="item.status ? '已答复' : '未答复'"
				:arrow="item.staus"
				@click="showDetailPopup(index)"
			></u-cell-item>
		</u-cell-group>

		<!-- 反馈详情 -->
		<u-popup v-model="detailPopupShow" mode="center" width="80%" height="460rpx" border-radius="20" closeable close-icon-color="#fff">
			<view class="bwin-popup">
				<view class="popup-header">反馈详情</view>
				<view class="popup-body">
					<view class="content">{{ dataList[currentItemIndex].content }}</view>
					<view class="comment">平台答复：{{ dataList[currentItemIndex].comment ? dataList[currentItemIndex].comment : '暂未答复' }}</view>
				</view>
			</view>
		</u-popup>

		<u-loadmore
			v-if="dataList.length > 0"
			marginTop="20"
			:line="true"
			:status="loadmore.status"
			:loading-text="loadmore.loadingText"
			:loadmore-text="loadmore.defaultText"
			:nomore-text="loadmore.nomoreText"
		/>
	</view>
</template>
<script>
var that;
export default {
	data() {
		return {
			content: '',
			submitDisable: false,
			dataList: [],
			detailPopupShow: false,
			currentItemIndex: 0,
			loadmore: {
				status: 'loadmore',
				loadingText: '努力加载中',
				defaultText: '轻轻上拉 查看更多',
				nomoreText: '实在没有了',
				currentPage: 1
			}
		};
	},
	onLoad() {
		that = this;

		let lastSuggestTime = vk.vuex.get('$user.history.lastSuggestTime');
		let currentTime = Date.parse(new Date()) / 1000; // 时间戳用来计算

		if (vk.pubfn.isNotNull(lastSuggestTime) && currentTime - lastSuggestTime < 3600) {
			that.submitDisable = true;
		}

		vk.callFunction({
			url: 'client/agent/kh/getSuggestLogList',
			title: '请求中...',
			needAlert: false
		}).then(res => {
			if (res.list.length < 10) {
				that.loadmore.status = 'nomore';
			}
			that.dataList = res.list;
		});
	},
	methods: {
		// 提交
		submit() {
			if (vk.pubfn.isNull(that.content)) {
				vk.toast('请填写反馈内容', 'none');
				return;
			}
			vk.callFunction({
				url: 'client/agent/kh/createSuggestLog',
				title: '提交中...',
				needAlert: false,
				data: {
					content: that.content
				}
			}).then(res => {
				vk.toast('提交成功', 'success');
				let currentTime = Date.parse(new Date()) / 1000; // 时间戳用来计算
				vk.vuex.set('$user.history.lastSuggestTime', currentTime);

				let newItem = {
					content: that.content,
					_add_time: currentTime
				};
				that.dataList.unshift(newItem);

				that.content = '';
			});
		},
		showDetailPopup(index) {
			that.currentItemIndex = index;
			that.detailPopupShow = true;
		}
	}
};
</script>
<style lang="scss">
.popup-body {
	font-size: $u-p1 !important;

	.content {
		padding-bottom: 20rpx;
	}

	.comment {
		border-top: 1rpx solid $u-border-color;
		padding-top: 20rpx;
	}
}
</style>
