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

	class _productList extends pageBase {
		public function load() {
			$this->limitID = "29";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				$this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/productList.html");
			parent::load();
		}

		public function loadData() {
			/* 宣告&參數 */
			$dal = new productDAL();
			$dalC = new categoryDAL();
			$cid = (int)parent::getInput("cid", 0);
			$kt = parent::getInput("kt", "ProductName");
			$kw = parent::getInput("kw", "");
			$ii = parent::getInput("ii", "");
			$v = parent::getInput("v", "");
			$o = (int)parent::getInput("o", 1);
			$pz = (int)parent::getInput("pz", 40);
			$p = (int)parent::getInput("p", 1);
		
			parent::addParameter("cid", $cid);
			parent::addParameter("kt", $kt);
			parent::addParameter("kw", $kw);
			parent::addParameter("ii", $ii);
			parent::addParameter("v", $v);
			parent::addParameter("o", $o);

			//搜尋條件、排序順序
			$intoAry = [];
			$sqlAry = [];
			if ($cid != "0") {
				$intoAry[] = parent::addArray(":categoryID", $cid, "int");
				$intoAry[] = parent::addArray(":parentID", $cid, "int");
				$intoAry[] = parent::addArray(":grandID", $cid, "int");
				$sqlAry[] = "(a.`CategoryID`=:categoryID OR b.`ParentID`=:parentID OR c.`ParentID`=:grandID)";
			}
			if ($kw != "") {
				$intoAry[] = parent::addArray(":" . $kt, "%" . $kw . "%", "str");
				$sqlAry[] = "a.`" . $kt . "` LIKE :" . $kt;
			}
			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", "刪除資料後將無法恢復!");
			} else {
				parent::addParameter("recoveryAllB", "<a class=\"mr_2 btn btn_success\"  data-action=\"delall\" data-type=\"0\" href=\"javascript:void(0);\">復原</a>");
			}
			$filter = count($sqlAry) ? implode(" AND ", $sqlAry) : "";
			
			switch($o){
				case "2":
				case "3":
					$orderExp = ($o == 2) ? "a.`CreatedTime` DESC" : "a.`CreatedTime`";
					break;
				case "4":
				case "5":
					$orderExp = ($o == 4) ? "a.`ModifiedTime` DESC" : "a.`ModifiedTime`";
					break;
				default :
					$orderExp = "";
			}

			//頁數參數
			$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 = '沒有資料!';
			$chk_all = "";
			if ($rows > 0) {
				$pagtxt = parent::generatePagination($p, $pMax);
				$chk_all = "<p class=\"pos_lg_abs d_flex chk chk_all\"><font class=\"el-checkbox\"><input type=\"checkbox\" id=\"chkAll\" name=\"chkAll\"><span class=\"el-checkbox-style\"></span><span class=\"d_lg_none d_inblock\">全選</span></font></p>";
			}

			//產品列表
			$data = $dal->execute3Recordset($intoAry, $filter, $orderExp, $start, $pz);
			$data = $this->formatData($data);
			$list = new repeater();
			$list->id = "list";
			$list->dataSource = $data;
			parent::addControl($list);
			

			//產品分類下拉
			$aryC = [];
			$dataC1 = $dalC->executeRecordset([], "a.`ParentID`=0 AND a.`Disable`=0");
			foreach ($dataC1 as $rows1) {
				$rows1["data"] = " data-action=\"lv1\"";
				$into2Ary = [parent::addArray(":parentID", $rows1["CategoryID"], "int")];
				$dataC2 = $dalC->executeRecordset($into2Ary, "a.`ParentID`=:parentID AND a.`Disable`=0");
				$rows1["disabled"] = (count($dataC2)) ? " disabled='disabled'" : "";
				$aryC[] = $rows1;
				
				foreach ($dataC2 as $rows2) {
					$rows2["data"] = " data-action=\"lv2\"";
					$into3Ary = [parent::addArray(":parentID", $rows2["CategoryID"], "int")];
					$dataC3 = $dalC->executeRecordset($into3Ary, "a.`ParentID`=:parentID AND a.`Disable`=0");
					$rows2["disabled"] = (count($dataC3)) ? " disabled='disabled'" : "";
					$aryC[] = $rows2;
					
					foreach ($dataC3 as $rows3) {
						$rows3["data"] = " data-action=\"lv3\"";
						$rows3["disabled"] = "";
						$aryC[] = $rows3;
					}
				}
			}
			$listC = new repeater();
			$listC->id = "listC";
			$listC->dataSource = $aryC;
			parent::addControl($listC);
			
			parent::addParameter("p", $p);
			parent::addParameter("pz", $pz);
			parent::addParameter("pagtxt", $pagtxt);
			parent::addParameter("chk_all", $chk_all);

			parent::addParameter("currentUrl", utility::getCurrentUrl());
			parent::addParameter("encodeUrl", urlencode(utility::getCurrentUrl()));
			parent::loadData();
		}
		
		public function formatData($data) {
			$product = new Product();
			$dalC = new categoryDAL();
			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>';

				if (!parent::image_exists($item["IconImage"])) $item["IconImage"] = "";

				$item["CDate"] = date("Y-m-d", $item["CreatedTime"]);
				$item["MDate"] = date("Y-m-d", $item["ModifiedTime"]);

				$categoryPath = [$item["CategoryName"]];
				if ($item["ParentID"] !== "0") {
					$categoryPath[] = $item["ParentName"];
					if ($item["GrandID"] !== "0" && $item["GrandID"] !== "") $categoryPath[] = $item["GrandName"];
				}
				$item["CategoryRoot"] = (count($categoryPath) > 0) ? implode(" > ", array_reverse($categoryPath)) : "";

				$countAry = [parent::addArray(":parentID", $item["CategoryID"], "int")];
				$countC = $dalC->countRecordset($countAry, "a.`ParentID`=:parentID");
				$item["cateStyle"] = ($countC) ? " color:red;" : "";
				
				$item["WebLink"] = $product->linkProductDetail($item["ProductID"]);

				$item["DelBtn"] = parent::generateDelButton($item["Disable"], $item["ProductID"]);

				$item["htLink"] = $product->linkProductList();
				$item["encodeUrl"] = urlencode(utility::getCurrentUrl());
			}
			return $data;
		}
	}

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