<?php
	require_once "pageBase.php";
	require_once "../../bin/system/conver.php";
	require_once "../../dal/menuDAL.php";
	class _menu2 extends pageBase {
		private $dal;
		private array $aryJ;

		public function __construct() {
			$this->dal = new menuDAL();
			$this->aryJ = ["err" => "0", "msg" => ""];
		}

		public function load(): void {
			$this->aryJ["msg"] = "";

			$action = parent::getPost("do");
			switch ($action) {
				case "vis":
					$this->visible();
					break;
				case "del":
					$this->delete();
					break;
				default:
					$this->handleError("參數異常!請重新整理頁面再試一次！");
			}
		
			if ($this->aryJ["msg"] !== "") $this->aryJ["msg"] = urlencode($this->aryJ["msg"]);
			echo urldecode(json_encode($this->aryJ));
			parent::load();
		}
		
		private function visible() {
			$id = parent::getPost("id");
			$vis = parent::getPost("vis");
			$this->dal->MenuID = $id;
			$this->dal->loadRecord();
			$this->dal->Visible = $vis;
			if (!$this->dal->updateRecord()) {
				$this->handleError("變更狀態失敗!請重新整理頁面再試一次!");
			}else{
				$this->aryJ["newVisible"] = $vis;
			}
		}
		private function delete() {
			$id = parent::getPost("id");
			if (!$this->dal->deleteRecord($id)) $this->handleError("刪除失敗!請重新整理頁面再試一次!");
		}
		
		private function handleError(string $errorMessage): void {
			$this->aryJ["err"] = "1";
			$this->aryJ["msg"] = $errorMessage;
		}
	}

	$page = new _menu2();
	$page->load();
?>