<?php
	require_once "pageBase.php";
	require_once "bin/web/repeater.php";
	require_once "dal/searchItemDAL.php";

	class _searchItem extends pageBase {
		public function load() {
			$this->loadData();
			parent::loadTemplateFile("templates/{$this->registry->webTemplates}/searchItem.html");
			parent::load();
		}
		
		public function loadData() {
			// 橫幅廣告
			$this->picturetype = "100";
			
			////logo
			parent::addParameter("fbImage",$this->NoPicImage);
			
			//宣告
			$dal = new searchItemDAL();
			$p = parent::getGet("p", "1");
			$pz = "12";
			if($pz == "0" || !preg_match("/^\d+$/", $pz)) $pz = "10";
			$p = (int)$p;
			$pz = (int)$pz;
			$rang = 10;
			$searchkw = parent::getGet("kw");
			$this->searchkw = $searchkw;
			parent::addParameter("searchkw",$this->searchkw);
			
			if($this->searchkw != ""){
				$article = new article();
				$news = new news();
				$faq = new faq();
				$product = new product();
				$book = new book();
				$rows = $dal->countRecordset(array(), $this->searchkw);
				$pMax = ceil($rows / $pz);
				if($p > $pMax) $p = $pMax;
				if($p < 1) $p = 1;
				$start = ($p - 1) * $pz;
				
				$data = $dal->executeRecordset(array(), $this->searchkw, $start, $pz);
				for($i=0;$i<count($data);$i++){
					if(trim($data[$i]["Image"]) == "" || !parent::image_exists($data[$i]["Image"])){
						$data[$i]["Image"] = $this->NoPicImage;
					}
					switch($data[$i]["ItemLINK"]){
						case "1":
							$data[$i]["Link"] = ($data[$i]["ItemType"] == "1") ?$article->linkArticleList($data[$i]["ID"]):$article->linkArticleDetail($data[$i]["ID"]);
							break;
						case "2":
							$data[$i]["Link"] = ($data[$i]["ItemType"] == "1") ?$news->linkNewsList($data[$i]["ID"]):$news->linkNewsDetail($data[$i]["ID"]);
							break;
						case "3":
							$data[$i]["Link"] = ($data[$i]["ItemType"] == "1") ?$faq->linkFaqList($data[$i]["ID"]):$faq->linkFaqDetail($data[$i]["ID"]);
							break;
						case "4":
							$data[$i]["Link"] = ($data[$i]["ItemType"] == "1") ?$product->linkProductList($data[$i]["ID"]):$product->linkProductDetail($data[$i]["ID"]);
							break;
						case "5":
							$data[$i]["Link"] = ($data[$i]["ItemType"] == "1") ?$book->linkBookList($data[$i]["ID"]):$book->linkBookDetail($data[$i]["ID"]);
							break;
						default :
							$data[$i]["Link"] = "javascript:void(0);";
					}
					$data[$i]["Description"] = strip_tags($data[$i]["Description"]);
					$data[$i]["Description"] = mb_substr($data[$i]["Description"], 0, 50, "utf-8");
					$data[$i]["Description"] = str_replace("\r\n", " ", $data[$i]["Description"]);
				}
				$list = new repeater();
				$list->id = "list";
				$list->dataSource = $data;
				parent::addControl($list);
	
				//頁導航
				if($id == "") $id = 0;
				if($rows > 0){
					$pr = ceil($p / $rang) - 1;
					$pu = ($p == 1)?1:($p - 1);
					$pd = ($p == $pMax)?$pMax:($p + 1);
	
					$pagtxt = '<a href="javascript:void(0);" data-action="gopage" data-id="'.$pu.'"><i class="fa fa-angle-double-left"></i></a>';
					for($i=1;$i<11;$i++){
						$nm = $pr * $rang + $i;
						$pagtxt .= ($nm == $p)?'<strong class="number_hit">'.$nm.'</strong>':'<a href="javascript:void(0);" data-action="gopage" data-id="'.$nm.'">'.$nm.'</a>';
						if($nm == $pMax) break;
					}
					$pagtxt .= '<a href="javascript:void(0);" data-action="gopage" data-id="'.$pd.'" class="next"><i class="fa fa-angle-double-right"></i></a>';
				}else{
					$pagtxt = '沒有資料';
				}
				parent::addParameter("pagtxt",$pagtxt);
			}

			//父類方法
			parent::loadData();
		}
	}

	$page = new _searchItem();
	$page->load();
	$page->render();
?>