class Empresa extends globalClass {
    actual_logo;
    current_action;

    constructor() {
        super();
        this.setEndpoint('Administrador');
    }

    async CambiarLogo() {
        let jsonRQ = new jsonRequest(this.endpoint, 'cambiarLogo');
        const input_file = document.getElementById('logo_empresa')
        if (input_file && input_file.files && input_file.files[0]) {

            const new_logo = input_file.files[0];
            await getBase64(new_logo).then(data => {
                jsonRQ.add('image', data);
            })
            jsonRQ.addAuto(false, async () => {
                showLoading('Cambiando Logo. Espere un momento por favor...');
                await jsonRQ.makeServerQuery((response) => {
                    swal.close();
                    const {message} = response;
                    showMessageWithAction(message, 'success', undefined, undefined, () => {
                        showOrHideModal('change_img')
                        window.location.reload();
                    });
                });
            })
        }
    }

    getValuesFromRadio() {
        const radios = document.getElementsByName('flexRadioDefault');
        for (let i = 0; i < radios.length; i++) {
            if (radios[i].checked) {
                return radios[i].id;
            }
        }
        return -1;
    }

    async registrarse() {
        if (getCookie('token') != null) {
            delete_cookie('token');
            console.log("Borrando token")
        }
        if(!document.getElementById('terminos_condiciones')){
            showMessage('Ha ocurrido un error al procesar los términos y condiciones','error');
            return;
        }
        const email = document.getElementById('correo_administrador').value;
        if (!email) {
            showMessage("Ha ocurrido un error al obtener el correo.", 'error')
            return;
        }
        let jsonRQ = new jsonRequest('Empresa', 'createCompany');
        jsonRQ.addAuto(false, async () => {
            if(!document.getElementById('terminos_condiciones').checked){
                showMessage('Debe aceptar los términos y condiciones','error');
                return;
            }
            const formData = new FormData();
            const files = $('#logo')[0].files[0];
            if (files) {
                formData.append('files[0]', files);
            }
            formData.append('request', jsonRQ.printAsJsonStringify())
            try {
                showLoading('Cargando');
                await fetch(config['serverApi'], {
                    method: 'POST',
                    body: formData
                }).then(response => response.json()).then(data => {
                    console.log(data);
                    if (!data.status) {
                        showMessage(data.message, 'error');
                        return;
                    }
                    window.location.href = '../login/?register=1#activar_cuenta';
                    let jsonRQ = new jsonRequest('Membresias', 'crearMembresia');
                });

            } catch (error) {
                showMessage('Ha ocurrido un error, contactar con el soporte.', 'error')
                return;
            }
        });

    }

    async CambiarInformacionBasica() {
        const jsonRQ = new jsonRequest(this.endpoint, 'cambiarInformacionBasica');
        const pwd = document.getElementById('pwd').value;
        const telefono = document.getElementById('telefono').value;
        jsonRQ.add('telefono', telefono, 'string');
        jsonRQ.add('password', pwd, 'string');
        jsonRQ.addAuto(false, async () => {
            showLoading('Actualizando Información, Por favor espre...');
            await jsonRQ.makeServerQuery((response) => {
                swal.close();
                const {message} = response;
                showMessageWithAction(message, 'success', undefined, undefined, () => {
                    showOrHideModal('input_password')
                    window.location.reload();
                });
            });
        }, 'informacion_basica')

    }

}

let objEmpresa = new Empresa();