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

	class _orderView extends pageBase {
		private string $id;
		private string $pUrl;
		private $dal;

		public function load() {
			$this->dal = new orderDAL();
			$this->id = parent::getInput("id","0");
			$this->pUrl = urldecode(parent::getInput("pUrl","orderList.php"));
			
			$this->limitID = "24";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				$this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/orderView.html");
			parent::load();
		}

		public function loadData(){
			$delTxt = "";
			if ($this->id != "" && $this->id != 0) {
				$this->dal->OrderID = $this->id;
				$this->dal->loadRecord();

				if (parent::getCurrentUserID() > "1" && $this->dal->Disable == "1") {
					$this->alertMsg = "訂單內容已經被刪除了!如有疑問請洽主管理者!";
					$this->locationHref = "orderList.php";
				} else {
					$this->addParametersFromOrder($this->dal);
					$this->addProductListParameters($this->dal);
					$delTxt = $this->getDeleteText($this->dal);
				}
			} else {
				parent::redirect($this->pUrl);
			}

			parent::addParameter("id", $this->id);
			parent::addParameter("pUrl", $this->pUrl);
			parent::addParameter("delTxt", $delTxt);
			
			parent::loadData();
		}

		/* 訂單相關 */
		private function addParametersFromOrder($order) {
			$parameters = [
				"ProductPrice" => $order->ProductPrice,
				"TransPrice" => $order->TransPrice,
				"OrderPrice" => $order->OrderPrice,
				"Name" => $order->Name,
				"Email" => $order->Email,
				"Phone" => $order->Phone,
				"Mobile" => $order->Mobile,
				"Address" => $order->Address,
				"Content" => $order->Content,
				"CreatedIP" => $order->CreatedIP,
				"CreatedTime" => date("Y-m-d", $order->CreatedTime)
			];
			foreach ($parameters as $name => $value) {
				parent::addParameter($name, $value);
			}
		
			if ($order->IsView == 0) $order->view();
		}

		/* 訂單產品相關 */
		private function addProductListParameters($order) {
			$product = new product();
			$dataOP = $order->OrderProduct;
			foreach ($dataOP as &$item) {
				$item["imageStyle"] = "";

				if (!parent::image_exists($item["IconImage"])) {
					$item["IconImage"] = "/images/no-pic.jpg";
					$item["imageStyle"] = " style='display:none;'";
				}
		
				$item["WebLink"] = $product->linkProductDetail($item["ProductID"]);
				$item["htLink"] = $product->linkProductList();
				$item["Amount"] = (int)$item["Price"] * (int)$item["Quantity"];
			}
			$listOP = new repeater();
			$listOP->id = "listOP";
			$listOP->dataSource = $dataOP;
			parent::addControl($listOP);
		}

		/* 獲取刪除文本 */
		private function getDeleteText($order) {
			return $order->Disable == "1" ? "<p class=\"mt_3 h4 txt_danger card_body\">注意:訂單內容已經被刪除。</p>" : "";
		}
	}

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