<?php
// 頁面專用WebAPI, 不管任何的頁面, class名稱固定為 [WebAPIForMenuPage], 不可變更
class WebAPIForMenuPage extends base_class
{
	// use GET 
	public function SelectTest()
	{
		throw new Exception("SelectTest error");
		return $this->GetStationInv(3);
	}
	public function DownloadTest($params)
	{
		return 'uploads/params.txt';
	}
	public function DownloadImage($params)
	{
		return '../img/' . $params['img'];
	}
	// use POST
	public function ChangeTest($params)
	{
		// 指定上傳檔案的目錄
		$upload_directory = FilepathJoin(UPLOAD_BASE_DIR, "test");
		return NewGUID();

		// 檢查目錄是否存在，若不存在則創建目錄
		if (!file_exists($upload_directory)) {
			mkdir($upload_directory, 0777, true);
		}
		// 將 $params 轉成 json 寫入 uploads/params.txt
		$json = json_encode($params);
		$fp = fopen(FilepathJoin($upload_directory, "params.txt"), "w");
		fwrite($fp, $json);

		$result = "";

		if (!empty($_FILES)) {
			// 循環處理每個上傳的檔案
			for ($i = 0; $i < count($_FILES['upload_file']['name']); $i++) {
				// 取得上傳檔案的臨時名稱和原始名稱
				$tmp_file = $_FILES['upload_file']['tmp_name'][$i];
				$file_name = $_FILES['upload_file']['name'][$i];
				// 確保檔案是上傳的檔案，而不是檔案系統中的檔案
				if (is_uploaded_file($tmp_file)) {
					// 將檔案從臨時目錄移動到指定目錄
					if (move_uploaded_file($tmp_file, FilepathJoin($upload_directory, $file_name))) {
						$result = $result . " File '{$file_name}' uploaded successfully";
					} else {
						return "Failed to move '{$file_name}' to the upload directory";
					}
				} else {
					return "'{$file_name}' is not an uploaded file";
				}
			}
		}
		return $result;
	}
}
