<?php
session_start();
include 'koneksi.php';

// Pastikan user telah login
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // Ambil dan bersihkan input
    $rfid_barang = trim($_POST['rfid_barang']);
    $nama_barang = trim($_POST['nama_barang']);
    $jumlah_barang = (int)$_POST['jumlah_barang'];
    $status_barang = $_POST['status_barang'];
    $keterangan = trim($_POST['keterangan']);
    $tempat_barang = $_POST['tempat_barang'];
    $foto = 'uploads/' . basename($_FILES["foto"]["name"]);

    // Upload foto
    if (move_uploaded_file($_FILES["foto"]["tmp_name"], $foto)) {
        // Menggunakan prepared statement untuk keamanan
        $stmt = $conn->prepare("INSERT INTO inventaris (rfid_barang, nama_barang, jumlah_barang, status_barang, foto, keterangan, tempat_barang) VALUES (?, ?, ?, ?, ?, ?, ?)");
        $stmt->bind_param("ssissss", $rfid_barang, $nama_barang, $jumlah_barang, $status_barang, $foto, $keterangan, $tempat_barang);

        if ($stmt->execute()) {
            echo "<script>alert('Barang berhasil ditambahkan');window.location='index.php';</script>";
        } else {
            echo "Error: " . $stmt->error;
        }

        $stmt->close();
    } else {
        echo "Error uploading file.";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tambah Barang</title>
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <!-- Font Awesome for Icons -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
    <link rel="stylesheet" href="adminlte/plugins/fontawesome-free/css/all.min.css">
    <link rel="stylesheet" href="adminlte/dist/css/adminlte.min.css">
    <link rel="stylesheet" href="adminlte/plugins/datatables-bs4/css/dataTables.bootstrap4.min.css">
    <link rel="stylesheet" href="adminlte/plugins/datatables-responsive/css/responsive.bootstrap4.min.css">
    <!-- Custom CSS -->
    <style>
        body {
            background-color: #f8f9fa;
        }
        .container {
            margin-top: 50px;
            max-width: 600px;
        }
        .card {
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }
        .card-title {
            font-size: 24px;
            font-weight: bold;
            margin-bottom: 20px;
            color: #007bff;
        }
        .btn-custom {
            background-color: #007bff;
            color: white;
            font-size: 16px;
            padding: 10px 20px;
            border-radius: 50px;
            transition: background-color 0.3s ease;
        }
        .btn-custom:hover {
            background-color: #0056b3;
        }
        .form-control, .form-control-file, textarea {
            border-radius: 10px;
        }
        textarea {
            width: 100%;
            padding: 10px;
        }
    </style>
</head>
<body>
<aside class="main-sidebar sidebar-dark-primary elevation-4">
    <!-- Sidebar -->
    <div class="sidebar">
        <!-- Sidebar Menu -->
        <nav class="mt-2">
            <ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu" data-accordion="false">
                <li class="nav-item">
                    <a href="index.php" class="nav-link">
                        <i class="nav-icon fas fa-user"></i>
                        <p>Dashboard</p>
                    </a>
                </li>
                <li class="nav-item">
                    <a href="tambah_barang.php" class="nav-link">
                        <i class="nav-icon fas fa-plus"></i>
                        <p>Tambah Barang</p>
                    </a>
                </li>
                <li class="nav-item">
                    <a href="laporan_barang.php" class="nav-link">
                        <i class="nav-icon fas fa-clipboard-list"></i>
                        <p>Laporan Barang</p>
                    </a>
                </li>
                <li class="nav-item">
                    <a href="riwayat_barang.php" class="nav-link">
                        <i class="nav-icon fas fa-history"></i>
                        <p>Riwayat Barang</p>
                    </a>
                </li>
            </ul>
        </nav>
        <!-- /.sidebar-menu -->
    </div>
    <!-- /.sidebar -->
</aside>
<div class="container">
    <div class="card">
        <h3 class="card-title text-center"><i class="fas fa-plus-circle"></i> Tambah Barang</h3>
        <form action="tambah_barang.php" method="POST" enctype="multipart/form-data">
            <div class="form-group">
                <label for="rfid_barang">RFID Barang:</label>
                <input type="text" class="form-control" name="rfid_barang" placeholder="Masukkan RFID Barang" required>
            </div>

            <div class="form-group">
                <label for="nama_barang">Nama Barang:</label>
                <input type="text" class="form-control" name="nama_barang" placeholder="Masukkan Nama Barang" required>
            </div>

            <div class="form-group">
                <label for="jumlah_barang">Jumlah Barang:</label>
                <input type="number" class="form-control" name="jumlah_barang" placeholder="Masukkan Jumlah Barang" required>
            </div>

            <div class="form-group">
                <label for="status_barang">Status Barang:</label>
                <select class="form-control" name="status_barang" required>
                    <option value="Masuk">Masuk</option>
                    <option value="Keluar">Keluar</option>
                </select>
            </div>

            <div class="form-group">
                <label for="foto">Foto Barang:</label>
                <input type="file" class="form-control-file" name="foto" required>
            </div>

            <div class="form-group">
                <label for="keterangan">Keterangan:</label>
                <textarea name="keterangan" class="form-control" rows="4" placeholder="Masukkan Keterangan Barang" required></textarea>
            </div>

            <div class="form-group">
                <label for="tempat_barang">Lokasi barang:</label>
                <input type="text" class="form-control" name="tempat_barang" placeholder="lokasi barang di tempatkan" required>
            </div>

            <button type="submit" class="btn btn-custom btn-block"><i class="fas fa-check-circle"></i> Tambah Barang</button>
        </form>
    </div>
</div>

<!-- Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.1/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>x`
</body>
</html>