
<?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";

if(isset($_POST['simpan'])){
    $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'];
   

    // Upload cover
    $cover = $_FILES['cover']['name'];
    $tmp = $_FILES['cover']['tmp_name'];
    move_uploaded_file($tmp, "uploads/".$cover);

    
    $judul   = mysqli_real_escape_string($conn, $_POST['judul']);
    $penulis = mysqli_real_escape_string($conn, $_POST['penulis']);
    $query = "INSERT INTO buku (judul,penulis,tahun,halaman,jenis,ukuran,isbn,edisi,stock,harga,terjual,deskripsi,cover) 
              VALUES ('$judul','$penulis','$tahun','$halaman','$jenis','$ukuran','$isbn','$edisi','$stock','$harga','$terjual','$deskripsi','$cover')";
    mysqli_query($conn, $query);

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

?>
<!DOCTYPE html>
<html lang="id">
<head>
  <meta charset="UTF-8">
  <title>Tambah 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">
    <div class="card-header bg-primary text-white">
      <h4 class="mb-0">📚 Tambah Buku Baru</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" required>
    </div>
    <div class="mb-3">
        <label>Penulis</label>
        <input type="text" name="penulis" class="form-control" required>
    </div>
    <div class="mb-3">
        <label>Tahun Terbit</label>
        <input type="number" name="tahun" class="form-control" required>
    </div>
    <div class="mb-3">
        <label>Jumlah Halaman</label>
        <input type="number" name="halaman" class="form-control">
    </div>
    <div class="mb-3">
        <label>Jenis Buku</label>
        <input type="text" name="jenis" class="form-control">
    </div>
    <div class="mb-3">
        <label>Ukuran Buku</label>
        <input type="text" name="ukuran" class="form-control">
    </div>
    <div class="mb-3">
        <label>ISBN</label>
        <input type="text" name="isbn" class="form-control">
    </div>
    <div class="mb-3">
        <label>Edisi</label>
        <input type="text" name="edisi" class="form-control">
    </div>
    <div class="mb-3">
        <label>Stock</label>
        <input type="number" name="stock" class="form-control">
    </div>
    <div class="mb-3">
        <label>Harga</label>
        <input type="number" name="harga" class="form-control">
    </div>
    <div class="mb-3">
        <label>Terjual</label>
        <input type="number" name="terjual" class="form-control">
    </div>
    <div class="mb-3">
        <label>Deskripsi</label>
        <textarea name="deskripsi" class="form-control" rows="4"></textarea>
    </div>
    <div class="mb-3">
        <label>Upload Cover</label>
        <input type="file" name="cover" class="form-control">
    </div class="d-flex justify-content-between">
    <button type="submit" name="simpan" class="btn btn-primary">💾 Simpan Buku</button>
    <a href="katalog.php" class="btn btn-secondary">⬅ Kembali</a>
</form>

</body>
</html>
