<template>
	<view class="publishPost">
		<navbar 
		leftClick
		@leftClick="$utils.navigateBack"
		title="发布动态"/>
		
		
		
		<!-- <view class="title-input box">
			<input type="text" placeholder="取个吸引人的标题吧" v-model="form.title"/>
		</view> -->
		
		
		<view class="content-input">
			<uv-textarea 
			v-model="form.title" 
			:maxlength="200" 
			autoHeight
			count
			style="min-height: 400rpx;"
			placeholder="说点什么吧"></uv-textarea>
		</view>
		
		
		<view class="images box">
			<uv-upload 
			:fileList="fileList" 
			:maxCount="imageMax" 
			multiple 
			width="150rpx" 
			height="150rpx"
			@delete="deleteImage"
			@afterRead="afterRead"
			:previewFullImage="true"></uv-upload>
		</view>
		
		<view class="category">
			<view class="title">
				选择分类
			</view>
			<view class="tagList">
				<view 
				:class="{act : t.id == form.classId}"
				@click="clickCategory(t, i)"
				v-for="(t, i) in category"
				:key="i">
					{{ t.title }}
				</view>
			</view>
		</view>
		
		<!-- <view class="category">
			<view class="title">
				选择地区
			</view>
			<view class="tagList">
				<view 
				:class="{act : t.name == form.address}"
				@click="clickAddress(t, i)"
				v-for="(t, i) in cityList"
				:key="i">
					{{ t.name }}
				</view>
			</view>
		</view> -->
		
		<view class="title-input box">
			<input type="text" placeholder="手机号码（选填）" v-model="form.phone"/>
		</view>
		
		<view class="title-input box"
		@click="selectAddr">
			<input type="text"
			placeholder="选择位置（选填）" 
			v-model="form.address"/>
			<uv-icon
			size="40rpx"
			name="map"></uv-icon>
		</view>
		
		<!-- <view class="upTop"
		>
			<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>
		
		<view class="uni-color-btn"
		@click="submit">
			发布
		</view>
		

		<configPopup
		ref="configPopup"
		/>
		
	</view>
</template>

<script>
	import { mapState } from 'vuex'
	import Position from '@/utils/position.js'
	export default {
		components : {
		},
		data() {
			return {
				form : {
					title : '',
					classId : 0,
					address : '',
					phone : '',
				},
				id : 0,
				fileList : [],
				imageMax : 9,
			};
		},
		computed : {
			...mapState(['cityList', 'category']),
		},
		onLoad(args) {
			this.id = args.id
			this.form.classId = this.category[0].id
			// this.form.address = this.cityList[0].name
			this.imageMax = args.imageMax || 9
			this.getDateil()
		},
		onShow() {
			this.$store.commit('getCategory')
		},
		methods : {
			clickCategory(item, index){
				this.form.classId = item.id
			},
			clickAddress(item, index){
				this.form.address = item.name
			},
			getDateil(){
				if(!this.id){
					return
				}
				
				let self = this
				
				this.$api('getPostDetail', {
					id : this.id
				}, res => {
					if (res.code == 200) {
						self.form.id = res.result.id
						self.form.title = res.result.title
						self.form.address = res.result.address || self.form.address
						self.form.classId = res.result.classId || self.form.classId
						self.form.latitude = res.result.latitude || self.form.latitude
						self.form.longitude = res.result.longitude || self.form.longitude
						
						if(res.result.image){
							res.result.image.split(',')
							.forEach(url => {
								self.fileList.push({
									url
								})
							})
						}
					}
				})
			},
			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
						})
					})
				})
			},
			
			// 提交
			submit(){
				
				// if(this.fileList.length == 0){
				// 	return uni.showToast({
				// 		title: '请上传图片',
				// 		icon : 'none'
				// 	})
				// }
				
				if (this.$utils.verificationAll(this.form, {
						title: '说点什么吧',
					})) {
					return
				}
				
				this.form.image = this.fileList.map((item) => item.url).join(",")
				
				this.$api('publishPost', this.form, res => {
					if(res.code == 200){
						uni.showToast({
							title: '发布成功!',
							icon: 'none'
						})
						
						setTimeout(uni.navigateBack, 1000, -1)
					}
				})
			},
			//地图上选择地址
			selectAddr() {
				Position.selectAddress(success => {
					this.setAddress(success)
				})
			},
			
			//提取用户选择的地址信息复制给表单数据
			setAddress(res) {
				
				//经纬度信息
				this.form.latitude = res.latitude
				this.form.longitude = res.longitude
			
				if (res.name) { //用户直接选择城市的逻辑
				// if (!res.address && res.name) { //用户直接选择城市的逻辑
					return this.form.address = res.name
				}
				// if (res.address || res.name) {
				// 	return this.form.address = res.address + res.name
				// }
				this.form.address = '' //用户啥都没选就点击勾选
				this.form.latitude = ''
				this.form.longitude = ''
			},
			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{
		margin: 10rpx;
		border-bottom: 1px solid #00000015;
		padding-bottom: 25rpx;
		margin-bottom: 15rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		input{
			width: 100%;
		}
	}
	.content-input{
		min-height: 400rpx;
		/deep/ .uv-textarea{
			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;
		}
	}
}
.category{
	padding: 20rpx;
	.title{
		// font-weight: 900;
		// font-size: 30rpx;
	}
	.tagList{
		display: flex;
		flex-wrap: wrap;
		padding: 10rpx 0;
		view{
			background: rgba($uni-color, 0.1);
			padding: 10rpx 20rpx;
			margin: 10rpx;
			border-radius: 10rpx;
			font-size: 26rpx;
		}
		.act{
			color: #fff;
			background: $uni-color;
		}
	}
}
</style>
