<template>
	<div class="container mw1088">
		<div class="tabheadtow clear">
			<h2 class="title">{{info.id>0?'编辑文章':'新增文章'}}</h2>
		</div>

		<el-form ref="elForm" :model="info" :rules="rules" class="p20" max-width="600px">
			<el-form-item label="所在科室" label-width="100px" prop="type">
				<el-input v-model="info.department"  clearable />
			</el-form-item>

			<el-form-item label="所属分类" label-width="100px" prop="type">
				<el-input v-model="info.category"  clearable />
			</el-form-item>

			<el-form-item label="文章标题" label-width="100px" prop="title">
				<el-input v-model="info.title"  clearable />
			</el-form-item>
				
			<el-form-item label="文章简介" label-width="100px" prop="title">
				<el-input v-model="info.brief"  type="textarea" clearable />
			</el-form-item>
			
			<el-form-item label="是否公开" label-width="100px" prop="title">
				<el-switch :active-value="1" :inactive-value="0" v-model="info.isOpen"></el-switch>
			</el-form-item>
			
			
			<el-form-item label="文件地址" label-width="100px" prop="title">
				<el-input v-model="info.filename" clearable/>
			</el-form-item>
			
			
				

			<el-form-item label="上传文件" label-width="100px" prop="title">
				<upload-image v-model="info.contract_img" placeholder="上传文档" :fileSize="2048" :maxWH="1080" @rmImage="info.filename=''"/>
			 </el-form-item>

			<div class="tc">
				<el-button type="primary" @click="onSubmit" class="btn-md">
					保存文章
				</el-button>
			</div>
		</el-form>
	</div>
</template>
<script>
	import editor from "@/utils/editor.js";
	import UploadImage from "@/components/upload/image.vue";
	import { getArticalInfo, addArtical } from "@/api/artical.js";
	import ImgView from '@/components/customPic/imgView.vue';
	export default {
		components:{UploadImage,ImgView},
		props: [],
		data() {
			return {
				info: {
					id:0,
					title:'',
					content:''
				},
				editorConfig: editor.artical,
				rules: {}	
			}
		},
		beforeMount() {
			let {id} = this.$route.params;
			if( id ){
				this.getData( +id )
			}
		},
		methods: {
			getData( id ){
				getArticalInfo( {id} ).then( (res)=>{
					if(res.code == 200){
						this.info = res.data
					}
				})
			},
			onSubmit() {
				let info = this.info;
				this.$refs['elForm'].validate(valid => {
					if (!valid) return
					addArtical( info ).then( (res)=>{
						console.log( "res", res )
						if(res.code == 200){
							this.$message.successMsg("保存成功",1)
							info.id =  res.data.id
						}
					})
					// TODO 提交表单
				})
			},
			resetForm() {
				this.$refs['elForm'].resetFields()
				this.info = {id:0, content:'', title:''}
			}
		}
	}
</script>
<style>
	.el-upload__tip {
		line-height: 1.2;
	}
	.row-box{
		padding-top: 8px;
		border:1px dotted gray;
	}
</style>
