<?php
	require_once "pageBase.php";
	require_once "../bin/web/repeater.php";
	require_once "../dal/registryDAL.php";
	class _teaching extends pageBase {
		public function load() {
			$this->limitID = "19";
			if (!parent::chkUserLimit($this->limitID)) {
				$this->alertMsg = "您沒有使用此頁面的權限!如有疑問請洽主管理者!";
				$this->locationHref = "index.php";
			} else {
				$this->loadData();
			}
			parent::loadTemplateFile(dirname(__FILE__) . "/templates/teaching.html");
			parent::load();
		}
		
		public function loadData() {
			$url = "https://tp60.appleseotw.com/apple/vault/teach/list";
			$ch = curl_init();
			curl_setopt_array($ch, [
				CURLOPT_URL => $url,
				CURLOPT_SSL_VERIFYPEER => false,
				CURLOPT_SSL_VERIFYHOST => 1,
				CURLOPT_HEADER => false,
				CURLOPT_RETURNTRANSFER => 1
			]);
			$result = curl_exec($ch);
			curl_close($ch);

			$aryJ = json_decode($result);
	
			if ($aryJ->status == 0) {
				$aryT = [];
				foreach($aryJ->aryT as $rows){
					$ary = [
						"Title" => $rows->Title,
						"Link" => "",
						"btn" => "",
						"Introduction" => $rows->Introduction
					];

					if (!empty($rows->Link) && parent::chklink($rows->Link)) {
						$referer = parse_url($rows->Link);
						$ary["Link"] = (in_array($referer["host"], ["www.youtube.com", "youtu.be"])) ? str_replace("//youtu.be/", "//www.youtube.com/embed/", $rows->Link) : $rows->Link;
						
						$ary["btn"] = sprintf('<a class="btn btn_primary fancybox" href="%s">查看</a>', $ary["Link"]);
					}
					$aryT[] = $ary;
				}
				$list = new repeater();
				$list->id = "list";
				$list->dataSource = $aryT;
				parent::addControl($list);
				
			} else {
				$this->alertMsg = $aryJ->message;
				$this->locationHref = "index.php";
			}
			
			parent::loadData();
		}
	}
	$page = new _teaching();
	$page->load();
	$page->render();
?>