<?php

	if(isset($_POST['cfms'])) {
		$desc = mb_substr(system :: text($_POST['desc']), 0, 300,'UTF-8');
		if($desc != $data['desc']) {
			DB :: $pdo -> query("UPDATE `{$table}` SET `desc` = ? WHERE `id` = ? LIMIT 1;", [$desc,$data['id']]);
			system :: redirect("Изменения успешно применены.", $thisLink."{$user['id']}/view{$id}?edit=1");
		}
	}

	if(AJAX && isset($_GET['tagger'])) {
	    
		try {
		$image = new image;
		$image->load(FILES_PATH.system :: photosGetView($data['hash'],4));
		$size = ($image->getWidth() < $image->getHeight() ? $image->getWidth() : $image->getHeight());
		$x1 = abs((int)$_POST['x1']);
		$y1 = abs((int)$_POST['y1']);
		$x2 = abs((int)$_POST['x2']);
		$y2 = abs((int)$_POST['y2']);
		$h = abs((int)$_POST['height']);
		$w = abs((int)$_POST['width']);

		//echo "x1 = {$x1} | x2 = {$x2} | y1 = {$y1} | y2 = {$y2} | h = {$h} | w = {$w}";
		if($h == $size && $w == $size) {
				$ww = $image->getWidth();
				$hh = $image->getHeight();
				// ЕБУЧАЯ СУКА ШИРИНА!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ЫФВОЛФЫРОВп
				if($image->getWidth() > $image->getHeight() && $x2 >= $h && $x2 == ($y2 + $x1) && $x2 <= $image->getWidth() && $y1 == 0 && $y2 == $w && $y2 == $h) {
					$image->crop(array($x1,0,$x2,$image->getHeight()));
					$ok = 1;
					$perc = $x1/($ww/100);
					// ЕБУЧАЯ СУКА ВЫСОТА
				} else if($image->getWidth() <= $image->getHeight() && $x1 == 0  && $y1 <= $image->getHeight()  && $y2 == ($x2 + $y1)) {
				// по высоте
					$image->crop(array(0,($y2-$size),$image->getWidth(),$y2));
					$ok = 1;
					$perc = $y1/($hh/100);
				}
				if(isset($ok)) {
					
					$image->preview2($image->getWidth(),$image->getHeight(),150);
					$image->save(FILES_PATH.system :: photosGetView($data['hash'],3));
					$image->preview2($image->getWidth(),$image->getHeight(),64);
					$image->save(FILES_PATH.system :: photosGetView($data['hash'],1));
					
					$arr2 = array(
							'x1'=>$x1,
							'y1'=>$y1,
							'x2'=>$x2,
							'y2'=>$y2,
							'height'=>$h,
							'width'=>$w
						);
					$rand = "".rand(100000,99999999999999).rand(1111,9999999999)."";
					DB :: $pdo -> query("UPDATE `{$table}` SET `mini` = ?, `perc` = ?, `key`=? WHERE `id` = ? LIMIT 1;", [json_encode($arr2), $perc,$rand,$data['id']]);
					if($data['id'] == $user['avatar']) {
						DB :: $pdo -> query("UPDATE `users` SET `count_avatar` = ? WHERE `id` = ? LIMIT 1;", [$rand,$user['id']]);
					}

				}
		}
		} catch(Exception $e) { die($e);}

	}


	$title = $l = "Редактирование фотографии";
	require (CORE."head.php");
	echo "<div class=\"app-block\">";
	$back = system :: back("{$thisLink}{$data['id_user']}/view{$data['id']}", "Фотография");
	
	echo $back;
	if($data['mini'] != "") {
		$arr = json_decode($data['mini'], true);
	}
	$arr['x1'] = (isset($arr['x1']) ? $arr['x1'] : 0);
	$arr['y1'] = (isset($arr['y1']) ? $arr['y1'] : 0);
	$arr['x2'] = (isset($arr['x2']) ? $arr['x2'] : 0);
	
	$arr['y2'] = (isset($arr['y2']) ? $arr['y2'] : 0);
	$arr['height'] = (isset($arr['height']) ? $arr['height'] : 0);
	$arr['width'] = (isset($arr['width']) ? $arr['width'] : 0);
	$arr = json_encode($arr);
	?>
		<link rel="stylesheet" type="text/css" href="/template/css/tagger.css" /><script type="text/javascript" src="/template/js/tagger.js"></script>
	<div class="list">
		<div class="_center">
		<div class="_gray _center _font-s">
		Вы можете выбрать область отображения для миниатюры фотографии.
		</div>
		<img onload="UI.loadTagger(this);" data-source="<?=$data['perc']?>" src="<?=FILES_SERVER.system :: photosGetView($data['hash'],4)?>">

		<div style="display: none;" id="tagger-btn">
			<input type="submit" name="tagger" data-url="<?="{$thisLink}{$data['id_user']}/view{$data['id']}"?>" value="Применить изменения" class="main-btn">
		</div>
		</div>
		<div>
			<form method="post" action="">
				<textarea placeholder="Введите описание к фотографии.." name="desc"><?=system :: check($data['desc'])?></textarea>

				<div class="_btn-up">
				<input type="submit" name="cfms" value="Сохранить" class="main-btn _center _dbl _w-100" />
				</div>
			</form>
		</div>

	</div>

	<?php
	echo "</div>";
	require (CORE."foot.php");