var ReponseForm = function(){};

/***
 * Init the form
 * @returns {undefined}
 */
ReponseForm.Init = function(){
    Event.AddById("btnSendReponse", "click", ReponseForm.SaveReponse);

    NewMessageForm.txtEditor = new TextRichEditor( Dom.GetById("Message"));
};

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

/**
 * Get the MessageId (sujet id)
 */
ReponseForm.GetMessageId = function(){
    return document.getElementById("MessageId").value;
};

/*
* Save the reponse
*/
ReponseForm.SaveReponse = function(e){
   
    Animation.AddLoading("ReponseFormForm");
    Animation.Hide("ReponseFormForm");

    if(Form.IsValid("ReponseFormForm"))
    {

        let formData = Form.Serialize("ReponseFormForm");
        
        if(!formData.includes("MessageId=")) {
            const messageId = ReponseForm.GetMessageId();
            formData += "&MessageId=" + messageId;
        }
        
        let request = new Http("Forum", "SendReponse");
        request.SetData(formData);

        request.Post(data => {

            Animation.RemoveLoading("ReponseFormForm");
            Animation.Show("ReponseFormForm");

            data = JSON.parse(data);

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

                Animation.Load("ReponseFormForm", Language.GetCode("Forum.ResponseAdded"));

                Form.SetId("ReponseFormForm", "");
                Form.ClearFields("ReponseFormForm");
                Form.RenderSuccess("ReponseFormForm", "Réponse envoyée avec succès");
                
                if(typeof ForumMessage !== 'undefined' && ForumMessage.RefreshReponses) {
                    ForumMessage.RefreshReponses(ReponseForm.GetMessageId());
                }
            }
        });
    }
};

/**
 * Charge une réponse existante pour édition
 */
ReponseForm.LoadReponse = function(reponseId) {
    let request = new Http("Forum", "GetReponse");
    request.SetData({reponseId: reponseId});
    
    request.Post(data => {
        data = JSON.parse(data);
        
        if(data.status !== "error") {
            Form.Load("ReponseFormForm", data.data);
        }
    });
};

/**
 * Met à jour une réponse existante
 */
ReponseForm.UpdateReponse = function(e) {
    if(Form.IsValid("ReponseFormForm")) {
        let request = new Http("Forum", "UpdateReponse");
        request.SetData(Form.Serialize("ReponseFormForm"));
        
        request.Post(data => {
            data = JSON.parse(data);
            
            if(data.status == "error") {
                Form.RenderError("ReponseFormForm", data.message);
            } else {
                Form.RenderSuccess("ReponseFormForm", "Réponse mise à jour avec succès");
                
                if(typeof ForumMessage !== 'undefined' && ForumMessage.RefreshReponses) {
                    ForumMessage.RefreshReponses(ReponseForm.GetMessageId());
                }
            }
        });
    }
};

