<?php
	require_once dirname(__FILE__) . "/dalBase.php";
	class contactDAL extends dalBase {
		public $ContactID;
		public $Name;
		public $Phone;
		public $Email;
		public $ProductID;
		public $Mobile;
		public $Company;
		public $Address;
		public $Url;
		public $Title;
		public $Content;
		public $ReMail;
		public $ReTitle;
		public $ReContent;
		public $IsView;
		public $IsReply;
		public $Disable;
		public $CreatedTime;
		public $CreatedIP;
		public $ModifiedTime;
		public $ModifiedUserID;
		public $ModifiedIP;
		//外部欄位
		public $ProductName;
		public $ProductNO;
		public $Price;
		public $IconImage;
		public $ModifiedLoginName;
		public $ModifiedUserName;

		public function loadRecord(): bool {
			$intoAry = [];
			$sql = "SELECT a.*,b.`ProductName`,b.`ProductNO`,b.`Price`,b.`IconImage`".
				",c.`LoginName` AS `ModifiedLoginName`,c.`UserName` AS `ModifiedUserName` FROM `contact` a".
				" LEFT JOIN `product` b ON a.`ProductID`=b.`ProductID`".
				" LEFT JOIN `user` c ON a.`ModifiedUserID`=c.`UserID` WHERE ";
			if ($this->ContactID !== null) {
				$sql .= "a.`ContactID`=:contactID";
				$intoAry[] = parent::addArray(":contactID", $this->ContactID, "int");
			} else {
				$sql .= "1 = 2";
			}
			$reader = $this->data->executeReader($sql, $intoAry);

			if (!empty($reader)) {
				$this->ContactID = $reader["ContactID"];
				$this->Name = $reader["Name"];
				$this->Phone = $reader["Phone"];
				$this->Email = $reader["Email"];
				$this->ProductID = $reader["ProductID"];
				$this->Mobile = $reader["Mobile"];
				$this->Company = $reader["Company"];
				$this->Address = $reader["Address"];
				$this->Url = $reader["Url"];
				$this->Title = $reader["Title"];
				$this->Content = $reader["Content"];
				$this->ReMail = $reader["ReMail"];
				$this->ReTitle = $reader["ReTitle"];
				$this->ReContent = $reader["ReContent"];
				$this->IsView = $reader["IsView"];
				$this->IsReply = $reader["IsReply"];
				$this->Disable = $reader["Disable"];
				$this->CreatedTime = $reader["CreatedTime"];
				$this->CreatedIP = $reader["CreatedIP"];
				$this->ModifiedTime = $reader["ModifiedTime"];
				$this->ModifiedUserID = $reader["ModifiedUserID"];
				$this->ModifiedIP = $reader["ModifiedIP"];
				//外部欄位
				$this->ProductName = $reader["ProductName"];
				$this->ProductNO = $reader["ProductNO"];
				$this->Price = $reader["Price"];
				$this->IconImage = $reader["IconImage"];
				$this->ModifiedLoginName = $reader["ModifiedLoginName"];
				$this->ModifiedUserName = $reader["ModifiedUserName"];
				return true;
			} else {
				return false;
			}
		}

		public function insertRecord(): bool|int {
			$nowTime = time();
			$intoAry = [
				parent::addArray(":name", $this->Name, "str", 20),
				parent::addArray(":phone", $this->Phone, "str", 20),
				parent::addArray(":email", $this->Email, "str", 128),
				parent::addArray(":productID", $this->ProductID, "int"),
				parent::addArray(":mobile", $this->Mobile, "str", 20),
				parent::addArray(":company", $this->Company, "str", 50),
				parent::addArray(":address", $this->Address, "str", 256),
				parent::addArray(":url", $this->Url, "str", 128),
				parent::addArray(":title", $this->Title, "str", 50),
				parent::addArray(":content", $this->Content, "str", 600),
				parent::addArray(":disable", $this->Disable, "int"),
				parent::addArray(":createdTime", $nowTime, "int"),
				parent::addArray(":createdIP", $this->CreatedIP, "str", 50)
			];
			$sql = "INSERT INTO `contact` (`Name`, `Phone`, `Email`, `ProductID`, `Mobile`, `Company`, `Address`, `Url`, `Title`, `Content`, `Disable`, `CreatedTime`, `CreatedIP`)".
				" VALUES (:name, :phone, :email, :productID, :mobile, :company, :address, :url, :title, :content, :disable, :createdTime, :createdIP)";
			$this->data->executeNonQuery($sql, $intoAry, true);
			return (!$this->data->error) ? $this->data->getInsertID() : false;
		}

		public function updateRecord(): bool {
			$nowTime = time();
			$intoAry = [
				parent::addArray(":contactID", $this->ContactID, "int"),
				parent::addArray(":reMail", $this->ReMail, "str", 128),
				parent::addArray(":reTitle", $this->ReTitle, "str", 50),
				parent::addArray(":reContent", $this->ReContent, "str"),
				parent::addArray(":isView", $this->IsView, "int"),
				parent::addArray(":isReply", $this->IsReply, "int"),
				parent::addArray(":disable", $this->Disable, "int"),
				parent::addArray(":modifiedTime", $this->ModifiedTime, "int"),
				parent::addArray(":modifiedUserID", $this->ModifiedUserID, "int"),
				parent::addArray(":modifiedIP", $this->ModifiedIP, "str")
			];
			$sql = "UPDATE `contact` SET ".
				"`ReMail`=:reMail".
				",`ReTitle`=:reTitle".
				",`ReContent`=:reContent".
				",`IsView`=:isView".
				",`IsReply`=:isReply".
				",`Disable`=:disable".
				",`ModifiedTime`=:modifiedTime".
				",`ModifiedUserID`=:modifiedUserID".
				",`ModifiedIP`=:modifiedIP".
				" WHERE `ContactID`=:contactID";
			$this->data->executeNonQuery($sql, $intoAry, true);
			return !$this->data->error;
		}

		//查詢
		public function executeRecordset(array $intoAry = [], string $filter = "", string $order = "", int $start = 0, int $length = -1): array {
			$sql = "SELECT a.*,b.`ProductName`,b.`ProductNO`,b.`Price`,b.`IconImage`".
				",c.`LoginName` AS `ModifiedLoginName`,c.`UserName` AS `ModifiedUserName` FROM `contact` a".
				" LEFT JOIN `product` b ON a.`ProductID`=b.`ProductID`".
				" LEFT JOIN `user` c ON a.`ModifiedUserID`=c.`UserID`";
			if ($filter !== "") $sql .= " WHERE $filter";
			$sql .= (!isset($order) || $order === "") ? " ORDER BY a.`CreatedTime` DESC" : " ORDER BY $order";
			$length = is_int($length) ? $length : -1;
			return $this->data->executeRecordset($sql, $intoAry, $start, $length);
		}
		//查詢記數
		public function countRecordset(array $intoAry = [], string $filter = ""): int {
			$sql = "SELECT COUNT(*) FROM `contact` a LEFT JOIN `product` b ON a.`ProductID`=b.`ProductID` LEFT JOIN `user` c ON a.`ModifiedUserID`=c.`UserID`";
			if ($filter !== "") $sql .= " WHERE $filter";
			return $this->data->executeScalar($sql, $intoAry);
		}
		//點擊計數遞增
		public function viewRecord(int $userID): bool {
			$intoAry = [
				parent::addArray(":contactID", $this->ContactID, "int"),
				parent::addArray(":modifiedUserID", $userID, "int")
			];
			$sql = "UPDATE `contact` SET `IsView`=1,`ModifiedUserID`=:modifiedUserID WHERE `ContactID`=:contactID";
			$this->data->executeNonQuery($sql, $intoAry, true);
			return (!$this->data->error) ? true : false;
		}
		//刪除
		public function deleteRecord(int $contactID, int $type): bool {
			$intoAry = [
				parent::addArray(":contactID", $contactID, "int"),
				parent::addArray(":disable", $type, "int")
			];
			$sql = "UPDATE `contact` SET `Disable`=:disable WHERE `ContactID`=:contactID";
			$this->data->executeNonQuery($sql, $intoAry, true);
			return !$this->data->error;
		}
		//批量刪除
		public function batDelete(string $contactIDs, int $type): bool {
			$sql = "UPDATE `contact` SET `Disable`=$type WHERE `ContactID` IN ($contactIDs)";
			$this->data->executeNonQuery($sql, [], true);
			return !$this->data->error;
		}
	}
?>