<?php
$page = (!empty($_GET['page-id'])) ? $_GET['page-id'] : 1;
$filter_keyword = (!empty($_GET['query'])) ? Secure($_GET['query']) : '';
$db->pageLimit  = 50;

if (!empty($filter_keyword)) {
    $sql   = "(
        `id`           = ". (int)$filter_keyword ." OR
        `track_id`     LIKE '%$filter_keyword%' OR
        `description`  LIKE '%$filter_keyword%'
    )";
    $albums = $db->objectbuilder()->where($sql)->orderBy('time', 'DESC')->paginate(T_REVIEWS, $page);
} else {
    $albums = $db->objectbuilder()->orderBy('time', 'DESC')->paginate(T_REVIEWS, $page);
}

if (($page > $db->totalPages) && !empty($_GET['page-id'])) {
    header("Location: " . LoadAdminLinkSettings('manage-track-reviews'));
    exit();
}
?>
<div class="container-fluid">
    <div class="block-header">
        <h2>Songs > Manage Song Reviews</h2>
    </div>
    <div class="row">
        <div class="col-lg-12 col-md-12">
            <div class="card">
                <div class="body">

                    <div class="row">
                        <div class="col-md-12">
                            <div class="alert alert-success site-settings-alert"></div>
                        </div>
                        <div class="col-md-4" style="margin-bottom:0;">
                            <form method="get" action="<?php echo LoadAdminLinkSettings('manage-track-reviews'); ?>">
                                <div class="row">
                                    <div class="col-md-8">
                                        <div class="form-group form-float">
                                            <div class="form-line">
                                                <input type="text" name="query" id="query" class="form-control" value="<?php echo $filter_keyword;?>">
                                                <label class="form-label search-form">
                                                    Search for review.
                                                </label>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-4">
                                        <button class="btn btn-info">Search</button>
                                        <a class="btn btn-default" href="<?php echo LoadAdminLinkSettings('manage-track-reviews');?>">Reset</a>
                                    </div>
                                </div>
                                <div class="clearfix"></div>
                            </form>
                        </div>
                    </div>
                    <div class="clearfix"></div>

                    <div class="table-responsive1">
                        <table class="table table-bordered table-striped table-hover">
                            <thead>
                            <tr>
                                <th style="width: 5%;">ID</th>
                                <th style="width: 7%;">Song</th>
                                <th style="width: 13%;">Artist / User</th>
                                <th style="width: 20%;">Review</th>
                                <th style="width: 10%;">Rate</th>
                                <th style="width: 10%;">Published On</th>
                                <th style="width: 15%;">Action</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php
                                foreach ($albums as $key => $value) {
                                    $song = songData($value->track_id);
                                    $user = userData($value->user_id);
                                    echo LoadAdminPage('manage-track-reviews/list',['SONG_DATA' => $song,'USER_DATA' => $user,'RATE' => $value->rate,'TM' => $value->time,'DESC' => $value->description,'ID' => $value->id]);
                                }
                            ?>
                            </tbody>
                        </table>
                        <div class="pull-left">
                            <?php echo "Showing $page out of " . $db->totalPages; ?>
                        </div>
                        <div class="pull-right">
                            <nav>
                                <ul class="pagination">
                                    <li>
                                        <a href="<?php echo LoadAdminLinkSettings('manage-track-reviews?page-id=1'); ?>" class="waves-effect" title='First Page'>
                                            <i class="material-icons">first_page</i>
                                        </a>
                                    </li>
                                    <?php if ($page > 1) {  ?>
                                    <li>
                                        <a href="<?php echo LoadAdminLinkSettings('manage-track-reviews?page-id=' . ($page - 1)); ?>" class="waves-effect" title='Previous Page'>
                                            <i class="material-icons">chevron_left</i>
                                        </a>
                                    </li>
                                    <?php  } ?>
                                    <?php
                                $nums = 0;
                                $nums_pages = ($page > 4) ? ($page - 4) : $page;
                                    for ($i=$nums_pages; $i <= $db->totalPages; $i++) {
                                    if ($nums < 20) {
                                    ?>
                                    <li class="<?php echo ($page == $i) ? 'active' : ''; ?>"><a href="<?php echo LoadAdminLinkSettings('manage-track-reviews?page-id=' . ($i)); ?>" class="waves-effect"><?php echo $i ?></a></li>
                                    <?php
                                	}
                                	$nums++;
                                }
                                ?>
                                    <?php if ($db->totalPages > $page) { ?>
                                    <li>
                                        <a href="<?php echo LoadAdminLinkSettings('manage-track-reviews?page-id=' . ($page + 1)); ?>" class="waves-effect" title="Next Page">
                                            <i class="material-icons">chevron_right</i>
                                        </a>
                                    </li>
                                    <?php } ?>
                                    <li>
                                        <a href="<?php echo LoadAdminLinkSettings('manage-track-reviews?page-id=' . ($db->totalPages)); ?>" class="waves-effect" title='Last Page'>
                                            <i class="material-icons">last_page</i>
                                        </a>
                                    </li>
                                </ul>
                            </nav>
                        </div>
                        <div class="clearfix"></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="clearfix"></div>
    </div>
</div>

<!-- #END# Vertical Layout -->
<div id="delete-modal" class="modal fade" role="dialog" data-id="">
    <div class="modal-dialog">
        <div class="modal-content modal-col-red">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">DELETE REVIEW</h4>
            </div>
            <div class="modal-body">
                <p>Are you sure you want to continue? this action can't be undo</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-link waves-effect delete-review-button">YES</button>
                <button type="button" class="btn btn-link waves-effect" data-dismiss="modal">CLOSE</button>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.btn-delete-album').on('click', function (event) {
            event.preventDefault();
            $('#delete-modal').attr('data-id', $(this).attr('data-id')).modal('show');
        });

        $('.delete-review-button').on('click', function (event) {
            event.preventDefault();
            let id = $('#delete-modal').attr('data-id');
            $.get('{{CONFIG ajax_url}}/delete-review', {id: id}, function( data ) {
                if(data.status == 200){
                    location.reload();
                }
            });
        });

    });
</script>