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

<?php if(!$media_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 that you do not have permission to access this page"); ?></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-header py-3">
                <h6 class="m-0 font-weight-bold text-primary"><i class="fas fa-grip-horizontal"></i> <?php echo _("Files List"); ?></h6>
            </div>
            <div class="card-body">
                <form action="ajax/upload_media_library_file.php" class="dropzone mb-3 noselect <?php echo ($demo) ? 'disabled' : ''; ?>" id="media-dropzone"></form>
                <div id="list_files">
                    <p><?php echo _("Loading files ..."); ?></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 : ''; ?>';
        window.media_library_files = [];
        Dropzone.autoDiscover = false;
        $(document).ready(function () {
            get_media_library_files(id_map);
            var media_files_dropzone = new Dropzone("#media-dropzone", {
                url: "ajax/upload_media_library_file.php",
                parallelUploads: 1,
                maxFilesize: <?php echo $max_file_size_upload; ?>,
                timeout: 120000,
                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."); ?>`,
                acceptedFiles: 'image/*,video/mp4,video/webm'
            });
            media_files_dropzone.on("addedfile", function(file) {
                $('#list_files').addClass('disabled');
            });
            media_files_dropzone.on("success", function(file,rsp) {
                if(rsp !== "" && !rsp.startsWith("ERROR")) {
                    add_file_to_media_library(id_map, rsp);
                }
            });
            media_files_dropzone.on("queuecomplete", function() {
                $('#list_files').removeClass('disabled');
                media_files_dropzone.removeAllFiles();
            });
        });
    })(jQuery); // End of use strict
</script>