import Animate from '../tools/effects/Animate.jsx';
import TextEffects from '../tools/effects/TextEffects.jsx';

class Mailer {
    //--------------------------
    // constructor
    //--------------------------
    constructor(element) {
        this.response = element
        if (document.getElementById('request-btn')) {
            document.getElementById('request-btn').addEventListener("click", f => {
                f.preventDefault();
                this.sendMail()
            })
        }
    }
    //--------------------------
    // methods
    //--------------------------
    sendMail()
    {

        var self = this;
        //if(!this.validateForm())
        var mailData = []

        let requestForm = document.forms.namedItem("request-form");
        let serviceType = document.getElementById('service_type')
        //console.log(serviceType.options[serviceType.selectedIndex].text);

        mailData = {
            "email": requestForm.email.value,
            "client": requestForm.client.value,
            "description": requestForm.description.value,
            "type": serviceType.options[serviceType.selectedIndex].text

        };

        var request = new XMLHttpRequest();
        request.open("POST", "/mailer", true);
        request.setRequestHeader("Content-type", "application/json")
        request.onload = function(oEvent) {
            if (request.status == 200) {
                let response = JSON.parse(request.response);
                if (response.message == "message sent") {

                    requestForm.reset();
                    self.response.innerHTML = "HEY. THANKS FOR REACHING OUT. YOU'RE AWESOME"
                    new TextEffects().scramble(self.response, 50, function () {

                    });

                } else {
                    //console.log(response.detail);
                }

            } else {
                //console.log(request);
            }
        };
        request.send(JSON.stringify(mailData));

    }

    //--------------------------
    // event handlers
    //--------------------------
}
export {Mailer as default}
