<template>
	<view class="publishPost">
		<navbar 
		leftClick
		@leftClick="$utils.navigateBack"
		title="发布帖子"/>
		
		<view class="images box">
			<uv-upload 
			:fileList="fileList" 
			:maxCount="4" 
			multiple 
			width="150rpx" 
			height="150rpx"
			@delete="deleteImage"
			@afterRead="afterRead"
			:previewFullImage="true"></uv-upload>
		</view>
		
		<view class="title-input box">
			<input type="text" placeholder="添加标题" v-model="form.title"/>
		</view>
		
		<view class="content-input">
			<uv-textarea 
			v-model="form.content" 
			:maxlength="200" 
			autoHeight
			count
			placeholder="添加正文"></uv-textarea>
		</view>
		
		<view class="upTop"
		v-if="isVedio">
			<view class="title">
				<uv-icon name="pushpin-fill"></uv-icon>
				是否置顶
			</view>
			<uv-radio-group v-model="form.topId">
				<view class="list">
					
					<view class="item">
						<view class="left">
							不需要置顶
						</view>
						<view class="right">
							<uv-radio
							size="35rpx"
							icon-size="35rpx"
							:disabled="!!id"
							:name="0">
							</uv-radio>
						</view>
					</view>
					
					<view class="item"
					v-for="(item, index) in upTopList"
					:key="index">
						<view class="left">
							置顶{{ item.day }}天{{ item.money }}元
						</view>
						<view class="right">
							<uv-radio
							size="35rpx"
							:disabled="!!id"
							icon-size="35rpx"
							:name="item.id">
							</uv-radio>
						</view>
					</view>
				</view>
			</uv-radio-group>
		</view>
		
		<view class="configBtn"
		@click="$refs.configPopup.open('getPublishPostNotice')">
			《发布须知》
		</view>
		
		<submit
		@submit="submit"
		@preview="preview"
		@draft="draft"
		:submitTitle="id ? '修改帖子' : '发布帖子'"
		/>
		
		<confirmationPopup
		ref="confirmationPopup"
		title="提示"
		@confirm="payOrder"
		confirmText="确认支付">
			<view class="confirmationPopup">
				<image src="/static/image/publish/pay.png" 
				style="width: 150rpx;height: 150rpx;"
				mode=""></image>
				<view class="info">
					确认支付{{ topInfo.money }}元，可置顶{{ topInfo.day }}天
				</view>
			</view>
		</confirmationPopup>
		
		<confirmationPopup
		ref="confirmationPopupUpload"
		title="提示"
		@confirm="$utils.navigateBack"
		confirmText="确认">
			<view class="confirmationPopup">
				<image src="/static/image/publish/upload.png" 
				style="width: 150rpx;height: 150rpx;"
				mode=""></image>
				<view class="info">
					已由平台进行审核，时间周期为24小时
				</view>
			</view>
		</confirmationPopup>
		
		<configPopup
		ref="configPopup"
		/>
		
	</view>
</template>

