<?php
	require_once "pageBase.php";
	require_once "../../dal/copy_product_logDAL.php";
	require_once "../../dal/productDAL.php";
	require_once "../../bin/system/guid.php";
	require_once "../../function/function.php";
	class _product extends pageBase{
		private $dal;
		private array $aryJ;

		public function __construct() {
			$this->dal = new productDAL();
			$this->aryJ = ["error" => "0", "type" => "", "uid" => parent::getCurrentUserID(), "msg" => ""];
		}

		public function load(): void {
			$this->aryJ["msg"] = "";

			$action = parent::getPost("do");
			switch ($action) {
				case "del":
					$this->delete();
					break;
				case "vis":
					$this->changeVisible(1);
					break;
				case "unvis":
					$this->changeVisible(0);
					break;
				case "copy":
					$this->copyProduct();
					break;
				case "ono":
					$this->orderNO();
					break;
				default:
					$this->handleError("操作異常！請重新整理頁面再試一次！");
			}
		
			if ($this->aryJ["msg"] !== "") $this->aryJ["msg"] = urlencode($this->aryJ["msg"]);
			echo urldecode(json_encode($this->aryJ));
			parent::load();
		}

		private function delete() {
			$id = parent::getPost("id");
			$ids = parent::getPost("ids");
			$type = parent::getPost("type");
			$this->aryJ["type"] = $type;
		
			if (($id && $id !== "0") || ($ids && $ids !== "0")) {
				$success = false;
				if ($id) {
					$success = $this->dal->deleteRecord($id, $type);
				} else if ($ids) {
					$success = $this->dal->batDelete($ids, $type);
				}
				if (!$success) $this->handleError("刪除失敗!");
			} else {
				$this->handleError("找不到要刪除的產品!");
			}
		}
		
		private function changeVisible($visible) {
			$ids = parent::getPost("ids");
			if ($ids && $ids !== "0") {
				$success = $this->dal->batUpdateVisible($ids, $visible);
				if (!$success) $this->handleError("變更狀態失敗!");
			} else {
				$this->handleError("找不到要變更狀態的產品!");
			}
		}

		private function orderNO() {
			$ids = parent::getPost("ids");
			$ono = parent::getPost("ono");
			$ary1 = explode(",", $ids);
			$ary2 = explode(",", $ono);
		
			if (count($ary1) !== count($ary2)) {
				$this->handleError("參數異常!排序不可輸入數字以外的字元!請重新整理頁面再試一次!");
				return false;
			}
		
			foreach ($ary1 as $index => $productID) {
				$this->dal->ProductID = $productID;
				$this->dal->loadRecord();
				if ($this->dal->ProductID === $productID) {
					$this->dal->OrderNO = $ary2[$index];
					$this->dal->ModifiedUserID = parent::getCurrentUserID();
					$this->dal->ModifiedIP = parent::getIP();
					$this->dal->updateRecord();
				} else {
					$this->handleError("找不到指定的商品!變更排序已中斷!請重新整理頁面再試一次!");
					return false;
				}
			}
		}
		
		private function copyProduct() {
			$id = parent::getPost("id");
			$ids = parent::getPost("ids");
			$num = parent::getPost("num");
			
			if (!preg_match("/^\d+$/", $num) || (int)$num < 1) {
				$this->handleError("複製數量錯誤!\\n只能輸入正整數!");
				return;
			}

			$successCount = 0;
			$failureCount = 0;
			
			if (isset($id) && $id != "" && $id != "0") {
				for ($i = 0; $i < (int)$num; $i++) {
					if ($this->copyNew($id) !== false) $successCount++;
				}
			} else if (isset($ids) && $ids != "" && $ids != "0") {
				$aryI = explode(",", $ids);
				foreach ($aryI as $v) {
					for ($i = 0; $i < (int)$num; $i++) {
						if ($this->copyNew($v) !== false) $successCount++;
					}
				}
			} else {
				$this->handleError("找不到要複製的產品!");
				return;
			}

			$failureCount = (int)$num - $successCount;
		
			if ($failureCount === 0) {
				$this->aryJ["msg"] = "成功複製{$successCount}個。";
			} else {
				$this->handleError("成功複製{$successCount}個，失敗{$failureCount}個!");
			}
		}

		private function copyNew($id) {
			$dalL = new copy_product_logDAL();
			$this->dal->ProductID = $id;
			$this->dal->loadRecord();
		
			if ($this->dal->ProductID != $id) return false;

			$this->dal->PathName = guid::newGuid()->toString();
			$twPathName = parent::nametopath(sbc2Dbc(parent::codetospecial($this->dal->ProductName)));
			$twPathName2 = $twPathName;
			$num = 0;
		
			while ($this->dal->checkTwPathNameExists($twPathName2)) {
				$num++;
				$twPathName2 = $twPathName . "_" . $num;
			}

			$this->dal->TwPathName = $twPathName2;
			$this->dal->CreatedUserID = $this->dal->ModifiedUserID = parent::getCurrentUserID();
			$this->dal->CreatedTime = $this->dal->ModifiedTime = date("Y-m-d H:i:s");
			$insertID = $this->dal->insertRecord();
		
			$dalL->ProductID = $insertID;
			$dalL->CopyProductID = $id;
			$dalL->CopyUserID = parent::getCurrentUserID();
			$dalL->CopyIP = parent::getIP();
			$dalL->CopyTime = time();
			$dalL->insertRecord();

			return $insertID;
		}
		
		private function handleError(string $errorMessage): void {
			$this->aryJ["error"] = "1";
			$this->aryJ["msg"] = $errorMessage;
		}
	}

	$page = new _product();
	$page->load();
?>