<template>
    <div>
        <div style="text-align: center;font-size: 16px;font-weight: bold;"><span>知情同意书</span></div>
        <a-form-model ref="formRef" :model="formData" :rules="formRules" style="margin-top: 20px">
            <a-row>
                <a-col :span="24">
                    <!--label="模板内容"-->
                    <a-form-model-item prop="content" :wrapper-col="{span: 24}">
                        <baiduEditor @baiduEditorReady="getEditorreadyed" ref="baiduEditor" v-model="formData.content"></baiduEditor>
                    </a-form-model-item>
                </a-col>
            </a-row>
        </a-form-model>
        <a-button style="margin-left: 36%" @click="backList">取消</a-button>
        <a-button type="primary" style="margin-left: 20px" class="search_btn"
                  @click="submitForm">保存
        </a-button>
    </div>
</template>

<script>
    import typeSelect from "../../components/commonSelect/typeSelect";
    import baiduEditor from "../../components/baiduEditor";
    import {closedDetail, isBlank} from "../../utils/common";

    export default {
        components: {typeSelect, baiduEditor},
        name: "addTemplate",
        data() {
            return {
                // form表单
                formData: {
                    id: "",
                    content: ""
                },
                formRules: {
                    content: [
                        {required: true, message: '请输入模板内容', trigger: 'blur'}
                    ]
                },
                formDisabled: false,
                modeType: "update",
                routerParams:{}
            }
        },
        created() {
            this.routerParams = this.$route.query
            if (this.routerParams.menuId) {
                window.sessionStorage.setItem('menuId', this.routerParams.menuId)
            }
        },
        mounted() {
        },
        methods: {
            getEditorreadyed(val) {
                if (val) {
                    this.getEditorContent()
                }
            },
            getEditorContent() {
                let vm = this;
                this.$api.common.fetchConsentInfo().then(res => {
                    if (res.code == 'SUCCESS') {
                        if (isBlank(res.data)) {
                            this.modeType = 'add';
                        } else {
                            // vm.$nextTick(() => {
                            vm.$refs.baiduEditor.setContent(res.data.content, false)
                            vm.formData.content = res.data.content;
                            vm.formData.id = res.data.id;
                            // })
                        }

                    }
                })
            },
            // 弹窗确定按钮
            submitForm() {
                let vm = this;
                this.formData.content = this.$refs.baiduEditor.getContent();
                this.$refs.formRef.validate(valid => {
                    if (valid) {
                        this.$confirm({
                            title: '确定提交吗？',
                            okType: 'success',
                            onOk: () => {
                                if (this.modeType === "add") {
                                    vm.$api.common.fetchAddConsentInfo(this.formData).then(res => {
                                        if (res.code === 'SUCCESS') {
                                            vm.$message.success('操作成功！')
                                        }
                                    })
                                } else {
                                    vm.$api.common.fetchUpdateConsentInfo(this.formData).then(res => {
                                        if (res.code === 'SUCCESS') {
                                            vm.$message.success('操作成功！')
                                        }
                                    })
                                }

                            },
                            onCancel: () => {
                            },
                        });
                    }
                });
            },
            backList() {
                closedDetail('/system/addTemplate')
            }
        },
    }
</script>

<style lang="less">
    .edui-default .edui-toolbar .edui-combox .edui-combox-body {
        line-height: 21px !important;
    }

    .edui-default .edui-button-body, .edui-splitbutton-body, .edui-menubutton-body, .edui-combox-body {
        line-height: 20px !important;
    }
</style>
