<?php

include_once('./settings.php');

$cacheid = 'index-' . $userrole . "-" . $country . "-" . $page;
//echo $cacheid;
//
if (!$smarty->isCached('index.tpl', $cacheid)) {

    $output = "";
    //echo $pageapiurl;
    if ($pageapiurl) {
        $res = file_get_contents($pageapiurl);
        $res = json_decode($res);

        $output = listdeals($res, 'main');

    }
    $smarty->assign('output', $output);

    if ($page == "comments" || $page == "deal")
        $url = getapiurl('act=gdeals&st=new&ppg=' . $perpage);
    else
        $url = getapiurl('act=gmaincomments&ppg=' . $perpage);

    $sres = file_get_contents($url);
    $sres = json_decode($sres);
    $sboutput = listdeals($sres, 'sidebar');

    $smarty->assign('sboutput', $sboutput);

    $meta = genmeta($page, null);
    $smarty->assign('meta', $meta);

    $jsversion = file_get_contents($template . '/js/version.txt');
    $smarty->assign('jsversion', $jsversion);

    $cssversion = file_get_contents($template . '/css/version.txt');
    $smarty->assign('cssversion', $cssversion);


} else {
    // echo " Cached";
}
$smarty->display('index.tpl', $cacheid);


function genmeta($page, $stxt)
{
    global $slug, $res, $q, $apidomain, $sitedomain, $sitename, $cname, $pagenumber, $metadesc;
    $pagename = ucfirst($page);
    $stitle = $res && $res->data && $res->data[0] && $res->data[0]->title ? ucwords(truncateSentence($res->data[0]->title), 14) : '';
    //print_r($res->data[0]->image);
    $query = ucwords($q);

    $sdesc = $res && $res->data && $res->data[0] && $res->data[0]->pdetails ?  truncateSentence($res->data[0]->pdetails , 25) : '';

    switch ($page) {
        case 'deal':
            $title = $stitle . " Low Price Deal & Offers";
          //  $desc = $res->data[0]->title . " {$cname} Low Price Deal, Offer";
          $desc = $sdesc ? $sdesc . " {$cname} Low Price Deal, Offer" : $res->data[0]->title . " {$cname} Low Price Deal, Offer";
            $keywords = join(' ', $res->data[0]->titletags) . " offers, best deals {$cname}, 'low price', top offers 2024, online shopping discounts, {$cname} promotions";
            $image = isset($res->data[0]->image) ? $res->data[0]->image : '';
            $url = $sitedomain . "/deal/" . $slug;
            break;

        case 'post':
            $title = $stitle . "";
            $desc = $res->data[0]->title . " {$cname} post";
            $keywords = $stitle . " post";
            $image = isset($res->data[0]->image) ? $res->data[0]->image : '';
            $url = $sitedomain . "/post/" . $slug;
            break;


        case 'search':
            $title = "{$query} {$cname} Offers & Deals";
            $desc = "Discover the " . $query . " deals and top " . $query . " offers in {$cname} for 2024. Save on online shopping with exclusive discounts and promotions.";
            $image = isset($res->data[0]->image) ? $res->data[0]->image : '';
            $keywords = "{$query} offers, best deals {$cname}, top offers 2024, online shopping discounts, {$cname} promotions";

            $url = $sitedomain . "/deals/" . strtolower($query) ;
            $url = $pagenumber>1 ? $url . "/". $pagenumber : $url;
            break;


        case 'new':
            $title = "{$cname} Low Price Deals & Offers Today ";
            $desc =  " Today's " . $pagename . " deals and top offers in {$cname} for 2024. Save on online shopping with exclusive discounts and promotions. " . $metadesc;
            $keywords = $pagename . " offers, best deals {$cname}, top offers 2024, low price, online shopping discounts, {$cname} promotions";
            $image = isset($res->data[0]->image) ? $res->data[0]->image : '';
            $url = $sitedomain . "/";
            $url = $pagenumber>1 ? $url . "new/". $pagenumber : $url;
            break;

        default:
            $title = "{$pagename} {$cname}  Offers & Deals";
            $desc =  " Today's " . $pagename . " deals and top offers in {$cname} for 2024. Save on online shopping with exclusive discounts and promotions. " . $metadesc;
            $keywords = $pagename . " offers, best deals {$cname}, top offers 2024, low price, online shopping discounts, {$cname} promotions";
            $image = isset($res->data[0]->image) ? $res->data[0]->image : '';
            $url = $sitedomain . "/" . $page;
            $url = $pagenumber>1 ? $url . "/". $pagenumber : $url;
            break;
    }

    $meta = array("title" => $title, "desc" => $desc, "keywords" => $keywords, "image" => $image, "url" => $url);

    return (object) $meta;

}


function truncateSentence($sentence, $wordLimit = 15)
{
    $words = explode(' ', $sentence);
    if (count($words) > $wordLimit) {
        $words = array_slice($words, 0, $wordLimit);
        return implode(' ', $words);
    }
    return $sentence;
}