<template name="publish">
	<view class="cu-modal show">
		<view class="cu-dialog">
			<view class="cu-bar bg-white justify-end">
				<view class="content">{{title}}</view>
				<view class="action" @tap="hideModal">
					<text class="cuIcon-close text-red"></text>
				</view>
			</view>
			<view class="padding-xls">
				<view class="publish-wrap bg-white">
					<view class="input-box padding-sm">
						<textarea class="area-top" placeholder="写点啥..." maxlength="10000" v-model="content"></textarea>
					</view>
					<view class="cu-form-group img-box" v-if="is_upload">
						<view class="next-title">上传图片（最多{{total}}张）</view>
						<view class="margin-none margin-left">
							<view class="grid col-5 grid-square flex-sub padding-top-lg">
								<view class="bg-img" v-for="(item,index) in pic" :key="index">
									<image :src="item" mode="aspectFill" @tap="Dever.viewPic(pic, item)" class="img-item"></image>
									<image src="@/static/img/publish/img_close.png" @tap.stop="del" :data-index="index" mode="widthFix" class="close-img"></image>
								</view>
								<view class="add-img" @tap="choose" v-if="pic.length<total">
									<text class='cuIcon-add link-color'></text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="cu-bar bg-white">
				<view class="action margin-0 flex-sub text-green" @tap="hideModal" v-if="is_upload" style="display: none;">
					<text class="cuIcon-moneybag"></text>藏点钱
				</view>
				
				<view class="action margin-0 flex-sub text-green solid-left" @tap="save">发布</view>
				
				<view class="action margin-0 flex-sub solid-left" @tap="hideModal">取消</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name : 'publish',
		props: {
			title : '',
			cate_id : null,
			type : null,
			type_id : 0,
			to_id : 0,
			to_uid : 0,
			index : 0,
			is_upload : true,
			seconds : 0,
			api : {
				type : String,
				default : 'app/community/?l=api.add'
			},
		},
		data() {
			return {
				pic : [],
				content : '',
				upload : 'img',
				total : 9,
			};
		},
		mounted() {
			// 初始化上传数据
			this.Dever.getUpload().data = {};
			this.Dever.getUpload().data[this.upload] = [];
			this.pic = [];
		},
		methods: {
			// 保存数据
			save : function() {
				var self = this;
				var data = {};
				data.content = this.content;
				if (this.pic.length > 0) {
					data.pic = this.pic.join(',');
				}
				if (this.cate_id) {
					data.cate_id = this.cate_id;
				}
				if (this.seconds) {
					data.seconds = this.seconds;
				}
				data.type = this.type;
				data.type_id = this.type_id;
				data.to_id = this.to_id;
				data.to_uid = this.to_uid;
				if (this.Dever.config.code) {
					data.code = this.Dever.config.code;
				}
				this.Dever.post(this.api, data, function(t) {
					self.getRefresh(data.content);
				});
			},
			// 选择图片
			choose: function() {
				var self = this;
				var upload = this.Dever.upload(this.upload, this.total, function(type, img) {
					self.pic = img;
				});
			},
			// 删除图片
			del : function(e) {
				var self = this;
				self.pic.splice(e.currentTarget.dataset.index, 1)
			},
			
			hideModal : function() {
				this.$emit('hideModal');
			},
			
			getRefresh : function(content) {
				this.$emit('getRefresh', this.cate_id, this.type, this.type_id, content);
			}
		}
	}
</script>

<style lang="less" scoped>
	.textarea-placeholder {
		text-align: left;;
	}
	.grid.grid-square {
		overflow: visible;
	}
	.margin-left {
		width: 100%;
		font-size: 13px;
		line-height: 21px;
		margin-left: -60rpx;
	}
	.publish-wrap {
		.input-box {
			.area-top {
				width: 100%;
				box-shadow: inset 0 -1px 0 0 #ECECEC;
				min-height: 280rpx;
				text-align: left;
			}
		}

		.img-box {
			padding: 30rpx;
			display: flex;
			flex-direction: column;
			margin-top: 20rpx;
			margin-bottom: 160rpx;

			.next-title {
				width: 100%;
				font-size: 36rpx;
				color: var(--mainColor);
			}

			.font-red {
				color: #ff0000;
			}

			.img-wrap {
				width: 100%;
				font-size: 28rpx;
				line-height: 44rpx;
				color: #999999;
				margin: 20rpx 0;
			}

			.add-img {
				left: 60rpx;
				border: 4rpx dashed #999;
				font-size: 40rpx;
			}
		}

		.bottom-btn {
			position: fixed;
			bottom: 0;
			width: 100%;
			padding: 20rpx 0;

			.btn {
				border-radius: 40rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #FFFFFF;
				border: 1rpx solid var(--activeColor);
				background-color: var(--activeColor);
				box-shadow: 0 1px 10px 0px var(--activeColor);
				width: 400rpx;
				height: 80rpx;
			}
		}
	}

	.bg-img {
		overflow: visible !important;

		.img-item {
			border-radius: 20rpx;
		}

		.close-img {
			position: absolute;
			width: 36rpx !important;
			top: -18rpx;
			right: -18rpx;
		}
	}
</style>
