<?php
	require_once dirname(dirname(__FILE__))."/bin/web/repeater.php";

	class _indexNews extends webPartBase {
		public function load() : void {
			$this->loadData();
			if ($this->registry->indexNShow == "1") parent::loadTemplateFile(dirname(__FILE__)."/../templates/webParts/".$this->registry->webTemplates."/indexNews.html");
			parent::load();
		}
		
		public function loadData() : void {
			$news = new news();
			$dalN = new newsDAL();
			
			// 首頁最新消息設定
			$arrNS = [];
			if ($this->registry->indexNBImg != "" && parent::image_exists($this->registry->indexNBImg)) $arrNS[] = "background-image:url({$this->registry->indexNBImg});";
			if ($this->registry->indexNBColor != "") $arrNS[] = "background-color: {$this->registry->indexNBColor};";
			$newsStyle = (count($arrNS) > 0) ? "style=\"" . implode(" ", $arrNS) . "\"" : "";
			parent::addParameter("newsStyle", $newsStyle);
			parent::addParameter("indexNTitle", $this->registry->indexNTitle);
			parent::addParameter("indexNMLink", $this->registry->indexNMLink);
			parent::addParameter("indexNMLinkTitle", $this->registry->indexNMLinkTitle);
			
			// 取得最新消息陣列
			$dataN = $dalN->executeRecordset(
				intoAry: [],
				filter: "a.`Visible`=1 AND a.`Disable`=0 AND a.`OnlineTime`<=" . time() . " AND b.`IsIndex`=1 AND b.`Visible`=1 AND b.`Disable`=0",
				start: 0,
				length: ($this->registry->indexNNO ?? 0)
			);
			foreach ($dataN as $key => &$item) {
				if (empty($item["Image"]) || !parent::image_exists($item["Image"])) $item["Image"] = $this->NoPicImage;
				$item["imageStyle"] = (empty($item["Image"]) || !parent::image_exists($item["Image"])) ? " style='display:none;'" : "";
				$item["link"] = $news->linkNewsDetail($item["NewsID"]);
				$item["linkC"] = $news->linkNewsList($item["CategoryID"]);

				$onlineTime = ($item["OnlineTime"] != "0") ? $item["OnlineTime"] : time();
				$item["OnlineDate"] = date("Y-m-d", $onlineTime);
				$item["OnlineDY"] = date("Y", $onlineTime);
				$item["OnlineDM"] = date("m", $onlineTime);
				$item["OnlineDD"] = date("d", $onlineTime);
				$item["OnlineDM2"] = date("M", $onlineTime);
				$item["newsStyle"] = ($this->registry->indexNBColor != "") ? " style='background-color:" . $this->registry->indexNBColor . "'" : "";
		
				$item["NO"] = ++$key;
				$item["NO2"] = str_pad($item["NO"], 2, '0', STR_PAD_LEFT);
			}
			$listN = new repeater();
			$listN->id = "listN";
			$listN->dataSource = $dataN;
			parent::addControl($listN);
			parent::addParameter("countN", count($dataN));
			parent::addParameter("countN2", str_pad(count($dataN), 2, '0', STR_PAD_LEFT));
		}
	}
?>