<template>
	<view >
		<view class="bgtop"></view>
		<uni-forms :modelValue="datainfo">
		<view class="addbox">
			<view class="boxs pdbox" style="padding-bottom: 2rpx;">
				<view class="titbox mb14">
					<view class="tit flexc">
						<image :src="titimg"></image>
						<view>基本详情</view>
					</view>
				</view>
				<uni-forms-item label="题目" name="noticeTitle" required>
					<uni-easyinput :inputBorder="false" type="text" v-model="datainfo.noticeTitle" placeholder="请输入标题" />
				</uni-forms-item>
				<picker  range-key='label' :value="tzlbidx" :range="tzlbList"  @change='bindDateChangea'>
					<uni-forms-item label="通知类别" name="noticeType" required>
						<view class="lbtabp">
							<view v-if="datainfo.noticeType&&!tzlblabe">{{statusFormat(datainfo.noticeType)}}</view>
							<view v-else :class="tzlblabe?'':'coa'">{{tzlblabe||"请选择通知类别"}}</view>
						</view>
					</uni-forms-item>
				</picker>
				<uni-forms-item label="发布人" name="issuer" required>
					<uni-easyinput  disabled :inputBorder="false" type="text" v-model="datainfo.issuer" placeholder="请输入发布人" />
				</uni-forms-item>
				<uni-forms-item label="发布单位" name="issuerDept" required>
					<uni-easyinput disabled :inputBorder="false" type="text" v-model="datainfo.issuerDept" placeholder="请输入发布单位" />
				</uni-forms-item>
			</view>
			<view class="boxs pdbox">
				<view class="titbox mb14 flexc">
					<view class="tit flexc flex1">
						<image :src="titimg"></image>
						<view>通知内容</view>
					</view>
					<view class="titr" @click="getPaiFn">一键排版</view>
				</view>
				<editor id="editor" :class="pbFlag?'pbbox':''" class="ql-container" placeholder="请输入通知内容" show-img-size show-img-toolbar
					show-img-resize @statuschange="onStatusChange" :read-only="readOnly" @ready="onEditorReady">
				</editor>
				<view class="edtip flext mb24 mt14"><text>*</text>通知正文内容输入完毕后，可点击右上角的“一键排版”按钮进行快 速排版</view>
			</view>
			<view class="boxs pdbox">
				<view class="titbox mb14">
					<view class="tit flexc">
						<image :src="titimg"></image>
						<view>附件内容</view>
					</view>
				</view>
				<view class="flexcj mb10">
					<view class="fw f15 co34">添加附件</view>
					<view class="fjadd"><lsj-upload
						    ref="lsjUpload"
						    childId="upload1"
						    :width="width"
						    :height="height"
						    :option="option"
						    :size="size"
						    :formats="formats"
						    :debug="debug"
						    :instantly="instantly"
						    @progress=""
							@uploadEnd="onuploadEnd" >
						        <view class="btn" :style="{width: width,height: height}">上传附件</view>
						</lsj-upload>
					</view>
				</view>
				<view class="ptb12" v-if="filelist&&filelist.length">
					<view class="fjlists"  v-for="(ite,idx) in filelist" :key='idx'>
						<view class="flext" @click="getDown(ite.url)">
							<view class="imgl"><image :src="fjimg" ></image></view>
							<view class="tit">{{ite.name}}</view>
						</view>
						<!-- 删除 -->
						<view class="delimg flex0" @click.stop="getDelFj(idx)">
							<image :src="delimg"></image>
						</view>
					</view>
				</view>
			</view>
			<view class="addbtn">
				<view class="btn btn1" @click="getZanFnt">暂存</view>
				<view class="btn btn2" @click="getSubmit">提交</view>
			</view>
		</view>
		</uni-forms>
	</view>
</template>

