<?php
if(!isset($_SESSION) && empty($_SESSION['user_id'])){
    echo "<script>window.location.href = '/Agent/agent-login'</script>";
    exit;
}
function getTotalAmount(array $rows): float {
    // array_column diye sudhu AT_AMOUNT gulo niye ashbo
    $amounts = array_column($rows, 'AT_AMOUNT');

    // jodi kono data na thake tahole 0 return hobe
    return array_sum($amounts);
}

$conn = new mysqli($GLOBALS['host'], $GLOBALS['user'], $GLOBALS['pass'], $GLOBALS['db']);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$today = date("Y-m-d");

// query
$sql = "SELECT * FROM {$GLOBALS['arif_tran']} WHERE AT_ADMIN = '{$_SESSION['user_id']}' AND DATE(AT_TIMESTAMP) = '$today'";

$result = $conn->query($sql);

// direct fetch_all
$rows = $result ? $result->fetch_all(MYSQLI_ASSOC) : [];
$totalAmount = getTotalAmount($rows);
// var_dump($rows); // ekhane pura array peye jabe

$conn->close();
// var_dump($_SESSION);
?>


<div class="container">
    <h3 style="font-size: 20px; font-weight: 600; margin-bottom: 15px;">
        Welcome, <?= $_SESSION['name'] ?> 👋
    </h3>

    <div class="dashboard-total-section">
        <!-- Total Collection Card -->
        <div class="card-box highlight">
            <h3><?= $totalAmount ?></h3>
            <p>Total Collection</p>
        </div>

        <!-- Loan & Recurring Card -->
        <div class="card-box normal">
            <!-- <p><strong>Loan:</strong> <?= $totalLoan ?? 0 ?></p>
            <p><strong>Recurring:</strong> <?= $totalRecurring ?? 0 ?></p> -->
        </div>
    </div>
    <a class="btn btn-primary w-100" href="/Agent/Receive" style="width: 100%; margin-top: 20px;">
        <i class="fa-solid fa-credit-card"></i>
        Recive New Payment
    </a>
</div>

<style>
    .dashboard-total-section {
        display: flex;
        gap: 20px;
        margin-top: 20px;
        flex-wrap: wrap;
    }

    .card-box {
        flex: 1;
        min-width: 180px;
        background: #fff;
        border-radius: 16px;
        padding: 20px;
        text-align: center;
        box-shadow: 0 4px 10px rgba(0,0,0,0.08);
        transition: transform 0.2s ease, box-shadow 0.2s ease;
    }

    .card-box:hover {
        transform: translateY(-5px);
        box-shadow: 0 6px 16px rgba(0,0,0,0.12);
    }

    .card-box h3 {
        margin: 0;
        font-size: 28px;
        font-weight: bold;
        color: #222;
    }

    .card-box p {
        margin: 5px 0 0;
        font-size: 14px;
        color: #555;
    }

    .card-box.highlight {
        background: #e95420;
        color: #fff;
    }

    .card-box.highlight h3,
    .card-box.highlight p {
        color: #fff;
    }
</style>
