<?php
	require_once "pageBase.php";
	require_once "../bin/system/utility.php";
	require_once "../bin/web/repeater.php";
	require_once "../dal/orderDAL.php";

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

		public function loadData() {
			/* 宣告&參數 */
			$dal = new orderDAL();
			$kw = parent::getInput("kw", "");
			$p = (int)parent::getInput("p", 1);
			$pz = (int)parent::getInput("pz", 40);
			$intoAry = [];
			$sqlAry = [];
			$filter = "";

			//過濾表達式
			$intoAry = [
				parent::addArray(":name", "%".$kw."%", "str"),
				parent::addArray(":email", "%".$kw."%", "str"),
				parent::addArray(":phone", "%".$kw."%", "str"),
				parent::addArray(":mobile", "%".$kw."%", "str"),
				parent::addArray(":address", "%".$kw."%", "str")
			];
			$sqlAry[] = "(a.`Name` LIKE :name OR a.`Email` LIKE :email OR a.`Phone` LIKE :phone OR a.`Mobile` LIKE :mobile OR a.`Address` LIKE :address)";
			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 : 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, "", $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::addParameter("currentUrl", utility::getCurrentUrl());
			
			parent::loadData();
		}
		
		public function formatData($data) {
			foreach($data as &$item){
				$item["IsView"] = ($item["IsView"] == "1") ? '<span class="badge badge_soft_success">讀取</span>' : '<span class="badge badge_soft_danger">未讀</span>';
				
				$item["DelBtn"] = parent::generateDelButton($item["Disable"], $item["OrderID"]);
				
				$item["CreatedTime"] = date("Y-m-d", $item["CreatedTime"]);
				$item["encodedUrl"] = urlencode(utility::getCurrentUrl());
			}
			return $data;
		}
	}

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