class Direccionmodal {
    constructor(modal, saveCallback) {
        this.modal = modal;
        this.saveCallback = saveCallback;
        
        this.alias = $(this.modal.find('#add_alias'));
        this.att = $(this.modal.find('#add_att'));
        this.email = $(this.modal.find('#add_email'));
        this.direccion = $(this.modal.find('#add_direccion'));
        this.pais = $(this.modal.find('#add_pais_id'));
        this.municipio = $(this.modal.find('#add_municipio'));
        this.provincia = $(this.modal.find('#add_provincia'));
        this.cp = $(this.modal.find('#add_cp'));
        this.telefono = $(this.modal.find('#add_telefono'));

        this.btnCancel = $(this.modal.find('#cancelAdd'));
        this.btnSave = $(this.modal.find('#saveAdd'));

        this.error = $(this.modal.find('.texto-error'));
    }

    init() {
        this.btnCancel.on('click', () => {
            
            this.modal.modal("hide");
        });

        this.modal.on('hidden.bs.modal', () => {
            this.error.addClass('d-none');
            this.alias.val("");
            this.att.val("");
            this.email.val("");
            this.direccion.val("");
            this.pais.val("").trigger('change');
            this.municipio.val("");
            this.provincia.val("");
            this.cp.val("");
            this.telefono.val("");
            this.btnSave.off('click');
        });

        this.pais.on('change',  () => {
            var nombre_pais = $("#add_pais_id option:selected").text().trim();
            if (nombre_pais.localeCompare('España') == 0) {
                $('#divPais').removeClass('col-lg-12').addClass('col-lg-6')
                $('#divMunicipio').removeClass('col-lg-12').addClass('col-lg-6')
                $('.spain-data').css('display', 'inline')
            }
            else {
                $('.spain-data').css('display', 'none')
                $('#divPais').removeClass('col-lg-6').addClass('col-lg-12')
                $('#divMunicipio').removeClass('col-lg-6').addClass('col-lg-12')
            }
        });

        this.btnSave.on('click', () => {
            if (this.#validatemodal()) {
                this.saveCallback();
            }
            else {
                $(this.modal.find('.texto-error')).removeClass('d-none');
            }

        });

        this.modal.modal('show');
    }

    #validatemodal() {

        if(this.alias.val() == "" || this.att.val() == "" || this.direccion.val() == ""  || this.municipio.val() == "" || this.cp.val() == "" ){
            return false;
        }
        if(this.pais.children("option:selected").val() < 1)
            return false;
        return true;
    }

    getData() {
        return {
            alias: this.alias.val(),
            att: this.att.val(),
            email: this.email.val(),
            direccion: this.direccion.val(),
            pais_id: this.pais.children("option:selected").val(),
            municipio: this.municipio.val(),
            provincia: this.provincia.val(),
            cp: this.cp.val(),
            telefono: this.telefono.val()
        }
    }
}

export default Direccionmodal;