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

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

// Mock Cash Flow Data (since tables might be missing)
$operating_inflow = 1200000; // From Payments
$operating_outflow = 800000; // Salaries, Utilities
$investing_outflow = 150000; // Equipment purchase
$financing_inflow = 50000; // Loans

$net_cash_flow = ($operating_inflow - $operating_outflow) - $investing_outflow + $financing_inflow;
$beginning_cash = 500000;
$ending_cash = $beginning_cash + $net_cash_flow;
?>

<!DOCTYPE html>
<html>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Cash Flow Statement')); ?>
    <?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">Cash Flow Statement</h4>
                                <div class="page-title-right">
                                    <div class="text-muted">For period ending <?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">
                                    <h4 class="card-title mb-4">Statement of Cash Flows</h4>

                                    <table class="table table-borderless">
                                        <tr class="table-light">
                                            <td colspan="2"><strong>Cash Flow from Operating Activities</strong></td>
                                        </tr>
                                        <tr>
                                            <td>Cash receipts from patients/insurance</td>
                                            <td class="text-end">KES <?php echo number_format($operating_inflow, 2); ?>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>Cash paid to suppliers and employees</td>
                                            <td class="text-end">(KES
                                                <?php echo number_format($operating_outflow, 2); ?>)</td>
                                        </tr>
                                        <tr class="fw-bold border-top">
                                            <td>Net Cash from Operating</td>
                                            <td class="text-end">KES
                                                <?php echo number_format($operating_inflow - $operating_outflow, 2); ?>
                                            </td>
                                        </tr>

                                        <tr>
                                            <td colspan="2">&nbsp;</td>
                                        </tr>

                                        <tr class="table-light">
                                            <td colspan="2"><strong>Cash Flow from Investing Activities</strong></td>
                                        </tr>
                                        <tr>
                                            <td>Purchase of Equipment</td>
                                            <td class="text-end">(KES
                                                <?php echo number_format($investing_outflow, 2); ?>)</td>
                                        </tr>
                                        <tr class="fw-bold border-top">
                                            <td>Net Cash from Investing</td>
                                            <td class="text-end">(KES
                                                <?php echo number_format($investing_outflow, 2); ?>)</td>
                                        </tr>

                                        <tr>
                                            <td colspan="2">&nbsp;</td>
                                        </tr>

                                        <tr class="table-light">
                                            <td colspan="2"><strong>Cash Flow from Financing Activities</strong></td>
                                        </tr>
                                        <tr>
                                            <td>Proceeds from loans</td>
                                            <td class="text-end">KES <?php echo number_format($financing_inflow, 2); ?>
                                            </td>
                                        </tr>
                                        <tr class="fw-bold border-top">
                                            <td>Net Cash from Financing</td>
                                            <td class="text-end">KES <?php echo number_format($financing_inflow, 2); ?>
                                            </td>
                                        </tr>

                                        <tr>
                                            <td colspan="2">&nbsp;</td>
                                        </tr>

                                        <tr class="table-success fw-bold fs-16">
                                            <td>NET INCREASE IN CASH</td>
                                            <td class="text-end">KES <?php echo number_format($net_cash_flow, 2); ?>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>Cash at Beginning of Period</td>
                                            <td class="text-end">KES <?php echo number_format($beginning_cash, 2); ?>
                                            </td>
                                        </tr>
                                        <tr class="fw-bold fs-16">
                                            <td>CASH AT END OF PERIOD</td>
                                            <td class="text-end text-primary">KES
                                                <?php echo number_format($ending_cash, 2); ?></td>
                                        </tr>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>

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

</html>