<?php
$page_title = 'Tambah Aset';
require_once('includes/load.php');
page_require_level(4);

$db = MySqli_DB::getInstance();

// Ambil data untuk dropdown dari database
$employees = find_by_sql("SELECT id, CONCAT(first_name, ' ', last_name) AS full_name FROM employees ORDER BY first_name ASC");
$categories = find_by_sql("SELECT id, category_name FROM assets_categories ORDER BY category_name ASC");

// Proses form saat data dikirimkan
if (isset($_POST['add_asset'])) {
    $req_fields = array('asset-name', 'category-id', 'condition', 'location');
    validate_fields($req_fields);

    if (empty($errors)) {
        $asset_name = remove_junk($db->escape($_POST['asset-name']));
        $category_id = (int)$db->escape($_POST['category-id']);
        $serial_number = remove_junk($db->escape($_POST['serial-number']));
        $purchase_date = remove_junk($db->escape($_POST['purchase-date']));
        $condition = remove_junk($db->escape($_POST['condition']));
        $location = remove_junk($db->escape($_POST['location']));
        $employee_id = (int)$db->escape($_POST['employee-id']);
        $notes = remove_junk($db->escape($_POST['notes']));

        // Perbaikan pada bind_param
        // Urutan parameter harus sesuai dengan urutan kolom di SQL
        // s = string, i = integer
        $sql  = "INSERT INTO assets (asset_name, category_id, serial_number, purchase_date, `condition`, location, employee_id, notes) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
        $stmt = $db->prepare($sql);
        $stmt->bind_param("sissssis", $asset_name, $category_id, $serial_number, $purchase_date, $condition, $location, $employee_id, $notes);

        if ($stmt->execute()) {
            $session->msg("s", "Aset berhasil ditambahkan.");
            redirect('asset_management.php', false);
        } else {
            $session->msg("d", "Gagal menambahkan aset: " . $stmt->error);
            redirect('add_asset.php', false);
        }
    } else {
        $session->msg("d", $errors);
        redirect('add_asset.php', false);
    }
}
?>

<?php include_once('layouts/header.php'); ?>

<div class="row">
    <div class="col-md-12">
        <?php echo display_msg($msg); ?>
    </div>
</div>

<div class="row">
    <div class="col-md-8">
        <div class="panel panel-default">
            <div class="panel-heading">
                <strong>
                    <span class="glyphicon glyphicon-plus"></span>
                    <span>Tambah Aset Baru</span>
                </strong>
            </div>
            <div class="panel-body">
                <form method="post" action="add_asset.php">
                    <div class="form-group">
                        <label for="asset-name">Nama Aset</label>
                        <input type="text" class="form-control" name="asset-name" placeholder="Nama Aset" required>
                    </div>
                    <div class="form-group">
                        <label for="category-id">Kategori</label>
                        <select class="form-control" name="category-id" required>
                            <option value="">Pilih Kategori</option>
                            <?php foreach ($categories as $cat): ?>
                                <option value="<?php echo (int)$cat['id']; ?>">
                                    <?php echo remove_junk($cat['category_name']); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="serial-number">Nomor Seri</label>
                        <input type="text" class="form-control" name="serial-number" placeholder="Nomor Seri">
                    </div>
                    <div class="form-group">
                        <label for="purchase-date">Tanggal Pembelian</label>
                        <input type="date" class="form-control" name="purchase-date">
                    </div>
                    <div class="form-group">
                        <label for="condition">Kondisi</label>
                        <select class="form-control" name="condition" required>
                            <option value="">Pilih Kondisi</option>
                            <option value="Baik">Baik</option>
                            <option value="Rusak">Rusak</option>
                            <option value="Perbaikan">Perbaikan</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="location">Lokasi</label>
                        <input type="text" class="form-control" name="location" placeholder="Lokasi Aset" required>
                    </div>
                    <div class="form-group">
                        <label for="employee-id">Penanggung Jawab</label>
                        <select class="form-control" name="employee-id">
                            <option value="">Pilih Karyawan (Opsional)</option>
                            <?php foreach ($employees as $emp): ?>
                                <option value="<?php echo (int)$emp['id']; ?>">
                                    <?php echo remove_junk($emp['full_name']); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="notes">Catatan</label>
                        <textarea class="form-control" name="notes" rows="3" placeholder="Catatan tambahan"></textarea>
                    </div>
                    <button type="submit" name="add_asset" class="btn btn-primary">Simpan Aset</button>
                    <a href="asset_management.php" class="btn btn-default">Batal</a>
                </form>
            </div>
        </div>
    </div>
</div>

<?php include_once('layouts/footer.php'); ?>