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

    $delete = $db->exec("DELETE FROM urun_kategori WHERE urun_id = '{$_GET['sil_id']}' ");
    $delete = $db->exec("DELETE FROM urun_renk WHERE urun_id = '{$_GET['sil_id']}'");
    $delete = $db->exec("DELETE FROM urun_img WHERE urun_id = '{$_GET['sil_id']}'");
    $delete = $db->exec("DELETE FROM vitrin_urun WHERE urun_id = '{$_GET['sil_id']}'");

    $query = $db->query("SELECT * FROM urun_secenek WHERE urun_id = '{$_GET['sil_id']}' ", PDO::FETCH_ASSOC);
	 if($query->rowCount()){
	    foreach( $query as $row ){

	    	$delete = $db->exec("DELETE FROM urun_secenek_alt WHERE urun_secenek_id = '{$row['id']}'");
	    	$delete = $db->exec("DELETE FROM urun_secenek WHERE id = '{$row['id']}'");

	    }
	 }
    echo b();
}


$sorgusay = $db->prepare("SELECT count(id) FROM urun");
$sorgusay->execute();
$say = $sorgusay->fetchColumn();

$top_sayfa = $say;
$page      = $_GET['no'];
$limit     = 10;
$page_url  = $sayfa.'/';
$baslangic = ($page * $limit) - $limit;
?>
<div class="page-header">
	<div class="page-leftheader">
		<h4 class="page-title">Ürünleri Listele</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">Ürünleri Listele</li>
		</ol>
	</div>
</div>
						
<div class="row">
	
	<div class="col-md-12">

		<div class="card">
			<div class="card-header">
				<form action="" method="post" style="float: right;display: contents;">
					<input name="ara" type="text" autocomplete="off" class="form-control" placeholder="Ürün adı ile ara" style="margin-right: 5%">
					<button type="submit" class="btn btn-primary">Ara</button>
				</form>
			</div>
		</div>

		<div class="card">
			<div class="card-body">
				<div class="row">
					<div class="col-md-12">
							
							<div class="table-responsive">
								<table class="table card-table table-vcenter text-nowrap mb-0">
									<thead >
										<tr>
											<th class="wd-15p border-bottom-0">ID</th>
											<th class="wd-15p border-bottom-0">Fotoğraf</th>
											<th class="wd-15p border-bottom-0">Ürün Adı</th>
											<th class="wd-15p border-bottom-0">Fiyat</th>
											<th class="wd-15p border-bottom-0">Link</th>
											<th class="wd-15p border-bottom-0">İşlem</th>
										</tr>
									</thead>
									<tbody>
										<?php
										  if($_POST){
										  	$query = $db->query("SELECT * FROM urun WHERE baslik LIKE '%{$_POST['ara']}%' ", PDO::FETCH_ASSOC);
										  }else{
					                      	$query = $db->query("SELECT * FROM urun LIMIT {$baslangic},{$limit}", PDO::FETCH_ASSOC);
					                      }
					                      if($query->rowCount()){
					                        foreach( $query as $row ){

					                        $img = $db->query("SELECT * FROM urun_img WHERE urun_id = '{$row['id']}' LIMIT 1")->fetch(PDO::FETCH_ASSOC);
					                          echo '
					                            <tr>
					                              <td>'.$row['id'].'</td>
					                              <td><img src="../upload/'.$img['img'].'" style="max-width:100px;border:1px solid #ddd;padding:5px"></td>
					                              <td>'.$row['baslik'].'</td>
					                              <td>'.$row['fiyat'].' TL</td>
					                              <td><a href="'.$site.'urun/'.$row['sef'].'">Ürüne gitmek için tıklayın...</a></td>
					                              <td>
					                                <a href="urun-ekle/duzenle/'.$row['id'].'" class="badge badge-success" style="margin-right:10px">Güncelle</span>
					                                <a href="'.$sayfa.'/sil/'.$row['id'].'" class="badge badge-danger">Sil</span>
					                              </td>
					                            </tr>
					                          ';
					                        }
					                      }
					                    ?>
									</tbody>
								</table>
							</div>

							<?php if(!$_POST){ ?>
							<div class="pagination-wrapper" style="margin-bottom: 10px;float: right;">
								<nav aria-label="Page navigation">
									<ul class="pagination mb-0">
										<?php Sayfala($top_sayfa,$page,$limit,$page_url); ?>
									</ul>
								</nav>
							</div>
							<?php } ?>


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