<?php 
// Security Headers
header("X-Frame-Options: SAMEORIGIN"); // cegah clickjacking
header("X-Content-Type-Options: nosniff"); // cegah MIME sniffing
header("X-XSS-Protection: 1; mode=block"); // cegah XSS di browser lama
header("Referrer-Policy: no-referrer-when-downgrade"); // batasi info referrer
header("Content-Security-Policy: default-src 'self' https: data:; script-src 'self' https://cdn.jsdelivr.net https://cdnjs.cloudflare.com; style-src 'self' https://cdn.jsdelivr.net https://fonts.googleapis.com 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' https://fonts.gstatic.com;"); 
?>

<?php
include "../config.php";
$id = $_GET['id'];
$data = mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM buku WHERE id='$id'"));

if(isset($_POST['update'])){
    $judul   = $_POST['judul'];
    $penulis = $_POST['penulis'];
    $tahun   = $_POST['tahun'];
    $halaman = $_POST['halaman'];
    $jenis = $_POST['jenis'];
    $ukuran  = $_POST['ukuran'];
    $isbn    = $_POST['isbn'];
    $edisi   = $_POST['edisi'];
    $stock    = $_POST['stock'];
    $harga   = $_POST['harga'];
    $terjual = $_POST['terjual'];
    $deskripsi = $_POST['deskripsi'];

    // Jika ganti cover
    if($_FILES['cover']['name']!=""){
        $cover = $_FILES['cover']['name'];
        $tmp = $_FILES['cover']['tmp_name'];
        move_uploaded_file($tmp, "uploads/".$cover);

        
    } else {
        $cover = $data['cover'];
    }
    $judul   = mysqli_real_escape_string($conn, $_POST['judul']);
    $penulis = mysqli_real_escape_string($conn, $_POST['penulis']);
    $query = "UPDATE buku SET 
                judul='$judul',penulis='$penulis',tahun='$tahun',halaman='$halaman',
                jenis='$jenis',ukuran='$ukuran',isbn='$isbn',edisi='$edisi',stock='$stock',harga='$harga',
                terjual='$terjual',deskripsi='$deskripsi',cover='$cover'
              WHERE id='$id'";
    mysqli_query($conn, $query);

    header("Location: katalog.php");
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Edit Buku</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="bg-light">

<div class="container mt-5">
  <div class="card shadow-lg border-0">
    <div class="card-header bg-warning text-dark">
      <h4 class="mb-0">✏️ Edit Buku</h4>
    </div>
    <div class="card-body">
      <form method="POST" enctype="multipart/form-data">
<form method="post" enctype="multipart/form-data">
    <div class="mb-3">
        <label>Judul</label>
        <input type="text" name="judul" class="form-control" value="<?= $data['judul'] ?>">
    </div>
    <div class="mb-3">
        <label>Penulis</label>
        <input type="text" name="penulis" class="form-control" value="<?= $data['penulis'] ?>">
    </div>
    <div class="mb-3">
        <label>Tahun Terbit</label>
        <input type="number" name="tahun" class="form-control" value="<?= $data['tahun'] ?>">
    </div>
    <div class="mb-3">
        <label>Jumlah Halaman</label>
        <input type="number" name="halaman" class="form-control" value="<?= $data['halaman'] ?>">
    </div>
    <div class="mb-3">
        <label>Jenis Buku</label>
        <input type="text" name="jenis" class="form-control" value="<?= $data['jenis'] ?>">
    </div>
    <div class="mb-3">
        <label>Ukuran Buku</label>
        <input type="text" name="ukuran" class="form-control" value="<?= $data['ukuran'] ?>">
    </div>
    <div class="mb-3">
        <label>ISBN</label>
        <input type="text" name="isbn" class="form-control" value="<?= $data['isbn'] ?>">
    </div>
    <div class="mb-3">
        <label>Edisi</label>
        <input type="text" name="edisi" class="form-control" value="<?= $data['edisi'] ?>">
    </div>
    <div class="mb-3">
        <label>Stock</label>
        <input type="number" name="stock" class="form-control" value="<?= $data['stock'] ?>">
    </div>
    <div class="mb-3">
        <label>Harga</label>
        <input type="number" name="harga" class="form-control" value="<?= $data['harga'] ?>">
    </div>
    <div class="mb-3">
        <label>Terjual</label>
        <input type="number" name="terjual" class="form-control" value="<?= $data['terjual'] ?>">
    </div>
    <div class="mb-3">
        <label>Deskripsi</label>
        <textarea name="deskripsi" class="form-control" rows="4"><?= $data['deskripsi'] ?></textarea>
    </div>
    <div class="mb-3">
        <label>Upload Cover (biarkan kosong jika tidak diganti)</label>
        <input type="file" name="cover" class="form-control">
        <img src="uploads/<?= $data['cover'] ?>" width="100" class="mt-2">
    </div>
    <button type="submit" name="update" class="btn btn-warning">💾 Update Buku</button>
    <a href="katalog.php" class="btn btn-secondary">⬅ Kembali</a>
</form>

</body>
</html>
