<template>
    <!-- 弹出框表单 -->
    <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-row>
                                            <Field label="分类id" prop="categoryId" v-model="form.categoryId" placeholder="请输入分类id"/>
                            <Field label="分类名称" prop="categoryName" v-model="form.categoryName" placeholder="请输入分类名称"/>
                            <Field label="标题" prop="title" v-model="form.title" type="textarea" placeholder="请输入标题"/>
                            <Field label="标题logo" prop="titleLogoPath" v-model="form.titleLogoPath" type="textarea" placeholder="请输入标题logo"/>
                            <Field label="内容"><editor v-model="form.content" :min-height="256"/></Field>
                            <Field label="置顶 " prop="top" v-model="form.top" type="select" :enumData="dict.top" placeholder="请选择置顶 "/>
                            <Field label="浏览次数" prop="viewNums" v-model="form.viewNums" type="select" :enumData="dict.viewNums" placeholder="请选择浏览次数"/>
                            <Field label="发布时间" prop="publishTime" v-model="form.publishTime" type="date" />

            </el-row>

        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" v-if="pageInfo.type !== 'view'" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import form from "@/assets/mixins/formdialog";
    import dialogShow from "./dialogshow";
    import Editor from '@/components/Editor';
    export default {
        mixins: [form],
        components: {
            dialogShow ,
            Editor,
        },
        data() {
            return {
                // 遮罩层
                loading: true,
                // 弹出层标题
                title: "新闻",
                // 是否显示弹出层
                open: false,
                toString:[
                    "top",
                    "viewNums",
                ],
                // 表单校验
                rules: {
                    title: [
                        {required: true,message: "请输入标题", trigger: "blur" },
                        {max: 512,message: "最多只能录入512个字符",trigger: "blur",},
                    ],
                    titleLogoPath: [
                        {required: true,message: "请输入标题logo", trigger: "blur" },
                        {max: 512,message: "最多只能录入512个字符",trigger: "blur",},
                    ],
                    top: [
                        {required: true,message: "请输入置顶 ", trigger: "blur" },
                    ],
                    viewNums: [
                        {required: true,message: "请输入浏览次数", trigger: "blur" },
                    ],
                    publishTime: [
                        {required: true,message: "请选择发布时间" },
                    ],
                    createTime: [
                        {required: true,message: "请选择创建时间" },
                    ],
                }
            };
        },

        methods: {
            /** 编辑 */
            edit(row) {
                this.reset()
                this.query = { id: row.id };
                this.urls.currUrl ="news/edit";
                this.getData();
                this.pageInfo.type="edit"
                this.title = "修改新闻";
            },
            /** 新增 */
            add(row) {
                this.reset()
                this.urls.currUrl = "news/add";
                this.getData();
                this.pageInfo.type="add"
                this.title = "新增新闻";
            },
            /** 查看*/
            view(row) {
                this.reset()
                this.query = { id: row.id };
                this.urls.currUrl ="news/view";
                this.getData();
                this.pageInfo.type="view"
                this.title = "新闻详细";
            },
            /**取消按钮 */
            cancel() {
                this.open = false;
            },
            /**获取数据后弹框 */
            afterRender(data) {
                this.open = true;
            },

            afterSubmit(data) {
                this.open = false;
                this.$emit("ok");
            },

            // 表单重置
            reset() {
                this.form = {
                    categoryId : null,
                    categoryName : "",
                    title : "",
                    titleLogoPath : "",
                    content : "",
                    top : 0,
                    viewNums : 0,
                    publishTime : null,
                };
                this.resetForm("form");
            },
            resetForm(refName) {
                if (this.$refs[refName]) {
                    this.$refs[refName].resetFields();
                }
            },
        },
    };
</script>
