<?php
require_once __DIR__ . '/../includes/init.php';
require_role('supplier');

require_once __DIR__ . '/../includes/status_updater.php';

$supplier_id = $_SESSION['user_id']; // Assuming the supplier is a user and their ID is in the session

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['order_item_id'], $_POST['item_status'])) {
    $order_item_id = $_POST['order_item_id'];
    $item_status = $_POST['item_status'];

    $stmt_get_order = db()->prepare('SELECT order_id FROM order_items WHERE id = ?');
    $stmt_get_order->execute([$order_item_id]);
    $order_id = $stmt_get_order->fetchColumn();

    if ($order_id) {
        $stmt = db()->prepare(
            'UPDATE order_items oi
            JOIN products p ON oi.product_id = p.id
            SET oi.item_status = ?
            WHERE oi.id = ? AND p.supplier_id = ?'
        );
        $stmt->execute([$item_status, $order_item_id, $supplier_id]);

        update_order_status($order_id);
    }

    header('Location: /supplier/order_items.php');
    exit;
}

$stmt = db()->prepare(
    'SELECT 
        oi.id AS order_item_id, 
        o.id AS order_id, 
        p.name AS product_name, 
        oi.quantity, 
        oi.item_status, 
        oi.updated_at
    FROM order_items oi
    JOIN orders o ON oi.order_id = o.id
    JOIN products p ON oi.product_id = p.id
    WHERE p.supplier_id = ?
    ORDER BY o.created_at DESC'
);
$stmt->execute([$supplier_id]);
$order_items = $stmt->fetchAll();

$item_statuses = ['pending', 'in_progress', 'shipped'];

$pageTitle = t('my_order_items');
require_once __DIR__ . '/../includes/html_head.php';
require_once __DIR__ . '/../includes/header.php';
?>

<main class="container mt-4">
    <div class="card">
        <div class="card-header">
            <h1><?php echo $pageTitle; ?></h1>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            <th><?php echo t('order_id'); ?></th>
                            <th><?php echo t('product'); ?></th>
                            <th><?php echo t('quantity'); ?></th>
                            <th><?php echo t('status'); ?></th>
                            <th><?php echo t('last_updated'); ?></th>
                            <th><?php echo t('action'); ?></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($order_items)): ?>
                            <tr>
                                <td colspan="6" class="text-center"><?php echo t('no_order_items_found'); ?></td>
                            </tr>
                        <?php else: ?>
                            <?php foreach ($order_items as $item): ?>
                                <tr>
                                    <td><a href="/order_details.php?id=<?php echo $item['order_id']; ?>"><?php echo htmlspecialchars($item['order_id']); ?></a></td>
                                    <td><?php echo htmlspecialchars($item['product_name']); ?></td>
                                    <td><?php echo htmlspecialchars($item['quantity']); ?></td>
                                    <td><span class="badge bg-info"><?php echo htmlspecialchars(get_item_status_translation($item['item_status'])); ?></span></td>
                                    <td><?php echo htmlspecialchars($item['updated_at']); ?></td>
                                    <td>
                                        <form method="POST" action="/supplier/order_items.php" class="d-flex">
                                            <input type="hidden" name="order_item_id" value="<?php echo $item['order_item_id']; ?>">
                                            <select name="item_status" class="form-select form-select-sm me-2">
                                                <?php foreach ($item_statuses as $status): ?>
                                                    <option value="<?php echo $status; ?>" <?php echo ($item['item_status'] === $status) ? 'selected' : ''; ?>>
                                                        <?php echo htmlspecialchars(get_item_status_translation($status)); ?>
                                                    </option>
                                                <?php endforeach; ?>
                                            </select>
                                            <button type="submit" class="btn btn-primary btn-sm"><?php echo t('save'); ?></button>
                                        </form>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</main>

<?php require_once __DIR__ . '/../includes/footer.php'; ?>