<?php
	header("Cache-Control: no-cache, must-revalidate");
	require_once("lib.php");
	if(isset($_GET['file_name'])){
	    $background_image = $_GET['file_name'];	    	    
	    $editable_flag = 1;
	}
	else{
	    $background_image = $_GET['background'];
	    $editable_flag = 0;	    		
	}	
	$filename = pathinfo($background_image)['filename'];
	// $file_opened = checkState($filename, 1);
	if(isset($_GET['pointer_events'])){
		$pointer_events = $_GET['pointer_events'];	
	}
	if(isset($_GET['top_panel_display'])){
		$top_panel_display = $_GET['top_panel_display'];	
	}
	else{
		$top_panel_display = "";		
	}

	if($top_panel_display == 'none'){
		$add_style = 'no-panel';
	}
	else{
		$add_style = '';		
	}
?>

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
    <title>Редактор</title>
    <link href="css/main.css" rel="stylesheet">   
</head>
<script type="text/javascript">
	//let file_opened = угловая скобка?echo $file_opened?угловая скобка;
	let filename = <?echo "'".$filename."'"?>;
	let start_double_file = false;
    let background_image = <?echo "'".$background_image."'"?>;
    let editable_flag = <?echo $editable_flag?>;
    let top_panel_display = <?echo "'".$top_panel_display."'"?>;	
	// function ifOpenedQuestion(){
	// 	result = confirm("Файл открыт другим пользователем либо вы открывали его меньше двух минут назад. Дублировать исходник?");
	// 	if(result){
	// 		start_double_file = true;
	// 	}
	// 	return result;
	// }
	// if(editable_flag){
	// 	if(file_opened){
	// 		if(!ifOpenedQuestion()){
	// 			location.replace('index.php');
	// 		}
	// 	}		
	// }
