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

	class _paypageList extends pageBase {
		public function load() {
			$this->limitID = "77";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				$this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/paypageList.html");
			parent::load();
		}
		
		public function loadData() {
			/* 宣告&參數 */
			$dal = new paypageDAL();
			$dal->overRecord();
			$kw = parent::getGet("kw");
			$kt = parent::getGet("kt", "1");
			$pz = parent::getGet("pz", "40");
			$p = parent::getGet("p", "1");
			parent::addParameter("kw", $kw);
			parent::addParameter("kt", $kt);

			//過濾表達式
			$intoAry = [];
			if ($kw == "") {
				$filter = "";
			} else {
				switch ($kt) {
					case "1":
						$column = "PayerName";
						break;
					case "2":
						$column = "ItemDesc";
						break;
					case "3":
						$column = "Email";
						break;
					default:
						$column = false;
				}

				$filter = $column ? "a.`$column` LIKE :$column" : "1=2";
				if ($column) $intoAry[] = parent::addArray(":$column", "%$kw%", "str");
			}

			//頁數參數
			$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) {
			$arrS = ["1" => "成功", "0" => "等待", "-1" => "失敗", "-2" => "逾期"];
			foreach ($data as &$item) {
				$item["txtStatus"] = ($item["Status"] >= "-2" && $item["Status"] <= "1") ? $arrS[$item["Status"]] : "-";
				
				$item["CreatedDate"] = date("Y-m-d", $item["CreatedTime"]);

				$item["PayDate"] = ($item["PayTime"] == "0000-00-00 00:00:00") ? "" : date("Y-m-d", strtotime($item["PayTime"]));

				$item["encodedUrl"] = urlencode(utility::getCurrentUrl());
			}
			return $data;
		}
	}

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