<script>
	import {getDictionaryFn} from "@/api/mine/register.js"
	import {getNoticeAdd,getNoticeDetail,getNoticeEdit } from "@/api/mine/news.js"
	import { selectValueKey } from '@/utils/common.js';
	import config from '@/config'
	const baseUrl = config.baseUrl
	import { getToken } from '@/utils/auth'
	export default{
		data(){
			return{
				//附件
				  option: {
				    // 上传服务器地址，需要替换为你的接口地址
				    url: baseUrl+'/common/upload', // 该地址非真实路径，需替换为你项目自己的接口地址
				    // 上传附件的key
				    name: 'file',
				     // 根据你接口需求自定义请求头,默认不要写content-type,让浏览器自适配
				    header: {
				        // 示例参数可删除
				        'Authorization':  'Bearer ' + getToken(),
				    },
				    // 根据你接口需求自定义body参数
				    formData: {}
				},
				// 选择文件后是否立即自动上传，true=选择后立即上传
				instantly: true,
				// 必传宽高且宽高应与slot宽高保持一致
				width: '',
				height: '48rpx',
				// 限制允许上传的格式，空串=不限制，默认为空
				formats: 'doc,docx,xls,xlsx,ppt,txt,pdf,zip,rar,word,png,jpg,jpeg',
				// 文件上传大小限制
				size: 100,
				// 文件数量限制 默认10
				count: 5,
				// 文件回显列表
				files: new Map(),
				// 微信小程序Map对象for循环不显示，所以转成普通数组，不要问为什么，我也不知道
				wxFiles: [],
				// 是否打印日志
				debug: false,
				filelist:[], 
				
				pbFlag:false,
				readOnly:false,
				editorCtx:'',
				fjimg:require("@/work/static/images/fjimg.png"),
				delimg:require("@/work/static/images/delimg.png"),
				titimg:require("@/work/static/images/titbg.png"),
				datainfo:{
					noticeTitle:'',
					issuer:this.$store.state.user.name,
					issuerDept:this.$store.state.user.deptName,
				},
				tzlbList:[],
				tzlblabe:'',
				tzlbidx:'',
				id:'',
				editinfo:'',
				ptype:"add"
			}
		},
		onLoad(e) {
			this.init()
			if(e.id){
				this.id=e.id;
				this.getNoticeDetail()
			}
			if(e.type){
				this.ptype=e.type;
			}
			if(e.type=='old'&&uni.getStorageSync('zcList')){
				var newObj=JSON.parse(JSON.stringify(uni.getStorageSync('zcList')))
				this.datainfo=newObj;
				this.editinfo=newObj.noticeContent;
				if(newObj.zxFjList&&newObj.zxFjList.length){
					this.filelist=JSON.parse(JSON.stringify(newObj.zxFjList))
				}
			}
		},
		
		methods:{
			statusFormat(ite) {
				var aite=selectValueKey(this.tzlbList, ite);
				this.tzlbidx=aite.key;
				this.tzlblabe=aite.actions;
				return aite.actions;
			},
			init(){
				getDictionaryFn('sys_notice_type').then(res=>{
					if(res.code==200&&res.data.length){
						this.tzlbList = res.data.map(v => {
							return {
								label: v.dictLabel,
								value: v.dictValue
							}
						})
					}
				})
			},
			bindDateChangea(e){
				var val=e.detail.value;
				this.tzlblabe=this.tzlbList[val].label;
				this.datainfo.noticeType=this.tzlbList[val].value;
			},
			getPaiFn(){
				this.pbFlag=true;
			},
			getNoticeDetail(){
				var that=this;
				getNoticeDetail(this.id).then(res=>{
					if(res.code==200){
						this.datainfo=res.data;
						this.editinfo=res.data.noticeContent;
						uni.createSelectorQuery().select('#editor').context(function(res) {
							that.editorCtx = res.context;
							that.editorCtx.setContents({
								html:that.editinfo
							})
						}).exec();
						if(res.data.zxFjList&&res.data.zxFjList.length){
							this.filelist=JSON.parse(JSON.stringify(res.data.zxFjList))
						}
					}
				})
			},
			// 暂存
			getZanFnt(){
				var that=this;
				that.editorCtx.getContents({
					success: function(data) { 
						// noticeContent=data.html;
						// that.datainfo.zxFjList=JSON.parse(JSON.stringify(that.filelist));
						var params=that.datainfo;
						params.noticeContent=data.html;
						
						uni.setStorageSync('zcList', JSON.parse(JSON.stringify(params)))
						that.$toast("暂存成功")
					}  
				})
			},
			getSubmit(){
				var that=this;
				if(!this.datainfo.noticeTitle){
					this.$toast("请输入标题")
					return
				}
				if(!this.datainfo.noticeType){
					this.$toast("请选择通知类别")
					return
				}
				var noticeContent='';
				that.editorCtx.getContents({  
					success: function(data) { 
						var params=that.datainfo;
						params.noticeContent=data.html;
						if(that.ptype=='edit'){
							getNoticeEdit(params).then(res=>{
								if(res.code==200){
									that.$toast("修改成功")
									
									setTimeout(function(){
										uni.$emit('refreshxxlist')
										uni.$emit('refreshdetail')
										uni.navigateBack({
											delta:1
										})
									},1200)
								}
							})
						}else{
							getNoticeAdd(params).then(res=>{
								if(res.code==200){
									that.$toast("发布成功")
									uni.removeStorageSync('zcList')
									setTimeout(function(){
										uni.$emit('refreshxxlist')
										uni.navigateBack({
											delta:1
										})
									},1200)
								}
							})
						}
						
					}  
				})
			},
			onuploadEnd(item) {
				var newobj={}
				var responseText=JSON.parse(item.responseText)
				newobj.name=responseText.originalFilename;
				newobj.url=responseText.fileName;
				this.filelist.push(newobj)
				this.datainfo.zxFjList=JSON.parse(JSON.stringify(this.filelist))
			},
			getDelFj(idx){
				var that=this;
				uni.showModal({
					title: '确认删除',
					content: "是否确认删除",
					cancelText: '取消',
					confirmText: '确认',
					success: function(res) {
						if (res.confirm) {
							that.filelist.splice(idx,1)
							that.datainfo.zxFjList=JSON.parse(JSON.stringify(that.filelist))
						} else if (res.cancel) {
						}
					}
				});
			},
			getDown(e){
				uni.showLoading({
					title: '加载中'
				});
				var url=baseUrl+e;
				uni.downloadFile({
					url: url,//文件的下载路径
					success(result) {
							uni.hideLoading()
						var filePath = result.tempFilePath;
						   uni.openDocument({
						     filePath: filePath,
						     showMenu: true,
						     success: function (res) {
						       // console.log('打开文档成功');
						     }
						   });
					},
					fail(res) {uni.hideLoading()}
				})
			},
			onStatusChange(e){
				console.log(e,2)
			},
			onEditorReady(e){
				var that=this;
				uni.createSelectorQuery().select('#editor').context(function(res) {
					that.editorCtx = res.context;
					that.editorCtx.setContents({
						html:that.editinfo
					})
				}).exec();
			}
		}
	}
