var ImmoEstimateForm = function () {};


/***
 * Add Event
 * @returns {undefined}
 */
ImmoEstimateForm.Init = function () {
    Event.AddById("btnStartEstimate", "click", ImmoEstimateForm.Estimate);
    Event.AddById("btnSendContactForm", "click", ImmoEstimateForm.SendContact);
    Event.AddById("btnSendCalledBackForm", "click", ImmoEstimateForm.SendCalledBack);
    Event.AddById("btnStartFind", "click", ImmoEstimateForm.FindProperty);
};

/***
 * Init Front
 */
ImmoEstimateForm.InitFront = function (app) {
    Event.AddByClass("immoLeadPicto", "click", function (e) {

        Dialog.open('', {"title": e.srcElement.title,
            "app": app,
            "class": "Dialog" + app,
            "method": "ShowActionUser",
            "params": e.srcElement.dataset.type
        });
    });
};

/*
 * Lancement de l'estimation 
 */
ImmoEstimateForm.Estimate = function () {

    Animation.Hide("#btnStartEstimate");
    Animation.AddLoading("estimateBlock");
    Animation.Hide("#estimateError");

    let descriptionProperty = Dom.GetById("descriptionProperty");

    if (descriptionProperty.value != "") {
        
        let request = new Http("Immo", "Estimate");
        request.Add("Estimate", descriptionProperty.value);

        request.Post(data => {


            Animation.RemoveLoading("estimateBlock");
            data = JSON.parse(data);

            if (data.status == "error") {

                Animation.Show("#estimateError");
                Animation.Show("#btnStartEstimate");

                if (data.data.length == 0) {
                    Animation.Load("estimateError", Language.GetCode("ImmoEstimateForm.NeedInformation") + " : Type de bien, surface, localisation");
                } else {
                    Animation.Load("estimateError", Language.GetCode("ImmoEstimateForm.NeedInformation") + " :" + data.data.join(","));
                }
            } else {

                Animation.Hide("#estimateBlock");
                Animation.Load("estimatePrice", data.data.prix_estime + "&euro;");
                Animation.Load("estimateDescription", data.data.explication);
                Animation.Show("#resultEstimateBlock");
                Animation.Show("#contactBlock");
            }

        });
    } else {
        Animation.Show("#estimateError");
        Animation.Show("#btnStartEstimate");
        Animation.RemoveLoading("estimateBlock");
        Animation.Load("estimateError", Language.GetCode("ImmoEstimateForm.YouMustEnterADescription"));
    }
};

/***
 * Demande de contact Estimation
 */
ImmoEstimateForm.SendContact = function () {

    if (Form.IsValid("contactForm"))
    {
        Animation.AddLoading("contactForm");
        Animation.Hide("contactForm");
               
        let request = new Http("Crm", "SendContact");
        
        contactForm = Form.Serialize("contactForm");
        contactForm += "&Description="+  Dom.GetById("descriptionProperty").value + ":" + Dom.GetById("estimateDescription").innerHTML;
        
        request.SetData(contactForm);

        request.Post(data => {

            Animation.RemoveLoading("contactForm");
            Animation.Show("contactForm");
            Animation.Load("contactForm", Language.GetCode("ImmoEstimateForm.ContactFormRecevied"));
        });
    }
};

/***
 * Envoie d'une demande de rappel
 * @returns {undefined}
 */
ImmoEstimateForm.SendCalledBack = function () {


    if (Form.IsValid("calledBackForm"))
    {
        Animation.AddLoading("calledBackForm");
        Animation.Hide("calledBackForm");

        let request = new Http("Crm", "SendContact");
        
        contactForm = Form.Serialize("calledBackForm");
        contactForm += "&Description="+  Dom.GetById("descriptionProperty").value + ":" + Dom.GetById("estimateDescription").innerHTML;
        
        request.SetData(contactForm);

        request.Post(data => {
            
            Animation.RemoveLoading("calledBackForm");
            Animation.Show("calledBackForm");
            Animation.Load("calledBackForm", Language.GetCode("ImmoEstimateForm.CalledBackFormReceveid"));
        });
    }
};

/***
 * Rechecher un bien
 * @returns {undefined}
 */
ImmoEstimateForm.FindProperty = function () {

    Animation.Hide("btnStartFind");
    Animation.AddLoading("findPropertyBlock");

    let descriptionProperty = Dom.GetById("descriptionProperty");

    if (descriptionProperty.value != "") {

        Plugin.SendRequest({"plugin": "ImmoEstimateForm",
            "method": "FindProperty",
            "description": descriptionProperty.value}, (data) => {

            Animation.RemoveLoading("findPropertyBlock");
            data = JSON.parse(data);

            if (data.status == "error") {

                Animation.Show("#findPropertyError");
                Animation.Show("#btnStartFind");

                if (data.data.length == 0) {
                    Animation.Load("findPropertyError", Language.GetCode("ImmoEstimateForm.NeedInformation") + " : Type de bien, surface, localisation");
                } else {
                    Animation.Load("findPropertyError", Language.GetCode("ImmoEstimateForm.NeedInformation") + " :" + data.data.join(","));
                }
            } else {

                Animation.Hide("#findPropertyBlock");
                // Animation.Load("estimatePrice", data.data.prix_estime + "&euro;");
                // Animation.Load("estimateDescription", data.data.explication);
                Animation.Show("#resultEstimateBlock");
                Animation.Show("#contactBlock");
            }
        });
    } else {
        Animation.Show("findPropertyError");
        Animation.Show("#btnStartFind");
        Animation.RemoveLoading("findPropertyBlock");
        Animation.Load("findPropertyError", Language.GetCode("ImmoEstimateForm.YouMustEnterADescription"));
    }
};