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

	class _message extends pageBase {
		public function load() {
			if ($this->registry->isCustomer == 0) {
				header("Location: /404.php");
				exit();
			}

			if (isset($_SESSION["member_id"]) && $_SESSION["member_id"] != "") {
				$this->loadData();
			} else {
				$this->locationHref = "member_login.html";
			}
			
			parent::loadTemplateFile("templates/{$this->registry->webTemplates}/message.html");
			parent::load();
		}

		public function loadData() {
			//橫幅廣告
			$this->picturetype = "130";

			/* 宣告 & 參數 */
			$dal = new messageDAL();
			$dalT = new message_typeDAL();
			$dalR = new re_messageDAL();
			$dalM = new memberDAL();
			$typ = (int)parent::getGet("typ", 0);
			$typ2 = $typ ? $typ : "";
			
			/* 訊息類型 */
			$typeName = "訊息";
			if ($typ != 0) {
				$dalT->TypeID = $typ;
				$dalT->loadRecord();
				$typeName = $dalT->TypeName;
			}

			/* 訊息下拉 */
			$filterT = "a.`Visible`=1";
			$dataT = $dalT->executeRecordset([], $filterT);

			/* 會員 */
			$dalM->MemberID = $_SESSION["member_id"];
			$dalM->loadRecord();

			/* 條件式 */
			$intoAry = [
				parent::addArray(":memberID", $_SESSION["member_id"], "int"),
				parent::addArray(":levelID", $dalM->LevelID, "int"),
			];
			$filter = "(a.`MemberID`=:memberID OR (a.`MemberID`=0 AND (a.`LevelID`=:levelID OR a.`LevelID`=0))) AND a.`Visible`=1";
			if ($typ != "0") {
				$intoAry[] = parent::addArray(":typeID", $typ, "int");
				$filter .= " AND a.`TypeID`=:typeID";
			}
			$orderExp = "a.`IsHot` DESC,a.`CreatedTime` DESC";

			/* 分頁 */
			$rows = $dal->countRecordset($intoAry, $filter);
			
			$p = (int) parent::getGet("p", 1);
			$pz = 10;
			$pMax = ceil($rows / $pz);
			$p = max(min($p, $pMax), 1);
			$start = ($p - 1) * $pz;

			$pagtxt = ($rows > 0) ? $this->generatePagination($p, $pMax, $typ2) : '沒有資料';
			
			/* 訊息 */
			$data = $dal->executeRecordset($intoAry, $filter, $orderExp, $start, $pz);
			foreach ($data as &$item) {
				$item["txtH"] = $item["IsHot"] ? "<b style='color: red;'>[重要]</b>" : "";

				$item["txtR"] = "";
				if ($item["FromType"] == "1") {
					$item["txtF"] = $item["MemberName"];
				} else if ($item["FromType"] == "2") {
					$item["txtF"] = "管理者";
					if ($item["IsRead"] == "0") $item["txtR"] = "<b style='color: red;'>[未讀]</b>";
				} else {
					$item["txtF"] = "-";
				}

				/* 回覆 */
				if (isset($intoAryR)) unset($intoAryR);
				$intoAryR = [parent::addArray(":messageID", $item["ID"], "int")];
				$filterR = "a.`MessageID`=:messageID AND a.`Visible`=1";
				$timeL = (int)$item["ModifiedTime"];
				$dataR = $dalR->executeRecordset($intoAryR, $filterR, "a.`ModifiedTime` DESC", 0, 1);
				if (count($dataR) == 1 && (int)$dataR[0]["ModifiedTime"] > $timeL) $timeL = (int)$dataR[0]["ModifiedTime"];
				$item["LTime"] = date("Y-m-d H:i:s", $timeL);
				$item["countR"] = $dalR->countRecordset($intoAryR, $filterR);
				if ($item["txtR"] == "") {
					$filterR .= " AND a.`FromType`=2 AND a.`IsRead`=0";
					if ($dalR->countRecordset($intoAryR, $filterR)) $item["txtR"] = "<b style='color: red;'>[未讀]</b>";
				}
			}
			parent::addParameter("typeName", $typeName);
			parent::addParameter("pagtxt", $pagtxt);
			$listT = new repeater();
			$listT->id = "listT";
			$listT->dataSource = $dataT;
			parent::addControl($listT);
			$list = new repeater();
			$list->id = "list";
			$list->dataSource = $data;
			parent::addControl($list);
			
			parent::loadData();
		}

		public function generatePagination($p, $pMax, $typ) {
			$rang = 10;
			$pr = ceil($p / $rang) - 1;
			$pu = ($p == 1) ? 1 : ($p - 1);
			$pd = ($p == $pMax) ? $pMax : ($p + 1);

			$pagtxt = '<a href="/message' . $typ . '_' . $pu . '.html"><i class="fa fa-angle-double-left"></i></a>';
			for ($i = 1; $i < 11; $i++) {
				$nm = $pr * $rang + $i;
				$pagtxt .= ($nm == $p) ? '<strong class="number_hit">' . $nm . '</strong>' : '<a href="/message' . $typ . '_' . $nm . '.html">' . $nm . '</a>';
				if ($nm == $pMax) break;
			}
			$pagtxt .= '<a href="/message' . $typ . '_' . $pd . '.html" class="next"><i class="fa fa-angle-double-right"></i></a>';

			return $pagtxt;
		}
	}

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