<?php
$IncomeAccounts = $this->acc->getAccountsByGroup(3);
$ExpenseAccounts = $this->acc->getAccountsByGroup(4);
$IncomesTotal = $this->acc->getAccountBalanceByGroup(3);
$ExpensesTotal = $this->acc->getAccountBalanceByGroup(4);
$PL = $IncomesTotal - $ExpensesTotal;

?>
<div class="content-wrapper">
  
    <div class="content">
        <div class="container-fluid">
            <div class="row">
                <div class="col">
                    <div class="card card-primary card-outline">
                        <div class="card-header">
                            <h5 class="m-0"><?php echo $pageTitle; ?> </h5>
                        </div>
                        <div class="card-body">
                            <table class="table table-bordered table-hover">
                                <thead>
                                    <tr>
                                        <th colspan="4">Profit & Loss Statement</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr class="bg-dark">

                                        <th>Incomes</th>
                                        <th>Amount</th>
                                        <th>Expenses</th>
                                        <th>Amount</th>
                                    </tr>

                                    <tr class="bg-light">
                                        <td colspan="2">
                                            <?php showTable($IncomeAccounts); ?>
                                        </td>
                                        <td colspan="2">
                                            <?php showTable($ExpenseAccounts); ?>
                                        </td>

                                    </tr>



                                </tbody>
                                <tfoot>
                                    <?php if($PL<0): ?>
                                    <tr class="bg-light">

                                        <th>Net Loss:</th>
                                        <th><?php echo myCurrency($PL); ?></th>
                                        <th></th>
                                        <th></th>
                                    </tr>
                                    <?php else: ?>
                                        <tr class="bg-light">
                                        <th></th>
                                        <th></th>
                                        <th>Net Profit:</th>
                                        <th><?php echo myCurrency($PL); ?></th>
                                       
                                    </tr>
                                    <?php endif; ?>
                              
                                    <?php if($PL<0): ?>
                                    <tr class="bg-light">

                                        <th>Total Loss:</th>
                                        <th><?php echo myCurrency($PL + $IncomesTotal); ?></th>
                                        <th>Total: </th>
                                        <th><?php echo myCurrency($IncomesTotal); ?></th>
                                    </tr>
                                    <?php else: ?>
                                        <tr class="bg-light">
                                        <th>Total:</th>
                                        <th><?php echo myCurrency($IncomesTotal); ?></th>
                                        <th>Total:</th>
                                        <th><?php echo myCurrency($PL + $ExpensesTotal); ?></th>
                                       
                                    </tr>
                                    <?php endif; ?>
                                </tfoot>

                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php
    function showTable($Accounts)
    {
        $total = 0;
    ?>
        <table class="table table-bordered table-hover">
            <thead>
                <tr>
                    <th>Account</th>
                    <th>Balance</th>

                </tr>
            </thead>
            <tbody>
                <?php foreach ($Accounts as $Account) : $total += abs($Account->balance) ?>
                    <tr>
                        <td><?php echo $Account->account_name; ?></td>
                        <td><?php echo myCurrency(abs($Account->balance)); ?></td>

                    </tr>
                <?php endforeach; ?>

            </tbody>
            <!-- <tfoot>
                <tr>
                    <th>Total</th>
                    <th><?php echo myCurrency($total); ?></th>

                </tr>
            </tfoot> -->
        </table>

    <?php
    }