﻿<template>
	<transition name="el-zoom-in-center">
		<div class="NCC-preview-main">
			<div class="NCC-common-page-header">
				<el-page-header @back="goBack" :content="!dataForm.id ? '新建' : isDetail ? '详情' : '编辑'"/>
				<div class="options">
					<el-button type="primary" @click="dataFormSubmit()" v-if="!isDetail">确 定</el-button>
					<el-button @click="goBack">取 消</el-button>
				</div>
			</div>
			<el-row :gutter="15"  class="main" :style="{margin: '0 auto',width:'100%'}">
				<el-form ref="elForm" :model="dataForm" size="medium" label-width="111px" label-position="right" :disabled="!!isDetail" :rules="rules">
					<el-col :span="24" >
						<el-form-item label="标题" prop="title">
							<el-input v-model="dataForm.title" placeholder="请输入" clearable required :style='{"width":"100%"}' prefix-icon="ym-custom ym-custom-format-line-weight" >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12" >
						<el-form-item label="栏目" prop="categoryId">
							<el-select v-model="dataForm.categoryId" placeholder="请选择" clearable required :style='{"width":"100%"}' >
								<el-option v-for="(item, index) in categoryIdOptions" :key="index" :label="item.fullName" :value="item.id" ></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="12" >
						<el-form-item label="类型" prop="isType">
							<el-select v-model="dataForm.isType" placeholder="请选择" clearable :style='{"width":"100%"}' >
								<el-option v-for="(item, index) in isTypeOptions" :key="index" :label="item.fullName" :value="item.id" ></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :span="24" >
						<el-form-item label="内容" prop="bodyContent">
							<NCC-Quill v-model="dataForm.bodyContent" placeholder="请输入内容..." required >
							</NCC-Quill>
						</el-form-item>
					</el-col>
					<el-col :span="24" >
						<el-form-item label="封面" prop="imgUrl">
							<NCC-UploadImg v-model="dataForm.imgUrl" :fileSize="2" sizeUnit="MB" :limit="9" >
							</NCC-UploadImg>
						</el-form-item>
					</el-col>
					<el-col :span="6" v-if="false" >
						<el-form-item label="创建人" prop="creater">
							<el-input v-model="dataForm.creater" placeholder="系统自动生成" readonly >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="6" v-if="false" >
						<el-form-item label="创建时间" prop="createTime">
							<el-input v-model="dataForm.createTime" placeholder="系统自动生成" readonly >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="6" v-if="false" >
						<el-form-item label="修改人员" prop="lastModifyUserId">
							<el-input v-model="dataForm.lastModifyUserId" placeholder="系统自动生成" readonly >
							</el-input>
						</el-form-item>
					</el-col>
					<el-col :span="6" v-if="false" >
						<el-form-item label="修改时间" prop="lastModifyTime">
							<el-input v-model="dataForm.lastModifyTime" placeholder="系统自动生成" readonly >
							</el-input>
						</el-form-item>
					</el-col>
				</el-form>
			</el-row>
		</div>
	</transition>
</template>
<script>
	import request from '@/utils/request'
	import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
	import { previewDataInterface } from '@/api/systemData/dataInterface'
	export default {
		components: {},
		props: [],
		data() {
			return {
				loading: false,
				visible: false,
				isDetail: false,
				dataForm: {
					title:undefined,
					categoryId:undefined,
					isType:undefined,
					bodyContent:undefined,
					imgUrl:[],
					creater:undefined,
					createTime:undefined,
					lastModifyUserId:undefined,
					lastModifyTime:undefined,
				},
				rules: {
					title:[
						{
							required:true,
							message:'请输入标题',
							trigger:'blur'
						},
					],
					categoryId:[
						{
							required:true,
							message:'请输入栏目',
							trigger:'change'
						},
					],
					bodyContent:[
						{
							required:true,
							message:'请输入内容',
							trigger:'blur'
						},
					],
				},
				categoryIdOptions : [],
				isTypeOptions:[{"fullName":"普通文章","id":"0"},{"fullName":"幻灯片","id":"1"},{"fullName":"首页栏目","id":"2"}],
			}
		},
		computed: {},
        watch: {},
        created() {
			this.getcategoryIdOptions();
		},
		mounted() {
        },
		methods: {
			getcategoryIdOptions(){
				getDictionaryDataSelector('f8b80c5a03a44fa0b42a8bbb0ff01d52').then(res => {
					this.categoryIdOptions = res.data.list
				});
			},
			goBack() {
                this.$emit('refresh')
            },
			init(id, isDetail) {
				this.dataForm.id = id || 0;
                this.visible = true;
                this.isDetail = isDetail || false;
				this.$nextTick(() => {
					this.$refs['elForm'].resetFields();
					if (this.dataForm.id) {
						request({
							url: '/api/SubDev/ZyOaArticle/' + this.dataForm.id,
							method: 'get'
						}).then(res =>{
							this.dataForm = res.data;
						})
					}
				})
			},
			dataFormSubmit() {
				this.$refs['elForm'].validate((valid) => {
                    if (valid) {
                        if (!this.dataForm.id) {
                            request({
                                url: `/api/SubDev/ZyOaArticle`,
                                method: 'post',
                                data: this.dataForm,
                            }).then((res) => {
                                this.$message({
                                    message: res.msg,
                                    type: 'success',
                                    duration: 1000,
                                    onClose: () => {
                                        this.visible = false,
                                            this.$emit('refresh', true)
                                    }
                                })
                            })
                        } else {
                            request({
                                url: '/api/SubDev/ZyOaArticle/' + this.dataForm.id,
                                method: 'PUT',
                                data: this.dataForm
                            }).then((res) => {
                                this.$message({
                                    message: res.msg,
                                    type: 'success',
                                    duration: 1000,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refresh', true)
                                    }
                                })
                            })
                        }
                    }
                })
			},
		}
	}
</script>
