<?php
if(isset($_GET['sil_id'])){
    $delete = $db->exec("DELETE FROM vitrin_urun WHERE id = '{$_GET['sil_id']}' LIMIT 1");
    echo b();
}

if($_POST AND !isset($_POST['siralama'])){

    if(isset($_GET['duzenle_id'])){
        $islem = $db->prepare("UPDATE vitrin_urun SET urun_id = ? WHERE id = ?");
        $islem = $islem->execute(array($_POST['urun_id'],$_GET['duzenle_id']));
    }else{
        $islem = $db->prepare("INSERT INTO vitrin_urun SET vitrin_id = ?, urun_id = ?, sira  = ?");
        $islem = $islem->execute(array($_GET['vitrin_id'],$_POST['urun_id'],0));
    }
    if($islem){
        echo b();
    }else{
        echo h();
    }
}

if(isset($_GET['duzenle_id'])){
    $duzenle = $db->query("SELECT * FROM vitrin_urun WHERE id = '{$_GET['duzenle_id']}' LIMIT 1")->fetch(PDO::FETCH_ASSOC);
    ?>
		<script type="text/javascript">
			$(function(){
				$('select[name="urun_id"] option[value="<?php echo $duzenle['urun_id']; ?>"]').attr('selected','select');
			});
		</script>
	<?php
}


if($_POST AND isset($_POST['siralama'])){

	$siralama = str_replace('\"', '"', $_POST['siralama']);
	$data = json_decode($siralama);
	$readbleArray = parseJsonArray($data);
	$i = 1;
	foreach($readbleArray as $row){
		$islem = $db->prepare("UPDATE vitrin_urun SET sira = ? WHERE id = ?");
		$islem = $islem->execute(array($i,$row['id']));
		$i++;
	}
	echo b();

}
?>
<div class="page-header">
	<div class="page-leftheader">
		<h4 class="page-title">Anasayfa Vitrin Yönetimi</h4>
	</div>
	<div class="page-rightheader ml-auto d-lg-flex d-none">
		<ol class="breadcrumb">
			<li class="breadcrumb-item"><a href="index.php">Anasayfa</a></li>
			<li class="breadcrumb-item active" aria-current="page">Anasayfa Vitrin Yönetimi</li>
		</ol>
	</div>
</div>
						
<div class="row">
	<div class="col-md-6">
		<div class="card">
			<div class="card-body">
				<div class="row">
					<div class="col-md-12">
						
						<?php 
							$cek = $db->query("SELECT * FROM vitrin_urun WHERE vitrin_id = '{$_GET['vitrin_id']}' ORDER BY sira ASC", PDO::FETCH_ASSOC);
							if($cek->rowCount()){
						?>
						<form action="" method="post">
							<div class="dd" id="nestable" style="float: left;width: 100%">
					            <ol class="dd-list">
					                
					                <?php
							            foreach ($cek as $ak) {
							            	$urun = $db->query("SELECT * FROM urun WHERE id = '{$ak['urun_id']}' LIMIT 1")->fetch(PDO::FETCH_ASSOC);
							              echo '<li data-id="'.$ak['id'].'" class="dd-item">
							                      <div class="dd-handle">
							                      	'.$urun['baslik'].'
							                      </div>
							                      	<a href="'.$sayfa.'/'.$_GET['vitrin_id'].'/sil/'.$ak['id'].'" class="sil" style="float:right;color:red">Sil</a>
							                    </li>';
							            }
							        ?>

					                

					            </ol>
					        </div>
					        <input type="hidden" name="siralama" id="nestable-output">

							<div class="form-group row">
								<div class="col-md-12"><center><button type="submit" class="btn btn-info">Kaydet</button></center></div>
							</div>
				        </form>
				    	<?php } ?>

					</div>
				</div>
			</div>
		</div>
	</div>
	<div class="col-md-6">
		<div class="card">
			<div class="card-body">
				<div class="row">
					<div class="col-md-12">
						
						<form class="form-horizontal" method="post" action="">

							<div class="form-group row">
							  <label class="col-sm-3 form-label">Ürün Seç</label>
							  <div class="col-sm-9">
								<select class="form-control" name="urun_id" required="">
									<?php
										$cek = $db->query("SELECT * FROM urun", PDO::FETCH_ASSOC);
										if($cek->rowCount()){
											foreach( $cek as $c ){
												echo '<option value="'.$c['id'].'">'.$c['baslik'].'</option>';
											}
										}
									?>
								</select>
							  </div>
							</div>

							<div class="form-group row">
								<div class="col-md-12"><center><button type="submit" class="btn btn-info">Kaydet</button></center></div>
							</div>

						</form>

					</div>
				</div>
			</div>
		</div>
	</div>
</div>

<script type="text/javascript" src="assets/js/jquery.nestable.js"></script>
<link rel="stylesheet" type="text/css" href="assets/css/nestable.css">
<script>
$(document).ready(function(){

    var updateOutput = function(e){
        var list   = e.length ? e : $(e.target),
            output = list.data('output');
        if (window.JSON) {
            output.val(window.JSON.stringify(list.nestable('serialize')));
        } else {
            output.val('JSON browser support required for this demo.');
        }
    };

    $('#nestable').nestable({
        group: 1
    }).on('change', updateOutput);

    updateOutput($('#nestable').data('output', $('#nestable-output')));

});
</script>