<?php
require_once 'config.php';

// Handle add to cart via POST
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_to_cart'])) {
    $medicine_id = (int)$_POST['medicine_id'];
    $medicine_name = $_POST['medicine_name'];
    $potency = (int)$_POST['potency'];
    $quantity = (int)$_POST['quantity'];
    $price = getPriceForPotency($potency, $pdo);

    $cart_item = [
        'medicine_id' => $medicine_id,
        'name' => $medicine_name,
        'potency' => $potency,
        'quantity' => $quantity,
        'price' => $price
    ];

    if (!isset($_SESSION['cart'])) {
        $_SESSION['cart'] = [];
    }

    // Check if same medicine & potency exists
    $found = false;
    foreach ($_SESSION['cart'] as &$item) {
        if ($item['medicine_id'] == $medicine_id && $item['potency'] == $potency) {
            $item['quantity'] += $quantity;
            $found = true;
            break;
        }
    }
    if (!$found) {
        $_SESSION['cart'][] = $cart_item;
    }
    header("Location: medicine.php?added=1");
    exit;
}

// Fetch all medicines
$stmt = $pdo->query("SELECT * FROM medicines ORDER BY name ASC");
$medicines = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HomoeoCare - Medicines</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
    <header>
        <h1>🌿 HomoeoCare</h1>
        <nav>
            <a href="medicine.php" class="active">Medicines</a>
            <a href="cart.php">🛒 Cart (<?= isset($_SESSION['cart']) ? count($_SESSION['cart']) : 0 ?>)</a>
            <a href="admin.php">Admin</a>
        </nav>
    </header>

    <?php if (isset($_GET['added'])): ?>
        <div class="alert success">✓ Added to cart!</div>
    <?php endif; ?>

    <div class="medicine-grid">
        <?php foreach ($medicines as $med): ?>
            <div class="medicine-card">
                <h2><?= htmlspecialchars($med['name']) ?></h2>
                <form method="post">
                    <input type="hidden" name="medicine_id" value="<?= $med['id'] ?>">
                    <input type="hidden" name="medicine_name" value="<?= htmlspecialchars($med['name']) ?>">
                    
                    <div class="potency-selector">
                        <label>Potency:</label>
                        <select name="potency" required>
                            <?php for ($p = 1; $p <= 30; $p++): ?>
                                <?php $price = getPriceForPotency($p, $pdo); ?>
                                <option value="<?= $p ?>">M/<?= $p ?> (<?= number_format($price, 2) ?> BDT)</option>
                            <?php endfor; ?>
                        </select>
                    </div>

                    <div class="quantity-field">
                        <label>Qty:</label>
                        <input type="number" name="quantity" value="1" min="1" required>
                    </div>

                    <button type="submit" name="add_to_cart" class="btn-add">+ Add to Cart</button>
                </form>
            </div>
        <?php endforeach; ?>
    </div>
</div>
</body>
</html>