<?php 
$page = isset($_GET['page-id']) ? $_GET['page-id'] : 1;
$limit_per_page = isset($_GET['limit']) ? (int) $_GET['limit'] : 50;
$video_source = 'all';
if (!empty($_GET['source'])) {
    $video_source = PT_Secure($_GET['source']);
}
$category_filter = '0';
if (!empty($_GET['category_id'])) {
    if (in_array($_GET['category_id'], array_keys(ToArray($pt->categories)))) {
        $category_filter = $_GET['category_id'];
    }
}
$filter_keyword = (!empty($_GET['query'])) ? PT_Secure($_GET['query']) : '';
$db->pageLimit = $limit_per_page;
$where = 'id > 0';
if ($video_source != 'all') {
    if ($video_source == 'youtube') {
        $where .= " AND `youtube` <> ''";
    }
    if ($video_source == 'dailymotion') {
        $where .= " AND `daily` <> ''";
    }
    if ($video_source == 'vimeo') {
        $where .= " AND `vimeo` <> ''";
    }
    if ($video_source == 'uploaded') {
        $where .= " AND `youtube` = '' AND `vimeo` = '' AND `daily` = ''";
    }
}
if (!empty($category_filter)) {
    $where .= " AND `category_id` = '$category_filter'";
}
if (!empty($filter_keyword)) {
    $where .= " AND MATCH (title) AGAINST ('{$filter_keyword}' IN BOOLEAN MODE) OR id = '$filter_keyword' OR (video_id = '$filter_keyword')";
} 
$videos = $db->where($where)->orderBy('id', 'DESC')->objectbuilder()->paginate(T_VIDEOS, $page);
if ($db->totalPages != 0) {
    if (($page > $db->totalPages) && !empty($_GET['page-id'])) {
        header("Location: " . PT_LoadAdminLinkSettings('manage-videos'));
        exit();
    }
}
?>
<div class="container-fluid">
    <div class="block-header">
        <h2>Videos > <a href="<?php echo PT_LoadAdminLinkSettings('manage-videos') ?>">Manage Videos</a> <small>{{CONFIG total_videos}} TOTAL VIDEOS</small></h2>

    </div>
    <!-- Vertical Layout -->
    <div class="row">
        <div class="col-lg-12 col-md-12">
            <div class="card">
                <div class="header">
                    <h2>Manage & Edit Videos</h2>
                </div>
                <div class="body">
                   <div class="row">
                       <form action="<?php echo PT_LoadAdminLinkSettings('manage-videos'); ?>" method="get">
                            <div class=" clearfix">
                                <div class="col-lg-2 col-md-2">
                                    <div class="form-group form-float">
                                        <div class="form-line">
                                            <input type="text" class="form-control" name="query" value="<?php echo $filter_keyword ?>">
                                            <label class="form-label">Keyword, ID, Title</label>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-lg-2 col-md-2">
                                    <select class="form-control show-tick" id="source" name="source">
                                        <option value="all" <?php echo ($video_source == 'all') ? 'selected' : '' ?>>All (Default)</option>
                                        <option value="uploaded" <?php echo ($video_source == 'uploaded') ? 'selected' : '' ?>>Uploaded</option>
                                        <option value="youtube" <?php echo ($video_source == 'youtube') ? 'selected' : '' ?>>YouTube</option>
                                        <option value="dailymotion" <?php echo ($video_source == 'dailymotion') ? 'selected' : '' ?>>Dailymotion</option>
                                        <option value="vimeo" <?php echo ($video_source == 'vimeo') ? 'selected' : '' ?>>Vimeo</option>
                                    </select>
                                </div>
                                <div class="col-lg-2 col-md-2">
                                    <select class="form-control show-tick" id="limit" name="limit">
                                        <option value="all" selected disabled>Limit Per Page</option>
                                        <option value="10" <?php echo ($limit_per_page == 10) ? 'selected' : '' ?>>10</option>
                                        <option value="25" <?php echo ($limit_per_page == 25) ? 'selected' : '' ?>>25</option>
                                        <option value="50" <?php echo ($limit_per_page == 50) ? 'selected' : '' ?>>50 (Default)</option>
                                        <option value="100" <?php echo ($limit_per_page == 100) ? 'selected' : '' ?>>100</option>
                                        <option value="500" <?php echo ($limit_per_page == 500) ? 'selected' : '' ?>>500</option>
                                        <option value="1000" <?php echo ($limit_per_page == 1000) ? 'selected' : '' ?>>1000</option>
                                    </select>
                                </div>
                                 <div class="col-lg-2 col-md-2">
                                    <select class="form-control show-tick" id="category_id" name="category_id">
                                        <option value="0" selected>Category</option>
                                        <?php 
                                        foreach($pt->categories as $key => $category) { 
                                            $selected = ($key == $category_filter) ? 'selected' : '';
                                        ?>
                                        <option value="<?php echo $key?>" <?php echo $selected?>><?php echo $category?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                                <div class="col-lg-2 col-md-2">
                                    <button type="submit" class="btn btn-primary btn-lg m-l-10 waves-effect">SEARCH</button>
                                </div>
                            </div>
                        </form>
                   </div>
                   <div class="clearfix"></div>
                   <div class="table-responsive1">
                        <table class="table table-bordered table-striped table-hover">
                            <thead>
                                <tr>
                                      <th><input type="checkbox" id="check-all" class="filled-in check-all" ><label for="check-all"></label></th>
                                      <th>ID</th>
					                  <th>Video ID</th>
					                  <th>Title</th>
                                      <th>Category</th>
                                      <th>Source</th>
                                      <th>Added By</th>
					                  <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php 
                                foreach ($videos as $key => $video) {
                                	$video = PT_GetVideoByID($video, 0, 0, 0);
                                    $video_data = array(
                                        'ID' => $video->id,
                                        'TITLE' => $video->title,
                                        'VIDEO_ID' => $video->video_id,
                                        'URL' => $video->url,
                                        'SOURCE' => $video->source,
                                        'CATEGORY' => $video->category_name,
                                        'USER_DATA' => PT_UserData($video->user_id)
                                    );
                                	echo PT_LoadAdminPage('manage-videos/list', $video_data);
                                }
                                ?>
                            </tbody>
                        </table>
                        <?php if (count($videos) == 0) { ?>
                           <div class="no-data-found">
                               No videos found
                           </div>
                        <?php } ?>
                    </div>
                    <?php if (count($videos) > 0) { ?>
                    <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 PT_LoadAdminLinkSettings("manage-videos?source=$video_source&query=$filter_keyword&limit=$limit_per_page&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 PT_LoadAdminLinkSettings("manage-videos?source=$video_source&query=$filter_keyword&limit=$limit_per_page&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 PT_LoadAdminLinkSettings("manage-videos?source=$video_source&query=$filter_keyword&limit=$limit_per_page&page-id=" . ($i)); ?>" class="waves-effect"><?php echo $i ?></a></li>
                                	<?php
                                	}
                                	$nums++;
                                }
                                ?>
                                <?php if ($db->totalPages > $page) { ?>
                                <li>
                                    <a href="<?php echo PT_LoadAdminLinkSettings("manage-videos?source=$video_source&query=$filter_keyword&limit=$limit_per_page&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 PT_LoadAdminLinkSettings("manage-videos?source=$video_source&query=$filter_keyword&limit=$limit_per_page&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>
                    <button type="button" class="btn btn-info waves-effect delete-selected" disabled>DELETE SELECTED<span></span></button>
                    <?php } ?>
                </div>
            </div>
        </div>
        <div class="clearfix"></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 VIDEO</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="video">DELETE</button>
        <button type="button" class="btn btn-link waves-effect" data-dismiss="modal">CLOSE</button>
      </div>
    </div>
  </div>
</div>
<script>
$(function () {
	$('.delete-content').on('click', function(event) {
		event.preventDefault();
		$('#delete-modal').attr('data-id', $(this).attr('data-id')).modal('show');
	});
    $('.check-all').on('click', function(event) {
        $('input:checkbox').not(this).prop('checked', this.checked);
    });
    $('.delete-checkbox, .check-all').change(function(event) {
        $('.delete-selected').attr('disabled', false);
        $('.delete-selected').find('span').text(' (' + $('.delete-checkbox:checked').size() + ')');
    });
    $('.delete-selected').on('click', function(event) {
        event.preventDefault();
        data = new Array();
        $('td input:checked').parents('tr').each(function () {
            data.push($(this).attr('data-id'));
        });
        if (confirm('Are you sure that you want to delete the selected video(s)?')) {
            $('.delete-selected').attr('disabled', true);
            $('.delete-selected').text('Please wait..');
            $.post('{{LINK aj/ap/delete-multi-videos}}', {ids: data}, function () {
                alert('Video successfully deleted!');
                $.each( data, function( index, value ){
                    $('#list-' + value).remove();
                });
                $('.delete-selected').text('DELETE SELECTED');
            });
        }
    });
});
</script>