var ImmoProjectForm = function(){};

/***
 * Init the form
 * @returns {undefined}
 */
ImmoProjectForm.Init = function(){
    Event.AddById("btnSaveProject", "click", ImmoProjectForm.SaveElement);
    Event.AddByClass("removeUser", "click", ImmoProjectForm.RemoveUser);
    Event.AddByClass("btnAddAnnonceToProjet", "click", ImmoProjectForm.AddAnnoceToProject);
    Event.AddByClass("removeAnnonce", "click", ImmoProjectForm.RemoveAnnonce);
   
};

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

/*
* Save the Element
*/
ImmoProjectForm.SaveElement = function(e){
   
    if(Form.IsValid("ImmoProjectFormForm"))
    {
    let request = new Http("Immo", "SaveProject");
        request.SetData(Form.Serialize("ImmoProjectFormForm"));

        request.Post(data => {

            data = JSON.parse(data);

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

                Immo.ReloadProject(data.data);    
                Dialog.Close();
            }
        });
    }
};

/***
 * Select a user
 */
ImmoProjectForm.SelectUser = function(e){
    let selectUser = document.querySelectorAll("#divResult input[type=checkbox]");
    let lstNewUser = document.getElementById("lstNewUser");
    let userId = Array();

    for(let i = 0; i < selectUser.length; i++){

        if(selectUser[i].checked){
          userId.push(selectUser[i].id);
          lstNewUser.innerHTML += "<span class='chips'>" + selectUser[i].name+ "</span>";
        }
    }

    Form.SetValue( "ImmoProjectFormForm", "UserIds", userId.join(","));

    let divResult = document.getElementById("divResult");
    if (divResult != undefined) {
        divResult.parentNode.removeChild(divResult);
    }
};

/***
 * Remove user to a project
 */
ImmoProjectForm.RemoveUser = function(e){

    let element = e.srcElement;
    let userProjetId = element.id;

    let request = new Http("Immo", "RemoveUserProjet");
    request.Add("UserProjetId", userProjetId);

    request.Post(data => {
        element.parentNode.parentNode.removeChild(element.parentNode);
    });
};

/****
 * Ajotue une annocne au projet 
 * @returns {undefined}
 */
ImmoProjectForm.AddAnnoceToProject = function(){
    
    let annonceId = document.getElementById("lstAnnonce").value;
    let projetId = Form.GetId("ImmoProjectFormForm");
    
    let request = new Http("Immo", "AddAnnonceToProjet");
    request.Add("projetId", projetId);
    request.Add("annonceId", annonceId);

    request.Post(data => {

        let lstAnnonce = document.getElementById("lstAnnonce");
        lstAnnonce.innerHTML += "<div class='chips'>"+annonceId+"</div>" +
        "<i class='fa fa-trash removeAnnonce'  id='"+annonceId+"'></i></div>";
    });
};

/***
 * Supprime une annonce
 */
ImmoProjectForm.RemoveAnnonce = function(){
    let element = e.srcElement;
    let container = e.srcElemnt.parentNode;
    let request = new Http("Immo", "RemoveAnnonce");

    request.Add("annonceId", element.id);

    request.Post(data => {

        container.parentNode.RemoveChild(container);
    });
};