<?php 
$page = isset($_GET['page-id']) ? $_GET['page-id'] : 1;
$limit_per_page = isset($_GET['limit']) ? (int) $_GET['limit'] : 50;
$link = "";
$video_source = 'all';
if (!empty($_GET['source'])) {
    $video_source = PT_Secure($_GET['source']);
}
$link .= "&source=".$video_source;
$video_filter = 'all';

$category_filter = '0';
if (!empty($_GET['category_id'])) {
    if (in_array($_GET['category_id'], array_keys(ToArray($pt->categories)))) {
        $category_filter = $_GET['category_id'];
    }
}
$link .= "&category_id=".$category_filter;
$video_filter = 'all';
if (isset($_GET['privacy'])) {
    if (in_array($_GET['privacy'], array('0', '1', '2'))) {
        $video_filter = $_GET['privacy'];
    }
}
$link .= "&privacy=".$video_filter;

$video_approve = 'all';
if (isset($_GET['status'])) {
    if (in_array($_GET['status'], array('0', '1'))) {
        $video_approve = $_GET['status'];
    }
}
$link .= "&status=".$video_approve;
$video_type = 'all';
if (isset($_GET['type'])) {
    if (in_array($_GET['type'], array('all', 'free','paid'))) {
        $video_type = $_GET['type'];
    }
}
$link .= "&type=".$video_type;

$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 (isset($video_filter)) {
    if ($video_filter != 'all') {
        $where .= " AND `privacy` = '$video_filter'";
    }
}
if (isset($video_approve)) {
    if ($video_approve != 'all') {
        $where .= " AND `approved` = '$video_approve'";
    }
}
if (isset($video_type)) {
    if ($video_type == 'paid') {
        $where .= " AND `sell_video` > 0 ";
    }
    elseif ($video_type == 'free') {
        $where .= " AND `sell_video` = 0 ";
    }
}
if (!empty($filter_keyword)) {
    $link .= "&query=".$filter_keyword;
    $where .= " AND MATCH (title) AGAINST ('{$filter_keyword}' IN BOOLEAN MODE) OR id = '$filter_keyword' OR (video_id = '$filter_keyword')";
} 

