<?php
session_start();
include 'layouts/main.php';
include 'layouts/dbconn.php';

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

// Fetch Accounts Receivable (Unpaid Invoices)
$ar_sql = "SELECT SUM(balance_due) as total_ar FROM invoices WHERE status != 'PAID'";
$ar_result = $conn->query($ar_sql)->fetch_assoc();
$accounts_receivable = $ar_result['total_ar'] ?? 0;

// Mock other values for Balance Sheet (Cash, Inventory, etc.)
// In a real app, query 'chart_of_accounts' or 'ledger'
$cash_bank = 1500000;
$inventory = 450000;
$fixed_assets = 5000000;

$accounts_payable = 300000;
$loans_payable = 2000000;

$total_assets = $cash_bank + $accounts_receivable + $inventory + $fixed_assets;
$total_liabilities = $accounts_payable + $loans_payable;
$equity = $total_assets - $total_liabilities;
?>

<!DOCTYPE html>
<html>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Balance Sheet')); ?>
    <?php include 'layouts/head-css.php'; ?>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>

        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">

                    <div class="row">
                        <div class="col-12">
                            <div class="page-title-box d-sm-flex align-items-center justify-content-between">
                                <h4 class="mb-sm-0">Balance Sheet</h4>
                                <div class="page-title-right">
                                    <div class="text-muted">As of <?php echo date('d M Y'); ?></div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="row justify-content-center">
                        <div class="col-lg-8">
                            <div class="card">
                                <div class="card-body">
                                    <div class="text-center mb-4">
                                        <h3>Statement of Financial Position</h3>
                                        <p class="text-muted">As of <?php echo date('d F Y'); ?></p>
                                    </div>

                                    <div class="row">
                                        <div class="col-md-6">
                                            <h5 class="text-uppercase text-muted mb-3">Assets</h5>
                                            <table class="table table-sm table-borderless">
                                                <tr>
                                                    <td>Cash & Bank</td>
                                                    <td class="text-end">KES <?php echo number_format($cash_bank, 2); ?>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>Accounts Receivable</td>
                                                    <td class="text-end">KES
                                                        <?php echo number_format($accounts_receivable, 2); ?></td>
                                                </tr>
                                                <tr>
                                                    <td>Inventory (Medical Supplies)</td>
                                                    <td class="text-end">KES <?php echo number_format($inventory, 2); ?>
                                                    </td>
                                                </tr>
                                                <tr>
                                                    <td>Fixed Assets (Equipment)</td>
                                                    <td class="text-end">KES
                                                        <?php echo number_format($fixed_assets, 2); ?></td>
                                                </tr>
                                                <tr class="fw-bold border-top">
                                                    <td>TOTAL ASSETS</td>
                                                    <td class="text-end text-primary">KES
                                                        <?php echo number_format($total_assets, 2); ?></td>
                                                </tr>
                                            </table>
                                        </div>

                                        <div class="col-md-6">
                                            <h5 class="text-uppercase text-muted mb-3">Liabilities & Equity</h5>
                                            <table class="table table-sm table-borderless">
                                                <tr>
                                                    <td>Accounts Payable</td>
                                                    <td class="text-end">KES
                                                        <?php echo number_format($accounts_payable, 2); ?></td>
                                                </tr>
                                                <tr>
                                                    <td>Long Term Loans</td>
                                                    <td class="text-end">KES
                                                        <?php echo number_format($loans_payable, 2); ?></td>
                                                </tr>
                                                <tr class="fw-bold border-top">
                                                    <td>Total Liabilities</td>
                                                    <td class="text-end">KES
                                                        <?php echo number_format($total_liabilities, 2); ?></td>
                                                </tr>

                                                <tr>
                                                    <td>&nbsp;</td>
                                                    <td>&nbsp;</td>
                                                </tr>

                                                <tr>
                                                    <td>Retained Earnings / Equity</td>
                                                    <td class="text-end">KES <?php echo number_format($equity, 2); ?>
                                                    </td>
                                                </tr>
                                                <tr class="fw-bold border-top table-light">
                                                    <td>TOTAL LIABILITIES & EQUITY</td>
                                                    <td class="text-end text-primary">KES
                                                        <?php echo number_format($total_liabilities + $equity, 2); ?>
                                                    </td>
                                                </tr>
                                            </table>
                                        </div>
                                    </div>

                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>

</html>