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

if($_POST){
	
    if(isset($_GET['duzenle_id'])){
        $islem = $db->prepare("UPDATE etiket SET baslik = ?, link = ? WHERE id = ?");
        $islem = $islem->execute(array($_POST['baslik'],$_POST['link'],$_GET['duzenle_id']));
    }else{
        $islem = $db->prepare("INSERT INTO etiket SET baslik = ?, link = ?");
        $islem = $islem->execute(array($_POST['baslik'],$_POST['link']));
    }

    if($islem){
        echo b();
    }else{
        echo h();
    }

}

if(isset($_GET['duzenle_id'])){
    $duzenle = $db->query("SELECT * FROM etiket WHERE id = '{$_GET['duzenle_id']}' LIMIT 1")->fetch(PDO::FETCH_ASSOC);
}
?>
<div class="page-header">
	<div class="page-leftheader">
		<h4 class="page-title">Site Altı Etiket 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">Site Altı Etiket 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">
						
						<div class="table-responsive">
							<table class="table table-bordered text-nowrap" id="example2">
								<thead>
									<tr>
										<th class="wd-15p border-bottom-0">ID</th>
										<th class="wd-15p border-bottom-0">Başkık</th>
										<th class="wd-15p border-bottom-0">Link</th>
										<th class="wd-15p border-bottom-0">İşlem</th>
									</tr>
								</thead>
								<tbody>
									<?php
				                      $query = $db->query("SELECT * FROM etiket ORDER BY id DESC", PDO::FETCH_ASSOC);
				                      if($query->rowCount()){
				                        foreach( $query as $row ){
				                          echo '
				                            <tr>
				                              <td>'.$row['id'].'</td>
				                              <td>'.$row['baslik'].'</td>
				                              <td>'.$row['link'].'</td>
				                              <td>
				                                <a href="'.$sayfa.'/duzenle/'.$row['id'].'" class="badge badge-success" style="margin-right:10px">Düzenle</span>
				                                <a href="'.$sayfa.'/sil/'.$row['id'].'" class="badge badge-danger" style="margin-right:10px">Sil</span>
				                              </td>
				                            </tr>
				                          ';
				                        }
				                      }
				                    ?>
								</tbody>
							</table>
						</div>

					</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-md-3 form-label">Başlık</label>
								<div class="col-md-9">
									<input type="text" class="form-control" name="baslik" value="<?php echo @$duzenle['baslik']; ?>" required="">
								</div>
							</div>

							<div class="form-group row">
								<label class="col-md-3 form-label">Link</label>
								<div class="col-md-9">
									<input type="text" class="form-control" name="link" value="<?php echo @$duzenle['link']; ?>" required="">
								</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>
