<?php
require_once('includes/header.php');

function getScalar($conn, $sql, $field, $default = 0.0) {
    $result = mysqli_query($conn, $sql);
    if (!$result) {
        return $default;
    }
    $row = mysqli_fetch_assoc($result);
    if (!$row || !array_key_exists($field, $row)) {
        return $default;
    }
    return (float)$row[$field];
}

$metrics = $_SESSION['admin_dashboard_metrics'] ?? null;

if (!is_array($metrics) || !isset($metrics['totalOrderOnHand'])) {
    $totalContractValue = getScalar(
        $conn,
        "SELECT COALESCE(SUM(contractAmt), 0) AS total_contract_value FROM contracts",
        'total_contract_value'
    );

    $totalBilledValue = getScalar(
        $conn,
        "SELECT COALESCE(SUM(bd.billAmount), 0) AS total_billed_value
         FROM ncbilldetails bd
         INNER JOIN ncmilestones m ON bd.ncmilestoneID = m.ncmilestoneID
         INNER JOIN contracts c ON m.contractID = c.contractID
         WHERE c.contractAmt >= 100",
        'total_billed_value'
    );

    $totalReceivedValue = getScalar(
        $conn,
        "SELECT COALESCE(SUM(bd.receivedAmt), 0) AS total_received_value
         FROM ncbilldetails bd
         JOIN ncmilestones m ON bd.ncmilestoneID = m.ncmilestoneID
         JOIN contracts c ON m.contractID = c.contractID
         WHERE c.contractAmt > 100",
        'total_received_value'
    );

    $metrics = [
        'totalContractValue' => $totalContractValue,
        'totalBilledValue' => $totalBilledValue,
        'totalReceivedValue' => $totalReceivedValue,
        'totalOrderOnHand' => $totalContractValue - $totalBilledValue,
        'receivableValue' => $totalBilledValue - $totalReceivedValue
    ];

    $_SESSION['admin_dashboard_metrics'] = array_merge($_SESSION['admin_dashboard_metrics'] ?? [], $metrics);
}
?>

<div class="row">
    <div class="col-12">
        <div class="card mb-4">
            <div class="card-header pb-0">
                <h6>Order On Hand (Testing)</h6>
            </div>
            <div class="card-body px-0 pt-0 pb-2">
                <div class="p-4">
                    <div class="row">
                        <div class="col-md-6 mb-4">
                            <div class="card card-body p-4">
                                <p class="text-sm mb-0 text-capitalize font-weight-bold">Total Contract Value</p>
                                <h5 class="font-weight-bolder mb-0">
                                    ₹<?php echo number_format(($metrics['totalContractValue'] ?? 0) / 100000, 2); ?> Lakhs
                                </h5>
                            </div>
                        </div>
                        <div class="col-md-6 mb-4">
                            <div class="card card-body p-4">
                                <p class="text-sm mb-0 text-capitalize font-weight-bold">Total Billed Value</p>
                                <h5 class="font-weight-bolder mb-0">
                                    ₹<?php echo number_format(($metrics['totalBilledValue'] ?? 0) / 100000, 2); ?> Lakhs
                                </h5>
                            </div>
                        </div>
                        <div class="col-md-6 mb-4">
                            <div class="card card-body p-4">
                                <p class="text-sm mb-0 text-capitalize font-weight-bold">Total Received Value</p>
                                <h5 class="font-weight-bolder mb-0">
                                    ₹<?php echo number_format(($metrics['totalReceivedValue'] ?? 0) / 100000, 2); ?> Lakhs
                                </h5>
                            </div>
                        </div>
                        <div class="col-md-6 mb-4">
                            <div class="card card-body p-4">
                                <p class="text-sm mb-0 text-capitalize font-weight-bold">Total Order On Hand</p>
                                <h5 class="font-weight-bolder mb-0">
                                    ₹<?php echo number_format(($metrics['totalOrderOnHand'] ?? 0) / 100000, 2); ?> Lakhs
                                </h5>
                            </div>
                        </div>
                        <div class="col-md-6 mb-4">
                            <div class="card card-body p-4">
                                <p class="text-sm mb-0 text-capitalize font-weight-bold">Receivable Value</p>
                                <h5 class="font-weight-bolder mb-0">
                                    ₹<?php echo number_format(($metrics['receivableValue'] ?? 0) / 100000, 2); ?> Lakhs
                                </h5>
                            </div>
                        </div>
                        <div class="col-md-6 mb-4 d-flex align-items-stretch">
                            <div class="card card-body p-4 w-100">
                                <p class="text-sm mb-2 text-capitalize font-weight-bold">Source</p>
                                <div class="text-sm">
                                    <?php echo isset($_SESSION['admin_dashboard_metrics']) ? 'Session / DB fallback' : 'DB fallback'; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php include('includes/footer.php'); ?>
