<?php
	require_once "pageBase.php";
	require_once "../dal/count_lastDAL.php";
	require_once "../bin/web/repeater.php";

	class _countIP2 extends pageBase {
		public function load() {
			$this->limitID = "42";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				$this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/countIP2.html");
			parent::load();
		}
		
		public function loadData() {
			$dalL = new count_lastDAL();
		
			// 獲取並設置 IP 和年月參數
			$ip = parent::getGet("ip", "");
			$ym = parent::getGet("ym", date("Y-m"));
			parent::addParameter("ip", $ip);
			parent::addParameter("ym", $ym);

			// 查詢條件
			$time = $ym . "%";
			$intoAry = [
				parent::addArray(":time", $time, "str"),
				parent::addArray(":ip", $ip, "str", 50),
			];
			$filter = "a.`Time` LIKE :time AND a.`IP` = :ip";
			$orderExp = "a.`CountID` DESC";
			
			$dataL = $dalL->executeRecordset($intoAry, $filter, $orderExp);
			foreach ($dataL as &$entry) {
				$refererHost = $entry["RefererHost"];
				if (mb_stripos($refererHost, "yahoo.com", 0, "utf-8") !== false) {
					$entry["SearchEngine"] = "Yahoo";
				} elseif (mb_stripos($refererHost, "google.com", 0, "utf-8") !== false) {
					$entry["SearchEngine"] = "Google";
				} elseif (mb_stripos($refererHost, "baidu.com", 0, "utf-8") !== false) {
					$entry["SearchEngine"] = "百度";
				}
			}
			$listL = new repeater();
			$listL->id = "listL";
			$listL->dataSource = $dataL;
			parent::addControl($listL);
			
			parent::addParameter("ipcount", count($dataL));

			parent::loadData();
		}
	}

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