<?php
	require_once "pageBase.php";
	require_once "../bin/web/repeater.php";
	require_once "../bin/web/tables/news.php";

	class _news_categoryList extends pageBase {
		public function load() {
			$this->limitID = "32";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				$this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/news_categoryList.html");
			parent::load();
		}
		
		public function loadData() {
			/* 宣告&參數 */
			$dal = new news_categoryDAL();
			$ii = parent::getInput("ii", "");
			$v = parent::getInput("v", "");
			$p = parent::getGet("p", "1");
			$pz = parent::getGet("pz", "10");
			
			parent::addParameter("ii", $ii);
			parent::addParameter("v", $v);
			
			//過濾表達式
			$intoAry = [];
			$sqlAry = [];
			if ($ii != "") {
				$intoAry[] = parent::addArray(":isIndex", $ii, "int");
				$sqlAry[] = "a.`IsIndex`=:isIndex";
			}
			if ($v != "") {
				$intoAry[] = parent::addArray(":visible", $v, "int");
				$sqlAry[] = "a.`Visible`=:visible";
			}
			if (parent::getCurrentUserID() > "1") {
				$sqlAry[] = "a.`Disable`=0";
				parent::addParameter("isDelTxt", "刪除資料後將無法恢復!");
			}
			$filter = count($sqlAry) ? implode(" AND ", $sqlAry) : "";
			
			//頁數參數
			$rows = $dal->countRecordset($intoAry, $filter);
			$p = ($p > 0) ? $p : 1;
			$pz = ($pz > 0) ? $pz : 10;
			$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, "", $start, $pz);
			$data = $this->formatData($data);
			$list = new repeater();
			$list->id = "list";
			$list->dataSource = $data;
			parent::addControl($list);
			
			parent::addParameter("p", $p);
			parent::addParameter("pz", $pz);
			parent::addParameter("pagtxt", $pagtxt);

			parent::loadData();
		}

		public function formatData($data) {
			$news = new news();
			foreach ($data as &$item) {
				$item["IsIndex"] = ($item["IsIndex"] == "1") ? '<span class="badge badge_soft_success">顯示</span>' : '<span class="badge badge_soft_danger">不顯示</span>';
				$item["Visible"] = ($item["Visible"] == "1") ? '<span class="badge badge_soft_success">啟用</span>' : '<span class="badge badge_soft_danger">不啟用</span>';
				
				$item["WebLink"] = $news->linkNewsList($item["CategoryID"]);

				$item["DelBtn"] = parent::generateDelButton($item["Disable"], $item["CategoryID"]);
			}
			return $data;
		}
	}

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