<?php

session_start();


if (!isset($_SESSION['user'])) {
    header("Location: login.php");
    exit;
}


$donor_name = $_SESSION['user']['name'];


$db_server = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "foodshare";


$conn = mysqli_connect($db_server, $db_user, $db_pass, $db_name);


if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
$query = "SELECT * FROM donations WHERE donor_name = ?";
$stmt = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($stmt, "s", $donor_name);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);

$query = "SELECT * FROM donations WHERE donor_name = ?";
$stmt = mysqli_prepare($conn, $query);
mysqli_stmt_bind_param($stmt, "s", $donor_name);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);


$donations = mysqli_fetch_all($result, MYSQLI_ASSOC);


mysqli_stmt_close($stmt);
mysqli_close($conn);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Donor Home</title>
    <link rel="stylesheet" href="donorhome.css">
</head>
<body>
    
    <div class="container">
        <header>
            <h1>Welcome, <span id="user-name"><?php echo htmlspecialchars($donor_name); ?></span>!</h1>
        </header>

        <div class="profile-section">
            <div class="profile-box">
                <div class="profile">
                    <img id="profile-picture" src="anshuman.jpg" alt="Profile Picture">
                </div>
                <div class="profile-info">
                    <div class="stat">
                        <label>Rating:</label>
                        <span id="rating">Null</span>
                    </div>
                    <div class="stat">
                        <label>Contribution Points:Null</label>
                       
                    </div>
                </div>
            </div>
        </div>

        <div class="actions">
            <button id="donate-food">Donate Food</button>
            <button id="toggle-history-btn" class="toggle-btn">Show Contribution History</button>

        </div>


        <div class="history-section" id="history-section">
            <h2>Contribution History</h2>
            <?php if (empty($donations)): ?>
                <p>Donated 10 kg of Vegetables from Anamnagar on 2025-01-12 22:25:37. Expires on 2025-01-20.</p>
            <?php else: ?>
                <ul id="history-list" class="history-list">
                    <?php foreach ($donations as $donation): ?>
                        <li>
                            Donated <?php echo htmlspecialchars($donation['quantity']); ?> of 
                            <?php echo htmlspecialchars($donation['food_type']); ?> from
                            <?php echo htmlspecialchars($donation['location']); ?> on 
                            <?php echo htmlspecialchars($donation['donation_date']); ?> 
                            (Expires on <?php echo htmlspecialchars($donation['expiration_date']); ?>)
                        </li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
        </div>

    </div>

    <script>
        
        const toggleHistoryBtn = document.getElementById('toggle-history-btn');
        const historySection = document.getElementById('history-section');

        toggleHistoryBtn.addEventListener('click', function() {

            if (historySection.style.display === 'none' || historySection.style.display === '') {
                historySection.style.display = 'block';
                toggleHistoryBtn.textContent = 'Hide Contribution History';
            } else {
                historySection.style.display = 'none';
                toggleHistoryBtn.textContent = 'Show Contribution History';
            }
        });


        document.getElementById("donate-food").addEventListener("click", function() {
            window.location.href = "donor.php"; 
        });
    </script>
</body>
</html>