<?php
if (empty($_GET['keyword'])) {
	header("Location: " . PT_Link('login'));
    exit();
}
$keyword = PT_Secure($_GET['keyword']);

$list = '<div class="text-center no-content-found empty_state"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-video-off"><path d="M16 16v1a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V7a2 2 0 0 1 2-2h2m5.66 0H14a2 2 0 0 1 2 2v3.34l1 1L23 7v10"></path><line x1="1" y1="1" x2="23" y2="23"></line></svg>' . $lang->no_videos_found_for_now . '</div>';
$list2 = '<div class="text-center no-content-found empty_state"><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-video-off"><path d="M16 16v1a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V7a2 2 0 0 1 2-2h2m5.66 0H14a2 2 0 0 1 2 2v3.34l1 1L23 7v10"></path><line x1="1" y1="1" x2="23" y2="23"></line></svg></div>';
$final = '';

if ($pt->config->total_videos > 1000000) {
    $get_videos = $db->rawQuery("SELECT * FROM " . T_VIDEOS . " WHERE MATCH (title) AGAINST ('$keyword') ORDER BY id ASC LIMIT 20");
} else {
    $get_videos = $db->rawQuery("SELECT * FROM " . T_VIDEOS . " WHERE title LIKE '%$keyword%' ORDER BY id ASC LIMIT 20");
}

if (!empty($get_videos)) {
    $len = count($get_videos);
    foreach ($get_videos as $key => $video) {
        $video = PT_GetVideoByID($video, 0, 0, 0);
        $pt->last_video = false;
        if ($key == $len - 1) {
            $pt->last_video = true;
        }
        $final .= PT_LoadPage('search/list', array(
            'ID' => $video->id,
            'USER_DATA' => $video->owner,
            'THUMBNAIL' => $video->thumbnail,
            'URL' => $video->url,
            'TITLE' => $video->title,
            'DESC' => $video->markup_description,
            'VIEWS' => $video->views,
            'VIEWS_NUM' => number_format($video->views),
            'TIME' => $video->time_ago,
            'DURATION' => $video->duration
        ));
    }
}
if (empty($final)) {
	$final = $list;
}

$get_users = $db->rawQuery("SELECT * FROM " . T_USERS . " WHERE ((`username` LIKE '%$keyword%') OR CONCAT( `first_name`,  ' ', `last_name` ) LIKE  '%$keyword%') ORDER BY id ASC LIMIT 50");
if (!empty($get_users)) {
    $len = count($get_users);
    foreach ($get_users as $key => $user) {
        $user = PT_UserData($user, array('data' => true));
        $pt->last_user = false;
        if ($key == $len - 1) {
            $pt->last_user = true;
        }
        $final2 .= PT_LoadPage('search/user-list', array(
            'ID' => $user->id,
            'USER_DATA' => $user,
        ));
    }
}

if (empty($final2)) {
    $final2 = $list2;
}


$pt->videos      = $get_videos;
$pt->users      = $get_users;
$pt->page        = 'search';
$pt->title       = $lang->search . ' | ' . $pt->config->title;
$pt->description = $pt->config->description;
$pt->keyword     = $pt->config->keyword;
$pt->content     = PT_LoadPage('search/content', array(
    'VIDEOS' => $final,
    'USERS' => $final2,
    'KEYWORD' => $keyword
));