if (!empty($_GET['range']) && in_array($_GET['range'], array('Today','Yesterday','This Week','This Month','Last Month','This Year'))) {
    if ($_GET['range'] == 'Today' || $_GET['range'] == 'Yesterday') {
        $this_start = strtotime(date('M')." ".date('d').", ".date('Y')." 12:00am");
        $this_end = strtotime(date('M')." ".date('d').", ".date('Y')." 11:59pm");
        if ($_GET['range'] == 'Yesterday') {
            $this_start = strtotime(date('M')." ".date('d',strtotime("-1 days")).", ".date('Y')." 12:00am");
            $this_end = strtotime(date('M')." ".date('d',strtotime("-1 days")).", ".date('Y')." 11:59pm");
        }
        $main_range = 'Today';
        $title = "Daily";
    }
    elseif ($_GET['range'] == 'This Week') {
        $time = strtotime(date('l').", ".date('M')." ".date('d').", ".date('Y'));

        if (date('l') == 'Saturday') {
            $this_start = strtotime(date('M')." ".date('d').", ".date('Y')." 12:00am");
        }
        else{
            $this_start = strtotime('last saturday, 12:00am', $time);
        }

        if (date('l') == 'Friday') {
            $this_end = strtotime(date('M')." ".date('d').", ".date('Y')." 11:59pm");
        }
        else{
            $this_end = strtotime('next Friday, 11:59pm', $time);
        }
        
        $main_range = 'This Week';
        $title = "Weekly";
    }
    elseif ($_GET['range'] == 'This Month' ||$_GET['range'] == 'Last Month') {
        $this_start = strtotime("1 ".date('M')." ".date('Y')." 12:00am");
        $this_end = strtotime(cal_days_in_month(CAL_GREGORIAN, date('m'), date('Y'))." ".date('M')." ".date('Y')." 11:59pm");
        if ($_GET['range'] == 'Last Month') {
            $this_start = strtotime("1 ".date('M',strtotime("-1 month"))." ".date('Y')." 12:00am");
            $this_end = strtotime(cal_days_in_month(CAL_GREGORIAN, date('m',strtotime("-1 month")), date('Y'))." ".date('M',strtotime("-1 month"))." ".date('Y')." 11:59pm");
        }
        $main_range = 'This Month';
        $title = "Monthly";
    }
    elseif ($_GET['range'] == 'This Year') {
        $this_start = strtotime("1 January ".date('Y')." 12:00am");
        $this_end = strtotime("31 December ".date('Y')." 11:59pm");
        $main_range = 'This Year';
        $title = "Yearly";
    }
}
$start = '';
$end = '';
$first_code = '';
$second_code = '';
if (!empty($_GET['range']) && !in_array($_GET['range'], array('Today','Yesterday','This Week','This Month','Last Month','This Year'))) {
    $arr = explode('-', $_GET['range']);
    if (preg_match('~(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d~m', $arr[0]) && preg_match('~(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d~m', $arr[1])) {
        $start = PT_Secure($arr[0]);
        $end = PT_Secure($arr[1]);
        $this_start = strtotime($start);
        $this_end = strtotime($end);
        $month_days = cal_days_in_month(CAL_GREGORIAN, date('m',strtotime($start)), date('Y',strtotime($start)));
        $diff = abs(strtotime($end) - strtotime($start));
        $years = floor($diff / (365*60*60*24));
        $months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
        $days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
        if ($years >= 1) {
            $array = array();
            for ($i=date('Y',strtotime($start)); $i <= date('Y',strtotime($end)); $i++) { 
                $array["'".$i."'"] = 0;
            }
            $main_range = 'Custom';
            $code = 'Y';
            $title = "Yearly";
        }
        elseif ($months >= 1) {
            $array = array('01' => 0 ,'02' => 0 ,'03' => 0 ,'04' => 0 ,'05' => 0 ,'06' => 0 ,'07' => 0 ,'08' => 0 ,'09' => 0 ,'10' => 0 ,'11' => 0 ,'12' => 0);
            $code = 'm';
            $main_range = 'This Year';
            $title = "Monthly";
            if (date('Y',strtotime($start)) == date('Y',strtotime($end))) {
                $array = array();
                for ($i=date('m',strtotime($start)); $i <= date('m',strtotime($end)); $i++) { 
                    $array["'".(int)$i."'"] = 0;
                }
                $code = 'm';
                $main_range = 'Custom';
            }
            else{
                $month = cal_days_in_month(CAL_GREGORIAN, date('m',strtotime($start)), date('Y',strtotime($start)));
                $array = array();
                for ($i=(int)date('m',strtotime($start)); $i <= 12; $i++) { 
                    $array["'".(int)$i.'-'.date('Y',strtotime($start))."'"] = 0;
                }
                for ($i=1; $i <= (int)date('m',strtotime($end)); $i++) { 
                    $array["'".(int)$i.'-'.date('Y',strtotime($end))."'"] = 0;
                }
                $first_code = 'm';
                $second_code = 'Y';
                $main_range = 'Custom';
            }
        }
        elseif ($days > 7) {
            if (date('m',strtotime($start)) == date('m',strtotime($end))) {
                $array = array();
                for ($i=date('d',strtotime($start)); $i <= date('d',strtotime($end)); $i++) { 
                    $array["'".(int)$i."'"] = 0;
                }
                $code = 'd';
                $main_range = 'Custom';
            }
            else{
                $month = cal_days_in_month(CAL_GREGORIAN, date('m',strtotime($start)), date('Y',strtotime($start)));
                $array = array();
                for ($i=(int)date('d',strtotime($start)); $i <= $month; $i++) { 
                    $array["'".(int)$i.'-'.date('m',strtotime($start))."'"] = 0;
                }
                for ($i=1; $i <= (int)date('d',strtotime($end)); $i++) { 
                    $array["'".(int)$i.'-'.date('m',strtotime($end))."'"] = 0;
                }
                $first_code = 'd';
                $second_code = 'm';
                $main_range = 'Custom';
            }
            $title = "Daily";
        }
        elseif ($days >= 1 && $days < 8) {
            $title = "Daily";
            $code = 'l';
            $array = array('Saturday' => 0 , 'Sunday' => 0 , 'Monday' => 0 , 'Tuesday' => 0 , 'Wednesday' => 0 , 'Thursday' => 0 , 'Friday' => 0);
            if (date('m',strtotime($start)) == date('m',strtotime($end))) {
                $array = array();
                for ($i=date('d',strtotime($start)); $i <= date('d',strtotime($end)); $i++) { 
                    $array["'".(int)$i."'"] = 0;
                }
                $code = 'd';
                $main_range = 'Custom';
            }
            else{
                $month = cal_days_in_month(CAL_GREGORIAN, date('m',strtotime($start)), date('Y',strtotime($start)));
                $array = array();
                for ($i=(int)date('d',strtotime($start)); $i <= $month; $i++) { 
                    $array["'".(int)$i.'-'.date('m',strtotime($start))."'"] = 0;
                }
                for ($i=1; $i <= (int)date('d',strtotime($end)); $i++) { 
                    $array["'".(int)$i.'-'.date('m',strtotime($end))."'"] = 0;
                }
                $first_code = 'd';
                $second_code = 'm';
                $main_range = 'Custom';
            }
        }
    }
}

