<div class="content-wrapper">
    
    <div class="container-fluid">
        <div class="card">
            <div class="card-header">
                <?php echo $pageTitle; ?>
            </div>
            <div class="card-body">
                <div class="row pb-4  justify-content-end">
                    <div class="col-8">
                        <div class="row">
                            <div class="col bg-primary text-white text-center">Table Heading</div>
                            <div class="col bg-success text-white text-center">Account Types</div>
                            <div class="col bg-warning text-dark text-center">Account Categories</div>
                            <div class="col bg-dark text-white text-center">Account Groups</div>
                            <div class="col bg-light text-center">List of Ledgers</div>
                        </div>
                    </div>
                </div>
                <style>
                    .expandable {
                        text-decoration: underline;
                    }
                </style>
                <?php
                $drWidth = 150;
                $crWidth = 150;
                $AccountGroups = $this->myaccounts->getAccountGroups();
                $showZero = true;
                ?>
                <table class="table table-bordered">
                    <thead>
                        <tr class="bg-primary">
                            <th>Particulars</th>
                            <th class="text-center" width="<?php echo $drWidth; ?>">Dr</th>
                            <th class="text-center" width="<?php echo $crWidth; ?>">Cr</th>
                        </tr>
                    </thead>
                </table>
                <div class="scrollable-content overflow-auto p-0" data-simplebar style="max-height: 800px; overflow-y: auto;">
                    <!-- Content goes here -->

                    <table class="table table-bordered">

                        <tbody>
                            <?php foreach ($AccountGroups as $AccountGroup) :
                                if (!$showZero && $AccountGroup->isNonZero()) {
                                    continue;
                                }
                                
                                $AccountTypes = $this->myaccounts->getParentAccountCategoriesByGroup($AccountGroup->acgroup_id, false);
                            ?>
                                <tr <?php if ($AccountTypes) : ?>onclick="toggleSubTable(this)" <?php endif; ?> >
                                    <th><?php echo $AccountGroup->acgroup_name; ?></th>
                                    <th width="<?php echo $drWidth; ?>"><?php echo myCurrency(abs($AccountGroup->dr)); ?></th>
                                    <th width="<?php echo $crWidth; ?>"><?php echo myCurrency(abs($AccountGroup->cr)); ?></th>
                                </tr>
                                <?php if ($AccountTypes) : ?>
                                    <tr class="sub-table ">
                                        <td colspan="3" class="p-0">
                                            <table class="table table-bordered">

                                                <tbody>
                                                    <?php foreach ($AccountTypes as $AccountType) :
                                                    //pre($AccountType);
                                                    if (!$showZero) {
                                                        
                                                        if ($AccountType->dr == 0 && $AccountType->cr == 0) continue;
                                                    }
                                                        $AccountGroups = $this->myaccounts->getChildAccountCategoriesByGroupAndParentCategory($AccountType->acgroup_id, $AccountType->accategory_id, true);
                                                    ?>
                                                        <tr <?php if ($AccountGroups) : ?>onclick="toggleSubTable(this)" <?php endif; ?> >
                                                            <td><span class="<?php if ($AccountGroups) : ?> expandable <?php endif; ?>"><?php echo $AccountType->accategory_name; ?></span></td>
                                                            <td width="<?php echo $drWidth; ?>"><?php echo myCurrency($AccountType->dr); ?></td>
                                                            <td width="<?php echo $drWidth; ?>"><?php echo myCurrency($AccountType->cr); ?></td>
                                                        </tr>
                                                        <?php if ($AccountGroups) : ?>
                                                            <tr class="sub-table">
                                                                <td colspan="3" class="p-0">
                                                                    <table class="table table-bordered">

                                                                        <tbody>
                                                                            <?php
                                                                            foreach ($AccountGroups as $AccountGroup) :
                                                                                $Accounts = $this->myaccounts->getAccountsByCategory($AccountGroup->accategory_id, true);
                                                                            ?>
                                                                                <tr <?php if ($Accounts) : ?> onclick="toggleSubTable(this)" <?php endif; ?> >
                                                                                    <td>>> >> <?php echo $AccountGroup->accategory_name; ?></td>
                                                                                    <td width="<?php echo $drWidth; ?>"><?php echo myCurrency($AccountGroup->dr); ?></td>
                                                                                    <td width="<?php echo $drWidth; ?>"><?php echo myCurrency($AccountGroup->cr); ?></td>
                                                                                </tr>
                                                                                <?php if ($Accounts) : ?>

                                                                                    <tr class="sub-table">
                                                                                        <td colspan="3" class="p-0">
                                                                                            <table class="table table-bordered bg-light">
                                                                                                <tbody>
                                                                                                    <?php

                                                                                                    foreach ($Accounts as $Account) : ?>
                                                                                                        <tr>
                                                                                                            <td><?php echo linkLedger($Account); ?> A/C</td>
                                                                                                            <td width="<?php echo $drWidth; ?>"><?php echo myCurrency($Account->dr); ?></td>
                                                                                                            <td width="<?php echo $drWidth; ?>"><?php echo myCurrency($Account->cr); ?></td>
                                                                                                        </tr>
                                                                                                    <?php endforeach; ?>

                                                                                                </tbody>
                                                                                            </table>
                                                                                        </td>
                                                                                    </tr>

                                                                            <?php endif;
                                                                            endforeach; ?>

                                                                        </tbody>
                                                                    </table>
                                                                </td>
                                                            </tr>
                                                        <?php endif; ?>
                                                    <?php endforeach; ?>

                                                </tbody>
                                            </table>
                                        </td>
                                    </tr>
                                <?php endif; ?>
                            <?php endforeach; ?>

                        </tbody>
                    </table>
                </div>

                <script>
                    function toggleSubTable(row) {
                        var subTable = row.nextElementSibling;
                        subTable.style.display = subTable.style.display === 'none' ? 'table-row' : 'none';
                    }
                </script>

            </div>
        </div>

    </div>
</div>