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

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

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

			$action = parent::getPost("do");
			switch ($action) {
				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 delete() {
			$id = parent::getPost("id");
			if (!$this->dal->deleteRecord($id)) $this->handleError("刪除失敗!請重新整理頁面再試一次!");
		}
		
		private function handleError(string $errorMessage): void {
			$this->aryJ["error"] = "1";
			$this->aryJ["msg"] = $errorMessage;
		}
	}

	$page = new _user();
	$page->load();
	echo $page->txtJ;
?>