<?php
session_start();
$id_map_sel = $_SESSION['id_map_sel'];
$icons_library = true;
if($user_info['role']=='editor') {
    $editor_permissions = get_editor_permissions($_SESSION['id_user'],$id_map_sel);
    if($editor_permissions['icons_library']==0) {
        $icons_library=false;
    }
}
if(isset($_SESSION['library_type'])) {
    $library_type = $_SESSION['library_type'];
} else {
    $library_type = 'map';
}
?>

<?php if(!$icons_library): ?>
    <div class="text-center">
        <div class="error mx-auto" data-text="401">401</div>
        <p class="lead text-gray-800 mb-5"><?php echo _("Permission denied"); ?></p>
        <p class="text-gray-500 mb-0"><?php echo _("It looks like you found a glitch in the matrix..."); ?></p>
        <a href="index.php?p=dashboard">← <?php echo _("Back to Dashboard"); ?></a>
    </div>
<?php die(); endif; ?>

<?php if($user_info['role']=='administrator') : ?>
    <div class="row mb-3">
        <div class="col-md-6">
            <button onclick="session_library('map');" class="btn btn-block <?php echo ($library_type=='map') ? 'btn-primary' : 'btn-outline-primary'; ?>"><?php echo _("Map Library"); ?></button>
        </div>
        <div class="col-md-6">
            <button onclick="session_library('public');" class="btn btn-block <?php echo ($library_type=='public') ? 'btn-primary' : 'btn-outline-primary'; ?>"><?php echo _("Public Library"); ?></button>
        </div>
    </div>
<?php endif; ?>

<?php if($library_type=='public') : ?>
    <div class="card bg-warning text-white shadow mb-4">
        <div class="card-body">
            <?php echo _("The contents of this library will be shared for all the maps."); ?>
        </div>
    </div>
<?php endif; ?>

<div class="row">
    <div class="col-md-12">
        <div class="card shadow mb-4">
            <div class="card-body">
                <form action="ajax/upload_icon_image.php" class="dropzone" id="gallery-dropzone"></form>
            </div>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-md-12">
        <div class="card shadow mb-4">
            <div class="card-header py-3">
                <h6 class="m-0 font-weight-bold text-primary"><i class="fas fa-grip-horizontal"></i> <?php echo _("Icons List"); ?></h6>
            </div>
            <div class="card-body">
                <div id="list_images">
                    <p><?php echo _("Loading icons"); ?> ...</p>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    (function($) {
        "use strict"; // Start of use strict
        window.id_user = '<?php echo $id_user; ?>';
        window.id_map = '<?php echo ($library_type=='map') ? $id_map_sel : ''; ?>';
        Dropzone.autoDiscover = false;
        $(document).ready(function () {
            get_icon_images(id_map);
            var gallery_dropzone = new Dropzone("#gallery-dropzone", {
                url: "ajax/upload_icon_image.php",
                parallelUploads: 1,
                maxFilesize: 20,
                timeout: 120000,
                acceptedFiles: 'image/*',
                dictDefaultMessage: `<?php echo _("Drop files or click here to upload"); ?>`,
                dictFallbackMessage: `<?php echo _("Your browser does not support drag'n'drop file uploads."); ?>`,
                dictFallbackText: `<?php echo _("Please use the fallback form below to upload your files like in the olden days."); ?>`,
                dictFileTooBig: `<?php echo sprintf(_("File is too big (%sMiB). Max filesize: %sMiB."),'{{filesize}}','{{maxFilesize}}'); ?>`,
                dictInvalidFileType: `<?php echo _("You can't upload files of this type."); ?>`,
                dictResponseError: `<?php echo sprintf(_("Server responded with %s code."),'{{statusCode}}'); ?>`,
                dictCancelUpload: `<?php echo _("Cancel upload"); ?>`,
                dictCancelUploadConfirmation: `<?php echo _("Are you sure you want to cancel this upload?"); ?>`,
                dictRemoveFile: `<?php echo _("Remove file"); ?>`,
                dictMaxFilesExceeded: `<?php echo _("You can not upload any more files."); ?>`,
            });
            gallery_dropzone.on("addedfile", function(file) {
                $('#list_images').addClass('disabled');
            });
            gallery_dropzone.on("success", function(file,rsp) {
                add_image_to_icon(id_map,rsp);
            });
            gallery_dropzone.on("queuecomplete", function() {
                $('#list_images').removeClass('disabled');
                gallery_dropzone.removeAllFiles();
            });
        });
    })(jQuery); // End of use strict
</script>