<?php
	require_once dirname(__FILE__) . "/dalBase.php";
	class pictureDAL extends dalBase {
		public $PictureID;
		public $PictureType;
		public $TypeOrderNO;
		public $Image;
		public $Title;
		public $Link;
		public $BanColor;
		public $DanDesc;
		public $ShowType;
		public $PictureVideo;

		public function loadRecord(): bool {
			$intoAry = [];
			$sql = "SELECT a.* FROM `picture` a WHERE ";
			if ($this->PictureType !== "" && $this->TypeOrderNO !== "") {
				$sql .= "a.`PictureType`=:pictureType AND a.`TypeOrderNO`=:typeOrderNO";
				$intoAry[] = parent::addArray(":pictureType", $this->PictureType, "int");
				$intoAry[] = parent::addArray(":typeOrderNO", $this->TypeOrderNO, "int");
			} else if ($this->PictureID !== "" && $this->PictureID !== "0") {
				$sql .= "a.`PictureID`=:pictureID";
				$intoAry[] = parent::addArray(":pictureID", $this->PictureID, "int");
			}else{
				$sql .= "1 = 2";
			}
			$reader = $this->data->executeReader($sql, $intoAry);

			if (!empty($reader)) {
				$this->PictureID = $reader["PictureID"];
				$this->PictureType = $reader["PictureType"];
				$this->TypeOrderNO = $reader["TypeOrderNO"];
				$this->Image = $reader["Image"];
				$this->Title = $reader["Title"];
				$this->Link = $reader["Link"];
				$this->BanColor = $reader["BanColor"];
				$this->DanDesc = $reader["DanDesc"];
				$this->ShowType = $reader["ShowType"];
				$this->PictureVideo = $reader["PictureVideo"];
				return true;
			} else {
				return false;
			}
		}
		public function insertRecord(): bool|int {
			$intoAry = [
				parent::addArray(":pictureType", $this->PictureType, "int"),
				parent::addArray(":typeOrderNO", $this->TypeOrderNO, "int"),
				parent::addArray(":image", $this->Image, "str", 128),
				parent::addArray(":title", $this->Title, "str", 50),
				parent::addArray(":link", $this->Link, "str", 128),
				parent::addArray(":banColor", $this->BanColor, "str", 11),
				parent::addArray(":danDesc", $this->DanDesc, "str", 200),
				parent::addArray(":showType", $this->ShowType, "int"),
				parent::addArray(":pictureVideo", $this->PictureVideo, "str", 128)
			];
			$sql = "INSERT INTO `picture` (`PictureType`, `TypeOrderNO`, `Image`, `Title`, `Link`, `BanColor`, `DanDesc`, `ShowType`, `PictureVideo`)".
				" VALUES (:pictureType, :typeOrderNO, :image, :title, :link, :banColor, :danDesc, :showType, :pictureVideo)";
			$this->data->executeNonQuery($sql, $intoAry, true);
			return (!$this->data->error) ? $this->data->getInsertID() : false;
		}
		public function updateRecord(): bool {
			$intoAry = [
				parent::addArray(":pictureType", $this->PictureType, "int"),
				parent::addArray(":typeOrderNO", $this->TypeOrderNO, "int"),
				parent::addArray(":image", $this->Image, "str", 128),
				parent::addArray(":title", $this->Title, "str", 50),
				parent::addArray(":link", $this->Link, "str", 128),
				parent::addArray(":banColor", $this->BanColor, "str", 11),
				parent::addArray(":danDesc", $this->DanDesc, "str", 200),
				parent::addArray(":showType", $this->ShowType, "int"),
				parent::addArray(":pictureVideo", $this->PictureVideo, "str", 128)
			];
			$sql = "UPDATE `picture` SET " .
				"`Image`=:image" .
				",`Title`=:title" .
				",`Link`=:link" .
				",`BanColor`=:banColor" .
				",`DanDesc`=:danDesc" .
				",`ShowType`=:showType" .
				",`PictureVideo`=:pictureVideo" .
				" WHERE `PictureType`=:pictureType AND `TypeOrderNO`=:typeOrderNO";
			$this->data->executeNonQuery($sql, $intoAry, true);
			return !$this->data->error;
		}
		public function replaceRecord(): bool {
			$intoAry = [
				parent::addArray(":pictureType", $this->PictureType, "int"),
				parent::addArray(":typeOrderNO", $this->TypeOrderNO, "int"),
				parent::addArray(":image", $this->Image, "str", 128),
				parent::addArray(":title", $this->Title, "str", 50),
				parent::addArray(":link", $this->Link, "str", 128),
				parent::addArray(":banColor", $this->BanColor, "str", 11),
				parent::addArray(":danDesc", $this->DanDesc, "str", 200),
				parent::addArray(":showType", $this->ShowType, "int"),
				parent::addArray(":pictureVideo", $this->PictureVideo, "str", 128)
			];
			$sql = "REPLACE INTO `picture` (`PictureType`, `TypeOrderNO`, `Image`, `Title`, `Link`, `BanColor`, `DanDesc`, `ShowType`, `PictureVideo`)".
				" VALUES (:pictureType, :typeOrderNO, :image, :title, :link, :banColor, :danDesc, :showType, :pictureVideo)";
			$this->data->executeNonQuery($sql, $intoAry, true);
			return !$this->data->error;
		}
		//查詢
		public function executeRecordset(array $intoAry = [], string $filter = "", string $order = "", int $start = 0, int $length = -1): array {
			$sql = "SELECT a.* FROM `picture` a";
			if ($filter !== "") $sql .= " WHERE $filter";
			$sql .= (!isset($order) || $order === "") ? " ORDER BY a.`TypeOrderNO`" : " ORDER BY $order";
			$length = is_int($length) ? $length : -1;
			return $this->data->executeRecordset($sql, $intoAry, $start, $length);
		}
		
		//查詢計數
		public function countRecordset(array $intoAry = [], string $filter = ""): int {
			$sql = "SELECT COUNT(*) FROM `picture` a";
			if ($filter !== "") $sql .= " WHERE $filter";
			return $this->data->executeScalar($sql, $intoAry);
		}

		//刪除
		public function deleteRecord(int $pictureID): bool {
			$intoAry = [
				parent::addArray(":pictureID", $pictureID, "int")
			];
			$sql = "DELETE FROM `picture` WHERE `PictureID`=:pictureID";
			$this->data->executeNonQuery($sql, $intoAry, true);
			return !$this->data->error;
		}

		//刪除類型
		public function deleteTypeRecord($pt): bool {
			$intoAry = [
				parent::addArray(":pictureType", $pt, "int")
			];
			$sql = "DELETE FROM `picture` WHERE `PictureType`=:pictureType";
			$this->data->executeNonQuery($sql, $intoAry, true);
			return !$this->data->error;
		}
	}
?>