<?php
session_start();
$id_map_sel = $_SESSION['id_map_sel'];
$review = true;
if($user_info['role']=='editor') {
    $editor_permissions = get_editor_permissions($_SESSION['id_user'],$id_map_sel);
    if($editor_permissions['review']==0) {
        $review=false;
    }
}
if(isset($_GET['id_marker'])) {
    $id_marker = $_GET['id_marker'];
    $marker = get_marker($id_marker,$_SESSION['id_user']);
} else {
    $id_marker = 0;
}
?>

<?php if(!$review): ?>
    <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($id_marker!=0) : ?>
    <div class="alert alert-secondary" role="alert">
        <?php echo sprintf(_("You are currently viewing reviews for the marker <b>%s</b>. Click <a style='color:unset !important;font-weight: bold' href='index.php?p=reviews'>here</a> to see all reviews."),$marker['name']); ?>
    </div>
<?php endif; ?>

<div class="row mt-2">
    <div class="col-md-12">
        <div class="card shadow mb-4">
            <div class="card-body">
                <table class="table table-bordered table-hover" id="reviews_table" width="100%" cellspacing="0">
                    <thead>
                    <tr>
                        <th><?php echo _("Date"); ?></th>
                        <th><?php echo _("Marker"); ?></th>
                        <th><?php echo _("User"); ?></th>
                        <th><?php echo _("Rating"); ?></th>
                        <th><?php echo _("Comment"); ?></th>
                        <th><?php echo _("Reply"); ?></th>
                        <th></th>
                    </tr>
                    </thead>
                    <tbody>

                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<div id="modal_reply" class="modal" tabindex="-1" role="dialog" data-backdrop="static">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title"><?php echo _("Reply to the review"); ?></h5>
            </div>
            <div class="modal-body">
                <div class="row">
                    <div class="col-md-12">
                        <b><?php echo "Comment"; ?></b>: <i id="comment_modal"></i>
                    </div>
                    <div class="col-md-12 mt-4">
                        <b><?php echo "Reply"; ?></b>:
                        <textarea id="reply_modal" rows="4" class="form-control"></textarea>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button <?php echo ($demo) ? 'disabled':''; ?> onclick="save_reply_review();" type="button" class="btn btn-success"><i class="fas fa-save"></i>&nbsp;&nbsp;<?php echo _("Save"); ?></button>
                <button type="button" class="btn btn-secondary" data-dismiss="modal"><?php echo _("Close"); ?></button>
            </div>
        </div>
    </div>
</div>

<script>
    (function($) {
        "use strict";
        window.id_user = '<?php echo $id_user; ?>';
        window.id_map = '<?php echo $id_map_sel; ?>';
        window.id_review_sel = null;
        var id_marker_sel = <?php echo $id_marker; ?>;
        $(document).ready(function () {
            $('#reviews_table').DataTable({
                "columnDefs": [{
                    targets: '_all',
                    className: 'dt-head-left dt-body-left'
                },{
                    targets: [1],
                    visible: (id_marker_sel!=0) ? false : true,
                    searchable: (id_marker_sel!=0) ? false : true
                }],
                "order": [[ 0, "desc" ]],
                "stateSave": false,
                "responsive": true,
                "scrollX": true,
                "processing": true,
                "serverSide": true,
                "ajax": "ajax/get_reviews.php?id_map="+window.id_map+'&id_marker='+id_marker_sel,
                "language": {
                    "decimal":        "",
                    "emptyTable":     "<?php echo _("No data available in table"); ?>",
                    "info":           "<?php echo sprintf(_("Showing %s to %s of %s entries"),'_START_','_END_','_TOTAL_'); ?>",
                    "infoEmpty":      "<?php echo _("Showing 0 to 0 of 0 entries"); ?>",
                    "infoFiltered":   "<?php echo sprintf(_("(filtered from %s total entries)"),'_MAX_'); ?>",
                    "infoPostFix":    "",
                    "thousands":      ",",
                    "lengthMenu":     "<?php echo sprintf(_("Show %s entries"),'_MENU_'); ?>",
                    "loadingRecords": "<?php echo _("Loading"); ?>...",
                    "processing":     "<?php echo _("Processing"); ?>...",
                    "search":         "<?php echo _("Search"); ?>:",
                    "zeroRecords":    "<?php echo _("No matching records found"); ?>",
                    "paginate": {
                        "first":      "<?php echo _("First"); ?>",
                        "last":       "<?php echo _("Last"); ?>",
                        "next":       "<?php echo _("Next"); ?>",
                        "previous":   "<?php echo _("Previous"); ?>"
                    },
                    "aria": {
                        "sortAscending":  ": <?php echo _("activate to sort column ascending"); ?>",
                        "sortDescending": ": <?php echo _("activate to sort column descending"); ?>"
                    }
                }
            });
        });
    })(jQuery);
</script>