<?php
	require_once "pageBase.php";
	require_once "../dal/message_typeDAL.php";
	require_once "../bin/web/repeater.php";

	class _message_typeList extends pageBase {
		public function load() {
			$this->limitID = "83";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				$this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/message_typeList.html");
			parent::load();
		}
		
		public function loadData() {
			/* 宣告&參數 */
			$dal = new message_typeDAL();
			$vv = parent::getGet("vv", "");
			$im = parent::getGet("im", "");
			$iu = parent::getGet("iu", "");

			// 確保 $vv、$im 和 $iu 只能是 "0"、"1" 或空字符串
			$vv = in_array($vv, ["0", "1"]) ? $vv : "";
			$im = in_array($im, ["0", "1"]) ? $im : "";
			$iu = in_array($iu, ["0", "1"]) ? $iu : "";

			//搜尋條件、排序順序
			$intoAry = [];
			$aryF = [];
			if ($vv !== "") {
				$intoAry[] = parent::addArray(":visible", $vv, "int");
				$aryF[] = "a.`Visible` = :visible";
			}
			if ($im !== "") {
				$intoAry[] = parent::addArray(":isM", $im, "int");
				$aryF[] = "a.`IsM` = :isM";
			}
			if ($iu !== "") {
				$intoAry[] = parent::addArray(":isU", $iu, "int");
				$aryF[] = "a.`IsU` = :isU";
			}
			$filter = count($aryF) ? implode(" AND ", $aryF) : "";
			$orderExp = "a.`Visible` DESC, a.`OrderNO`";

			/*  */
			parent::addParameter("vv", $vv);
			parent::addParameter("im", $im);
			parent::addParameter("iu", $iu);
			
			$data = $dal->executeRecordset($intoAry, $filter, $orderExp);
			$data = $this->formatData($data);
			$list = new repeater();
			$list->id = "list";
			$list->dataSource = $data;
			parent::addControl($list);

			parent::loadData();
		}

		private function formatData($data) {
			foreach ($data as &$item) {
				$item["txtV"] = $item["Visible"] ? "&#10003;" : "&#10005;";
				$item["txtM"] = $item["IsM"] ? "&#10003;" : "&#10005;";
				$item["txtU"] = $item["IsU"] ? "&#10003;" : "&#10005;";
			}
			return $data;
		}
	}

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