<template>
	<view class="content" v-if="is_show == 1">
		<view class="form">
			<view class="form-item">
				<view class="form-label">贴子标题(分享标题)</view>
				<input type="text" v-model="title"
					style="border: 1rpx solid #EEEEEE;height: 60rpx;padding-left: 10rpx;font-size: 26rpx;"
					placeholder="请输入贴子标题" />
			</view>
			<view class="form-item">
				<view class="form-label">贴子内容</view>
				<textarea placeholder="请输入贴子内容" v-model="pjs" maxlength="500" class="form-textarea"></textarea>
				<view class="count">{{pjs.length}}/500</view>
			</view>
			<view class="form-item">
				<view class="form-label">贴子图片</view>
				<view class="image-list flex">
					<view class="image-item" v-for="(item, index) in pjImg" :key="'pj' + index">
						<image :src="item" mode="aspectFill" class="image"></image>
						<view class="delete-btn" @click.stop="scPjImg(index)">
							<image src="../../../static/icon/goodsExit.png" mode="widthFix"></image>
						</view>
					</view>
					<view class="image-item add" @click="imgsub(pjImg.length)" v-if="pjImg.length < 6">
						<image src="../../../static/image/upImg.png" mode="aspectFill"></image>
						<text class="add-text">添加图片</text>
					</view>
				</view>
				<view class="hint">最多可上传6张图片</view>
			</view>
		</view>
		<view class="submit-btn" @click="goPj">
			提交审核
		</view>
	</view>
</template>

<script>
	import {
		upload
	} from '@/api/set.js';
	import {
		addPost
	} from '@/api/index.js';
	import {
		share
	} from '@/api/wx.js'
	export default {
		
		data() {
			return {
				title: '',
				pjImg: [],
				pjs: '',
				is_show: 0
			}
		},
		onLoad() {
			this.getShow()
		},
		methods: {
			getShow() {
				share({}).then(({
					data
				}) => {
					this.is_show = data.is_show
					if (data.is_show ==1) {
						uni.setNavigationBarTitle({
							title: '发贴'
						})
					}
				});
			},
			// 返回
			navBack() {
				uni.navigateBack();
			},
			// 选择图片
			imgsub(index) {
				upload({
					filename: ''
				}).then(data => {
					this.$set(this.pjImg, index, data[0].img)
				})
			},
			// 删除图片
			scPjImg(index) {
				this.pjImg.splice(index, 1);
			},
			// 提交
			goPj() {
				if (!this.pjs.trim()) {
					uni.showToast({
						title: '请输入贴子内容',
						icon: 'none'
					});
					return;
				}
				if (!this.title) {
					uni.showToast({
						title: '请输入贴子铁标题',
						icon: 'none'
					});
					return;
				}
				addPost({
					title: this.title,
					content: this.pjs,
					images: this.pjImg.join(',')
				}).then(res => {
					uni.showToast({
						title: '发布成功，等待审核',
						icon: 'success'
					});

					setTimeout(() => {
						uni.redirectTo({
							url: '/pages/user/forum/index'
						})
					}, 1500);
				}).catch(err => {
					uni.showToast({
						title: '发布失败',
						icon: 'none'
					});
				})
				// 这里可以添加提交逻辑
			}
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
		background-color: #F5F5F5;
	}

	.content {
		min-height: 100%;
		padding-bottom: 100rpx;
	}

	.header {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 30rpx;
		height: 80rpx;
		background-color: #FFFFFF;

		.back {
			width: 40rpx;
			height: 40rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.title {
			font-size: 32rpx;
			font-weight: bold;
			color: #333333;
		}

		.right {
			width: 40rpx;
		}
	}

	.form {
		margin-top: 10rpx;
		background-color: #FFFFFF;
		padding: 30rpx;

		.form-item {
			margin-bottom: 40rpx;

			.form-label {
				font-size: 28rpx;
				font-weight: bold;
				color: #333333;
				margin-bottom: 20rpx;
			}

			.form-textarea {
				width: 100%;
				height: 200rpx;
				padding: 20rpx;
				border: 1rpx solid #EEEEEE;
				border-radius: 10rpx;
				font-size: 26rpx;
				color: #333333;
				line-height: 36rpx;
				resize: none;
			}

			.count {
				text-align: right;
				font-size: 22rpx;
				color: #999999;
				margin-top: 10rpx;
			}

			.image-list {
				flex-wrap: wrap;
				justify-content: flex-start;

				.image-item {
					width: 150rpx;
					height: 150rpx;
					margin-right: 20rpx;
					margin-bottom: 20rpx;
					position: relative;
					border-radius: 10rpx;
					background-color: #F5F5F5;

					&:nth-child(4n) {
						margin-right: 0;
					}

					&.add {
						display: flex;
						flex-direction: column;
						align-items: center;
						justify-content: center;

						image {
							width: 50rpx;
							height: 50rpx;
							margin-bottom: 10rpx;
						}

						.add-text {
							font-size: 22rpx;
							color: #999999;
						}
					}

					.image {
						width: 100%;
						height: 100%;
					}

					.delete-btn {
						position: absolute;
						top: -12rpx;
						right: -12rpx;
						width: 40rpx;
						height: 40rpx;
						background-color: #fff;
						border-radius: 50%;
						display: flex;
						align-items: center;
						justify-content: center;

						image {
							width: 24rpx;
							height: 24rpx;
						}
					}
				}
			}

			.hint {
				font-size: 22rpx;
				color: #999999;
				margin-top: 10rpx;
			}
		}
	}

	.submit-btn {
		position: fixed;
		bottom: 30rpx;
		left: 30rpx;
		right: 30rpx;
		height: 90rpx;
		background-color: #f65486;
		border-radius: 45rpx;
		font-size: 32rpx;
		font-weight: bold;
		color: #FFFFFF;
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>