<script>
	import submit from '@/components/content/submit.vue'
	import confirmationPopup from '@/components/toast/confirmationPopup.vue'
	import { mapGetters } from 'vuex'
	export default {
		components : {
			submit,
			confirmationPopup
		},
		data() {
			return {
				upTopList : [],
				form : {
					// image : [],
					content : '',
					topId : 0,
					title : '',
				},
				fileList: [
					// {
					// 	url: 'https://cdn.uviewui.com/uview/swiper/2.jpg'
					// },
				],
				id : 0,
			};
		},
		computed : {
			topInfo(){
				for (var i = 0; i < this.upTopList.length; i++) {
					if(this.upTopList[i].id == this.form.topId){
						return this.upTopList[i]
					}
				}
				return {}
			},
			...mapGetters(['isVedio']),
		},
		onLoad(args) {
			this.id = args.id
		},
		onShow() {
			this.indexTopPayList()
			this.getDateil()
		},
		methods : {
			getDateil(){
				if(!this.id){
					return
				}
				
				let self = this
				
				this.$api('indexGetTrendsDetail', {
					id : this.id
				}, res => {
					if (res.code == 200) {
						
						res.result.details.image.split(',')
						.forEach(url => {
							self.fileList.push({
								url
							})
						})
						
						res.result.details.topId = res.result.details.topId || 0
						
						this.form = res.result.details
					}
				})
			},
			deleteImage(e){
				this.fileList.splice(e.index, 1)
			},
			afterRead(e){
				let self = this
				e.file.forEach(file => {
					self.$Oss.ossUpload(file.url).then(url => {
						self.fileList.push({
							url
						})
					})
				})
			},
			indexTopPayList(){
				this.$api('indexTopPayList', res => {
					if(res.code == 200){
						this.upTopList = res.result
					}
				})
			},
			
			// 发起支付
			payOrder(){
				let self = this
				
				let data = {
					// ...this.form,
					content : this.form.content,
					topId : this.form.topId,
					title : this.form.title,
					isCard : 'Y',
					state : 0,
					image : this.fileList.map((item) => item.url).join(","),
					isTop : this.form.topId ? 'Y' : 'N',
				}
				
				if(this.id){
					data.id = this.id
				}
				
				this.$api('infoReleaseTrends', data,
				res => {
					if(res.code == 200){
						if(self.form.topId && !this.id){
							uni.requestPayment({
								provider: 'wxpay', // 服务提提供商
								timeStamp: res.result.timeStamp, // 时间戳
								nonceStr: res.result.nonceStr, // 随机字符串
								package: res.result.packageValue,
								signType: res.result.signType, // 签名算法
								paySign: res.result.paySign, // 签名
								success: function (res) {
									console.log('支付成功',res);
									self.$refs.confirmationPopupUpload.open()
									// self.$refs.confirmationPopup.close()
								},
								fail: function (err) {
									console.log('支付失败',err);
									self.$refs.confirmationPopup.close()
									uni.showToast({
										icon:'none',
										title:"支付失败"
									})
								}
							});
						}else{
							self.$refs.confirmationPopupUpload.open()
						}
						
					}
				})
				
			},
			
			// 提交
			submit(){
				
				if(this.fileList.length == 0){
					return uni.showToast({
						title: '请上传图片',
						icon : 'none'
					})
				}
				
				if (this.$utils.verificationAll(this.form, {
						title: '请输入标题',
						content: '请输入正文',
					})) {
					return
				}
				
				if(this.form.topId && !this.id){
					// 选择付费模式
					this.$refs.confirmationPopup.open()
				}else{
					// 没有选择付费
					this.payOrder()
				}
			},
			preview(){},
			draft(){},
		}
	}
</script>

<style lang="scss" scoped>
.publishPost{
	background-color: #fff;
	min-height: 100vh;
	font-size: 28rpx;
	padding-bottom: 150rpx;
	/deep/ .uv-textarea{
		background-color: transparent;
		border: none;
	}
	/deep/ .uv-textarea__count{
		background-color: transparent !important;
	}
	.box{
		padding: 0 20rpx;
	}
	.images{
		display: flex;
		flex-wrap: wrap;
		padding: 20rpx;
	}
	.title-input{
		border-bottom: 1px solid #00000015;
		padding-bottom: 25rpx;
		margin-bottom: 15rpx;
	}
	.content-input{
		min-height: 400rpx;
	}
	.upTop{
		.title{
			padding-top: 20rpx;
			padding-left: 20rpx;
			border-top: 1px solid #00000015;
			display: flex;
			align-items: center;
		}
		.list{
			padding-top: 30rpx;
			width: 100%;
			.item{
				display: flex;
				padding: 20rpx;
				padding-left: 80rpx;
				justify-content: space-between;
				width: 600rpx;
				border-bottom: 1px solid #00000015;
				align-items: center;
			}
		}
	}
	
	.configBtn{
		padding: 20rpx;
		color: #777;
		padding-top: 40rpx;
		font-size: 28rpx;
	}
	
	.confirmationPopup{
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		width: 100%;
		height: 300rpx;
		image{
			margin-top: 40rpx;
		}
		.info{
			margin-top: 40rpx;
			font-size: 26rpx;
		}
	}
}
</style>
