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

	class _messageList extends pageBase {
		public function load() {
			$this->limitID = "21";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				$this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/messageList.html");
			parent::load();
		}
		
		public function loadData() {
			/* 宣告 & 參數 */
			$dal = new messageDAL();
			$dalT = new message_typeDAL();
			$kw = parent::getGet("kw", "");
			$kt = parent::getGet("kt", "1");
			$ty = parent::getGet("ty", "0");
			$ft = parent::getGet("ft", "0");
			$lv = parent::getGet("lv", "0");
			$vv = parent::getGet("vv", "");
			$ih = parent::getGet("ih", "");
			$ir = parent::getGet("ir", "");
			$p = parent::getGet("p", "1");
			$pz = parent::getGet("pz", "40");
			$pr = 10;
			/* 條件式 */
			$intoAry = [];
			$aryF = [];
			if ($kw != "") {
				switch ($kt) {
					case "1":
						$intoAry[] = parent::addArray(":title", $kw);
						$aryF[] = "a.`Title`=:title";
						break;
					case "2":
						$intoAry[] = parent::addArray(":memberName", $kw);
						$aryF[] = "c.`MemberName`=:memberName";
						break;
					case "3":
						$intoAry[] = parent::addArray(":memberNick", $kw);
						$aryF[] = "c.`MemberNick`=:memberNick";
						break;
					case "4":
						$intoAry[] = parent::addArray(":loginName", $kw);
						$aryF[] = "c.`LoginName`=:loginName";
						break;
					case "5":
						$intoAry[] = parent::addArray(":memberEmail", $kw);
						$aryF[] = "c.`MemberEmail`=:memberEmail";
						break;
					case "6":
						$intoAry[] = parent::addArray(":memberMobile", $kw);
						$aryF[] = "c.`MemberMobile`=:memberMobile";
						break;
				}
			}
			if (preg_match("/^[1-9]\d*$/", $ty)) {
				$intoAry[] = parent::addArray(":typeID", $ty, "int");
				$aryF[] = "a.`TypeID`=:typeID";
			}
			if ($ft == "1" || $ft == "2") {
				$intoAry[] = parent::addArray(":fromType", $ft, "int");
				$aryF[] = "a.`FromType`=:fromType";
			}
			if (preg_match("/^[1-9]\d*$/", $lv)) {
				$intoAry[] = parent::addArray(":levelID", $lv, "int");
				$aryF[] = "a.`LevelID`=:levelID";
			}
			if ($vv == "1" || $vv == "0") {
				$intoAry[] = parent::addArray(":visible", $vv, "int");
				$aryF[] = "a.`Visible`=:visible";
			}
			if ($ih == "1" || $ih == "0") {
				$intoAry[] = parent::addArray(":isHot", $ih, "int");
				$aryF[] = "a.`IsHot`=:isHot";
			}
			if ($ir == "1" || $ir == "0") {
				$intoAry[] = parent::addArray(":isRead", $ir, "int");
				$aryF[] = "a.`IsRead`=:isRead";
			}
			$filter = count($aryF) ? implode(" AND ", $aryF) : "";
			$orderExp = "a.`Visible` DESC,a.`IsHot` DESC,a.`IsRead` DESC,a.`ID` DESC";

			/* 分頁 */
			$rows = $dal->countRecordset($intoAry, $filter);
			$p = ($p > 0) ? $p : 1;
			$pz = ($pz > 0) ? $pz : 40;
			$pMax = ($rows > 0) ? ceil($rows / $pz) : 1;
			$p = ($p > $pMax) ? $pMax : $p;
			$start = ($p - 1) * $pz;

			// 頁導航
			$pagtxt = ($rows > 0) ? parent::generatePagination($p, $pMax) : '沒有資料!';
			
			/* 資料 */
			$data = $dal->executeRecordset($intoAry, $filter, $orderExp, $start, $pz);
			$data = $this->formatData($data);
			$list = new repeater();
			$list->id = "list";
			$list->dataSource = $data;
			parent::addControl($list);

			/* 類型下拉 */
			$dataT = $dalT->executeRecordset([], "a.`Visible`=1", "a.`OrderNO`");
			$listT = new repeater();
			$listT->id = "listT";
			$listT->dataSource = $dataT;
			parent::addControl($listT);

			/*  */
			parent::addParameter("kw", $kw);
			parent::addParameter("kt", $kt);
			parent::addParameter("ty", $ty);
			parent::addParameter("ft", $ft);
			parent::addParameter("lv", $lv);
			parent::addParameter("vv", $vv);
			parent::addParameter("ih", $ih);
			parent::addParameter("ir", $ir);
			parent::addParameter("p", $p);
			parent::addParameter("pz", $pz);
			parent::addParameter("pagtxt", $pagtxt);
			parent::loadData();
		}

		private function formatData($data) {
			$dalR = new re_messageDAL();
			foreach ($data as &$item) {
				$intoAry = [parent::addArray(":messageID", $item["ID"], "int")];
				$item["countR"] = $dalR->countRecordset($intoAry, "a.`MessageID`=:messageID AND a.`Visible`=1");
				/* ------ */
				$item["txtV"] = $item["Visible"] ? "<span style='color:#090;'>&#10003;</span>" : "<span style='color:#f00;'>&#10005;</span>";
				$item["txtH"] = $item["IsHot"] ? "<span style='color:#090;'>重要</span>" : "<span> - </span>";
				$item["txtR"] = $item["IsRead"] ? "<span style='color:#090;'> - </span>" : "<span style='color:#f00;'>未讀</span>";
				
				switch ($item["FromType"]) {
					case "1":
						$item["txtF"] = $item["MemberName"];
						$item["txtT"] = "管理者";
						break;
					case "2":
						$item["txtF"] = "管理者";
						if ($item["MemberID"] == "0") {
							$item["txtT"] = $item["LevelID"] ? ("群發(" . $item["LevelName"] . ")") : "群發";
						} else {
							$item["txtT"] = $item["MemberName"];
						}
						break;
					default:
						$item["txtF"] = "異常";
						$item["txtT"] = "異常";
						break;
				}
			}
			return $data;
		}
	}

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