<?php
$this->load->library("NewAcc");
$Acc = new NewAcc();

$CI = &get_instance();
$txt = "SELECT
  g.acgroup_id AS group_id,
  g.acgroup_name AS group_name,
  cp.accategory_id AS parent_id,
  cp.acgroup_id AS group_id,
  cp.accategory_name AS parent_category_name,
  cc.accategory_id AS child_id,
  cc.accategory_name AS child_category_name,
  ccc.accategory_id AS child_child_id,
  ccc.accategory_name AS child_child_category_name,
  a.account_id,
  a.account_name
FROM tbl_acgroups g
LEFT JOIN tbl_accategories cp ON g.acgroup_id = cp.acgroup_id AND cp.parent_category_id = 0
LEFT JOIN tbl_accategories cc ON cp.accategory_id = cc.parent_category_id
LEFT JOIN tbl_accategories ccc ON cc.accategory_id = ccc.parent_category_id
LEFT JOIN tbl_accounts a ON ccc.accategory_id = a.accategory_id
ORDER BY g.display_order, cp.accategory_id, cc.accategory_id, ccc.accategory_id;
";
$AccountTree = $CI->db->query($txt)->result();

function displayAccountTree($accountTree, $parentCategory = 0)
{
    echo '<ul>';

    foreach ($accountTree as $row) {
        if ($row->parent_id == $parentCategory) {
            echo '<li>';
            echo 'Category Name: ' . $row->parent_category_name;
            echo ' | Account Name: ' . $row->account_name;
            displayAccountTree($accountTree, $row->child_id);
            echo '</li>';
        }
    }

    echo '</ul>';
}


$IncomeCategories = $Acc->getAccountCategories(3);

$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-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1 class="m-0"><?php echo $pageTitle; ?></h1>
                </div>
                <div class="col-sm-6">
                    <ol class="breadcrumb float-sm-right">
                        <li class="breadcrumb-item"><a href="<?php echo base_url(); ?>">Dashboard </a></li>
                        <li class="breadcrumb-item active"><?php echo $pageTitle; ?></li>
                    </ol>
                </div>
            </div>
        </div>
    </div>


    <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">
                            <?php displayAccountTree($AccountTree); ?>
                            <?php pre($AccountTree); ?>
                        </div>
                        <div class="card-body">
                            <div class="col-6">
                                <div id="accordion">
                                    <?php displayCategories($IncomeCategories); ?>
                                </div>

                                <?php
                                function displayCategories($categories, $level = 0)
                                {
                                    $CI = &get_instance();
                                    $CI->load->library("NewAcc");
                                    $Acc = new NewAcc();
                                    echo '<div id="accordion_' . $level . '">';
                                    foreach ($categories as $category) {
                                ?>
                                        <div class="card">
                                            <div class="card-header" id="heading<?php echo $category->accategory_id; ?>_<?php echo $level; ?>">
                                                <h5 class="mb-0">
                                                    <?php
                                                    // Add column offset for child categories
                                                    if ($level > 0) {
                                                        echo '<div class="offset-1">';
                                                    }
                                                    ?>

                                                    <div class="row" data-toggle="collapse" data-target="#collapse<?php echo $category->accategory_id; ?>_<?php echo $level; ?>" aria-expanded="true" aria-controls="collapse<?php echo $category->accategory_id; ?>_<?php echo $level; ?>">
                                                        <div class="col-6"><?php echo $category->accategory_name; ?></div>
                                                        <div class="col-3"><?php echo myCurrency($category->dr); ?></div>
                                                        <div class="col-3"><?php echo myCurrency($category->cr); ?></div>

                                                    </div>
                                                    <?php
                                                    if ($level > 0) {
                                                        echo '</div>';
                                                    }
                                                    ?>
                                                </h5>
                                            </div>
                                            <div id="collapse<?php echo $category->accategory_id; ?>_<?php echo $level; ?>" class="collapse show" aria-labelledby="heading<?php echo $category->accategory_id; ?>_<?php echo $level; ?>" data-parent="#accordion_<?php echo $level; ?>">
                                                <div class="card-body">

                                                    <?php
                                                    $category->children = $Acc->getChildCategories($category->accategory_id);
                                                    if (!empty($category->children)) {
                                                        // Increase the level for child categories
                                                        $childLevel = $level + 1;
                                                        displayCategories($category->children, $childLevel);
                                                    }
                                                    ?>
                                                </div>
                                            </div>

                                        </div>
                                <?php
                                    }
                                    echo '</div>';
                                }



                                ?>
                            </div>

                            <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
    }