<template>
	<view class="con">
		<view class="search ddflex">
			<image src="../../static/images/ssico.png" class="ssico"></image>
			<input v-model="cTitle" placeholder="自定义话题" @input="inputTopicText" placeholder-class="placeholder" class="ipt fflex" />
			<!-- <view class="clear">取消</view> -->
		</view>
		<view class="new" v-if="cTitle">
			<view class="tip">没有找到想要参与的话题？</view>
			<view class="create" @click="getSaveTopic()">
				创建新话题：
				<text>{{ cTitle }}</text>
			</view>
		</view>
		<view class="list">
			<view class="li ddflex" @click="selectedBack('')">
				<image src="../static/images/ht_no.png"></image>
				<view class="fflex tover">不参与任何话题</view>
			</view>
			<view class="li ddflex" v-for="(item, index) in topicList" :key="index" @click="selectedBack(item)">
				<image src="../static/images/ht_ico.png"></image>
				<view class="fflex tover">{{ item.title }}</view>
			</view>
		</view>
	</view>
</template>

<script>
const app = getApp();
const api = require('../../utils/api.js');
const req = require('../../utils/request.js');
export default {
	data() {
		return {
			isLoad: true,
			form: {
				// page: 1,
				// limit: 10
			},
			topicList: [],
			cTitle: '' //输入的话题关键词
		};
	},
	onLoad() {
		this.getListTopic();
	},
	onReachBottom() {
		this.form.page++;
		this.getListTopic();
	},
	methods: {
		// 监听获取话题输入
		inputTopicText(e) {
			this.isLoad = true;
			this.form.page = 1;
			this.getListTopic();
		},

		selectedBack(obj) {
			let pages = getCurrentPages(); //获取所有页面栈实例列表
			let prevPage = pages[pages.length - 2]; //上一页页面实例
			prevPage.$vm.htObj = obj; //修改上一页data里面的searchVal参数值为1211
			uni.navigateBack({
				//uni.navigateTo跳转的返回，默认1为返回上一级
				delta: 1
			});
		},

		getListTopic() {
			if (!this.isLoad) return false;
			this.isLoad = false;
			let that = this;
			if (this.cTitle) {
				this.form.dialogue = this.cTitle;
			} else {
				this.form.dialogue = '';
			}
			this.form.poiLongitude = req.getStorage('loctionAddressMap').location.lng;
			this.form.poiLatitude = req.getStorage('loctionAddressMap').location.lat;
			// this.form.orderType=1;
			// '/api/v3/dialogue/list'
			req.getRequest(api.get_nearby_dialogue, this.form, data => {
				if (data && data.length >= this.form.limit) {
					this.isLoad = true;
				}
				if (that.form.page > 1) {
					data = that.topicList.concat(data);
				}
				that.topicList = data;
			});
		},

		getSaveTopic() {
			var dataP = {
				title: this.cTitle
			};
			req.postRequest('/api/v3/dialogue/save', dataP, data => {
				this.selectedBack(data);
			});
		}
	}
};
</script>

<style>
@import './topic.css';
</style>
