<?php
$searchq = filter_input(INPUT_GET, 'q', FILTER_SANITIZE_STRING);
$type = filter_input(INPUT_GET, 'search', FILTER_SANITIZE_STRING);
$page = (isset($_GET['page'])) ? filter_input(INPUT_GET, 'page', FILTER_SANITIZE_STRING) : 1;
$collection = array();
$files = array();
$categories = array();
$collection = array();
$orderby = ['name', 'ASC'];
$schar = '';
if (strlen($searchq) >= 3) {
    $schar = str_split($searchq, 3);
    $schar = implode('', $schar);
}
$YouMayFiles = array();
$ReletedAlso = array();
if ($type == 'song') {
    $pagelimit = FILES_PER_PAGE;
    $db->pageLimit = $pagelimit;
    $db->join(TABLE_CAT . ' c', 'f.cid=c.id', "INNER");
    $db->orderBy('f.' . $orderby[0], $orderby[1]);
    $db->orderBy('f.id');
    $db->where('f.name', '%' . $searchq . '%', 'LIKE');
    $db->where('f.status', 1);
    $db->where('f.approved_at', NULL, 'IS NOT');
    $db->where('f.deleted_at', NULL,'IS');
    $files = $db->paginate(TABLE_FILES . ' f', $page, 'f.id,f.slug,f.dname,f.ext,f.thumb,f.name,f.duration,f.download,c.name as category_name,c.folder,c.thumb as category_thumb');
    $totalPage = $db->totalPages;
    if ($files) {
        $db->where('f.id', array_column($files, 'id'), 'NOT IN');
    }
    $YouMayFiles =  $db->join(TABLE_CAT . ' c', 'f.cid=c.id', "INNER")->orderBy('RAND()')->where('f.status', 1)
        ->where('f.approved_at', NULL, 'IS NOT')
        ->where('f.deleted_at', NULL,'IS')->get(TABLE_FILES . ' f', 5, 'f.name,f.id,f.dname,f.ext,f.slug,f.thumb,f.duration,f.download,c.folder');
}

if ($type == 'album') {
    $pagelimit = CATEGORY_PER_PAGE;
    $db->pageLimit = $pagelimit;
    $db->orderBy('c.' . $orderby[0], $orderby[1]);
    $db->orderBy('c.id');
    $db->where('c.name', '%' . $searchq . '%', 'LIKE');
    $db->where('c.status', 1);
    $categories = $db->paginate(TABLE_CAT . ' c', $page, 'c.name,c.slug,c.thumb,c.totalitem,c.release_year,c.is_cat');
    $totalPage = $db->totalPages;
    $YouMayFiles =  $db->join(TABLE_CAT . ' c', 'f.cid=c.id', "INNER")->orderBy('RAND()')->where('f.deleted_at',NULL,'IS')->where('f.approved_at', NULL, 'IS NOT')->where('f.status',1)->get(TABLE_FILES . ' f', 5, 'f.name,f.slug,f.thumb,f.duration,f.download,f.dname,f.ext,c.folder');
}

if ($type == 'artist') {
    $pagelimit = CATEGORY_PER_PAGE;
    $db->pageLimit = $pagelimit;
    $db->orderBy('a.' . $orderby[0], $orderby[1]);
    $db->orderBy('a.id');
    $db->where('a.name', '%' . $searchq . '%', 'LIKE');
    $db->where('a.status', 1);
    $collection = $db->paginate('artist' . ' a', $page, 'a.name,a.slug,a.thumb');
    $totalPage = $db->totalPages;
    $YouMayFiles =  $db->join(TABLE_CAT . ' c', 'f.cid=c.id', "INNER")->orderBy('RAND()')->where('f.deleted_at',NULL,'IS')->where('f.approved_at', NULL, 'IS NOT')->where('f.status',1)->get(TABLE_FILES . ' f', 5, 'f.name,f.slug,f.thumb,f.duration,f.download,f.dname,f.ext,c.folder');
}

$pageLink = APP_URL . '/search/list?q=' . $searchq . '&search=' . $type . '&page={{PAGE}}';
$breadhtml = '<nav aria-label="breadcrumb"><ol class="breadcrumb mt-2"><li class="breadcrumb-item"><a href="' . APP_URL . '"><i class="fa fa-home" aria-hidden="true"></i> Home</a></li>';
$breadcrumb[] =  [
    "@type" => "ListItem",
    "position" => 2,
    "item" => [
        "@id" => $AppProtocol . $request_uri,
        "name" => 'Search'
    ]
];
$breadhtml .= '<li class="breadcrumb-item"><a href="' . $AppProtocol . $request_uri . '">Search</a></li>';
$breadhtml .= '</ol></nav>';
$schema = [
    "@context" => "https://schema.org",
    "@type" => "BreadcrumbList",
    "itemListElement" => $breadcrumb
];
$title = $searchq ? $searchq . ' - Search Result' : 'Search Result';
$title .= ' - ' . APP_NAME;
require_once $Template . '/header.php';
require_once $Template . '/Search.php';
require_once $Template . '/footer.php';