</script>

<style scoped lang="scss">
.bgtop{height: 150rpx;background-color: $com-cd3;width: 100%;}
.pdbox{padding: 34rpx 24rpx 16rpx;}
.addbox /deep/ .uni-forms-item{margin-bottom:36rpx;}
.addbox /deep/ .uni-easyinput{flex: 1;text-align: right;font-size: 30rpx;}
.addbox /deep/ .uni-forms-item__label{font-weight: bold;font-size: 30rpx;color: #343434;width: 198rpx !important;padding-left:20rpx;}
.addbox /deep/ .is-required{position: absolute;left: 0;top: 50%;margin-top: -10rpx;}
.addbox /deep/ .uni-data-checklist{display: flex;align-items: center;justify-content: flex-end;}
.addbox /deep/ .uni-easyinput__placeholder-class{font-weight: 500;font-size: 30rpx;color: #AAAAAA;}
.addbox /deep/ .checklist-box{margin-right: 0 !important;margin-left: 40rpx !important;}
.fjadd{
	.btn{font-weight: bold;font-size: 30rpx;color: #1D64E2;margin: 0;display: flex;align-items: center;}
}
.addbox{margin-top: -120rpx;
	padding: 0 24rpx 50rpx;
	.boxs{background: #FFFFFF;border-radius: 30rpx;margin-bottom: 24rpx;overflow: hidden;position: relative;
		.titbox{
			.tit{
				image{width: 32rpx;height: 18rpx;margin-right: 10rpx;}
				view{font-weight: bold;font-size: 32rpx;color: #222327;}
			}
			.titr{width: 146rpx;height: 50rpx;background: #E4EEFF;border-radius: 26rpx;font-weight: 500;font-size: 24rpx;color: #1D64E2;text-align: center;line-height: 50rpx;}
		}
		
		.openbox{display: flex;align-items: center;justify-content: flex-end;font-weight: 500;
font-size: 30rpx;height: 100%;color: #AAAAAA;}
		.edtip{font-weight: 500;font-size: 22rpx;color: #FF0000;
			text{margin: 4rpx 8rpx 0 0;}
			&.pa{position: absolute;right: 0;bottom: -15px;text-align: right;width: 702rpx;}
		}
	}
	
}
	
	// 附件
	.fjlists {display: flex;align-items: flex-start;justify-content: space-between;margin-bottom: 12rpx;
		image{margin-right: 18rpx;flex: 0 0 auto;}
		.imgl{width: 40rpx;height: 40rpx;display: flex;align-items: center;justify-content: center;margin-right: 16rpx;
			image{width: 26rpx;height: 24rpx;}
			}
			.tit{font-size: 26rpx;color: #343434;font-weight: 500;margin-top: 4rpx;}
		.delimg{width: 40rpx;height:40rpx;margin-left: 16rpx;display: flex;align-items: center;justify-content: center;
			image{width: 26rpx;height: 26rpx;}
		}
	}
	// 按钮
	.addbtn{display: flex;align-items: center;justify-content: space-between;padding-top: 26rpx;
		.btn{width: 336rpx;height: 80rpx;font-weight: bold;font-size: 30rpx;box-sizing: border-box;border-radius: 40rpx;display: flex;align-items: center;justify-content: center;
			&.btn1{border: 2rpx solid #1D64E2;background: #ffffff;color: #1D64E2;}
			&.btn2{background: #1D64E2;color: #ffffff;}
		}
	}
	.lbtabp{display: flex;align-items: center;flex: 0 0 auto;margin-left: 24rpx;height: 100%;justify-content: flex-end;
		view{font-size: 30rpx;color: #222327;}
	}
.addbox /deep/ .pbbox .ql-editor  p{text-indent: 2rem;}
</style>