<?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 type="application/javascript">
		var delFavoriteList = [];
		var i = 0;
		var path = [];
		var categoryList = [];
		var categoryTable = [];
		var id = [];
		var revitVersion = [];
		var userName = "<?php echo $_SESSION['name'] ?>";
		var role = "<?php echo ($groupName); ?>";
		var right = <?php echo ($right); ?>;
		var $grid;
		//var year = [];
		pageHeader = "元件庫";

		var parent = [];
		var type = [];
		var object = [];
		var typeInEnglish = [];
		var parentInEnglish = [];
	</script>
</head>

<body>
	<canvas id="c"></canvas>
	<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>
									<search-bar></search-bar>
									<filter-bar></filter-bar>
									<li class="right">
										<a href="#" id="mgSelectAll"><i class="fa fa-check-square"></i> <span data-all-text="選擇全部" data-none-text="取消全選">選擇全部</span></a>
									</li>
									<li class="right">
										<a href="#" onclick="DelFavorite();"><i class="fa fa-trash-o"></i> 移出清單</a>
									</li>
								</ul>
							</div>
							<filter-content></filter-content>
							<div class="row mg-files" data-sort-destination data-sort-id="media-gallery" id="content">
							</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() {

			$("a").each(function() {
				var searchText = "清單列表";
				var found;

				if ($(this).context.innerText == searchText) {
					$(this).context.classList.add("highlight");
				}
			});

			//$.getJSON
			$.ajaxSetup({
				async: false
			});

			$.ajax({
				url: "./script/php/equipment_group_favorite.php",
				type: "GET",
				data: {
					userName: userName,
				},
				contentType: "application/json",
				dataType: "json"
			}).done(function(data) {
				id = data.id;
				path = data.path;
				for (let i = 0; i < path.length; i++) {
					var array = path[i].split('/');
					parent[i] = array[0];
					type[i] = array[1];
					object[i] = array[2];
				}

				$.getJSON('./assets/equipmentTable2.json', function(data) {
					for (var k = 0; k < path.length; k++) {
						for (var i = 0; i < data.length; i++) {
							if (parent[k] == data[i].category_name) {
								parentInEnglish[k] = data[i].category_code;
							}
							for (var j = 0; j < data[i].children.length; j++) {
								if (type[k] == data[i].children[j].component_name) {
									typeInEnglish[k] = data[i].children[j].component_code;
								}
							}
						}
					}
				});


				revitVersion = data.revitVersion;
				for (var i = 0; i < data.year.length; i++) {
					$("#revitVersion").append('<label><input type="checkbox" value=".' + data.year[i] + '" />   ' + data.year[i] + '</label> ');
				}

				$.when(initThreeJS()).then(function() {
					$grid = $("#content").isotope({
						itemSelector: ".isotope-item",
						layoutMode: 'fitRows'
					});
				}).then(function() {
					setTimeout(function() {
						$grid.isotope()
					}, 100);
				});

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

		});

		//刪除元件checkbox
		$(document).on("change", "input[name='delCheck']", function() {
			if ($(this).prop('checked') === true) {
				delFavoriteList.push($(this).attr('value'));
			} else {
				delFavoriteList.splice(delFavoriteList.indexOf($(this).attr('value')), 1);
			}
		});

		function DelFavorite() {
			$.ajax({
				url: "./script/php/addFavorite.php",
				type: "POST",
				data: {
					delFavoriteList: delFavoriteList,
					username: userName
				}
			}).done(function(data) {
				window.location.reload();
			}).error(function(error) {
				console.log(error);
			});
		}

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

</body>

</html>