$(document).ready(function() {
    $('#modaltablaseleccion').DataTable({
        "paging"      : true,
        "pageLength"  : 5,
        "lengthChange": false,
        "ordering"    : true,
        "info"        : false,
        "autoWidth"   : true,
        "orderMulti"  : true,
        "responsive"  : true,
        "dom"         : '<"search"f><"top"l>rt<"bottom"ip><"clear">',
        "language": {
            "search": "Buscar",
            "emptyTable"  : "No hay registros",
        }
    });
    $('form[name="DeleteConfirmation"]').on("click", function (e){
        e.preventDefault();
        var $form = $(this);
        //console.log($form)
        $("#DeleteModalConfirmation").modal({ backdrop: "static", keyboard: false })
            .on("click", ".btn-delete-confirmation", function() {
                $form.submit();
        });
    });

    $('form[name="LostConfirmation"]').on("click", function (e){
        e.preventDefault();
        var $form = $(this);
        console.log($form)
        $("#LostModalConfirmation").modal({ backdrop: "static", keyboard: false })
            .on("click", ".btn-lost-confirmation", function() {
                $form.submit();
        });
    });
    
    function errorHandlerFileLoad(evt)
    {
        switch (evt.target.error.code) {
            case evt.target.error.NOT_FOUND_ERR:
                console.log("File Not Found!");
                break;
            case evt.target.error.NOT_READABLE_ERR:
                console.log("File is not readable");
                break;
            case evt.target.error.ABORT_ERR:
                break;
            default:
                console.log("An error occurred reading this file.");
        }
    }
    function handleFileLoad(evt) {
        let el = evt.srcElement.getAttribute('name')
        let elementCheck = $(evt.srcElement).closest('.form-group').find('span.'+el+'-success')[0]
        let imageTag = $(evt.srcElement).closest('.card-body').find('img.'+el+'-preview')[0]
        let elementFail = $(evt.srcElement).closest('.card-body').find('.'+el+'-fail')[0]
        $(elementFail).children("p").text("")
        if (!window.FileReader)
        {
            $(elementFail).children("p").text("The browser does not support image loading, try another browser")
            $(elementFail).toggle()
            return
        }
        let files = evt.target.files
        if (files && files[0])
        {
            let f = files[0]
            if (!f.type.match("image.*")) {
                $(elementFail).children("p").text("The file is not an image, please try uploading an image")
                $(elementFail).toggle()
                return
            }
            let fileReader = new FileReader()
            fileReader.onerror = errorHandlerFileLoad
            fileReader.onload = function(evt)
            {
                elementCheck.classList.remove("invisible")
                elementCheck.classList.add('visible')
                imageTag.src = evt.target.result
            }
            fileReader.readAsDataURL(f)
        }
    }
    function handleFilePDFLoad(evt) {
        let el = evt.srcElement.getAttribute('name')
        let elementCheck = $(evt.srcElement).closest('.form-group').find('span.'+el+'-success')[0]
        let pdfTag = $(evt.srcElement).closest('.card-body').find('pdf.'+el+'-preview')[0]
        let elementFail = $(evt.srcElement).closest('.card-body').find('.'+el+'-fail')[0]
        $(elementFail).children("p").text("")
        if (!window.FileReader)
        {
            $(elementFail).children("p").text("The browser does not support uploading pdf files, please try another browser")
            $(elementFail).toggle()
            return
        }
        let files = evt.target.files
        if (files && files[0])
        {
            let f = files[0]
            if (!f.type.match("pdf.*")) {
                $(elementFail).children("p").text("The file is not a pdf, please try to upload a valid pdf file")
                $(elementFail).toggle()
                return
            }
            let fileReader = new FileReader()
            fileReader.onerror = errorHandlerFileLoad
            fileReader.onload = function(evt)
            {
                elementCheck.classList.remove("invisible")
                elementCheck.classList.add('visible')
                /*pdfTag.src = evt.target.result*/
            }
            fileReader.readAsDataURL(f)
        }
    }
    
    let elements = document.getElementsByClassName("upload-image")
    for (let i = 0; i < elements.length; i++) {
        elements[i].addEventListener('change', handleFileLoad);
    }

    let pdfelements = document.getElementsByClassName("upload-pdf")
    for (let i = 0; i < pdfelements.length; i++) {
        pdfelements[i].addEventListener('change', handleFilePDFLoad);
    }
    
});
$('.btn_selectmodalinformation').click(function () {
    let hiddenId = $(this).data('hidden')
    let input = $(this).data('input')
    let formName = $(this).data('formname')
    let origin = $(this).data('origin')
    $('form[name="' + formName + '"] input[name="' + hiddenId + '"]').val($(this).data('id'))
    $('form[name="' + formName + '"] input[name="' + input + '"]').val($(this).data('name'))
    $(origin).modal('toggle')
});