var DownloadManagerEmailDownloadForm = function () {};

/***
 * Initialise le formulaire de téléchargement avec email
 */
DownloadManagerEmailDownloadForm.Init = function () {
    Event.AddById("btnDownload", "click", DownloadManagerEmailDownloadForm.Download);
};

/***
 * Lance le téléchargement après validation de l'email
 */
DownloadManagerEmailDownloadForm.Download = function (e) {

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


        var data = "Class=DownloadManager&Methode=SaveEmailAndDownload&App=DownloadManager";
         data += Form.Serialize("DownloadManagerEmailDownloadForm");

        Request.Post("Ajax.php", data).then(result => {

            result = JSON.parse(result);
            
            Animation.Notify(Language.GetCode("DownloadManager.DownloadStarted"));
            
            if(result.status == "error"){
                Form.RenderError("DownloadManagerEmailDownloadForm", result.data.message);
            } else{

                Dialog.Close();
                window.location.href = document.location.origin + "/DownloadManager/DownloadFile/"+  result.data.token;
            }
        });
    }
};