$rang_link = $link;
if (!empty($this_start) && !empty($this_end)) {
    $link .= "&range=".PT_Secure($_GET['range']);
    $db->where('time',$this_start,'>=')->where('time',$this_end,'<=');
}
$sort_link = $link;
$sort_array = array('DESC_i' => array('id' , 'DESC'),
                    'ASC_i'  => array('id' , 'ASC'),
                    'DESC_v' => array('video_id' , 'DESC'),
                    'ASC_v'  => array('video_id' , 'ASC'),
                    'DESC_t' => array('title' , 'DESC'),
                    'ASC_t'  => array('title' , 'ASC'),
                    'DESC_c' => array('category_id' , 'DESC'),
                    'ASC_c'  => array('category_id' , 'ASC'),
                    'DESC_p' => array('privacy' , 'DESC'),
                    'ASC_p'  => array('privacy' , 'ASC'));
if (!empty($_GET['sort']) && in_array($_GET['sort'], array_keys($sort_array))) {
    $db->orderBy($sort_array[$_GET['sort']][0],$sort_array[$_GET['sort']][1]);
    $link .= "&sort=".PT_Secure($_GET['sort']);
    $rang_link .= "&sort=".PT_Secure($_GET['sort']);
}
else{
    $_GET['sort'] = 'DESC_i';
    $db->orderBy('id', 'DESC');
}
$videos = $db->where($where)->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>
        <h3>Manage Videos</h3>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item">
                    <a href="#">Home</a>
                </li>
                <li class="breadcrumb-item">
                    <a href="#">Videos</a>
                </li>
                <li class="breadcrumb-item active" aria-current="page">Manage Videos</li>
            </ol>
        </nav>
    </div>
    <!-- Vertical Layout -->
    <div class="row">
        <div class="col-lg-12 col-md-12">
            <div class="card">
                <div class="card-body">
					<div id="dashboard-daterangepicker" class="btn btn-outline-light pull-right">
                            <?php 
                            if (!empty($_GET['range']) && in_array($_GET['range'], array('Today','Yesterday','This Week','This Month','Last Month','This Year'))) {
                                echo $_GET['range'];
                            }else if (!empty($start) && !empty($end)){
                                echo $_GET['range'];
                            }else{
                                echo 'All';
                            } ?>
                        </div>
                    <h6 class="card-title">Manage & Edit Videos</h6>
                    <div class="row">
                       <div class="col-md-12" style="margin-bottom:0;">
                            <form method="get" action="<?php echo PT_LoadAdminLinkSettings('manage-videos'); ?>">
                              <div class="row">
                                <div class="col-lg-4 col-md-2">
                                    <div class="form-group form-float">
                                        <div class="form-line">
                                            <input type="text" name="query" id="query" class="form-control" placeholder="Keyword, ID, Title" value="<?php echo $filter_keyword ?>">
                                            
                                        </div>
                                    </div>
                                  </div>
                                  <div class="col-lg-3 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-3 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-3 col-md-2">
                                        <select class="form-control show-tick" id="privacy" name="privacy">
                                            <option value="all" <?php echo ($video_filter == 'all') ? 'selected' : '' ?>>All Videos</option>
                                            <option value="0" <?php echo ($video_filter == '0') ? 'selected' : '' ?>>Public</option>
                                            <option value="1" <?php echo ($video_filter == '1') ? 'selected' : '' ?>>Private</option>
                                            <option value="2" <?php echo ($video_filter == '2') ? 'selected' : '' ?>>Unlisted</option>
                                        </select>
                                    </div>
                                    <?php if ($pt->config->approve_videos == 'on') { ?>
                                  <div class="col-lg-3 col-md-2">
                                        <select class="form-control show-tick" id="status" name="status">
                                            <option value="all" <?php echo ($video_approve == 'all') ? 'selected' : '' ?>>All Videos</option>
                                            <option value="0" <?php echo ($video_approve == '0') ? 'selected' : '' ?>>Pending Apporval</option>
                                            <option value="1" <?php echo ($video_approve == '1') ? 'selected' : '' ?>>Approved</option>
                                        </select>
                                    </div>
                                    <?php } ?>
                                    <div class="col-lg-3 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-3 col-md-2">
                                        <select class="form-control show-tick" id="type" name="type">
                                            <option value="all" <?php echo ($video_type == 'all') ? 'selected' : '' ?>>Paid & Free</option>
                                            <option value="paid" <?php echo ($video_type == 'paid') ? 'selected' : '' ?>>Paid Videos</option>
                                            <option value="free" <?php echo ($video_type == 'free') ? 'selected' : '' ?>>Free Videos</option>
                                        </select>
                                    </div>
									<div class="col-lg-2 col-md-2">
										<button class="btn btn-info">Search</button>
									</div>
                              </div>

                              <div class="clearfix"></div>
                           </form>
                       </div>
                       <div class="clearfix"></div>
                       <br><br><br><br><br>
                   </div>
                   <!-- <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">
                                        
                                    </select>
                                </div>
                                <?php if ($pt->config->approve_videos == 'on') { ?>
                                <div class="clear"></div>
                                    <div class="col-lg-2 col-md-2">
                                        <select class="form-control show-tick" id="status" name="status">
                                            <option value="all" <?php echo ($video_approve == 'all') ? 'selected' : '' ?>>All Videos</option>
                                            <option value="0" <?php echo ($video_approve == '0') ? 'selected' : '' ?>>Pending Apporval</option>
                                            <option value="1" <?php echo ($video_approve == '1') ? 'selected' : '' ?>>Approved</option>
                                        </select>
                                    </div>
                                <?php } ?>
                                 <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">
                                        <select class="form-control show-tick" id="type" name="type">
                                            <option value="all" <?php echo ($video_type == 'all') ? 'selected' : '' ?>>Paid & Free</option>
                                            <option value="paid" <?php echo ($video_type == 'paid') ? 'selected' : '' ?>>Paid Videos</option>
                                            <option value="free" <?php echo ($video_type == 'free') ? 'selected' : '' ?>>Free Videos</option>
                                        </select>
                                    </div>
                                    <div class="clearfix"></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>
                   <br>
                   <div class="table-responsive">
                        <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 
                                        <?php if (!empty($_GET['sort']) && $_GET['sort'] == 'DESC_i') { ?>
                                            <svg onclick="location.href = '<?php echo(PT_LoadAdminLinkSettings('manage-videos?page-id=1').$sort_link."&sort=ASC_i") ?>'" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#000000" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-up cursor-p"><line x1="12" y1="19" x2="12" y2="5"></line><polyline points="5 12 12 5 19 12"></polyline></svg>
                                        <?php }else{ ?>
                                            <svg onclick="location.href = '<?php echo(PT_LoadAdminLinkSettings('manage-videos?page-id=1').$sort_link."&sort=DESC_i") ?>'" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#000000" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-down cursor-p"><line x1="12" y1="5" x2="12" y2="19"></line><polyline points="19 12 12 19 5 12"></polyline></svg>
                                        <?php } ?></th>
					                  <th>Video ID 
                                        <?php if (!empty($_GET['sort']) && $_GET['sort'] == 'DESC_v') { ?>
                                            <svg onclick="location.href = '<?php echo(PT_LoadAdminLinkSettings('manage-videos?page-id=1').$sort_link."&sort=ASC_v") ?>'" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#000000" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-up cursor-p"><line x1="12" y1="19" x2="12" y2="5"></line><polyline points="5 12 12 5 19 12"></polyline></svg>
                                        <?php }else{ ?>
                                            <svg onclick="location.href = '<?php echo(PT_LoadAdminLinkSettings('manage-videos?page-id=1').$sort_link."&sort=DESC_v") ?>'" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#000000" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-down cursor-p"><line x1="12" y1="5" x2="12" y2="19"></line><polyline points="19 12 12 19 5 12"></polyline></svg>
                                        <?php } ?></th>
					                  <th>Title 
                                        <?php if (!empty($_GET['sort']) && $_GET['sort'] == 'DESC_t') { ?>
                                            <svg onclick="location.href = '<?php echo(PT_LoadAdminLinkSettings('manage-videos?page-id=1').$sort_link."&sort=ASC_t") ?>'" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#000000" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-up cursor-p"><line x1="12" y1="19" x2="12" y2="5"></line><polyline points="5 12 12 5 19 12"></polyline></svg>
                                        <?php }else{ ?>
                                            <svg onclick="location.href = '<?php echo(PT_LoadAdminLinkSettings('manage-videos?page-id=1').$sort_link."&sort=DESC_t") ?>'" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#000000" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-down cursor-p"><line x1="12" y1="5" x2="12" y2="19"></line><polyline points="19 12 12 19 5 12"></polyline></svg>
                                        <?php } ?></th>
                                      <th>Category 
                                        <?php if (!empty($_GET['sort']) && $_GET['sort'] == 'DESC_c') { ?>
                                            <svg onclick="location.href = '<?php echo(PT_LoadAdminLinkSettings('manage-videos?page-id=1').$sort_link."&sort=ASC_c") ?>'" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#000000" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-up cursor-p"><line x1="12" y1="19" x2="12" y2="5"></line><polyline points="5 12 12 5 19 12"></polyline></svg>
                                        <?php }else{ ?>
                                            <svg onclick="location.href = '<?php echo(PT_LoadAdminLinkSettings('manage-videos?page-id=1').$sort_link."&sort=DESC_c") ?>'" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#000000" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-down cursor-p"><line x1="12" y1="5" x2="12" y2="19"></line><polyline points="19 12 12 19 5 12"></polyline></svg>
                                        <?php } ?></th>
                                      <th>Source</th>
                                      <th>Privacy 
                                        <?php if (!empty($_GET['sort']) && $_GET['sort'] == 'DESC_p') { ?>
                                            <svg onclick="location.href = '<?php echo(PT_LoadAdminLinkSettings('manage-videos?page-id=1').$sort_link."&sort=ASC_p") ?>'" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#000000" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-up cursor-p"><line x1="12" y1="19" x2="12" y2="5"></line><polyline points="5 12 12 5 19 12"></polyline></svg>
                                        <?php }else{ ?>
                                            <svg onclick="location.href = '<?php echo(PT_LoadAdminLinkSettings('manage-videos?page-id=1').$sort_link."&sort=DESC_p") ?>'" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="#000000" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-arrow-down cursor-p"><line x1="12" y1="5" x2="12" y2="19"></line><polyline points="19 12 12 19 5 12"></polyline></svg>
                                        <?php } ?></th>
                                      <th>Added By</th>
					                  <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php 
                                foreach ($videos as $key => $video) {
                                	$video = PT_GetVideoByID($video, 0, 0, 0);
                                    $privacy = 'Public';
                                    if ($video->privacy == 1) {
                                        $privacy = 'Private';
                                    } else if ($video->privacy == 2) {
                                        $privacy = 'Unlisted';
                                    }
                                    $pt->sell_video = $video->sell_video;
                                    $video_data = array(
                                        'ID' => $video->id,
                                        'TITLE' => mb_substr($video->title, 0, 40, "UTF-8") . '..',
                                        'VIDEO_ID' => $video->video_id,
                                        'URL' => $video->url,
                                        'SOURCE' => $video->source,
                                        'CATEGORY' => $video->category_name,
                                        'USER_DATA' => PT_UserData($video->user_id),
                                        'PRIVACY' => $privacy
                                    );
                                    $pt->video_approve = $video->approved;
                                	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?page-id=1").$link; ?>" 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?page-id=" . ($page - 1)).$link; ?>" 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?page-id=" . ($i)).$link; ?>" class="waves-effect"><?php echo $i ?></a></li>
                                	<?php
                                	}
                                	$nums++;
                                }
                                ?>
                                <?php if ($db->totalPages > $page) { ?>
                                <li>
                                    <a href="<?php echo PT_LoadAdminLinkSettings("manage-videos?page-id=" . ($page + 1)).$link; ?>" 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?page-id=" . ($db->totalPages)).$link; ?>" class="waves-effect" title='Last Page'>
                                        <i class="material-icons">last_page</i>
                                    </a>
                                </li>
                            </ul>
                        </nav>
                    </div>
                    <div class="clearfix"></div>
                    <div class="row">
                        <div class="col-lg-2 col-md-2">
                            <span>Action</span>
                            <select class="form-control show-tick" id="action_type">
                                <option value="approve">Approve</option>
                                <option value="disapprove">Disapprove</option>
                                <option value="delete">Delete</option>
                            </select>
                        </div>
                        <div class="col-lg-3 col-md-3">
                            <span>&nbsp;</span>
                            <button type="button" class="btn btn-info waves-effect delete-selected d-block" disabled>Submit<span></span></button>
                        </div>
                    </div>
                    <?php } ?>
                </div>
            </div>
        </div>
        <div class="clearfix"></div>
    </div>
    <!-- #END# Vertical Layout -->

