<?php

include '../Members/short.php';

$mid = (int)$_SESSION['Mid'];
$success_message = '';
$error_message = '';
if (isset($_SESSION['success_message'])) {
    $success_message = $_SESSION['success_message'];
    unset($_SESSION['success_message']);
}
if (isset($_SESSION['error_message'])) {
    $error_message = $_SESSION['error_message'];
    unset($_SESSION['error_message']);
}

// Get total saving from saving table
$stmt = $conn->prepare("SELECT IFNULL(SUM(Amount), 0) AS total_saving FROM saving WHERE Mid = ? AND Status = 'Success'");
if ($stmt === false) {
    error_log("Prepare failed for savings: " . $conn->error);
    $_SESSION['error_message'] = "Database error.";
    header("Location: balance.php");
    exit();
}
$stmt->bind_param("i", $mid);
$stmt->execute();
$result = $stmt->get_result();
$total_saving = 0;
if ($row = $result->fetch_assoc()) {
    $total_saving = $row['total_saving'];
}
$stmt->close();

// Get total unpaid loan amount from loan_amount table
$stmt = $conn->prepare("SELECT IFNULL(SUM(LAmount), 0) AS total_unpaid_loan FROM loan_amount WHERE Mid = ? AND LoanStatus = 'Not_Paid'");
if ($stmt === false) {
    error_log("Prepare failed for loans: " . $conn->error);
    $_SESSION['error_message'] = "Database error.";
    header("Location: balance.php");
    exit();
}
$stmt->bind_param("i", $mid);
$stmt->execute();
$result = $stmt->get_result();
$total_unpaid_loan = 0;
if ($row = $result->fetch_assoc()) {
    $total_unpaid_loan = $row['total_unpaid_loan'];
}
$stmt->close();

// Get balance and TransactionUUID from balance table
$stmt = $conn->prepare("SELECT Amount, TransactionUUID FROM balance WHERE Mid = ?");
if ($stmt === false) {
    error_log("Prepare failed for balance check: " . $conn->error);
    $_SESSION['error_message'] = "Database error.";
    header("Location: balance.php");
    exit();
}
$stmt->bind_param("i", $mid);
$stmt->execute();
$result = $stmt->get_result();
$balance_amount = 0;
$transaction_uuid = null;
if ($row = $result->fetch_assoc()) {
    $balance_amount = $row['Amount'];
    $transaction_uuid = $row['TransactionUUID'];
} else {
    $error_message = "No balance record found for this member.";
}
$stmt->close();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Your Balance - Financial Management</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
    <script src="https://cdn.tailwindcss.com"></script>
    <style>
        .active {
            background-color: #ff0000 !important;
        }
        body {
            margin: 0;
            font-family: 'Poppins', Arial, sans-serif;
            background: #f0f2f5;
            min-height: 100vh;
            padding-top: 96px; /* Adjusted for navbar height */
        }
        nav {
            position: fixed;
            top: 0;
            width: 100%;
            z-index: 1000;
        }
        .container {
            max-width: 800px;
            margin: 20px auto;
            padding: 20px;
            background: #ffffff;
            border-radius: 15px;
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
        }
        h1 {
            text-align: center;
            color: #2b2d42;
            margin-bottom: 20px;
            font-weight: 600;
        }
        h1 i {
            margin-right: 10px;
            color: #3a86ff;
        }
        .balance-section {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            gap: 20px;
        }
        .balance-card {
            background: #f8f9fa;
            border: 2px solid #e0e0e0;
            border-radius: 10px;
            padding: 20px;
            width: 200px;
            text-align: center;
            transition: all 0.3s ease;
        }
        .balance-card:hover {
            border-color: #3a86ff;
            transform: translateY(-5px);
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
        }
        .balance-card i {
            font-size: 30px;
            color: #3a86ff;
            margin-bottom: 10px;
        }
        .balance-card .label {
            font-size: 16px;
            color: #2b2d42;
            margin-bottom: 5px;
        }
        .balance-card .amount {
            font-size: 24px;
            font-weight: 600;
            color: #2d7a2d;
        }
        .success-message {
            text-align: center;
            color: #2ecc71;
            font-weight: 500;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .success-message i {
            margin-right: 8px;
            font-size: 18px;
        }
        .error-message {
            text-align: center;
            color: #e74c3c;
            font-weight: 500;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        .error-message i {
            margin-right: 8px;
            font-size: 18px;
        }
        footer {
            text-align: center;
            padding: 20px;
            color: #2b2d42;
            font-size: 14px;
            margin-top: 30px;
            background: #ffffff;
            border-top: 1px solid #e0e0e0;
        }
        footer a {
            color: #3a86ff;
            text-decoration: none;
        }
        footer a:hover {
            text-decoration: underline;
        }
        @media (max-width: 768px) {
            body {
                padding-top: 120px; /* Adjust for stacked navbar */
            }
            .container {
                width: 90%;
                margin: 15px auto;
            }
            .balance-card {
                width: 160px;
                padding: 15px;
            }
            .balance-card .label {
                font-size: 14px;
            }
            .balance-card .amount {
                font-size: 20px;
            }
            .balance-card i {
                font-size: 24px;
            }
            h1 {
                font-size: 24px;
            }
        }
        @media (max-width: 480px) {
            body {
                padding-top: 160px; /* Adjust for stacked navbar */
            }
            .balance-section {
                flex-direction: column;
                align-items: center;
            }
            .balance-card {
                width: 90%;
                max-width: 300px;
            }
            h1 {
                font-size: 20px;
            }
            footer {
                font-size: 12px;
                padding: 15px;
            }
        }
    </style>
</head>
<body>
    <?php include '../includes/navbar.php'; ?>
    <div class="container">
        <h1><i class="fas fa-wallet"></i> Your Current Balance</h1>
        <?php if ($success_message): ?>
            <p class="success-message"><i class="fas fa-check-circle"></i> <?php echo htmlspecialchars($success_message); ?></p>
        <?php endif; ?>
        <?php if ($error_message): ?>
            <p class="error-message"><i class="fas fa-exclamation-circle"></i> <?php echo htmlspecialchars($error_message); ?></p>
        <?php endif; ?>
        <div class="balance-section">
            <div class="balance-card">
                <i class="fas fa-wallet"></i>
                <div class="label">Total Balance</div>
                <div class="amount">Rs. <?php echo number_format($balance_amount, 2); ?></div>

            </div>
            <div class="balance-card">
                <i class="fas fa-hand-holding-usd"></i>
                <div class="label">Unpaid Loans</div>
                <div class="amount">Rs. <?php echo number_format($total_unpaid_loan, 2); ?></div>
            </div>
        </div>
    </div>
</body>
</html>