<?php
	require_once "pageBase.php";
	require_once "../bin/web/repeater.php";
	require_once "../bin/web/tables/article.php";
	require_once "../bin/web/tables/news.php";
	require_once "../bin/web/tables/faq.php";
	require_once "../bin/web/tables/product.php";
	require_once "../bin/web/tables/book.php";
	require_once "../bin/web/tables/contact.php";
	require_once "../bin/web/tables/order.php";
	require_once "../dal/webmenuDAL.php";
	class _webmenuList extends pageBase {
		public function load() {
			$this->limitID = "49";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				$this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/webmenuList.html");
			parent::load();
		}
		
		public function loadData() {
			$dal = new webmenuDAL();
			$data = $dal->executeRecordset([], "a.`ParentID`=0", "a.`OrderNO`, a.`MenuID`");
			$data = $this->formatData($data);
			$list = new repeater();
			$list->id = "list";
			$list->dataSource = $data;
			parent::addControl($list);

			parent::loadData();
		}

		public function formatData($data) {
			$aryCT = ["沒有子選單", "子分類選單", "內頁選單", "自訂選單"];
			$aryMT = ["自訂", "首頁", "文章", "產品", "最新消息", "問與答", "影音相本", "聯絡我們", "線上訂單"];

			foreach ($data as &$item) {
				$item["Visible"] = ($item["Visible"] == "1") ? "<span class=\"badge badge_soft_success\">啟用</span>" : "<span class=\"badge badge_soft_danger\">不啟用</span>";
				$item["ChildTypeText"] = $aryCT[$item["ChildType"]] ?? "-";
				$item["MenuTypeText"] = $aryMT[$item["MenuType"]] ?? "-";
				
				switch ($item["MenuType"]) {
					case "0":
						$item["WebLink"] = $item["LinkValue"];
						break;
					case "1":
						$item["WebLink"] = "/";
						break;
					default:
						$item["WebLink"] = $this->getLink($item["MenuType"], $item["LinkType"], $item["LinkID"]);
				}

				if ($item["MenuType"] == 0 && $item["LinkValue"] !== "") {
					$item["WebLink2"] = $item["WebLink"];
				} else if ($item["ChildType"] !== 0) {
					$item["WebLink"] = "-";
					$item["WebLink2"] = "javascript:void(0);";
				} else {
					$item["WebLink2"] = $item["WebLink"];
				}
				
				$item["LinkTargetText"] = ($item["LinkTarget"] == "1") ? "另開分頁" : "同頁";
			}
			return $data;
		}
		
		private function getLink($mt = 2, $lt = 1, $id = 0): string {
			$article = new article();
			$news = new news();
			$faq = new faq();
			$product = new product();
			$book = new book();
			$contact = new contact();
			$order = new order();

			$linkMethods = [
				"2" => [$article, "linkArticleList", "linkArticleDetail"],
				"3" => [$product, "linkProductList", "linkProductDetail"],
				"4" => [$news, "linkNewsList", "linkNewsDetail"],
				"5" => [$faq, "linkFaqList", "linkFaqDetail"],
				"6" => [$book, "linkBookList", "linkBookDetail"],
				"7" => [$contact, "linkContact", ""],
				"8" => [$order, "linkOrder", ""]
			];
			
			if (isset($linkMethods[$mt])) {
				[$linkObject, $listMethod, $detailMethod] = $linkMethods[$mt];
		
				if ($lt === 2) {
					$link = ($detailMethod !== "") ? $linkObject->{$detailMethod}($id) : "";
				} else {
					$link = $linkObject->{$listMethod}($id);
				}
			} else {
				$link = "/";
			}

			return $link;
		}
	}

	$page = new _webmenuList();
	$page->load();
	$page->render();
?>