<?php
	require_once "pageBase.php";
	require_once "../bin/system/guid.php";
	require_once "../bin/web/repeater.php";
	require_once "../dal/categoryDAL.php";
	require_once "../dal/productDAL.php";
	require_once "../function/function.php";

	class _productMultiAdd extends pageBase {
		private string $pUrl;
		private $dal;
		
		public function load() {
			$this->dal = new productDAL();
			$this->pUrl = urldecode(parent::getInput("pUrl","productList.php"));
			
			$this->limitID = "29";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				parent::isPostBack() ? $this->saveData() : $this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/productMultiAdd.html");
			parent::load();
		}

		public function loadData() {
			$dalC = new categoryDAL();
			$aryC = [];
			$dataC1 = $dalC->executeRecordset([], "a.`ParentID`=0 AND a.`Disable`=0");
			foreach ($dataC1 as $rows1) {
				$rows1["data"] = " data-action=\"lv1\"";
				$intoAry = [parent::addArray(":parentID", $rows1["CategoryID"], "int")];
				$dataC2 = $dalC->executeRecordset($intoAry, "a.`ParentID`=:parentID AND a.`Disable`=0");
				$rows1["disabled"] = (count($dataC2) > 0) ? " disabled='disabled'" : "";
				$aryC[] = $rows1;
				
				foreach ($dataC2 as $rows2) {
					$rows2["data"] = " data-action=\"lv2\"";
					$into2Ary = [parent::addArray(":parentID", $rows2["CategoryID"], "int")];
					$dataC3 = $dalC->executeRecordset($into2Ary, "a.`ParentID`=:parentID AND a.`Disable`=0");
					$rows2["disabled"] = (count($dataC3) > 0) ? " disabled='disabled'" : "";
					$aryC[] = $rows2;
					
					foreach ($dataC3 as $rows3) {
						$rows3["data"] = " data-action=\"lv3\"";
						$rows3["disabled"] = "";
						$aryC[] = $rows3;
					}
				}
			}
			$listC = new repeater();
			$listC->id = "listC";
			$listC->dataSource = $aryC;
			parent::addControl($listC);
			
			parent::addParameter("pUrl", $this->pUrl);
			parent::loadData();
		}

		public function saveData() {
			$iconImages = parent::getPostArray("IconImage");
			$productNames = parent::getPostArray("ProductName");
			$productNOs = parent::getPostArray("ProductNO");
			$prices = parent::getPostArray("Price");
			$introductions = parent::getPostArray("Introduction");
			$orderNOs = parent::getPostArray("OrderNO");

			$count = count($iconImages);
			if ($count === count($productNames) && $count === count($productNOs) && $count === count($prices) && $count === count($introductions) && $count === count($orderNOs)) {
				$this->dal->CategoryID = parent::getPost("cid", "0");
				$this->dal->ViewCount = "0";
				$this->dal->IsIndex = 1;
				$this->dal->Visible = 1;
				$this->dal->CreatedUserID = parent::getCurrentUserID();
				$this->dal->CreatedIP = parent::getIP();
				$this->dal->ModifiedUserID = parent::getCurrentUserID();
				$this->dal->ModifiedIP = $this->dal->CreatedIP;
				for ($i = 0; $i < $count; $i++) {
					$this->dal->ProductName = $productNames[$i];
					$this->dal->ProductNO = $productNOs[$i];
					$this->dal->PathName = guid::newGuid()->toString();
					$twPathName = parent::generateTwPathName($this->dal->ProductName);
					$twPathName2 = $twPathName;
					$num = 0;
					while ($this->dal->checkTwPathNameExists($twPathName2)) {
						$num++;
						$twPathName2 = $twPathName . "_" . $num;
					}
					$this->dal->TwPathName = $twPathName2;
					$this->dal->Price = (preg_match("/^\d+$/", $prices[$i])) ? $prices[$i] : "0";
					$this->dal->IconImage = $iconImages[$i];
					$this->dal->Introduction = $introductions[$i];
					$this->dal->Introduction = preg_replace("/\s+/", " ", $this->dal->Introduction);
					$this->dal->OrderNO = $orderNOs[$i];
					$this->dal->insertRecord();
				}
			}
			parent::redirect($this->pUrl);
		}
	}

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