var TranslateForm = function(){};

/***
 * Init the form
 * @returns {undefined}
 */
TranslateForm.Init = function(){
    Event.AddById("btnSaveTranslate", "click", TranslateForm.SaveElement);
    Event.AddById("LangId", "change", TranslateForm.GetTranslateElement);
};

/***
 * Get the translate in the Selected Langue
 */
TranslateForm.GetTranslateElement = function(){

    let request = new Http("Lang", "GetTranslateElement");
    request.SetData(Form.Serialize("TranslateFormForm"));

    request.Post(data => {
    
        data = JSON.parse(data);
        let Content = document.getElementById("Content");
        let Title = document.getElementById("Title");
        let Description = document.getElementById("Description");
       
        if(data.data.element){
            Content.value = data.data.element.Content;
            Title.value = data.data.element.Title;
            Description.value = data.data.element.Description;
        } else {
            Content.value = "";
            Title.value = "";
            Description.value = "";
        }
    });
};


/***
* Get the Id of element 
*/
TranslateForm.GetId = function(){
    return Form.GetId("TranslateFormForm");
};

/*
* Save the Element
*/
TranslateForm.SaveElement = function(e){
   
    if(Form.IsValid("TranslateFormForm"))
    {
    let request = new Http("Lang", "TranslateElement");
        request.SetData(Form.Serialize("TranslateFormForm"));

        request.Post(data => {

            data = JSON.parse(data);

            if(data.status == "error"){
                Form.RenderError("TranslateFormForm", data.data.message);
            } 
            else 
            {
                Animation.Notify(Language.GetCode("Lang.TranslateOk"));    
                Form.SetId("TranslateFormForm", data.data.Id);
            }
        });
    }
};

