<?php
	require_once dirname(__FILE__) . "/dalBase.php";
	class webmenuDAL extends dalBase {
		public $MenuID;
		public $ParentID;
		public $ChildType;
		public $MenuType;
		public $LinkType;
		public $LinkID;
		public $LinkValue;
		public $LinkTarget;
		public $ChildTarget;
		public $MenuTitle;
		public $SubTitle;
		public $MenuIcon;
		public $SubIcon;
		public $Visible;
		public $OrderNO;

		public function loadRecord(): bool {
			$intoAry = [];
			$sql = "SELECT a.* FROM `webmenu` a WHERE a.`MenuID`=:menuID";
			$intoAry[] = parent::addArray(":menuID", $this->MenuID, "int");
			$reader = $this->data->executeReader($sql, $intoAry);

			if (!empty($reader)) {
				$this->MenuID = $reader["MenuID"];
				$this->ParentID = $reader["ParentID"];
				$this->ChildType = $reader["ChildType"];
				$this->MenuType = $reader["MenuType"];
				$this->LinkType = $reader["LinkType"];
				$this->LinkID = $reader["LinkID"];
				$this->LinkValue = $reader["LinkValue"];
				$this->LinkTarget = $reader["LinkTarget"];
				$this->ChildTarget = $reader["ChildTarget"];
				$this->MenuTitle = $reader["MenuTitle"];
				$this->SubTitle = $reader["SubTitle"];
				$this->MenuIcon = $reader["MenuIcon"];
				$this->SubIcon = $reader["SubIcon"];
				$this->Visible = $reader["Visible"];
				$this->OrderNO = $reader["OrderNO"];
				return true;
			} else {
				return false;
			}
		}
		public function insertRecord(): bool|int {
			$intoAry = [
				parent::addArray(":parentID", $this->ParentID, "int"),
				parent::addArray(":childType", $this->ChildType, "int"),
				parent::addArray(":menuType", $this->MenuType, "int"),
				parent::addArray(":linkType", $this->LinkType, "int"),
				parent::addArray(":linkID", $this->LinkID, "int"),
				parent::addArray(":linkValue", $this->LinkValue, "str", 200),
				parent::addArray(":linkTarget", $this->LinkTarget, "int"),
				parent::addArray(":childTarget", $this->ChildTarget, "int"),
				parent::addArray(":menuTitle", $this->MenuTitle, "str", 50),
				parent::addArray(":subTitle", $this->SubTitle, "str", 200),
				parent::addArray(":menuIcon", $this->MenuIcon, "str", 200),
				parent::addArray(":subIcon", $this->SubIcon, "str", 200),
				parent::addArray(":visible", $this->Visible, "int"),
				parent::addArray(":orderNO", $this->OrderNO, "int")
			];
			$sql = "INSERT INTO `webmenu` (`ParentID`, `ChildType`, `MenuType`, `LinkType`, `LinkID`, `LinkValue`, `LinkTarget`, `ChildTarget`, `MenuTitle`, `SubTitle`, `MenuIcon`, `SubIcon`, `Visible`, `OrderNO`)".
				" VALUES (:parentID, :childType, :menuType, :linkType, :linkID, :linkValue, :linkTarget, :childTarget, :menuTitle, :subTitle, :menuIcon, :subIcon, :visible, :orderNO)";
			$this->data->executeNonQuery($sql, $intoAry, true);
			return (!$this->data->error) ? $this->data->getInsertID() : false;
		}
		public function updateRecord(): bool {
			$intoAry = [
				parent::addArray(":menuID", $this->MenuID, "int"),
				parent::addArray(":parentID", $this->ParentID, "int"),
				parent::addArray(":childType", $this->ChildType, "int"),
				parent::addArray(":menuType", $this->MenuType, "int"),
				parent::addArray(":linkType", $this->LinkType, "int"),
				parent::addArray(":linkID", $this->LinkID, "int"),
				parent::addArray(":linkValue", $this->LinkValue, "str", 200),
				parent::addArray(":linkTarget", $this->LinkTarget, "int"),
				parent::addArray(":childTarget", $this->ChildTarget, "int"),
				parent::addArray(":menuTitle", $this->MenuTitle, "str", 50),
				parent::addArray(":subTitle", $this->SubTitle, "str", 200),
				parent::addArray(":menuIcon", $this->MenuIcon, "str", 200),
				parent::addArray(":subIcon", $this->SubIcon, "str", 200),
				parent::addArray(":visible", $this->Visible, "int"),
				parent::addArray(":orderNO", $this->OrderNO, "int")
			];
			$sql = "UPDATE `webmenu` SET ".
				"`ParentID`=:parentID".
				",`ChildType`=:childType".
				",`MenuType`=:menuType".
				",`LinkType`=:linkType".
				",`LinkID`=:linkID".
				",`LinkValue`=:linkValue".
				",`LinkTarget`=:linkTarget".
				",`ChildTarget`=:childTarget".
				",`MenuTitle`=:menuTitle".
				",`SubTitle`=:subTitle".
				",`MenuIcon`=:menuIcon".
				",`SubIcon`=:subIcon".
				",`Visible`=:visible".
				",`OrderNO`=:orderNO".
				" WHERE `MenuID`=:menuID";
			$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 `webmenu` a";
			if ($filter !== "") $sql .= " WHERE $filter";
			$sql .= (!isset($order) || $order === "") ? " ORDER BY a.`OrderNO`" : " 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 `webmenu` a";
			if ($filter !== "") $sql .= " WHERE $filter";
			return $this->data->executeScalar($sql, $intoAry);
		}

		//刪除
		public function deleteRecord(int $menuID): bool {
			$intoAry = [
				parent::addArray(":menuID", $menuID, "int")
			];
			$sql = "DELETE FROM `webmenu` WHERE `MenuID`=:menuID";
			$this->data->executeNonQuery($sql, $intoAry, true);
			return !$this->data->error;
		}
		//刪除2
		public function delete2Record(int $parentID): bool {
			$intoAry = [
				parent::addArray(":parentID", $parentID, "int")
			];
			$sql = "DELETE FROM `webmenu` WHERE `ParentID`=:parentID";
			$this->data->executeNonQuery($sql, $intoAry, true);
			return !$this->data->error;
		}
	}
?>