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

	class _categoryList extends pageBase {
		private $dal;
		public function load() {
			$this->dal = new categoryDAL();
			$this->limitID = "28";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				$this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/categoryList.html");
			parent::load();
		}
		
		public function loadData() {
			/* 宣告&參數 */
			$id = parent::getGet("id", "0");
			$ii = parent::getInput("ii", "");
			$v = parent::getInput("v", "");
			$p = parent::getGet("p", "1");
			$pz = parent::getGet("pz", "10");
			
			parent::addParameter("id", $id);
			parent::addParameter("ii", $ii);
			parent::addParameter("v", $v);
			
			//過濾表達式
			$intoAry = [];
			$sqlAry = [];
			$aryR = [];

			$add_link = "categoryEdit.php";
			$dataaction = " data-action=\"true\"";
            $intoAry[] = parent::addArray(":parentID", $id, "int");
            $sqlAry[] = "a.`ParentID`=:parentID";
			if ($id != 0) {
				$add_link .= "?pid=" . $id;
				$dataaction = "";
				$this->dal->CategoryID = $id;
				$this->dal->loadRecord();
				$aryR[] = ["data" => " data-action=\"true\"", "Link" => "categoryList.php?id=" . $id, "Name" => $this->dal->CategoryName];
				if ($this->dal->ParentID != "0") {
					$aryR[] = ["data" => "", "Link" => "categoryList.php?id=" . $this->dal->ParentID, "Name" => $this->dal->ParentName];
					if ($this->dal->GrandID != "0") $aryR[] = ["data" => "", "Link" => "categoryList.php?id=" . $this->dal->GrandID, "Name" => $this->dal->GrandName];
				}
			}
			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) : "";
			
			$aryR[] = ["data" => $dataaction, "Link" => "categoryList.php", "Name" => parent::getMenuName(28)];
			$aryR = array_reverse($aryR);
			$listR = new repeater();
			$listR->id = "listR";
			$listR->dataSource = $aryR;
			parent::addControl($listR);
			parent::addParameter("add_link", $add_link);
			
			//頁數參數
			$rows = $this->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 = $this->dal->executeRecordset($intoAry, $filter, "a.`OrderNO`, a.`CategoryID`", $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) {
			$product = new product();
			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>';

				$categoryChain = [];
				$item["next_name"] = "中分類";
				if ($item["ParentID"] != "0") {
					$categoryChain[] = $item["ParentName"];
					$item["next_name"] = "小分類";
					if ($item["GrandID"] != "0" && $item["GrandID"] != "") {
						$categoryChain[] = $item["GrandName"];
						$item["next_name"] = "";
					}
				}
				$categoryChain[] = "根目錄";
				$item["FamilyRoot"] = (count($categoryChain)) ? implode(" > ", array_reverse($categoryChain)) : "";

				$item["ParentName"] = $item["ParentID"] ? $item["ParentName"] : "根分類";
		
				$item["CatBtn"] = (isset($item["next_name"]) && $item["next_name"] != "") ? '<a class="mr_2 btn btn_purple" href="categoryList.php?id=' . $item["CategoryID"] . '">' . $item["next_name"] . '</a>' : '';
		
				$item["WebLink"] = $product->linkProductList($item["CategoryID"]);

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

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