<?php
include($_SERVER['DOCUMENT_ROOT'] . "/Authorization/script/php/permission/check_right.php");
?>
<!doctype html>
<html class="fixed sidebar-left-collapsed">

<head>
	<?php include("bim-support-header.html"); ?>
	<script src="./script/js/global.js"></script>
	<script>
		var isFavorite = false;
		var revitVersion = "";

		var url_href = window.location.href;
		var url = new URL(url_href);
		name = url.searchParams.get("object");
		type = url.searchParams.get("type");
		parent = url.searchParams.get("parent");

		var userName = "<?php echo $_SESSION['name'] ?>";
		var role = "<?php echo ($groupName); ?>";
		var right = <?php echo ($right); ?>;
		var header = [];
		var sizeHeader = [];
		var value = [];
		var sizeValue = [];
		pageHeader = "元件庫";
	</script>
	<style>
		.hide-image {
			display: none;

		}

		.preview:hover .hide-image {
			display: block;
			position: absolute;
			padding: 8px;
		}
	</style>
</head>

<body>
	<section class="body">
		<!-- start: header -->
		<header-menu></header-menu>
		<!-- end: header -->
		<div class="inner-wrapper">
			<!-- start: sidebar -->
			<side-bar></side-bar>
			<!-- end: sidebar -->
			<section role="main" class="content-body">
				<header class="page-header">
					<h2>{{pageHeader}}</h2>
				</header>
				<!-- start: page -->
				<section class="content-with-menu content-with-menu-has-toolbar media-gallery">
					<div class="content-with-menu-container">
						<inner-menu>
							<template v-slot:inner-equipment>
								<equipment-list></equipment-list>
							</template>
						</inner-menu>
						<div class="inner-body mg-main">
							<div class="inner-toolbar">
								<ul>
									<li>
										<label>
											<span id="insertComponentName" style="font-size: 30px; color: white;"></span>
										</label>
									</li>
									<li class="right">
										<a href="#" onclick="window.history.go(-1); return false;"><i class="fa fa-undo"></i> 返回</a>
									</li>
									<li class="right">
										<div style="cursor: pointer;" onclick="AddFavorite();"><i id="heart" class="fa fa-heart"></i><span id="favorite">　加入清單</span></div>
									</li>
								</ul>
							</div>
							<div class="row">

								<div class="col-md-6">
									<div class="tabs">
										<ul class="nav nav-tabs nav-justified">
											<li class="active">
												<a href="#content" data-toggle="tab" class="text-center">3D元件模型</a>
											</li>
											<li>
												<a href="#2DImage" data-toggle="tab" class="text-center">2D圖例</a>
											</li>
										</ul>
										<div class="tab-content">
											<div id="content" class="tab-pane active" style="width:100%; position: relative; padding: 0 0 0;">

											</div>
											<div id="2DImage" class="tab-pane" style="overflow: auto;">

											</div>
										</div>
									</div>
								</div>
								<div class="col-md-6">
									<section class="panel">
										<div class="panel-body" style="padding: 0px;">
											<div class="table-responsive">
												<table class="table table-bordered mb-none">
													<tbody id="table">

													</tbody>
												</table>
											</div>
										</div>
									</section>
									<h3>尺寸：</h3>
									<section class="panel">
										<div class="panel-body" style="padding: 0px;">
											<div class="table-responsive">
												<table class="table table-bordered mb-none">
													<tbody id="sizeTable">

													</tbody>
												</table>
											</div>
										</div>
									</section>
								</div>
							</div>
						</div>
				</section>
				<!-- end: page -->
			</section>
		</div>
	</section>
	<script>
		/*Vue */
		vm.mount('.body');
		addUserImage(userName.charAt(0), role);
	</script>
	<?php include("bim-support-body.html"); ?>
	<script>
		$(document).ready(function() {
			$.ajax({
				url: "./script/php/equipment_group_detail.php",
				type: "GET",
				data: {
					type: type,
					object: name,
					parent: parent
				},
				contentType: "application/json",
				dataType: "json"
			}).done(function(data) {
				header = data.header;
				value = data.value;
				sizeHeader = data.sizeHeader;
				sizeValue = data.sizeValue;

				for (var i = 0; i < header.length; i++) {
					if (value[i] == null)
						value[i] = "";
					if (header[i] == "元件版本")
						revitVersion = value[i];
					if (data.header[i] == "族群品類" || data.header[i] == "族群名稱" || data.header[i] == "類型名稱") {
						$('#table').append("<tr><td><div class='preview'>" + data.header[i] + "<img src='/Common/assets/images/" + data.header[i] + ".jpg' class='hide-image'></div></td><td><div>" + data.value[i] + "</div></td></tr>");
					} else {
						$('#table').append("<tr><td><div>" + data.header[i] + "</div></td><td><div>" + data.value[i] + "</div></td></tr>");
					}
				}

				for (var i = 0; i < sizeHeader.length; i++) {
					if (value[i] == null)
						value[i] = "";
					if (data.sizeHeader[i] != 0 && data.sizeValue[i] != 0) {
						$('#sizeTable').append("<tr><td>" + data.sizeHeader[i] + "</td><td>" + data.sizeValue[i] + "</td></tr>");
					} else
						$('#sizeTable').append("<tr><td>無資料</td><td>無資料</td></tr>");
				}
				$("#insertComponentName").text(name);

				$.ajax({
					url: "./assets/glb/Revit元件/Components/" + parent + "/" + type + "/" + name + ".PNG",
					success: function() {
						$("#2DImage").append("<img src='./assets/glb/Revit元件/Components/" + parent + "/" + type + "/" + name + ".PNG' style='width: 100%; aspect-ratio: 1 / 1;'>")
					},
					error: function() {
						$("#2DImage").append("<span>此元件沒有2D圖例</span>");
					}
				});

			}).error(function(error) {
				console.log(error.responseText);
			});

			$.ajax({
				url: "./script/php/addFavorite.php",
				type: "GET",
				data: {
					CheckFavorite: parent + "/" + type + "/" + name
				},
				contentType: "application/json",
				dataType: "json"
			}).done(function(data) {
				isFavorite = data;
				if (isFavorite) {
					$("#heart").css("color", "Tomato");
					$("#favorite").html('移出清單');
				} else {
					$("#heart").css("color", "white");
					$("#favorite").html('加入清單');
				}
			}).error(function(error) {
				console.log(error.responseText);
			});

		});

		for (var i = 0; i < jsonData.length; i++) {
			for (var j = 0; j < jsonData[i].children.length; j++) {
				$("a").each(function() {
					if (jsonData[i].category_name == parent) {
						if (jsonData[i].children[j].component_name == type) {
							var searchText = jsonData[i].children[j].component_name;
							var found;

							if ($(this).context.innerText == searchText && $(this).context.name == jsonData[i].category_name) {
								$(this).context.classList.add("highlight");
								$(this).context.parentNode.parentNode.parentNode.classList.add("nav-expanded");
							}
						}
					}
				});
			}
		}

		function AddFavorite() {
			var stack_bar_top = {
				"dir1": "down",
				"dir2": "right",
				"push": "top",
				"spacing1": 0,
				"spacing2": 0
			};

			if (isFavorite) {
				$.ajax({
					url: "./script/php/addFavorite.php",
					type: "POST",
					data: {
						DelFavorite: parent + "/" + type + "/" + name,
						username: userName
					}
				}).done(function(data) {
					var notice = new PNotify({
						title: '通知',
						text: '移出清單成功!',
						type: 'success',
						addclass: 'stack-bar-top',
						stack: stack_bar_top,
						width: "100%"
					});
					isFavorite = false;
					$("#heart").css("color", "white");
					$("#favorite").html('加入清單');
				}).error(function(error) {
					var notice = new PNotify({
						title: '通知',
						text: '移出清單失敗!',
						type: 'error',
						addclass: 'stack-bar-top',
						stack: stack_bar_top,
						width: "100%"
					});
					console.log(error);
				});
			} else {
				$.ajax({
					url: "./script/php/addFavorite.php",
					type: "POST",
					data: {
						AddFavorite: parent + "/" + type + "/" + name,
						username: userName,
						revitVersion: revitVersion
					}
				}).done(function(data) {
					var notice = new PNotify({
						title: '通知',
						text: '加入清單成功!',
						type: 'success',
						addclass: 'stack-bar-top',
						stack: stack_bar_top,
						width: "100%"
					});
					isFavorite = true;
					$("#heart").css("color", "Tomato");
					$("#favorite").html('移出清單');
				}).error(function() {
					var notice = new PNotify({
						title: '通知',
						text: '加入清單失敗!',
						type: 'error',
						addclass: 'stack-bar-top',
						stack: stack_bar_top,
						width: "100%"
					});
				});

			}
		}
	</script>
	<!-- Vendor -->

	<script src="script/js/3Dpic-detail.js" type="module" async></script>

</body>

</html>