var Schema = {
    timeout: 0,
    ajax_request: null,
    init: function () {
        let tipo = document.getElementById('data_type_art').value;
        $('.'+tipo).show();

        $('#data_type_art').change(function(){
            $('.schema').hide();
            $('.'+this.value).show();
        })

        // descartar edición de datos de config.
        $('.descartar_schema').click(function () {
            window.location.reload();
            return false;
        });
        $('.guardar_schema').click(function(e) {
            e.stopPropagation();
            showModalConfirm({
                'title': 'Guardar Configuración',
                'msg': 'Estás seguro de modificar la configuración?',
                type: "orange",
                icon: "glyphicon glyphicon-exclamation-sign",
                btnClassOk: 'hovwhite radius15 noborder txtnextwhite pad-top-bot7 pad-lef-rig20 int-nextred nextred',
                okTitle: 'Guardar',
                cbBtnOK: function() {
                    Schema.guardarCfg();
                }
            });
            return false;
        });
    },

    guardarCfg: function() {
        let form = document.querySelector('#schema-form');
        let formData = new FormData(form);

        let data = new Object();
        for (let entry of formData) {
            data[entry[0]] = entry[1];
        }
        $.ajax({
            url: '/' + Admin.dir_cgi_cpn + '/seo/prontus_schema_save.cgi',
            type: 'POST',
            dataType: 'json',
            data: data,
            success: function (response) {
                if (response.status == 0) {
                    showModalAlert({
                        title: 'Error',
                        type: 'red',
                        msg: response.msg
                    });
                } else {
                    showModalAlert({
                        title: 'Mensaje del servidor',
                        type: 'green',
                        msg: response.msg
                    });
                }
            },
            error: function(_xhr, _ajaxOptions, thrownError) {
                if (thrownError != 'abort') {
                    showModalAlert({
                        title: 'Error',
                        type: 'red',
                        msg: thrownError
                    });
                }
            }
        });

    }
};

$(document).ready(function () {
    Schema.init();
});
