var ListAnnonce = function(){};

/***
 * Init the form
 * @returns {undefined}
 */
ListAnnonce.Init = function(){
    Plugin.Register('CaracteristicPlugin', 'CaracteristicPlugin', function(){ CaracteristicPlugin.InitFilter(ListAnnonce.Filter); });
    Event.AddById("btnSave", "click", ListAnnonce.SaveElement);
    
    Plugin.Register('AlertPlugin', 'AlertPlugin', function(){ AlertPlugin.Init("filterContainer"); });
};

ListAnnonce.Filter = function(caracteristiqueFilter){
    let request = new Http("Annonce", "Search");
    request.SetData(caracteristiqueFilter);

    request.Post(function(data){
        data = JSON.parse(data);
        
        if(data.data != false){
            Animation.Hide("noResultAnnonce");
            View.Bind(  "lstAnnonce"  , data.data);
        } else {
            View.Bind(  "lstAnnonce"  , []);
            Animation.Show("noResultAnnonce");
        }
    });
};

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

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

        request.Post(data => {

            data = JSON.parse(data);

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

                Form.SetId("ListAnnonceForm", data.data.Id);
            }
        });
    }
};

