<?php
$page_title = 'Supplier Relationship Management';
require_once('includes/load.php');
page_require_level(4);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $vendor_name = $db->escape($_POST['vendor_name']);
    $description = $db->escape($_POST['description']);
    $quantity_receivable = (int) $_POST['quantity_receivable'];
    $quantity_unreceivable = (int) $_POST['quantity_unreceivable'];

    // Lakukan validasi data di sini jika diperlukan

    // Lakukan query SQL INSERT untuk menyimpan data ke database
    $sql = "INSERT INTO srm_data (vendor_name, description, quantity_receivable, quantity_unreceivable) 
            VALUES ('$vendor_name', '$description', $quantity_receivable, $quantity_unreceivable)";
    
    if ($db->query($sql)) {
        $session->msg("s", "Data SRM berhasil disimpan.");
        redirect('srm.php');
    } else {
        $session->msg("d", "Gagal menyimpan data SRM.");
        redirect('srm.php');
    }
}

// Query SQL untuk mengambil data SRM dari database
$srm_data = find_all('srm_data');

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

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">Supplier Relationship Management</h3>
            </div>
            <div class="panel-body">
                <form action="srm.php" method="post">
                    <div class="form-group">
                        <label for="vendor_name">Nama Vendor:</label>
                        <select class="form-control" name="vendor_name" id="vendor_name" required>
                            <option value="Almuh">Almuh</option>
                            <!-- Anda juga bisa menambahkan opsi lain di sini -->
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="description">Deskripsi Material:</label>
                        <textarea class="form-control" name="description" id="description" rows="3" required></textarea>
                    </div>
                    <div class="form-group">
                        <label for="quantity_receivable">Quantity Receivable:</label>
                        <input type="number" class="form-control" name="quantity_receivable" id="quantity_receivable" required>
                    </div>
                    <div class="form-group">
                        <label for="quantity_unreceivable">Quantity Unreceivable:</label>
                        <input type="number" class="form-control" name="quantity_unreceivable" id="quantity_unreceivable" required>
                    </div>
                    <button type="submit" class="btn btn-primary">Simpan Data SRM</button>
                </form>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">Tabel Supplier Relationship Management</h3>
            </div>
            <div class="panel-body">
                <table class="table table-bordered table-hover">
                    <thead>
                        <tr>
                            <th>Nama Vendor</th>
                            <th>Deskripsi Material</th>
                            <th>Quantity Receivable</th>
                            <th>Quantity Unreceivable</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($srm_data as $srm) : ?>
                            <tr>
                                <td><?php echo htmlspecialchars($srm['vendor_name']); ?></td>
                                <td><?php echo htmlspecialchars($srm['description']); ?></td>
                                <td><?php echo htmlspecialchars($srm['quantity_receivable']); ?></td>
                                <td><?php echo htmlspecialchars($srm['quantity_unreceivable']); ?></td>
                                <td>
                                    <a href="edit_srm.php?id=<?php echo $srm['id']; ?>" class="btn btn-primary btn-xs">Edit</a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>


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