<template>
	<view class="detail-page-container">
		<view class="topic-list-box" v-if="topicList.length > 0">
			<view class="topic-box-common" v-for="(item,index) in topicList" @click="handleDeleteTopic(index)">
				# {{item.name}}
			</view>
		</view>
		<view class="publish-box">
			<textarea class="textarea" v-model="form.content" placeholder="分享有趣事~"
				placeholder-class="placeholder-class" />
			<image-upload v-model="form.attachments" :limit="9" :showIcons="true" @componentsType="handleSetComponentsType" @handleTopic="handleOpenTopicSelection"></image-upload>
		</view>
		<view class="permission-box">
			<view class="item">
				<view class="text">允许评论</view>
				<switch color="#00E165"  @change="(val)=>handleChange(val,'isEnableComment')" :checked="form.isEnableComment==1" />
			</view>
			<view class="item">
				<view class="text">开启评论精选</view>
				<switch color="#00E165" @change="(val)=>handleChange(val,'isEnableFeaturedComment')" :checked="form.isEnableFeaturedComment==1"/>
			</view>
			<view class="item">
				<view class="text">开启PK</view>
				<switch color="#00E165" @change="(val)=>handleChange(val,'isEnableVote')" :checked="form.isEnableVote==1"/>
			</view>
		</view>

		<!-- pk数据编辑 -->
		<view class="pk-edit-box" v-if="form.isEnableVote==1">
			<view class="textarea-box">
				<textarea class="textarea" v-model="form.voteTitle" placeholder="请输入PK标题~（0/12）" placeholder-class="placeholder-class" />
			</view>
			<view class="pk-type-box">
				<radio-group v-model="form.voteOptionType" @change="radioChange">
					<label class="radio" style="margin-right: 40rpx;">
						<radio value="IMAGE" :checked="form.voteOptionType=='IMAGE'" />图片PK
					</label>
					<label class="radio">
						<radio value="TEXT" :checked="form.voteOptionType=='TEXT'" />文字PK
					</label>
				</radio-group>

				<view class="choice-box" v-if="form.voteOptionType=='IMAGE'">
					<view class="choice-item">
						<image-upload v-model="voteOptionsAffirmative" :limit="1" :width="120" :height="120" :showIcons="false"></image-upload>
						<input class="text" v-model="form.voteOptions[0].name" placeholder="请输入选项1（4字以内）" />
					</view>
					<view class="choice-item">
						<image-upload v-model="voteOptionsOpposing" :limit="1" :width="120" :height="120" :showIcons="false"></image-upload>
						<input class="text" v-model="form.voteOptions[1].name" placeholder="请输入选项2（4字以内）" />
					</view>
				</view>
				<view class="choice-box" v-if="form.voteOptionType=='TEXT'">
					<view class="choice-item text-item">
						<view class="label">选项1</view>
						<input class="text" v-model="form.voteOptions[0].name" placeholder="请输入选项1（4字以内）" />
					</view>
					<view class="choice-item text-item">
						<view class="label">选项2</view>
						<input class="text" v-model="form.voteOptions[1].name" placeholder="请输入选项2（4字以内）" />
					</view>
				</view>
			</view>
		</view>


		<!-- 提交按钮 -->
		<view class="submit-btn" @click="handleSubmit">确定</view>

		<!-- 选择话题弹窗 -->
		<view class="topic-selection-shadow" v-if="showTopicSelection">
			<view class="topic-selection-container">
				<view class="title-box">
					<view class="label">选择话题</view>
					<image src="/static/images/close-icon.png" class="close-icon" @click="handleCloseTopicSelection">
					</image>
				</view>
				<view class="topic-search-box">
					<view class="top-tag">#</view>
					<input v-model="topicParams.name" placeholder="请输入话题名称" class="input"
						placeholder-class="input-class" @keypress.enter="handleSearchTopic" />
				</view>
				<view class="topic-result-list">
					<template v-if="topicResultList&&topicResultList.length > 0">
						<view class="list-item" v-for="(item,index) in topicResultList" :key="index"
							@click="handleAddTopic(item)">
							<view class="circle-tag">#</view>
							<view class="result-text">{{item.name}}</view>
						</view>
					</template>
					<view class="list-item-none"
						v-if="topicResultList&&topicResultList.length == 0 && topicParams.name">
						<view class="list-item">
							<view class="circle-tag">#</view>
							<view class="result-text">{{topicParams.name}}</view>
						</view>
						<view class="create-text" @click="handleCreateTopic">创建此话题</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getTopicSelection,
		createNewTopic,
		crateNewDynamic
	} from '@/api/api.js';
	import ImageUpload from '@/components/ImageUpload.vue'
	export default {
		data() {
			return {
				type: 'r1',
				showTopicSelection: false,
				topicParams: {
					pageSize: 10,
					pageNum: 1,
					name: ''
				},
				topicResultList: undefined,
				topicList: [],
				form: {
					content: '',
					type: 'TEXT',
					topicNames: [],
					topicIds: [],
					linkUrl: '',
					attachmentUrls: [],
					isEnableComment: 1,
					isEnableFeaturedComment: 0,
					isEnableVote: 0,
					voteTitle: '',
					voteOptionType: 'TEXT',
					voteOptions: [{
						name:'',
						imageUrl:''
					},{
						name:'',
						imageUrl:''
					}],
				},
				voteOptionsAffirmative:[],
				voteOptionsOpposing:[]
			}
		},
		components: {
			ImageUpload
		},
		onLoad(options) {
			let {topicId,topicName} = options;
			if(topicId){
				this.topicList.push({name:topicName,id:topicId})
			}
		},
		methods: {
			// 允许评论，开启精选等操作
			handleChange(e,field){
				console.log(e,field)
				this.form[field] = e.detail.value?1:0
			},
			// 图文PK或者文字PK
			radioChange(e) {
				console.log(e)
				this.form.voteOptionType = e.detail.value
			},
			handleSetComponentsType(type){
				if(type && this.form.attachments.length > 0){
					this.form.type = type
				}else{
					this.form.type = 'TEXT'
				}
			},
			// 发布动态
			handleSubmit(){
				if(this.topicList.length > 0){
					this.form.topicNames = [];
					this.form.topicIds = [];
					this.topicList.forEach((item)=>{
						this.form.topicNames.push(item.name);
						this.form.topicIds.push(item.id)
					})
				}
				if(this.form.voteOptionType=='IMAGE'){
					if(this.voteOptionsAffirmative.length == 0){
						uni.showToast({
							title:'请上传PK图片'
						})
						return;
					}
					if(this.voteOptionsOpposing.length == 0){
						uni.showToast({
							title:'请上传PK图片'
						})
						return;
					}
				}
				
				if(this.form.voteOptionType == 'IMAGE'){
					this.$set(this.form.voteOptions[0], 'imageUrl', this.voteOptionsAffirmative[0].url);
					this.$set(this.form.voteOptions[1], 'imageUrl', this.voteOptionsOpposing[0].url);
				}
				console.log(this.form)
				crateNewDynamic(this.form).then(res=>{
					if(res.code == 200){
						uni.showToast({
							title:'创建成功',
						})
						uni.$emit('hanldeDynamicRefresh')
						uni.switchTab({
							url:'/pages/index/index'
						})
					}
				})
			},
			// 选择话题弹窗
			handleOpenTopicSelection() {
				this.showTopicSelection = true
			},
			// 关闭弹窗
			handleCloseTopicSelection() {
				this.showTopicSelection = false,
					this.topicResultList = undefined
			},
			// 搜索话题
			handleSearchTopic() {
				getTopicSelection(this.topicParams).then(res => {
					console.log(res)
					this.topicResultList = res.rows
				})
			},
			// 创建话题
			handleCreateTopic() {
				let data = {
					name: this.topicParams.name
				}
				createNewTopic(data).then(res => {
					if (res.code == 200) {
						this.handleAddTopic(res.data);
					} else {
						uni.showToast({
							title: res.msg || '话题创建失败',
							icon: 'none'
						})
					}
				})
			},
			// 添加话题
			handleAddTopic(item) {
				this.topicList.push(item);
				// 关闭弹窗
				this.handleCloseTopicSelection();
			},
			// 删除话题
			handleDeleteTopic(index) {
				uni.showModal({
					title: '提示',
					content: '是否取消选择话题',
					success: function(res) {
						if (res.confirm) {
							this.topicList.splice(index, 1)
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});

			}
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/static/styles/publish.scss';

	.topic-list-box {
		display: flex;
		min-height: 120rpx;
		background-color: white;
		padding: 34rpx 34rpx 0 34rpx;
		box-sizing: border-box;
		flex-wrap: wrap;

	}
</style>