<div class="modal fade" id="DeleteModal" tabindex="-1" role="dialog" aria-labelledby="exampleModal1Label" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModal1Label">Delete Video?</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                Are you sure you want to delete this video?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary" data-dismiss="modal">Delete</button>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="SelectedDeleteModal" tabindex="-1" role="dialog" aria-labelledby="exampleModal1Label" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModal1Label">Delete Video?</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                Are you sure that you want to remove the selected video(s)?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary" onclick="DeleteSelected()" data-dismiss="modal">Submit</button>
            </div>
        </div>
    </div>
</div>
<script>
jQuery(document).ready(function($) {
        var range = { "All": [moment().startOf('year') , moment().endOf('year')],
                      "Today": [moment() , moment()], 
                      "Yesterday": [moment().subtract(1, 'days') , moment().subtract(1, 'days')], 
                      "This_Week": [moment().subtract(6, 'days') , moment()],
                      "This_Month": [moment().startOf('month') , moment().endOf('month')],
                      "Last_Month": [moment().subtract(1, 'month').startOf('month') , moment().subtract(1, 'month').endOf('month')],
                      "This_Year": [moment().subtract(1, 'year').startOf('year') , moment().subtract(1, 'year').endOf('year')]}; 
        <?php 
        if (!empty($_GET['range']) && in_array($_GET['range'], array('Today','Yesterday','This Week','This Month','Last Month','This Year'))) { 
            if ($_GET['range'] == 'Today') { ?>
                var start = range.Today[0];
                var end = range.Today[1];
            <?php }elseif ($_GET['range'] == 'Yesterday') { ?>
                var start = range.Yesterday[0];
                var end = range.Yesterday[1];
            <?php }elseif ($_GET['range'] == 'This Week') { ?>
                var start = range.This_Week[0];
                var end = range.This_Week[1];
            <?php }elseif ($_GET['range'] == 'This Month') { ?>
                var start = range.This_Month[0];
                var end = range.This_Month[1];
            <?php }elseif ($_GET['range'] == 'Last Month') { ?>
                var start = range.Last_Month[0];
                var end = range.Last_Month[1];
            <?php }elseif ($_GET['range'] == 'This Year') { ?>
                var start = range.This_Year[0];
                var end = range.This_Year[1];
            <?php } ?>
        <?php } elseif (!empty($_GET['range']) && !empty($start) && !empty($end)) { ?>
            var start = "<?php echo($start) ?>";
            var end = "<?php echo($end) ?>";
        <?php } else{ ?>
            var start = range.All[0];
            var end = range.All[1];
        <?php } ?>

        function cb(start, end) {
            //$('#dashboard-daterangepicker span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
        }
        $('#dashboard-daterangepicker').daterangepicker({
            startDate: start,
            endDate: end,
            opens: $('body').hasClass('rtl') ? 'right' : 'left',
            ranges: {
                'All': [moment().startOf('year') , moment().endOf('year')],
                'Today': [moment(), moment()],
                'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                'This Week': [moment().subtract(6, 'days'), moment()],
                'This Month': [moment().startOf('month'), moment().endOf('month')],
                'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
                'This Year': [moment().subtract(1, 'year').startOf('year'), moment().subtract(1, 'year').endOf('year')],
            }
        }, cb);

        

        cb(start, end);

        // setTimeout(function (argument) {
        //     $('.ranges ul li').removeClass('active');
        // },800);
        
        $(document).on('click', '.ranges ul li', function(event) {
            event.preventDefault();
            if ($(this).attr('data-range-key') != 'Custom Range') {
                $(document).off('click', '.ranges ul li');
                $('#redirect_link').attr('href', "<?php echo PT_LoadAdminLinkSettings('manage-videos').$rang_link; ?>&range="+$(this).attr('data-range-key'));
                $('#redirect_link').attr('data-ajax', "?path=manage-videos<?php echo('&'.$rang_link) ?>&range="+$(this).attr('data-range-key'));
                $('#redirect_link').click();
            }
        });
        $(document).on('click', '.applyBtn', function(event) {
            event.preventDefault();
            $(document).off('click', '.applyBtn');
            $('#redirect_link').attr('href', "<?php echo PT_LoadAdminLinkSettings('manage-videos').$rang_link; ?>&range="+$(this).parent('.drp-buttons').find('.drp-selected').html());
            $('#redirect_link').attr('data-ajax', "?path=manage-videos<?php echo('&'.$rang_link) ?>&range="+$(this).parent('.drp-buttons').find('.drp-selected').html());
            $('#redirect_link').click();
        });
    var rgbToHex = function (rgb) {
        var hex = Number(rgb).toString(16);
        if (hex.length < 2) {
            hex = "0" + hex;
        }
        return hex;
    };

    var fullColorHex = function (r, g, b) {
        var red = rgbToHex(r);
        var green = rgbToHex(g);
        var blue = rgbToHex(b);
        return red + green + blue;
    };

    var colors = {
        primary: $('.colors .bg-primary').css('background-color').replace('rgb', '').replace(')', '').replace('(', '').split(','),
        secondary: $('.colors .bg-secondary').css('background-color').replace('rgb', '').replace(')', '').replace('(', '').split(','),
        info: $('.colors .bg-info').css('background-color').replace('rgb', '').replace(')', '').replace('(', '').split(','),
        success: $('.colors .bg-success').css('background-color').replace('rgb', '').replace(')', '').replace('(', '').split(','),
        danger: $('.colors .bg-danger').css('background-color').replace('rgb', '').replace(')', '').replace('(', '').split(','),
        warning: $('.colors .bg-warning').css('background-color').replace('rgb', '').replace(')', '').replace('(', '').split(','),
    };
    //console.log(colors.primary[1]);
    colors.primary = '#' + fullColorHex(colors.primary[0], colors.primary[1], colors.primary[2]);
    colors.secondary = '#' + fullColorHex(colors.secondary[0], colors.secondary[1], colors.secondary[2]);
    colors.info = '#' + fullColorHex(colors.info[0], colors.info[1], colors.info[2]);
    colors.success = '#' + fullColorHex(colors.success[0], colors.success[1], colors.success[2]);
    colors.danger = '#' + fullColorHex(colors.danger[0], colors.danger[1], colors.danger[2]);
    colors.warning = '#' + fullColorHex(colors.warning[0], colors.warning[1], colors.warning[2]);
});


