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

if($_POST){
    if(isset($_GET['duzenle_id'])){
        $islem = $db->prepare("UPDATE admin SET kullanici_adi = ?, sifre = ? WHERE id = ?");
        $islem = $islem->execute(array($_POST['kullanici_adi'],$_POST['sifre'],$_GET['duzenle_id']));
    }else{
        $islem = $db->prepare("INSERT INTO admin SET kullanici_adi = ?, sifre = ?");
        $islem = $islem->execute(array($_POST['kullanici_adi'],$_POST['sifre']));
    }
    if($islem){
        echo b();
    }else{
        echo h();
    }
}
if(isset($_GET['duzenle_id'])){
    $duzenle = $db->query("SELECT * FROM admin WHERE id = '{$_GET['duzenle_id']}' LIMIT 1")->fetch(PDO::FETCH_ASSOC);
}
?>
<div class="page-header">
	<div class="page-leftheader">
		<h4 class="page-title">Panel Kullanıcıları</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">Panel Kullanıcıları</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">Kullanıcı Adı</th>
										<th class="wd-15p border-bottom-0">Şifre</th>
										<th class="wd-15p border-bottom-0">İşlem</th>
									</tr>
								</thead>
								<tbody>
									<?php
				                      $query = $db->query("SELECT * FROM admin", PDO::FETCH_ASSOC);
				                      if($query->rowCount()){
				                        foreach( $query as $row ){
				                          echo '
				                            <tr>
				                              <td>'.$row['id'].'</td>
				                              <td>'.$row['kullanici_adi'].'</td>
				                              <td>'.$row['sifre'].'</td>
				                              <td>
				                                <a href="'.$sayfa.'/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>

					</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">Kullanıcı Adı</label>
								<div class="col-md-9">
									<input type="text" class="form-control" name="kullanici_adi" value="<?php echo @$duzenle['kullanici_adi']; ?>" required="">
								</div>
							</div>
							<div class="form-group row">
								<label class="col-md-3 form-label">Şifre</label>
								<div class="col-md-9">
									<input type="text" class="form-control" name="sifre" value="<?php echo @$duzenle['sifre']; ?>" 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>
