<?php
	require_once "pageBase.php";
	require_once "../dal/registryDAL.php";
	class _searchGoogleSet extends pageBase {
		public function load() {
			$this->limitID = "61";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				parent::isPostBack() ? $this->saveData() : $this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/searchGoogleSet.html");
			parent::load();
		}
		
		public function loadData() {
			$webConfig = registryDAL::getRegistryValues();

			$defaults = [
				"indexBestRating" => "5",
				"indexIsShowJason" => 0,
				"indexIsShowStars" => 0,
				"contactBestRating" => "5",
				"contactIsShowJason" => 0,
				"contactIsShowStars" => 0,
				"orderBestRating" => "5",
				"orderIsShowJason" => 0,
				"orderIsShowStars" => 0,
			];

			$ratingTypes = ["index", "contact", "order"];
			foreach ($ratingTypes as $type) {
				$ratingValue = rand(40, 49) / 10;
				$ratingCount = rand(10, 99);
				
				$defaults["{$type}RatingValue"] = $ratingValue;
				$defaults["{$type}RatingCount"] = $ratingCount;
				$defaults["{$type}ReviewCount"] = round($ratingValue * $ratingCount);
			}

			foreach ($webConfig as $key => $value) {
				if (isset($defaults[$key]) && ($value == "0" || $value == "")) $value = $defaults[$key];
				parent::addParameter($key, $value);
			}
			parent::loadData();
		}
		
		public function saveData() {
			$ratingDefaults = [
				"index" => ["BestRating" => "5"],
				"contact" => ["BestRating" => "5"],
				"order" => ["BestRating" => "5"],
			];

			$ratingTypes = ["index", "contact", "order"];

			$aryR = [];
			foreach ($ratingTypes as $type) {
				$ratingValue = rand(40, 49) / 10;
				$ratingCount = rand(10, 99);
        
				$aryR["{$type}BestRating"] = parent::getPost("{$type}BestRating", $ratingDefaults[$type]["BestRating"]);
				$aryR["{$type}RatingValue"] = parent::getPost("{$type}RatingValue", $ratingValue);
				$aryR["{$type}RatingCount"] = parent::getPost("{$type}RatingCount", $ratingCount);
				$aryR["{$type}ReviewCount"] = parent::getPost("{$type}ReviewCount", round($ratingValue * $ratingCount));
				$aryR["{$type}IsShowJason"] = parent::getPost("{$type}IsShowJason", "0");
				$aryR["{$type}IsShowStars"] = parent::getPost("{$type}IsShowStars", "0");
			}

			$aryR["ModifiedTime"] = time();

			foreach ($aryR as $key => $value) {
				registryDAL::saveRegistry($key, $value);
			}
		
			$this->alertMsg = "儲存完畢!";
			$this->locationHref = "searchGoogleSet.php";
		}
	}
	$page = new _searchGoogleSet();
	$page->load();
	$page->render();
?>