<?php
	require_once dirname(__FILE__) . "/dalBase.php";
	class menuDAL extends dalBase {
		public $MenuID;
		public $ParentID;
		public $MenuName;
		public $MenuIcon;
		public $MenuLink;
		public $LinkTarget;
		public $Visible;
		public $OrderNO;
		public $IsOA;
		//
		public $ParentName;

		public function loadRecord(): bool {
			$intoAry = [];
			$sql = "SELECT a.*,b.`MenuName` AS `ParentName` FROM `menu` a LEFT JOIN `menu` b ON a.`ParentID`=b.`MenuID` WHERE ";
			if ($this->MenuID !== null) {
				$sql .= "a.`MenuID`=:menuID";
				$intoAry[] = parent::addArray(":menuID", $this->MenuID, "int");
			} else {
				$sql .= "a.`MenuName`=:menuName";
				$intoAry[] = parent::addArray(":menuName", $this->MenuName, "str", 50);
			}
			$reader = $this->data->executeReader($sql, $intoAry);

			if (!empty($reader)) {
				$this->MenuID = $reader["MenuID"];
				$this->ParentID = $reader["ParentID"];
				$this->MenuName = $reader["MenuName"];
				$this->MenuIcon = $reader["MenuIcon"];
				$this->MenuLink = $reader["MenuLink"];
				$this->LinkTarget = $reader["LinkTarget"];
				$this->Visible = $reader["Visible"];
				$this->OrderNO = $reader["OrderNO"];
				$this->IsOA = $reader["IsOA"];
				$this->ParentName = $reader["ParentName"];
				return true;
			} else {
				return false;
			}
		}
		public function insertRecord(): bool|int {
			$intoAry = [
				parent::addArray(":parentID", $this->ParentID, "int"),
				parent::addArray(":menuName", $this->MenuName, "str", 50),
				parent::addArray(":menuIcon", $this->MenuIcon, "str", 100),
				parent::addArray(":menuLink", $this->MenuLink, "str", 100),
				parent::addArray(":linkTarget", $this->LinkTarget, "str", 10),
				parent::addArray(":visible", $this->Visible, "int"),
				parent::addArray(":orderNO", $this->OrderNO, "int"),
				parent::addArray(":isOA", $this->IsOA, "int")
			];
			$sql = "INSERT INTO `menu` (`ParentID`, `MenuName`, `MenuIcon`, `MenuLink`, `LinkTarget`, `Visible`, `OrderNO`, `IsOA`)".
				" VALUES (:parentID, :menuName, :menuIcon, :menuLink, :linkTarget, :visible, :orderNO, :isOA)";
			$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(":menuName", $this->MenuName, "str", 50),
				parent::addArray(":menuIcon", $this->MenuIcon, "str", 100),
				parent::addArray(":menuLink", $this->MenuLink, "str", 100),
				parent::addArray(":linkTarget", $this->LinkTarget, "str", 10),
				parent::addArray(":visible", $this->Visible, "int"),
				parent::addArray(":orderNO", $this->OrderNO, "int"),
				parent::addArray(":isOA", $this->IsOA, "int")
			];
			$sql = "UPDATE `menu` SET ".
				"`ParentID`=:parentID".
				",`MenuName`=:menuName".
				",`MenuIcon`=:menuIcon".
				",`MenuLink`=:menuLink".
				",`LinkTarget`=:linkTarget".
				",`Visible`=:visible".
				",`OrderNO`=:orderNO".
				",`IsOA`=:isOA".
				" 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.*,b.`MenuName` AS `ParentName` FROM `menu` a LEFT JOIN `menu` b ON a.`ParentID`=b.`MenuID`";
			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 `menu` a LEFT JOIN `menu` b ON a.`ParentID`=b.`MenuID`";
			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 `menu` WHERE `MenuID`=:menuID";
			$this->data->executeNonQuery($sql, $intoAry, true);
			return !$this->data->error;
		}
	}
?>