<template>
    <el-drawer :title="titleMap[mode]" :append-to-body="true" v-model="visible" :size="1000" destroy-on-close :close-on-click-modal="false" @closed="$emit('closed')" :with-header="false">
        <el-container class="flex-column" v-loading="loading">
            <div class="drawer-detail-main">
                <div class="drawer-detail-header">
                    <div class="drawer-detail-header-body">
                        <div class="drawer-detail-header-left">{{ titleMap[mode] }}</div>
                        <div class="drawer-detail-header-left">
                            <el-button type="default" icon="el-icon-close" @click="visible=false"></el-button>
                        </div>
                    </div>
                </div>
            </div>
            <el-main>
                <el-form ref="dialogForm" :model="formData" :rules="rules" label-width="100px" label-position="top">
                    <template v-if="mode=='add'">
                        <el-form-item label="单页名称" prop="desc">
                            <el-input v-model="formData.desc" clearable placeholder="单页名称"></el-input>
                            <div class="el-form-item-msg"></div>
                        </el-form-item>
                        <el-form-item label="单页标识" prop="name">
                            <el-input v-model="formData.name" clearable placeholder="单页名称"></el-input>
                            <div class="el-form-item-msg"></div>
                        </el-form-item>
                    </template>
                    <template v-if="mode=='edit'">
                        <el-form-item label="单页名称" prop="desc">
                            <el-input v-model="formData.desc" readonly placeholder="单页名称"></el-input>
                            <div class="el-form-item-msg"></div>
                        </el-form-item>
                    </template>
                    <el-form-item label="单页内容" prop="value" class="label-item">
                        <sc-editor v-model="formData.value" placeholder="请输入" :height="500" style="width: 100%;"></sc-editor>
                        <div class="el-form-item-msg"></div>
                    </el-form-item>
                    
                </el-form>
            </el-main>
            <el-footer style="text-align: right;">
                <el-button @click="visible=false" >取 消</el-button>
                <el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
            </el-footer>
        </el-container>
    </el-drawer>
</template>

<script>
import { defineAsyncComponent } from 'vue';
const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
export default {
    components: {
        scEditor
    },
    data() {
        return {
            loading: false,
            mode:"add",
            visible: false,
            isSaveing: false,
            titleMap:{
                add:"新增单页",
                edit:"编辑单页"
            },
            rules:{
                name: [
                    {required: true, message: '请输入'}
                ],
                desc: [
                    {required: true, message: '请输入'}
                ],
                value: [
                    {required: true, message: '请输入'}
                ],
            },
            formData:{}
        }
    },
    methods: {
        open(mode = 'add'){
            this.mode = mode;
            this.visible = true;
            return this
        },
        //表单注入数据
        setData(data){
            this.formData = JSON.parse(JSON.stringify(data));
            this.getPageDetail();
        },
        async getPageDetail(){
            var resp = await this.$API.page.detail.get({"name":this.formData.name});
            if (resp.code == 0) {
                return this.$message.error(resp.msg)
            }
            this.formData = resp.data;
        },
        async submit(){
            var validate = await this.$refs.dialogForm.validate().catch(()=>{});
            if(!validate){ return false }
            this.isSaveing = true;
            var resp = await this.$API.page.save.post(this.formData);
            this.isSaveing = false;
            if (resp.code == 0) {
                return this.$message.error(resp.msg);
            }
            this.$message.success(resp.msg);
            this.visible = false;
            this.formData = {};
            this.$emit("success");
        }
    }
}
</script>