$('.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').length + ')');
});
$('.delete-selected').on('click', function(event) {
    event.preventDefault();
    action_type = $('#action_type').val();
    $('#SelectedDeleteModal').find('.modal-body').html('Are you sure that you want to '+action_type+' the selected video(s)?');
    $('#SelectedDeleteModal').find('#exampleModal1Label').html(action_type+' video(s)');
    $('#SelectedDeleteModal').modal('show');
});
function DeleteSelected() {
    action_type = $('#action_type').val();
    data = new Array();
    $('td input:checked').parents('tr').each(function () {
        data.push($(this).attr('data_selected'));
    });
    $('.delete-selected').attr('disabled', true);
    $('.delete-selected').text('Please wait..');
    $.post('{{LINK aj/ap/delete-multi-videos}}', {ids: data,type: action_type}, function () {
        if (action_type == 'delete') {
            $.each( data, function( index, value ){
                $('#list-' + value).remove();
            });
        }
        else{
            location.reload();
        }
        $('.delete-selected').text('Submit');
    });
}
$(document).on('click', '.approve-content', function(event) {
    event.preventDefault();
    $video_id_approve = $(this).attr('data-id');
    $('#list-' + $video_id_approve).find('.approve-content').removeClass('btn-success').addClass('btn-default').removeClass('approve-content').addClass('disapprove-content').text('Disapporve');
    $.post('{{LINK aj/ap/videos-status}}', {id: $video_id_approve, status:'approve'}, function(data, textStatus, xhr) {
        /*optional stuff to do after success */
    });
});
$(document).on('click', '.disapprove-content', function(event) {
    event.preventDefault();
    $video_id_approve = $(this).attr('data-id');
    $('#list-' + $video_id_approve).find('.disapprove-content').addClass('btn-success').removeClass('btn-default').removeClass('disapprove-content').addClass('approve-content').text('Approve');
    $.post('{{LINK aj/ap/videos-status}}', {id: $video_id_approve, status:'disapporve'}, function(data, textStatus, xhr) {
        /*optional stuff to do after success */
    });
});
function DeleteVideo(id,type = 'show') {
    if (id == '') {
        return false;
    }
    if (type == 'hide') {
        $('#DeleteModal').find('.btn-primary').attr('onclick', "DeleteVideo('"+id+"')");
        $('#DeleteModal').modal('show');
        return false;
    }
    $('#list-' + id).fadeOut(300, function () {
          $('#list-' + id).remove();
      });
      $.post('{{LINK aj/ap/delete-video}}', {id: id});
}
$(function () {
    
	$('.delete-content').on('click', function(event) {
		event.preventDefault();
		$('#delete-modal').attr('data-id', $(this).attr('data-id')).modal('show');
	});
    $('.fake-content').on('click', function(event) {
        event.preventDefault();
        $('#fake_views_modal').modal('show');
        $('#video_id_generate').val($(this).attr('data-id'));
        $('#views_generate').val('0');
        $('#likes_generate').val('0');
    });
    $('.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() + ')');
    });


    var fake_views_settings = $('form.fake_views_settings');
    fake_views_settings.ajaxForm({
        url: '{{LINK aj/ap/fake_views_likes}}',
        beforeSend: function() {
            $('.fake_views_settings').find('.btn-save').text('PLEASE WAIT..');
            $('.fake_views_alert').html('<div class="alert alert-warning">This process may take some time.</div>')
        },
        success: function(data) {
            if (data.status == 200) {
                $('.fake_views_settings').find('.btn-save').text('SAVE CHANGES');
                $('.fake_views_alert').html('<div class="alert alert-success">Views and Likes are being generated, please check your site after few mins.</div>')
                setTimeout(function (argument) {
                    $('.fake_views_alert').html('');
                    $('#fake_views_modal').modal('hide');
                },2000);
                
            }
            else{
                $('.fake_views_settings').find('.btn-save').text('SAVE CHANGES');
                $('.fake_views_alert').html('<div class="alert alert-danger">'+data.message+'</div>');
                setTimeout(function (argument) {
                    $('.fake_views_alert').html('');
                },2000);
            }
        }
    });



});
</script>

<div class="modal fade" id="fake_views_modal" tabindex="-1" role="dialog" aria-labelledby="exampleModal1Label" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModal1Label">Generate Fake Views And Likes</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form class="fake_views_settings" method="POST">
                <div class="modal-body">
                    <div class="alert fake_views_alert"></div>
                    
                        <div class="form-group">
                            <div class="form-lins">
                                <label class="form-lasbel pull-left">
                                    How many views you want to generate?
                                </label>

                                <div>
                                    <input style="resize: none; border: 1px solid #eee; padding: 5px;" name="views" id="views_generate" class="form-control" value="0" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');">
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="form-lins">
                                <label class="form-lasbel pull-left">
                                    How many likes you want to generate?
                                </label>

                                <div>
                                    <input style="resize: none; border: 1px solid #eee; padding: 5px;" name="likes" id="likes_generate" class="form-control" value="0" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');">
                                </div>
                            </div>
                        </div>
                        <input type="hidden" name="video_id" id="video_id_generate" value="">
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">SAVE CHANGES</button>
                </div>
            </form>
        </div>
    </div>
</div>