<template>
	<!--添加文章-->
	<view style="background-color: #fff;">
		<u-toast ref="messager"></u-toast>
		<!-- <page-title :showBack="true">提问题</page-title> -->
		<view style="background-color: #fff;padding-top: 18rpx;">
			<view class="bar-box">
				<view class="bar-cancel"  @click="toBack()">取消</view>
				<!-- <view class="bar-center">提问题</view> -->
				<view @click="goSubmit()" class="bar-go">发布</view>
			</view>
			<view class="content" >
				<view class="big-box-content">
					<view class="row row-lin">
						<u--input placeholder="请输入标题" placeholder-style="font-size:36upx;color:#c1c1c1;font-weight: normal;" maxlength="30" class="row-input" border="surround" v-model="qa.title" @change="change"></u--input>
					</view>
					<view class="row" style="min-height: 300upx !important;">
						<!-- <u--textarea class="row-input row-text" placeholder-style="font-size:42upx;color:#c1c1c1;font-weight: normal;" :height="300" count maxlength="500" v-model="qa.content" placeholder="请输入正文"></u--textarea> -->
					    <editor id="editor" class="editor-content" placeholder="请输入正文"
					    	:show-img-size="true"
					    	:show-img-toolbar="true"
					    	:show-img-resize="true"
					    	@statuschange="onStatusChange"
					    	@ready="onEditorReady">
					    </editor>
					</view>
					<view v-if="qaimage" class="row">
						<u-upload uploadIcon="plus" :fileList="fileList" multiple @afterRead="afterRead" @delete="deletePic" name="coverImg" :maxCount="1">
						</u-upload>
					</view>
				</view>
			</view>
		</view>
		<view>
			<view style="display: flex;justify-content:space-around;">
			
				<view  class="Articleicon">
					<text class="editor-btn" @tap="insertImage()">
						 <image src="../../static/images/article/image.png" mode=""></image>
					</text>
				</view>
			
				<view  class="Articleicon">
					<text class="editor-btn" @tap="undo()">
						 <image src="../../static/images/icon/artleft.png" mode=""></image>
					</text>
				</view>
				<view  class="Articleicon">
					<text class="editor-btn" @tap="redo()">
						 <image src="../../static/images/icon/artright.png" mode=""></image>
					</text>
				</view>
				<view  class="Articleicon">
					<text class="editor-btn" @tap="clear()">
						 <image src="../../static/images/icon/del.png" mode=""></image>
					</text>
				</view>
			</view>
		</view>
		<!-- <view style="margin-top: 20px;">
			<view>
				<u-button type="primary" text="提交问题"></u-button>
			</view>
		</view> -->
	</view>
</template>

