<style lang="less" scoped>

</style>
<template>
    <bModal v-model="modal_create" :title="title" @on-ok="create_ok" size="fullscreen" :loading="loading" @on-cancel="create_cancel" :mask-closable="false" :closable="false">
         <Form ref="form" :model="formData"  :label-width="80">
            <Col span="12">
            <Form-item label="标题" prop="title">
                <Input v-model="formData.title" ></Input>
            </Form-item>
            <Form-item label="分组" prop="title">
                <Input v-model="formData.function" ></Input>
            </Form-item>
            <Form-item label="缩略图" prop="img">
                <Input v-model="formData.img" ></Input>
            </Form-item>
            <Form-item label="类型" prop="typeName">
                <Input v-model="formData.typeName" ></Input>
            </Form-item>
            <Form-item label="标签" prop="tags">
                <Input v-model="formData.tags" ></Input>
            </Form-item>
            </Col>
            <Col span="12">
             <Form-item label="" prop="editorContent">
                <bUEditor :ueditorConfig="editorConfig" @ready="editorReady"></bUEditor>
            </Form-item>
            </Col>
        </Form>
    </bModal>
</template>
<script>

export default {
    data() {
        var baseTitle = '新建新闻';
        var baseFormData = {
            id:undefined,
            title:"",
            img:"",
            tags:"",
            typeName:"",
            function:"默认"
            // contentData:''
        };
        let _editorConfig = {
            autoHeightEnabled:false,
            initialFrameHeight:450,
            // serverUrl: SEDU.Config.server.apiGateway + "/ue",
            // cookie_keys_tenant_id: SEDU.Util.tenant_key,
            // cookie_keys_x_auth_token: SEDU.Util.token_key
        };
        return {
            
            loading: true,
            title:baseTitle,
            formData: baseFormData,
            editable: false,
            editorContent:'',
            editorConfig: _editorConfig,
        }
    },
   
    mounted: function() {
            this.editorContent = '';
            if(this.select && this.select.id){
                
                this.title = '修改新闻';
                var that = this;
                var jsonData = {methodParameters:[this.select.id]};
                // SEDU.Util.invokeServer({
                //     path: '/biz/simple/com.beecode.inz.opm.news.bill.News/simpleLoad',
                //     contentType: "application/json",
                //     data: JSON.stringify(jsonData),
                //     type: 'POST'
                // }).then((data, textStatus, response) => {
                //     var result = JSON.parse(data.result);
                //     that.editorInstance.setContent(result.content);
                //     that.formData = {
                //         title:result.title,
                //         img:result.img,
                //         id:result.id
                //     }
                //     that.title = '修改新闻';
                // });
            }
    },
    methods: {
        
        create_ok() {
            var that = this;
            this.loading = true;
            //修改逻辑
            if(this.formData.id){
                var jsonData = {methodParameters:[this.formData.id]};
                // SEDU.Util.invokeServer({
                //     path: '/biz/simple/com.beecode.inz.opm.news.bill.News/simpleLoad',
                //     contentType: "application/json",
                //     data: JSON.stringify(jsonData),
                //     type: 'POST'
                // }).then((data, textStatus, response) => {
                //     var result = JSON.parse(data.result);
                //     result.title = that.formData.title;
                //     result.img = that.formData.img;
                //     result.functionGroup = that.formData.function;
                //     result.tags = that.formData.tags;
                //     result.typeName = that.formData.typeName;
                //     result.content = that.editorInstance.getContent();
                //     // result.description = that.editorInstance.getContent();
                //     var array = [];
                //     array.push(JSON.stringify(result));
                //     array.push("修改新闻");
                //     var jsonData2 = {methodParameters:array};
                //     SEDU.Util.invokeServer({
                //         path: '/biz/simple/com.beecode.inz.opm.news.bill.News/simpleSaveAndSpecifyLog',
                //         contentType: "application/json",
                //         data: JSON.stringify(jsonData2),
                //         type: 'POST'
                //     }).then((data, textStatus, response) => {
                        
                //         that.loading = false;
                //         that.modal_create = false;
                //         that.initForm();
                //         that.$emit('ok', result);
                //     }).catch((XMLHttpRequest, textStatus, errorThrown) => {
                //         that.initForm();
                //     });
                // });
            //新建逻辑
            }else{

                var result = this.formData;
                var jsonData = {methodParameters:[]};
                // SEDU.Util.invokeServer({
                //     path: '/biz/simple/com.beecode.inz.opm.news.bill.News/simpleAdd',
                //     contentType: "application/json",
                //     data: JSON.stringify(jsonData),
                //     type: 'POST'
                // }).then((data, textStatus, response) => {
                //     var result = JSON.parse(data.result);
                //     result.title = that.formData.title;
                //     result.img = that.formData.img;
                //     result.tags = that.formData.tags;
                //     result.typeName = that.formData.typeName;
                //     result.content = that.editorInstance.getContent();
                //     // result.description = that.editorInstance.getContent();
                //     result.functionGroup = that.formData.function;
                //     var array = [];
                //     array.push(JSON.stringify(result));
                //     array.push("新建新闻");
                //     var jsonData2 = {methodParameters:array};
                //     SEDU.Util.invokeServer({
                //         path: '/biz/simple/com.beecode.inz.opm.news.bill.News/simpleSaveAndSpecifyLog',
                //         contentType: "application/json",
                //         data: JSON.stringify(jsonData2),
                //         type: 'POST'
                //     }).then((data, textStatus, response) => {
                        
                //         that.loading = false;
                //         that.modal_create = false;
                //         that.initForm();
                //         that.$emit('ok', result);
                //     }).catch((XMLHttpRequest, textStatus, errorThrown) => {
                //         that.initForm();
                //     });
                // });
            }
        },
        initForm(){
            this.formData={
                title:"",
                img:"",
                id:undefined,
                tags:"",
                typeName:"",
                function:"默认"
            };
            this.editorInstance.setContent('');

        },
        create_cancel() {
           console.log("cancel");
           this.modal_create = false;
           this.editorInstance = null;
        },
        editorReady (editorInstance) {

                var that = this;
                that.editorInstance = editorInstance;
                that.editorInstance.setContent('');
                editorInstance.addListener('contentChange',()=>{
                    // that.formData.contentData=editorInstance.getContent();
                })
        },
    },
    components: {
        
    },
    props:{
       value: {
            type: Boolean
        },
        select:{
            type: Object,
        },
        config:{
            type: Object,
            default:function(){
                return {function:"默认"};
            }
        }
    },
    watch: {
 
        "select":function(selectItem){
            if(selectItem && selectItem.id){
                var that = this;
                var jsonData = {methodParameters:[selectItem.id]};
                // SEDU.Util.invokeServer({
                //     path: '/biz/simple/com.beecode.inz.opm.news.bill.News/simpleLoad',
                //     contentType: "application/json",
                //     data: JSON.stringify(jsonData),
                //     type: 'POST'
                // }).then((data, textStatus, response) => {
                //     var result = JSON.parse(data.result);
                //     if(!result.content||result.content===''){
                //         result.content= '';
                //     }
                //     that.formData = {
                //         title:result.title,
                //         img:result.img,
                //         // contentData:result.content,
                //         tags:result.tags,
                //         typeName:result.typeName,
                //         id:result.id,
                //         function:result.functionGroup
                //     }
                //     that.title = '修改新闻';
                    
                //     that.editorInstance.setContent(result.content);
                //     // that.editorInstance.setContentTxt(that.formData.contentData.text);
                // });




                
            }else{
                this.formData={
                    id:undefined,
                    title:"",
                    img:"",
                    function:selectItem.function
                    // contentData:''
                }
                this.editorInstance.setContent("");
            }
            // console.log(this.formData);
        }
    },
    computed : {
        modal_create: {
            set : function(val){
                this.$emit('input', val);
            },
            get : function(){
                
                return this.value;
            }
        },
    }
}
</script>