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"); });
    Plugin.Register('NoResultPlugin', 'NoResultPlugin', function(){ NoResultPlugin.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.Annonces != false){
            Animation.Hide("noResultAnnonce");
            View.Bind(  "lstAnnonce"  , data.data.Annonces);
        } else {
            View.Bind(  "lstAnnonce"  , []);
            Animation.Show("noResultAnnonce");
            
            Plugin.NoResultPlugin.ShowInfo(caracteristiqueFilter);
        }
    });
};

/***
* 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);
            }
        });
    }
};

