<?php  
error_reporting(E_ALL);  
ini_set('display_errors', 1);  
  
require_once('includes/load.php');  
page_require_level(2); // Hanya pengguna dengan level 2 dan seterusnya yang bisa mengakses halaman ini  
  
// Memastikan bahwa user sudah login dan memiliki hak akses  
if (!$session->isUserLoggedIn(true)) {  
    redirect('login.php', false);  
}  
  
// Ambil semua permintaan dengan status Pending  
$sql = "SELECT * FROM purchase_indirect WHERE status = 'Pending' ORDER BY request_date DESC";  
$result = $db->query($sql);  
  
// Proses untuk menyetujui atau menolak permintaan  
if (isset($_POST['approve_request'])) {  
    $id = (int)$_POST['id'];  
    $status = $db->escape($_POST['status']);  
    $approval_date = date('Y-m-d');  
  
    if ($status === 'Approved' || $status === 'Rejected') {  
        $sql_update = "UPDATE purchase_indirect SET status = '{$status}', approval_date = '{$approval_date}' WHERE id = {$id}";  
        if ($db->query($sql_update)) {  
            $session->msg('s', "Purchase request has been {$status} successfully.");  
            redirect('approve_purchase_indirect.php', false);  
        } else {  
            $session->msg('d', 'Failed to update the request status.');  
            redirect('approve_purchase_indirect.php', false);  
        }  
    }  
}  
  
// Proses untuk menyetujui semua permintaan  
if (isset($_POST['approve_all'])) {  
    $approval_date = date('Y-m-d');  
    $sql_update_all = "UPDATE purchase_indirect SET status = 'Approved', approval_date = '{$approval_date}' WHERE status = 'Pending'";  
    if ($db->query($sql_update_all)) {  
        $session->msg('s', "All pending purchase requests have been approved successfully.");  
        redirect('approve_purchase_indirect.php', false);  
    } else {  
        $session->msg('d', 'Failed to approve all requests.');  
        redirect('approve_purchase_indirect.php', false);  
    }  
}  
  
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-12">  
        <div class="card">  
            <div class="card-header bg-primary text-white">  
                <h4 class="card-title">Approve Purchase Indirect Requests</h4>  
            </div>  
            <div class="card-body">  
                <form method="post" action="approve_purchase_indirect.php" class="mb-3">  
                    <button type="submit" name="approve_all" class="btn btn-success">Approve All Pending Requests</button>  
                </form>  
                <table class="table table-bordered">  
                    <thead>  
                        <tr>  
                            <th>No.</th>  
                            <th>Request Number</th>  
                            <th>Request Date</th>  
                            <th>Department</th>  
                            <th>Product Name</th>  
                            <th>Quantity</th>  
                            <th>UOM</th>  
                            <th>Status</th>  
                            <th>Action</th>  
                        </tr>  
                    </thead>  
                    <tbody>  
                        <?php if ($result && $db->num_rows($result) > 0): ?>  
                            <?php $i = 1; while ($row = $db->fetch_assoc($result)): ?>  
                                <tr>  
                                    <td><?php echo $i++; ?></td>  
                                    <td><?php echo $row['request_number']; ?></td>  
                                    <td><?php echo $row['request_date']; ?></td>  
                                    <td><?php echo $row['department']; ?></td>  
                                    <td><?php echo $row['product_name']; ?></td>  
                                    <td><?php echo $row['quantity']; ?></td>  
                                    <td><?php echo $row['uom']; ?></td>  
                                    <td><?php echo $row['status']; ?></td>  
                                    <td>  
                                        <form method="post" action="approve_purchase_indirect.php">  
                                            <input type="hidden" name="id" value="<?php echo $row['id']; ?>">  
                                            <select name="status" class="form-control" required>  
                                                <option value="">Select Status</option>  
                                                <option value="Approved" <?php echo ($row['status'] == 'Approved' ? 'selected' : ''); ?>>Approve</option>  
                                                <option value="Rejected" <?php echo ($row['status'] == 'Rejected' ? 'selected' : ''); ?>>Reject</option>  
                                            </select>  
                                            <button type="submit" name="approve_request" class="btn btn-success mt-2">Update Status</button>  
                                        </form>  
                                    </td>  
                                </tr>  
                            <?php endwhile; ?>  
                        <?php else: ?>  
                            <tr>  
                                <td colspan="9" class="text-center">No pending requests found.</td>  
                            </tr>  
                        <?php endif; ?>  
                    </tbody>  
                </table>  
            </div>  
        </div>  
    </div>  
</div>  
  
<?php include_once('layouts/footer.php'); ?>  
