<?php
require_once dirname(dirname(__FILE__))."/page.php";
require_once dirname(dirname(dirname(dirname(__FILE__))))."/dal/categoryDAL.php";
require_once dirname(dirname(dirname(dirname(__FILE__))))."/dal/productDAL.php";
class product extends page{
	/* 取得商品列表網址
	 * @param $id 商品分類ID
	 * @param $p 頁數
	 */
	public function linkProductList($id = 0, $p = 0) {
		if ($this->htVisible[4] === 1) {
			$dalP = new productDAL();
			$dal = new categoryDAL();
			if (!empty($id) && $id != 0) {
				$dal->CategoryID = $id;
				$dal->loadRecord();
				if (($dal->Visible != 1 && $dal->Disable != 0) || ((int)$dal->ParentID > 0 && $dal->ParentVisible != 1 && $dal->ParentDisable != 0) || ((int)$dal->ParentID > 0 && (int)$dal->GrandID > 0 && $dal->GrandVisible != 1 && $dal->GrandDisable != 0)) {
					$id = 0;
					$p = 0;
				}
			}
			
			$pz = $this->registry->productPageSize ?? "20";
			$pz = (int)$pz;
		
			$intoAry = [];
			$filter = "a.`Visible`=1 AND a.`Disable`=0 AND b.`Visible`=1 AND b.`Disable`=0 AND (b.`ParentID`=0 OR c.`Visible`=1 OR c.`Disable`=0) AND (b.`ParentID`=0 OR c.`ParentID`=0 OR d.`Visible`=1 OR d.`Disable`=0)";
			if ($id != 0) {
				$intoAry[] = $this->addArray(":categoryID", $id, "int");
				$intoAry[] = $this->addArray(":parentID", $id, "int");
				$intoAry[] = $this->addArray(":grandID", $id, "int");
				$filter .= " AND (a.`CategoryID`=:categoryID OR b.`ParentID`=:parentID OR c.`ParentID`=:grandID)";
			}
			$rows = $dalP->countRecordset($intoAry, $filter);
			$max = ceil($rows / $pz);
			if ((int)$p > $max) $p = $max;
		
			$link = "/{$this->htLink[4]}";
			if ($this->registry->webLinkType === "2") {
				if ($id != 0) $link .= "/cate/{$dal->TwPathName}";
				if ($p != 0) $link .= "/page/{$p}";
				$link .= "/";
			} else {
				if ($id != 0) $link .= $id;
				if ($p != 0) $link .= "_{$p}";
				$link .= ".html";
			}
		} else {
			$link = "/";
		}
		return $link;
	}
	
	/* 取得商品網址
	 * @param $id 商品ID
	 */
	public function linkProductDetail($id = 0) {
		if ($this->htVisible[4] === 1) {
			$dal = new productDAL();
			$dal->ProductID = $id;
			$dal->loadRecord();
			if (($dal->Visible != 1 && $dal->Disable != 0) || ($dal->CategoryVisible != 1 && $dal->CategoryDisable != 0) || ((int)$dal->ParentID > 0 && $dal->ParentVisible != 1 && $dal->ParentDisable != 0) || ((int)$dal->ParentID > 0 && (int)$dal->GrandID > 0 && $dal->GrandVisible != 1 && $dal->GrandDisable != 0)) $id = 0;
		
			$link = "/{$this->htLink[4]}";
			switch ($this->registry->webLinkType) {
				case "2":
					$link .= ($id != 0) ? "/{$dal->TwPathName}/" : "/";
					break;
				case "3":
					$link .= ($id != 0) ? "-{$dal->PathName}.html" : ".html";
					break;
				default:
					$link .= ($id != 0) ? "-{$id}.html" : ".html";
			}
		} else {
			$link = "/";
		}
		return $link;
	}
	
	/* 結構化資料
	 * @param $type 列表/內頁
	 * @param $id 分類ID/文章ID
	 */
	public function seoGoogleSearch($id = 0, $type = 0) {
		// 分數(真)
		$dalS = new scoreDAL();
		$txt = "";
		$image = $this->getLogo();
	
		if ($type == 0) {
			$dal = new categoryDAL();
			$dal->CategoryID = $id;
			$dal->loadRecord();
			if ($dal->OGImage != "" && $this->image_exists($dal->OGImage)) $image = $dal->OGImage;
			if ($dal->IsShowJason == 1) $txt .= $this->reviewHtml($image, $dal->CategoryName, $dal->RatingValue, $dal->BestRating);
			if ($dal->IsShowStars == 1) $txt .= $this->isShowStars($dal->RatingValue, $dal->ReviewCount);
		} else if ($type == 1) {
			$dal = new productDAL();
			$dal->ProductID = $id;
			$dal->loadRecord();
			if ($dal->IconImage != "" && $this->image_exists($dal->IconImage)) $image = $dal->IconImage;
			// 按照設定取得分數
			if ($this->registry->isScore === 2 && $this->registry->isCustomer === 1) {    // 啟用真實評分功能，必須同時啟用會員功能
				$dalS->TargetType = 3;
				$dalS->TargetID = $id;
				$ratingValue = $dalS->getScore;
			} else {
				$ratingValue = $dal->RatingValue;
			}
			if ($dal->IsShowJason == 1) $txt .= $this->productHtml($id, $dal->Price, $image, $dal->ProductName, $dal->Introduction, $dal->BestRating, $dal->RatingCount, $dal->RatingValue);
			if ($dal->IsShowStars == 1) $txt .= $this->isShowStars($dal->RatingValue, $dal->ReviewCount);
		}
		return $txt;
	}
}
?>