<?php

class SearchController 
    extends baseController 
{
    function index() 
    {
        $is_json = !$_GET['disable_json'];
        $term    = $_GET['term'] ? $_GET['term'] : $_GET['q'];

        $term = str_replace(["%"], "\\%", $term);

        //$deals          = [];
        //$groups       = [];
        //$objects      = [];
        //$categories   = [];
        //$store_cats   = [];
        //$items        = [];
        //$pages        = [];
        // $users       = [];
        //$comments     = [];
		$blogs			= [];

        //$projectModel   = new projectModel();
        //$groupModel     = new groupModel();
        //$objectModel    = new objectModel();
        //$categoryModel  = new categoryModel();
        //$storeModel     = new storeModel();
        //$pageModel      = new pageModel();
        //$userModel      = new userModel();
		$blogModel        = new blogModel();
        
        //$deals      = $projectModel->getList(['search' => $term, 'is_active' => 1]);
		//$deals      = $projectModel->getList(['is_active' => 1]);
//		print_r($deals);
//		exit;
        //$groups     = $groupModel->getList(['search' => $term]);
        //$objects      = $objectModel->getList(['search' => $term]);
		$blogs    	  = $blogModel->getList(['search' => $term]);
		
        //$categories = $categoryModel->getList(['search' => $term]);
        //$store_cats = $storeModel->getCategories(['search' => $term]);
        //$items      = $storeModel->getList(['search' => $term]);
        //$pages      = $pageModel->getList(['search' => $term]);
        //$users      = $userModel->getList(['search' => $term]);
        //$comments     = $groupModel->getCommentslist(['search' => $term]);
        
        $results = [
        //    "deals"        => $deals,
        //    "groups"     => $groups,
        //    "objects"      => $objects,
        //    "categories" => $categories,
        //    "store_cats" => $store_cats,
        //    "items"      => $items,
        //    "pages"      => $pages,
        //    "users"      => $users,
        //  "comments"     => $comments,
        	"blogs"		   => $blogs
        ];

        if (! $is_json) {
            //$total_count_items = count($deals) /* + count($groups) + count($objects) + count($categories) + count($store_cats) + count($items) + count($pages)*/ + count($users) /* + count($comments) */;
			$total_count_items = count($blogs);
            require 'views/search/search.tpl.php';
            exit;
        }

        $i = 0;
        echo '[';
        foreach ($results as $type => $type_items) {
            foreach ($type_items as $current_item) {
                $infos        = $this->getAutoCompleteInfo($type, $current_item);
                $current_item = $current_item + $infos;

                echo ($i++ > 0 ? ", " : "") . json_encode($current_item);
            }
        }
        echo ']';
        exit;
    }

    function getAutoCompleteInfo($type, $item)
    {
        $infos = [
            "type"  => $type,
            "id"    => "",
            "value" => "",
            "label" => "",
            "url"   => "",
            "thumb" => "",
        ];

        switch ($type) {
        case 'deals':
            $infos['id']    = $item['id'];
            $infos['value'] = $item['title'];
            $infos['label'] = substr($item['title'],0,50);
            $infos['url']   = "/listing/" . $item['id'];
            $infos['thumb'] = "" . $item['main_img'];
            break;
        case 'groups':
            $infos['id']    = $item['id'];
            $infos['value'] = $item['title'];
            $infos['label'] = $item['title'];
            $infos['url']   = "/group/" . $item['id'];
            $infos['thumb'] = "/" . $item['main_img'];
            break;
        case 'objects':
            $infos['id']    = $item['id'];
            $infos['value'] = $item['title'];
            $infos['label'] = $item['title'];
            $infos['url']   = "/content/" . $item['id'];
            $infos['thumb'] = "/" . $item['main_img'];
            break;
        case 'categories':
            $infos['id']    = $item['id'];
            $infos['value'] = $item['title'];
            $infos['label'] = $item['title'];
            $infos['url']   = "/category/" . $item['id'];
            $infos['thumb'] = "/static/images/users/default-avatar.png";
            break;
        case 'store_cats':
            $infos['id']    = $item['id'];
            $infos['value'] = $item['title'];
            $infos['label'] = $item['name'];
            $infos['url']   = "/store?cat=" . $item['id'];
            $infos['thumb'] = "/static/images/users/default-avatar.png";
            break;
        case 'items':
            $infos['id']    = $item['id'];
            $infos['value'] = $item['title'];
            $infos['label'] = $item['name'];
            $infos['url']   = "/store?item=" . $item['id'];
            $infos['thumb'] = "/static/images/users/default-avatar.png";
            break;
		case 'blogs':
			$infos['id']    = $item['id'];
			$infos['value'] = $item['title'];
			$infos['label'] = $item['title'];
			$infos['url']   = "/blog/" . $item['id'];
			$infos['thumb'] = "/static/system/item_categories_placeholder.png";
			break;            

//        case 'users':
//            $user_image = $item['image'];
//            if (empty($user_image) || $user_image === NULL)
//                $user_image = "/static/images/users/default_avatar.png";
//
//            if (false === strpos($user_image, "graph.facebook.com") || false === strpos($user_image, "http"))
//                $user_image = $user_image;
//                //$user_image = "/static/images/users/agency" . $user_image;
//
//            $infos['id']    = $item['id'];
//            if($item['name'] === NULL){
//                $infos['value'] = 'User';
//                $infos['label'] = 'User';                     
//            } else {
//                $infos['value'] = $item['name'];
//                $infos['label'] = $item['name'];
//            }        
//            $infos['url']   = "/profile/" . $item['user_slug'];
//            $infos['thumb'] = $user_image;
//            break;
        case 'pages':
            $infos['id']    = $item['id'];
            $infos['value'] = $item['name'];
            $infos['label'] = $item['name'];
            $infos['url']   = $item['slug'];
            $infos['thumb'] = "static/images/users/default_avatar.png";
            break;
        case 'comments':
            $infos['id']    = $item['id'];
            $infos['value'] = $item['content'] . " & " . $item['user_id'];
            $infos['label'] = $item['content'] . " & " . $item['user_id'];
            $infos['url']   = "/group/" . $item['group_id'];
			$infos['thumb'] = "/static/images/users/default_avatar.png";
            break;          
        }
        return $infos;
    }
}
?>
