var CalendarEventForm = function(){};

/***
 * Init the form
 * @returns {undefined}
 */
CalendarEventForm.Init = function(){
    Event.AddById("btnSaveEvent", "click", CalendarEventForm.SaveElement);

    CalendarEventForm.txtEditor = new TextRichEditor( Dom.GetById("Description"),
    {tools: ["ImageTool", "SourceCode", "ShowBlock"],
        events: [{"tool": "ImageTool", "events": [{"type": "mousedown", "handler": CalendarEventForm.OpenImageLibray}]}],
    }
);

};

/***
 * Open the libray image
 * @param {type} editor
 * @param {type} node
 * @returns {undefined}
 */
CalendarEventForm.OpenImageLibray = function(editor, node){
       
    if (node.tagName != "DIV") {
        node = node.parentElement
    }

    editor.setSelectedNode(node);
    Dashboard.currentEditor = editor;

    let blockId = editor.sourceControl.parentNode.id;

    Dialog.open('', {"title": Dashboard.GetCode("Cms.Addmage"),
        "app": "Cms",
        "class": "DialogCms",
        "method": "AddImage",
        "type": "left",
        "params": ""
    });
} ;
/***
* Get the Id of element 
*/
CalendarEventForm.GetId = function(){
    return Form.GetId("CalendarEventFormForm");
};

/*
* Save the Element
*/
CalendarEventForm.SaveElement = function(e){
   
    if(Form.IsValid("CalendarEventFormForm"))
    {
    let request = new Http("Calendar", "SaveEvent");
        request.SetData(Form.Serialize("CalendarEventFormForm"));

        request.Post(data => {

            data = JSON.parse(data);

            if(data.status == "error"){
                Form.RenderError("CalendarEventFormForm", data.data.message);
            } else{

                Form.SetId("CalendarEventFormForm", data.data.Id);
                Dialog.Close();
                Calendar.ReloadEvent(data.data.eventGrid);
            }
        });
    }
};

