<template>
	<view style="background: #F1F2F8;min-height: 100vh;" v-cloak>
		<tn-nav-bar :isBack="false" backTitle="" :bottomShadow="true" backgroundColor="#FFFFFF">
			<view class="custom-nav tn-flex tn-flex-col-center tn-flex-row-left">
				<view style="padding-left: 15rpx;" @click="goBack()">
					<text class="tn-icon-left" style="font-size: 40rpx;"></text>
				</view>
				<view style="width: 83%;font-weight: bold;">
					<text>发布动态</text>
				</view>
			</view>
		</tn-nav-bar>
		<view :style="{paddingTop: vuex_custom_bar_height + 'px'}">
			<view style="padding: 30rpx;">
				<view style="background-color: #ffffff;border-radius: 20rpx;">
					<view style="padding: 20rpx;">
						<view style="width: 100%;">
							<tn-input :clearable="false"
								placeholderStyle="font-size: 34rpx;font-weight: bold;color:#BABDC7"
								:customStyle="{width:'650rpx',padding:'20rpx'}" :height="100"
								placeholder="填写吸引人的标题,更容易收获赞哦" v-model="title" />
						</view>
						<view style="border-top: #BABDC7 1rpx dashed;padding-top: 20rpx;margin-top: 20rpx;">
							<tn-input :clearable="false" :customStyle="{padding:'20rpx'}" :height="200" type="textarea"
								placeholder="在这里发布内容吧..." v-model="content" />
						</view>
						<!--						<view style="padding: 20rpx;">-->
						<!--							<text style="color: #666666;font-size: 28rpx;font-weight: 500;">活动分类</text>-->
						<!--							<text style="color: #BABDC7;" class="tn-icon-down-triangle"></text>-->
						<!--						</view>-->
						<view style="margin-top: 20rpx;">
							<view>
								<view class="tn-flex" style=" flex-wrap: wrap;" slot="addBtn"
									hover-class="tn-hover-class" hover-stay-time="150">
									<view v-for="(item,index) in images" style="margin: 5rpx;position: relative">
										<image :src="apiImgUrl+item"
											style="width: 200rpx;height: 200rpx;border-radius: 20rpx;" mode="aspectFit">
										</image>
										<view style="position:absolute;right: -5rpx;top: -5rpx;"
											@click="del_img(index)">
											<text class="tn-icon-delete-fill"
												style="color: red;font-size: 40rpx"></text>
										</view>
									</view>
									<view v-if="images.length<9" @click="upload_img"
										style="margin: 5rpx 5rpx;background: #F1F2F8;border-radius: 20rpx;width: 200rpx;height: 200rpx;text-align: center;line-height: 200rpx;">
										<text style="color: #BABDC7;font-size: 100rpx;" class="tn-icon-add"></text>
									</view>
								</view>
							</view>
							<view style="font-size: 14px;padding: 20rpx 0rpx;color: #999999;">
								发布动态使用：{{info.consumption_poin}}积分</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view
			style="position: fixed;bottom:0px;width: 100%;background-color: #FFFFFF;box-shadow: 0rpx -2rpx 4rpx 0rpx rgba(186,189,199,0.2);padding:30rpx 30rpx 40rpx 30rpx;">
			<view class="tn-flex tn-flex-center tn-flex-col-center tn-flex-row-center">
				<view
					style="line-height: 90rpx;height: 90rpx;text-align: center;width: 40%;;border-radius: 90rpx 0rpx 0rpx 90rpx;background: #FFFFFF;border: 1rpx solid #BABDC7;">
					<text style="margin-left: 10rpx;color: #999999;">取消</text>
				</view>
				<view @click="addSubmit()"
					style="line-height: 90rpx;height: 90rpx;font-size: 32rpx;font-weight: bold;text-align: center;width: 80%;color: #ffffff;;background: #3056D3;border: 1rpx solid #3056D3;border-radius: 0rpx 90rpx 90rpx 0rpx;">
					<text>发布</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		tweetsPostAdd,
		pointsRuleFind
	} from '@/util/api.js';
	import store from '@/store/index.js'
	export default {
		data() {
			return {
				title: '',
				content: '',
				images: [],
				apiImgUrl: this.$store.state.imgUrl,
				apiUpUrl: this.$store.state.apiUrl,
				info: {}
			}
		},
		onLoad() {
			this.getInfo();
		},
		methods: {
			getInfo() {
				pointsRuleFind({
						id: 1,
					})
					.then(res => {
						console.log(res);
						this.info = res.data;
					})
					.catch(error => {
						uni.showToast({
							title: error,
							duration: 2000
						});
					})
			},
			addSubmit() {
				tweetsPostAdd({
						association_id: 1,
						title: this.title,
						content: this.content,
						images: this.images
					})
					.then(res => {
						console.log(res);
						if (res.code == 1) {
							uni.showModal({
								title: '提示',
								content: '发布成功,审核通过后显示！',
								success: function(res) {
									uni.navigateBack()
								}
							});
						} else {
							uni.showModal({
								title: '提示',
								content: res.msg,
								success: function(res) {}
							});
						}
					})
					.catch(error => {
						uni.showToast({
							title: error,
							duration: 2000
						});
					})
			},
			del_img(index) {
				this.images.splice(index, 1);
			},
			upload_img() {
				var user_info = uni.getStorageSync('userInfo');
				var that = this;
				uni.chooseMedia({
					count: 9, //默认9
					mediaType: ['image'],
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: function(res) {
						console.log(res);
						var url = res.tempFiles[0].tempFilePath;
						uni.uploadFile({
							url: that.apiUpUrl + '/common/upload', //仅为示例，非真实的接口地址
							filePath: url,
							name: 'file',
							formData: {
								association_id: 1,
							},
							success: (uploadFileRes) => {
								var data = JSON.parse(uploadFileRes.data);
								console.log(data);
								that.images.push(data.data.url);
							}
						});
					}
				});
			},
			goBack() {
				if (getCurrentPages().length > 1) {
					uni.navigateBack()
				} else {
					uni.redirectTo({
						url: '/pages/index/index'
					})

				}
			}
		}
	}
</script>

<style>

</style>