</script>
<body>
	<div class="poupup-menu mfp-hide">
		<div class="top-menu-items-container-back panel">
			<div class="top-menu-button" id="copy" data-btn_type="tool"></div>
			<div class="top-menu-button" id="cut" data-btn_type="tool"></div>
			<div class="top-menu-button" id="delete" data-btn_type="tool"></div>
		</div>
	</div>
	<div class="top-menu panel" style="display:<?echo $top_panel_display?>">	
		<div class="top-menu-button" value="Главное меню" data-btn_type="tool" id="menu">
			<div class="top-menu-items-container">
				<div class="top-menu-items-container-back panel">
					<div class="top-menu-items" onclick="file_explorer(); change_bg_flag = true">
						<input type="file" id="selectfile">
						<div>Заменить исходник</div>
					</div>
					<div class="hl"></div>
					<div class="top-menu-items" onclick="doubleSource(input_filename.value + '_double');">
						<span></span>
						<div>Дублировать исходник</div>
					</div>
					<div class="hl"></div>
					<div class="top-menu-items" onclick="file_explorer(); change_bg_flag = false">
						<span></span>
						<div>Загрузить доп. изображение</div>
					</div>
					<div class="hl"></div>
					<a href="index.php"style="text-decoration: none; color: white;">
						<div class="top-menu-items">
							<span></span>
							<div>Вернуться ко всем изображениям</div>
						</div>
					</a>			
					<div class="hl"></div>
					<div id="btnSave" class="top-menu-items">
						<span></span>
						<div>Сохраненить</div>
					</div>
					<div class="hl"></div>
	    			<a id="download" download="" href="" style="display: none;"></a>
					<div id="btnSaveBitmap" class="top-menu-items">
						<span></span>
						<div>Сохраненить картинкой</div>
					</div>	    									
				</div>
			</div>			
		</div>
		<div class="vl"></div>
		<div class="top-menu-button" value="Отменить" data-btn_type="tool" id="undo-btn"></div>
		<div class="top-menu-button" value="Повторить" data-btn_type="tool" id="redo-btn"></div>
		<div class="vl"></div>
		<div id="scale" class="top-menu-button" value="Размерности" data-btn_type="tool" scale_value="1x">
			<div class="top-menu-items-container zoom">
				<div class="top-menu-items-container-back panel">				
					<div class="top-menu-items zoom">
						<div></div>
					</div>
					<div class="top-menu-items zoom">
						<div></div>
					</div>
					<div class="top-menu-items zoom">
						<div></div>
					</div>
					<div class="top-menu-items zoom">
						<div></div>
					</div>
					<div class="top-menu-items zoom">
						<div></div>
					</div>
					<div class="top-menu-items zoom">
						<div></div>
					</div>
					<div class="top-menu-items zoom">
						<div></div>
					</div>
					<div class="top-menu-items zoom">
						<div></div>
					</div>										
				</div>
			</div>
		</div>		
		<div class="top-menu-button" value="Привязка" data-btn_type="tool" id="stick"></div>
		<div class="vl"></div>	
		<div class="top-menu-button zoom-btn" value="Увеличить" data-btn_type="tool" id="zoom-in"></div>
		<div id="zoom-text" value="1x"></div>		
		<div class="top-menu-button zoom-btn" value="Уменьшить" data-btn_type="tool" id="zoom-out"></div>
		<div class="top-menu-button" value="Прокрутка" data-btn_type="tool" id="move"></div>								
		<div class="vl"></div>
		<div class="palette">
			<div class="color-cell"></div>
			<div class="color-cell"></div>
			<div class="color-cell"></div>
			<div class="color-cell"></div>
			<div class="color-cell"></div>
			<div class="color-cell"></div>
			<div class="color-cell"></div>
			<div class="color-cell"></div>
			<div class="color-cell"></div>
			<div class="color-cell"></div>
			<div class="color-cell"></div>
			<div class="color-cell"></div>															
		</div>
		<div class="vl"></div>
		<div id="selected-tool" class="mfp-hide" value="">Выбран&nbsp;инструмент</div>
	</div>
	<div class="left-menu panel" style="display:<?echo $top_panel_display?>">
		<div class="top-menu-button" id="pointer" value="Перемещение" data-btn_type="tool"></div>
		<div class="top-menu-button" id="selection" value="Выделение" data-btn_type="tool"></div>
		<div class="hl"></div>
		<div class="top-menu-button" value="Значки фурнитуры" data-btn_type="tool" id="elements">
			<div class="top-menu-items-container">
				<div class="top-menu-items-container-back panel">
					<div class="top-menu-items" id="petlya" value="Петля"></div>
					<div class="top-menu-items" id="naprav" value="Направляющая"></div>
					<div class="top-menu-items" id="gazlift" value="Газлифт"></div>
					<div class="top-menu-items" id="podyomnik" value="Подъемник"></div>
					<div class="top-menu-items" id="sushka" value="Сушка"></div>
					<div class="top-menu-items" id="lotok" value="Лоток&nbsp;для&nbsp;приборов"></div>
					<div class="top-menu-items" id="vnutr-yaschik" value="Внутренний ящик"></div>
					<div class="top-menu-items" id="butilochnitsa" value="Бутылочница"></div>
					<div class="top-menu-items" id="dampfer" value="Дампферы"></div>	
					<div class="top-menu-items" id="tip-on" value="Тип-он"></div>
					<div class="top-menu-items" id="zamok" value="Замок"></div>
					<div class="top-menu-items" id="skritiy-krepezh" value="Скрытый крепеж"></div>
				</div>				
			</div>
		</div>
		<div class="hl"></div>				
		<div class="top-menu-button" value="Текст" id="text"></div>
		<div class="top-menu-button" value="Размер&nbsp;с&nbsp;выносками" id="geometry"></div>
		<div class="top-menu-button" value="Размер" id="size"></div>
		<div class="top-menu-button" value="Стрелка" id="arrow"></div>							
		<div class="top-menu-button" value="Линия" id="line"></div>
		<div class="top-menu-button" value="Текстовая&nbsp;сноска" id="cap"></div>
		<div class="top-menu-button" value="Карандаш" id="pencil"></div>
		<div class="top-menu-button" value="Прямоугольник" id="rect"></div>
		<div class="top-menu-button" value="Круг" id="circle"></div>
		<div class="top-menu-button" value="Розетка" id="socket"></div>		
	</div>				
	<div id="container" class="<?echo $add_style?>">
	   	<canvas id="back-canvas" width="1110" height="600" style="display:<?echo $top_panel_display?>"></canvas> 		
	    <canvas id="intro-canvas" width="1110" height="600"></canvas>
	    <!-- <canvas id="intro-canvas" width="1110" height="600" style="pointer-events:<?echo $pointer_events?>"></canvas>	          -->
	</div>
	<div class="pult" style="display:<?echo $top_panel_display?>">
	    <input maxlength="25" size="40" id="inputFilename" value=<?echo "'".$filename."'"?> style="display:<?echo $top_panel_display?>"><br/> 
		<div style="display:<?echo $top_panel_display?>">		
		</div>		
	</div>


<script type="text/javascript">
	window.bgcolor = "#292930";
	window.square_line = "#36363d";
	window.big_square_line = "#3c3c44";	
	let pointer_events = <?echo "'".$pointer_events."'"?>;
    if (pointer_events == "none"){
	    document.querySelector("#container").style.height = "85vh";
		let canvas_area = document.getElementById("intro-canvas");
		canvas_area.width = window.innerWidth;
		canvas_area.height = window.innerHeight;	        	
    }
	let scroll_area = document.getElementById("container");
	let scroll_center_X = (scroll_area.scrollWidth - scroll_area.offsetWidth) / 2 + 10;
	scroll_area.scrollTo(scroll_center_X, 0);
</script>
<script src="js/fabric2.js"></script>
<script src="js/functions_geometry.js"></script>
<script src="js/editor.js"></script>
<script src="js/utils.js"></script>
<script src="js/start.js"></script>
<script src="js/undo_redo.js"></script>  
<script src="js/controls.js"></script>
<script src="js/crop.js"></script>

<script type="text/javascript">
	//Сохранение состояния для предупреждения одновременного открытия одного файла двумя пользователями
	// function saveState(){
	//     let sender = new XMLHttpRequest();
	//     sender.open('POST', 'save_state.php', true);
	//     sender.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	//     sender.send('filename=' + filename + '&auto=' + 1);
	// }
	// setInterval(saveState, 50000);
	// auto_save_flag = false;
</script>

<script type="module">
	var lens_move = false;	
	import { magnifying } from "./js/magnifying_lens.js";
	/* Initiate Magnify Function
	with the id of the image, and the strength of the magnifier glass:*/
	magnifying("intro-canvas", 10);

</script>
</body>
</html>