<?php
session_start();
//Can't come in without logged in
if (!isset($_SESSION['loggedin'])) {
	header('Location: pages-signin.html');
	exit;
}
?>
<!doctype html>
<html class="fixed">

<head>
	<!-- Basic -->
	<meta charset="UTF-8">
	<title>資料庫管理</title>
	<meta name="keywords" content="HTML5 Admin Template" />
	<meta name="description" content="JSOFT Admin - Responsive HTML5 Template">
	<meta name="author" content="JSOFT.net">

	<script src="assets/vendor/jquery/jquery.js"></script>
	<!-- Mobile Metas -->
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />

	<!-- Web Fonts  -->
	<link href="http://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800|Shadows+Into+Light" rel="stylesheet" type="text/css">
	<!-- jstree CSS -->
	<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.4/themes/default/style.min.css">
	<!-- Vendor CSS -->
	<link rel="stylesheet" href="assets/vendor/bootstrap/css/bootstrap.css" />
	<link rel="stylesheet" href="assets/vendor/font-awesome/css/font-awesome.css" />
	<link rel="stylesheet" href="assets/vendor/magnific-popup/magnific-popup.css" />
	<link rel="stylesheet" href="assets/vendor/bootstrap-datepicker/css/datepicker3.css" />
	<link rel="stylesheet" href="assets/vendor/pnotify/pnotify.custom.css" />
	<!-- Specific Page Vendor CSS -->
	<link rel="stylesheet" href="assets/vendor/jquery-ui/css/ui-lightness/jquery-ui-1.10.4.custom.css" />
	<link rel="stylesheet" href="assets/vendor/bootstrap-multiselect/bootstrap-multiselect.css" />
	<link rel="stylesheet" href="assets/vendor/morris/morris.css" />
	<link rel="stylesheet" href="assets/vendor/bootstrap-fileupload/bootstrap-fileupload.min.css" />
	<!-- Theme CSS -->
	<link rel="stylesheet" href="assets/stylesheets/theme.css" />
	<!-- Skin CSS -->
	<link rel="stylesheet" href="assets/stylesheets/skins/default.css" />
	<!-- Theme Custom CSS -->
	<link rel="stylesheet" href="assets/stylesheets/theme-custom.css">
	<!-- Head Libs -->
	<script src="assets/vendor/modernizr/modernizr.js"></script>
	<!-- chart.js -->
	<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
	<!-- DataTable -->

	<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.25/css/jquery.dataTables.css">
	<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.js"></script>
	<!-- Icon -->
	<link rel="shortcut icon" href="assets/images/favicon.ico" />
	<link href="https://js.arcgis.com/4.21/esri/themes/light/main.css" rel="stylesheet">
	<link href="./CustomCSS/main.css" rel="stylesheet">
	<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.4/jstree.min.js"></script>
	<script src="./script/js/jstreeAJAX.js"></script>
	<script src="https://js.arcgis.com/4.21/"></script>
	<style>
		.row {
			margin-top: -15px;
		}

		.page-header>ul {
			list-style: none;
			padding: 0;
			margin: 0;
		}

		.page-header>ul>li {
			display: inline-block;
			font-size: 25px;
			color: #FFFFFF;
		}


		.page-header>ul>li.right {
			float: right;
			padding-right: 10px;
		}

		.input-search {
			margin-top: 8px;
		}

		.page-header li>label {
			margin-top: 13px;
			border-bottom: 4px solid #0088cc;
			height: 41px;
		}

		.sidebar-left {
			box-shadow: 0 0 0;
		}

		.jstree-default .jstree-icon {
			color: #ffe693;
		}

		.jqsfield {
			color: white;
			font: 10px arial, san serif;
			text-align: left;
		}

		.jstree-anchor {
			width: 250px;
			text-overflow: ellipsis;
			overflow: hidden;
			white-space: nowrap;
		}

		.jstree-anchor,
		.jstree-anchor:link,
		.jstree-anchor:visited,
		.jstree-anchor:hover,
		.jstree-anchor:active {
			color: #abb4be;
		}

		td,
		th {
			text-align: center;

		}

		th {
			width: 30%;
		}

		.nav-main {
			padding: 15px;
		}

		.node-hidden {
			display: none;
		}

		html.fixed .page-header {
			left: 349px;
		}

		html.fixed .content-body {
			margin-left: 350px;
		}

		.sidebar-left {
			width: 350px;
		}

		#datatable-ajax tr:hover {
			background-color: #e8e8e8;
		}

		#viewDiv {
			height: 100%;
			width: 100%;
		}

		#zoom {
			margin-bottom: 5px;
		}

		#actions {
			padding: 5px;
		}

		.container {
			height: 50%;
			width: 100%;
		}

		#createSlideDiv {
			background-color: white;
			opacity: 0.9;
			color: black;
			padding: 6px;
		}

		#slidesDiv {
			background-color: white;
			opacity: 0.9;
			color: black;
			padding: 10px;
			visibility: hidden;
			bottom: 20px;
			overflow-y: auto;
			text-align: center;
			height: 260px;
		}

		#slidesDiv .slide {
			/* Show cursor as pointer when on a slide */
			cursor: pointer;
			margin-bottom: 6px;
		}

		#slidesDiv .slide .title {
			/* Center the title text */
			text-align: center;
		}

		/* Draw active slide with a nice border around the thumbnail */

		#slidesDiv .slide.active img {
			box-shadow: 0px 0px 12px black;
			border-style: solid;
			border-width: thin;
			border-color: black;
		}
	</style>

	<script>
		$(document).ready(function() {
			getToken();
		});
		var token;

		function getToken() {
			var t;
			$.ajax({
				url: "./scripts/PHP/access_token.php",
				type: "GET",
				async: false,
			}).done(function(data) {
				token = data;
				//data = JSON.parse(data);
			}).error(function() {

			});
		}

		require([
			"esri/config",
			"esri/Map",
			"esri/WebScene",
			"esri/views/SceneView",
			"esri/core/urlUtils",
			"esri/identity/OAuthInfo",
			"esri/identity/IdentityManager",
			"esri/widgets/Legend",
			"esri/widgets/Home",
			"esri/widgets/Fullscreen",
			"esri/widgets/Slider",
			"esri/widgets/BasemapGallery",
			"esri/widgets/LayerList",
			"esri/layers/GroupLayer",
			"esri/layers/FeatureLayer",
			"esri/widgets/FeatureTable",
			"esri/tasks/support/Query",
			"esri/layers/SceneLayer",
			"esri/widgets/Zoom",
			"esri/widgets/Compass",
			"esri/widgets/NavigationToggle",
			"esri/widgets/Slice",
			"esri/widgets/Search",
			"esri/geometry/Point",
			"esri/geometry/Polyline",
			"esri/webscene/Slide",
			"esri/symbols/WebStyleSymbol",
			"esri/widgets/DirectLineMeasurement3D",
			"esri/Graphic",
			"esri/widgets/Search/LocatorSearchSource",
			"esri/renderers/UniqueValueRenderer",
			"esri/geometry/geometryEngine",
			"esri/widgets/Expand",
			"esri/layers/GeoJSONLayer",
			"esri/PopupTemplate",
			"esri/renderers/Renderer",
			"esri/geometry/projection"
		], (esriConfig, Map, WebScene, SceneView, urlUtils, OAuthInfo, esriId,
			Legend,
			Home,
			Fullscreen,
			Slider,
			BasemapGallery,
			LayerList,
			GroupLayer,
			FeatureLayer,
			FeatureTable,
			Query,
			SceneLayer,
			Zoom,
			Compass,
			NavigationToggle,
			Slice,
			Search,
			Point,
			Polyline,
			Slide,
			WebStyleSymbol,
			DirectLineMeasurement3D,
			Graphic,
			LocatorSearchSource,
			UniqueValueRenderer,
			geometryEngine,
			Expand, GeoJsonLayer, PopupTemplate, Renderer, projection) => {
			esriConfig.apiKey = token;
			const webscene = new WebScene({
				portalItem: {
					id: "dea6580d8d4547df9915a822fe7f2b9d"

				},
				opacity: .75,
				showAttribution: false,

			});

			var featureLayer = new FeatureLayer({

				url: "https://services3.arcgis.com/W6g8dmWX0ILAHcNd/arcgis/rest/services/%E5%B7%A5%E7%A8%8B%E8%A8%AD%E8%A8%88%E8%B3%87%E6%96%99%E5%BA%AB/FeatureServer",
				outFields: ["*"],
				//renderer: poleRenderer,
				title: "工程設計資料庫",
				elevationInfo: "on-the-ground",
				visible: true
			});
			webscene.add(featureLayer);

			const view = new SceneView({
				container: "viewDiv",
				map: webscene,
				popup: {
					defaultPopupTemplateEnabled: true,
					dockEnabled: true,
					dockOptions: {
						breakpoint: false,
						buttonEnabled: false,
						position: "bottom-right",
					}

				}
			});


			window.view = view;

			view.ui.empty("top-left");
			//view.ui.empty("manual");
			view.ui.add(
				new Expand({
					view: view,
					content: new BasemapGallery({
						view: view
					}),
					autoCollapse: true,
					group: "top-left"
				}),
				"top-left"
			);



			view.ui.add(
				new Expand({
					view: view,
					content: new Legend({
						view: view
					}),
					autoCollapse: true,
					group: "top-left"
				}),
				"top-left"
			);

			view.ui.add(
				new Home({
					view: view,
					group: "top-left"
				}),
				"top-left"
			);

			view.ui.add(
				new Fullscreen({
					view: view,
					group: "top-left"
				}),
				"top-left"
			);

			var activeMeasureWidget = new DirectLineMeasurement3D({
				view: view
			});

			var pMeasureExpand = new Expand({
				view: view,
				container: document.getElementById("measureExpendDiv"),
				content: activeMeasureWidget,
				group: "top-left"
			});

			view.ui.add(
				pMeasureExpand,
				"top-left"
			);

			$("#measureExpendDiv .esri-widget--button").click(function() {
				if (pMeasureExpand.expanded) {
					activeMeasureWidget.viewModel.newMeasurement();
				} else {
					activeMeasureWidget.viewModel.clearMeasurement();
				}
			});

			function zoomToLayer(layer) {
				return layer.queryExtent().then((response) => {
					view.goTo({
						target: response.extent,
						tilt: 55,
						heading: 0,
					}).catch((error) => {
						console.error(error);
					});
				});
			}

			$('#treeAjaxHTML').on("select_node.jstree", function(e, data) {
				if (data.node.text.includes("-")) {
					project_id = data.node.text.split("-")[0];
					featureLayer.definitionExpression = "計畫編號 = " + project_id;
					zoomToLayer(featureLayer);
				}
				/*else{
									type = data.node.text;
									console.log(type);
									featureLayer.definitionExpression = "工程類別 = '"+ type + "'";
									zoomToLayer(featureLayer);
								}*/
			});
			view.when(function() {

			});



		});
	</script>
