<?php
UpdateSeenReports();
$page = (!empty($_GET['page-id'])) ? $_GET['page-id'] : 1;
$db->pageLimit  = 50;
$users = $db->objectbuilder()->where('ignored',0)->orderBy('id', 'DESC')->paginate(T_REPORTS, $page);

if (($page > $db->totalPages) && !empty($_GET['page-id'])) {
    header("Location: " . LoadAdminLinkSettings('manage-reports'));
    exit();
}
?>
<div class="container-fluid">
    <div class="block-header">
        <h2>Users > Manage Reports</h2>
    </div>
    <!-- Vertical Layout -->
    <div class="row">
        <div class="col-lg-12 col-md-12">
            <div class="card">
                <div class="header">
                    <h2>Manage Reports</h2>
                </div>
                <div class="body">
                    <div class="clearfix"></div>
                    <div class="table-responsive1">
                        <table class="table table-bordered table-striped table-hover">
                            <thead>
                            <tr>
                                <th>ID</th>
                                <th>Song | Article</th>
                                <th>Reporter</th>
                                <th>Report text</th>
                                <th>Report Time</th>
                                <th>Action</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php
                                foreach ($users as $key => $user) {
                                    $user_data = UserData($user->user_id);
                                    $url = '';
                                    $type = 'Track';
                                    $track = null;
                                    if($user->track_id > 0 && $user->comment_id == 0){
                                        $track = songData($user->track_id);
                                        $url = $music->config->site_url . '/track/' . $track->audio_id;
                                    }
                                    if($user->track_id == 0 && $user->comment_id > 0){
                                        $type = 'Comment';
                                        $comment = getComment($user->comment_id);
                                        $track = songData($comment->track_id);
                                        $url = $music->config->site_url . '/track/' . $track->audio_id;
                                    }
                                    $user->link = $url;
                                    $user->type = $type;
                                    if($user->mode == 'blog'){
                                        $articleData = $db->arrayBuilder()->where('id', $user->track_id)->getOne(T_BLOG);

                                        $track['title'] = $articleData['title'];
                                        $track['thumbnail'] = '';
                                        $user->link = $music->config->site_url . '/article/' . $articleData['id'] . '_' . url_slug(html_entity_decode($articleData['title']));
                                    }
                                    echo LoadAdminPage('manage-reports/list', array('REPORT_DATA' => $user,'USER_DATA' => $user_data,'SONG_DATA' => $track));
                                }
                            ?>
                            </tbody>
                        </table>
                    </div>
                    <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-reports?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-reports?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-reports?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-reports?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-reports?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 class="clearfix"></div>
    </div>
    <!-- #END# Vertical Layout -->
</div>
    <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 REPORT</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-button" data-dismiss="modal" data-type="reports">DELETE</button>
                    <button type="button" class="btn btn-link waves-effect" data-dismiss="modal">CLOSE</button>
                </div>
            </div>
        </div>
    </div>

    <div id="delete-song-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 SONG</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-album-button">DELETE</button>
                    <button type="button" class="btn btn-link waves-effect" data-dismiss="modal">CLOSE</button>
                </div>
            </div>
        </div>
    </div>

    <script>
        function getUserId(user_id) {
            $('#user_id').val(user_id);
        }

        $(function () {
            $('.delete-content').on('click', function(event) {
                event.preventDefault();
                $('#delete-modal').attr('data-id', $(this).attr('data-id')).modal('show');
            });
        });

        $('.delete-song-content').on('click', function (event) {
            event.preventDefault();
            $('#delete-song-modal').attr('data-id', $(this).attr('data-id')).modal('show');
        });

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

        $('.ignore-report').on('click', function (event) {
            event.preventDefault();
            let id = $(this).attr('data-id');
            $.get('{{CONFIG ajax_url}}/ap/ignore-report', {id: id}, function( data ) {
                if(data.status == 200){
                    location.reload();
                }
            });
        });

    </script>