<script>
	import { mapGetters } from 'vuex';
	import apiQa from '@/api/modules/qa.js'
	import uploadUtil from '@/utils/upload.js'
	import apiStat from '@/api/phase2/stat.js'
	export default {
		data() {
			return {
				qaimage:false,
				editorCtx: '',
				formats: {},
				fileUrl:this.$config.fileUrl,
				qa:{
					id:'',
					title:'',
					content:'',
					images:''
				},
				fileList: [],
			}
		},
		computed:{
			  ...mapGetters(['userInfo']),
		},
		methods: {
			insertLabel(){
				let $this=this;
				this.editorCtx.getContents({
					success(data) {
						// 获取编辑器中的文本
						let _html = data.html;
						// 去掉文本中默认的标签
						_html = _html.replace('<p><br></p>','');
						// 接去掉文本中最后面的</p>标签
						_html = _html.substring(0,_html.length-4);
						// 重新拼接文本,并在末尾添加</p>标签
						let html = `${_html}<span style="color: #3a8afb;"> #标签名#</span><i style="font-style: normal;"> </i></p>`;
						// 给富文本设置内容
						$this.editorCtx.setContents({html});
					}
				})
			},
			insertDate() {
				const date = new Date()
				const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
				this.editorCtx.insertText({
					text: formatDate
				})
			},
			insertImage() {
				this.qaimage = true
			},
			//初始化富文本编辑器
			onEditorReady() {
				let $this=this;
				/** #ifdef APP-PLUS || H5 ||MP-WEIXIN */
				uni.createSelectorQuery().select('#editor').context((res) => {
					//将内容写入编辑器
					$this.editorCtx = res.context;
				}).exec();
				 /** #endif */
			},
			onStatusChange(e){
				const myFormats = e.detail;
				this.formats = myFormats;
			},
			undo(){
				this.editorCtx.undo()
			},
			redo(){
				this.editorCtx.redo()
			},
			clear() {
				this.editorCtx.clear({
					success: function(res) {
						console.log("clear success")
					}
				})
			},
			toBack(){
				uni.navigateBack();
			},
			goSubmit() {
				let $this=this;
				let images=[];
				this.fileList.forEach(file=>{
					images.push(file.path);
				})
				$this.qa.images=images.join();
				uni.showLoading({title:'提交中...'})
				this.editorCtx.getContents({
					success(res){
						$this.qa.content=res.html;
						apiQa.save($this.qa).then((rs)=>{
							setTimeout(function(){uni.hideLoading()},1000);
							if(rs.status==200){
								let event = {
								  key: "PulishQuestion",//后台的事件key  发布文章且审核通过
								  title: '发布问题',//事件的标题
								  parameters:"",//用户自定义参数 name:value,name:value
								  content: "提出了问题",//事件的内容
								  objId: rs.result.id,//关联的id
								  objType: "4",//关联的类型
								  objInfo: $this.qa.title,
								  aid: $this.userInfo.aid, //当前登录人的id
								  aname: $this.userInfo.name,//当前人的姓名
								  status: 1 ,//状态，直接写1
								  source:"h5",
								}
								apiStat.sendEvent(event);
								$this.$refs.messager.show({message:'提交成功',type:'success'});
								uni.redirectTo({
									url:'/pages/resource/qaDetail?id='+rs.result.id
								})
							}else{
								$this.$refs.messager.show({message:rs.message,type:'error'});
								setTimeout(function(){uni.hideLoading()},1000);
							}
						})
					}
				})
				
				
				
			},
			change(e) {
				//console.log('change', e);
			},

			//删除图片
			deletePic(event) {
				this.fileList.splice(event.index, 1);
			},
			//新增图片
			async afterRead(event) {
				//当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				//console.log(event.file);
				let $this=this;
				event.file.forEach(img=>{
					uploadUtil.uploadFile(img.url).then(rs=>{
						//console.log(rs);
							
						let item={
							status: 'success',
							message: '已上传',
							path:rs.result.filePath,
							url:rs.result.httpPath
						}
						
						this.fileList.push(item)
						
						uni.hideLoading();
					});
				})
				
			},
			reverse() {
				uni.navigateBack({
					url: '/pages/plus/index'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	::v-deep .content{
		height: 750upx !important;
	}
	.editor-btn{
		padding: 0upx 12upx;
		color: #656565;
		border-radius: 6upx;
		image{
			width: 35upx;
			height: 35upx;
		}
	}
	.Articleicon{
		height: 120upx;
		line-height: 120upx;
		background-color: #fff;
	}
	.sub-box{
		padding: 20rpx;
		
		.sub-success{
			.sub-title{
				font-size: 40rpx;
				font-weight: 500;
				line-height: 45rpx;
				margin-bottom: 10rpx;
				padding: 20rpx;
			}
			.sub-text{
				font-size: 30rpx;
				line-height: 30rpx;
				margin-bottom: 20rpx;
				padding: 20rpx;
			}
			.sub-bar{
				display: flex;
				justify-content: space-between;
			    padding: 20rpx;
				border-bottom: 2rpx solid #ccc;
				.bar-time{
					font-size: 26rpx;
					color: #ccc;
				}
			}
			.bar-reply{
				text-align: center;
				padding: 30rpx;
				.field-icon{
					display: block;
					width: 40rpx;
					margin: 0 auto;
				}
			}
			.bar-cor{
				height: 20rpx;
				background-color: #ebebeb;
			}
			.bar-bon{
				text-align: center;
				min-height: 600rpx;
				margin-top: 35rpx;
				.bon-icon{
					display: inline-block;
					
				}
				.bon-text{
					display: inline-block;
					margin-right: 10rpx;
				}
			}
		}
	}
	.bar-box{
		display: flex;
		margin: 18rpx;
	    justify-content: space-between;
		.bar-cancel{
			font-size: 32upx;
			color: #7F7F7F;
			font-family: Source Han Sans CN;
			line-height: 36rpx;
		}
		.bar-center{
			font-size: 36upx;
			font-weight: 600;
			color: #0D0D0D;
		}
		.bar-go{
			color: #387DF7;
			font-size: 30rpx;
			font-family: Source Han Sans CN;
			font-weight: 400;
			line-height: 36rpx;
		}
	}
	.row {
		margin-bottom: 10rpx;
		.row-input{
			border: none;
		}
		
	}
	.row-lin{
		// border-top: 1px solid #eee;
		border-bottom: 1px solid #eee;
	}

	.btn {
		padding: 0px;
	}
</style>