</head>

<body>
	<script type="application/javascript">
		var test;
		var i = 0;
		var download;
		var fileName;
		var folder;
		var software;
		var userName = 'maabim';


		jQuery(window).load(function() {
			var searchURL = window.location.search;
			searchURL = searchURL.substring(1, searchURL.length);
			if (searchURL.includes("=")) {
				projectId = decodeURIComponent(searchURL.split("&")[1].split("=")[1]);
				var projectNode = $(".jstree-anchor").filter(function() {
					return $(this).text().includes(projectId);
				});
				var projectId = projectNode[0].attributes.id.value;
				$('#treeAjaxHTML').jstree("select_node", projectId);
				$("#treeAjaxHTML").jstree("close_all");
			}
		});

		//var typeId = node[0].attributes.id.value;
	</script>
	<section class="body">
		<!-- start: header -->
		<header class="header">
			<div class="logo-container">
				<div class="banner-txt">資料庫管理</div>
				<div class="visible-xs toggle-sidebar-left" data-toggle-class="sidebar-left-opened" data-target="html" data-fire-event="sidebar-left-opened">
					<i class="fa fa-bars" aria-label="Toggle sidebar"></i>
				</div>
			</div>
			<!-- start: search & user box -->
			<div class="header-right">
				<span class="separator"></span>
				<div id="userbox" class="userbox">
					<a href="#" data-toggle="dropdown">
						<figure class="profile-picture"> <img src="assets/images/!logged-user.jpg" alt="Joseph Doe" class="img-circle" data-lock-picture="assets/images/!logged-user.jpg" /> </figure>
						<div class="profile-info" data-lock-name="John Doe" data-lock-email="johndoe@JSOFT.com"> <span class="name"><?php echo $_SESSION['name'] ?></span> <span class="role">administrator</span> </div> <i class="fa custom-caret"></i>
					</a>
					<div class="dropdown-menu">
						<ul class="list-unstyled">
							<li class="divider"></li>
							<li> <a role="menuitem" tabindex="-1" href="./script/php/Logout.php"><i class="fa fa-power-off"></i> Logout</a> </li>
						</ul>
					</div>
				</div>
			</div>
			<!-- end: search & user box -->
		</header>
		<!-- end: header -->
		<div class="inner-wrapper">
			<!-- start: sidebar -->
			<aside id="sidebar-left" class="sidebar-left">

				<div class="nano">
					<div class="nano-content">
						<nav id="menu" class="nav-main" role="navigation">
							<div class="widget-header clearfix">
								<h6 class="title pull-left mt-xs">資料庫文件</h6>
							</div>
							<div class="widget-content">
								<div class="jstree jstree-1 jstree-default" id="treeAjaxHTML"></div>
							</div>
						</nav>
					</div>
				</div>
			</aside>
			<!-- end: sidebar -->
			<section role="main" class="content-body">
				<header class="page-header">
					<ul>
						<li>
							<label id="header"></label>
						</li>
						<li class="right">
							<div class="input-group input-search" style="table-layout: auto;"><input type="text" class="form-control" name="search" id="search" placeholder="Search..."><span class="input-group-btn"><button class="btn btn-default" id="btn-search"><i class="fa fa-search"></i></button></span></div>
						</li>
					</ul>
				</header>
				<div class="row">
					<div class="col-md-12">
						<section class="panel">
							<div class="panel-body">
								<div id="folderPath" style="float: left;"></div>
								<button id="reset" type="button" class="mb-xs mr-xs btn btn-default" style="float: right;"><i class="fa fa-reply"></i> 上一層</button>
								<table style="width:99%;" class="table table-bordered table-striped" id="datatable-ajax">
									<thead>
										<tr>
											<th id="SN">項次</th>
											<th id="name">類別</th>
											<th>關鍵字</th>
											<th>版次</th>
										</tr>
									</thead>
									<tbody>
									</tbody>
								</table>
							</div>
						</section>
					</div>
				</div>
				<div class="row">
					<div class="col-md-6">
						<section class="panel">
							<div id="panel" class="panel-body" style="height:370px">
								<div class="table-responsive" style="display:none;">
									<table class="table table-bordered mb-none" id="tableDetail">
										<tbody>
											<tr>
												<th>計畫編號</th>
												<td id="plan_code"></td>
											</tr>
											<tr>
												<th>計畫名稱</th>
												<td id="plan_name"></td>
											</tr>
											<tr>
												<th>圖名</th>
												<td id="fileName"></td>
											</tr>
											<tr>
												<th>版次</th>
												<td id="version"></td>
											</tr>
											<tr>
												<th>文件</th>
												<td id="download"></td>
											</tr>
										</tbody>
									</table>
								</div>
							</div>
						</section>
					</div>
					<div class="col-md-6">
						<section class="panel">
							<div id="arcgis" class="panel-body" style="height:370px">
								<div class="mapok" style="height:100%">
									<div id="viewDiv"></div>
									<div id="measureExpendDiv"></div>
									<div id="slidesDiv" class="esri-widget"></div>
									<span style="display:none;" id="project-id"></span>
								</div>
							</div>
						</section>
					</div>
				</div>
		</div>
	</section>
	</div>






	</section>



</body>

</html>