<?php
	require_once "pageBase.php";
	require_once "../bin/web/repeater.php";
	require_once "../dal/registryDAL.php";
	require_once "../dal/templateDAL.php";
	class _templateSet extends pageBase {
		public function load() {
			$this->limitID = "53";
			if (!parent::chkUserLimit()) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				parent::isPostBack() ? $this->saveData() : $this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/templateSet.html");
			parent::load();
		}
		
		public function loadData() {
			$dal = new templateDAL();
			$aryT = [];
			$template_box = "";
			$webTemplates = registryDAL::getRegistryValue("webTemplates");
			
			$data = $dal->executeRecordset([], "a.`Visible`=1", "a.`OrderNO` DESC");
			foreach ($data as $rows) {
				$rows["Description"] = str_replace(PHP_EOL, "<br />", $rows["Description"]);
				if (!parent::image_exists($rows["Image"])) $rows["Image"] = "/images/no-pic.jpg";
				$rows["Download"] = ($rows["File"] != "" && file_exists("..".$rows["File"])) ? "<a href=\"templateDownload.php?id=".$rows["TemplateID"]."\" target=\"_blank\">下載</a>" : "";
				
				if ($webTemplates == $rows["PathName"]) {
					$template_box .= <<<HTML
					<li class="row action">
						<a class="d_block fancybox" data-fancybox href="{$rows["Image"]}"><img src="{$rows["Image"]}" /></a>
						<div class="pall_3 d_flex btnBox">
							<p class="txt_white txt_clamp txt"><svg class="mr_1 fa svg_stroke_success"><use xlink:href="svg/icons.svg#checkcircle"></use></svg>{$rows["TemplateName"]}</p>
							<p class="btns">
								<a class="mr_2 btn btn_primary" href="templateEdit.php?id={$rows["TemplateID"]}">自訂</a>
								<a class="btn btn_warning" href="{$rows["Website"]}" target="_blank">範例</a>
							</p>
						</div>
						<a href="javascript:void(0);" data-action="delItem" data-id="{$rows["TemplateID"]}"><svg class="fa"><use xlink:href="svg/icons.svg#close"></use></svg></a>
					</li>
HTML;
				} else {
					$aryT[] = $rows;
				}
			}

			$list = new repeater();
			$list->id = "list";
			$list->dataSource = $aryT;
			parent::addControl($list);

			parent::addParameter("template_box", $template_box);
			parent::addParameter("webTemplates", $webTemplates);
			parent::loadData();
		}
		
		public function saveData() {
			registryDAL::saveRegistry("webTemplates", parent::getPost("TemplateID"));
			$this->locationHref = "templateSet.php";
		}
	}

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