<?php
require_once dirname(dirname(__FILE__))."/page.php";
require_once dirname(dirname(dirname(dirname(__FILE__))))."/dal/cityDAL.php";
require_once dirname(dirname(dirname(dirname(__FILE__))))."/dal/areaDAL.php";
class address extends page{
	// 函數：取得縣市、地區陣列資料
	public function getCityAreaList(): string {
		$dalC = new cityDAL();
		$dalA = new areaDAL();

		$aryC = [];
		$dataC = $dalC->executeRecordset([], "a.`Visible`=1");
		foreach ($dataC as $rows) {
			$aryA = [];
			$intoAry = [parent::addArray(":cityID", $rows["CityID"], "int")];
			$dataA = $dalA->executeRecordset($intoAry, "a.`CityID`=:cityID AND a.`Visible`=1");
			foreach ($dataA as $rows2) {
				$aryA[] = ["AreaID" => $rows2["AreaID"], "ZipCode" => $rows2["ZipCode"], "AreaName" => urlencode($rows2["AreaName"])];
			}
			$aryC[] = ["CityID" => $rows["CityID"], "CityName" => urlencode($rows["CityName"]), "aryA" => $aryA];
		}
		
		return urldecode(json_encode($aryC));
	}
		
	// 函數：取得地址資訊 type = 0:縣市名稱；1:鄉鎮市區名稱；2:郵遞區號
	public function getAddressData(int $id = 0, int $type = 0): string {
		if ($id === 0) return '';

		$dal = $type === 0 ? new cityDAL() : new areaDAL();
		$dal->{$type === 0 ? 'CityID' : 'AreaID'} = $id;
		$dal->loadRecord();

		return $dal->Visible ? ($type === '1' ? $dal->AreaName : ($type === '2' ? $dal->ZipCode : $dal->CityName)) : '';
	}

	public function showAddress(int $city = 0, int $area = 0, string $txt = ""): string {
		$address = '';
		if ($city) $address .= $this->getAddressData($city);
		if ($area) $address .= $this->getAddressData($area, 1);
		$address .= $txt;

		return $address;
	}
}
?>