<style type="text/css">
::-webkit-scrollbar {
    display: none;
}
#dynamic-content-container{background: #fff;}
    .navbar-default.navbar-static-side{display: none !important;}
    #page-wrapper{margin: 0px !important; padding: 0 !important;}
    .row.border-bottom{display: none !important;}
    .footer.fixed{display: none;}
    .ibox-content{padding: 0 !important;}
    body {
    -webkit-print-color-adjust:exact;
    background: #fff;
    }

</style>

<table style="width: 100%; background: #fff;">
    <tr>
        <td><img style="width: 100%; height: 200px;" src="<?= get_branch_image('report'); ?>"></td>
    </tr>
</table>


<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">

                    <h2 class="text-center font-bold" style="margin: 10px 0 20px;"><?= $breadcrums_title; ?> <?= print_lang($app_lang,'listing'); ?></h2>

                    <div class="table-responsive">
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <th class="text-center"><?= print_lang($app_lang,'sr_#'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'client_balance_summary_client_name'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'client_balance_summary_client_mobile'); ?></th>
                                    <?php
                                    $for_totals = array();
                                    foreach($currencies as $row){
                                        $for_totals[$row['uid']] = array('debit' => array(), 'credit' => array());
                                    ?>
                                        <th class="text-center"><?= print_lang($app_lang,'balance'); ?> (<?= $row['name']; ?>)</th>
                                    <?php } ?>
                                </tr>
                            </thead>
                            <tbody>
                                
                                <?php $balance_color = '#ec4758'; if(count($accounts) > 0){ ?>
                                    <?php $i = 1; foreach($accounts as $row){ ?>
                                        <tr class="text-center">
                                            <td><?= $i++; ?></td>
                                            <td><?= $row['name']; ?></td>
                                            <td><?= $row['mobile']; ?></td>
                                            
                                            <?php
                                            foreach($currencies as $cur_row){
                                                $currency_account_ledgers = 100;
                                                if($row['shared'] == 'yes')
                                                {
                                                    $balance = fetch_shared_account_balance($row['uid'],$cur_row['uid'],'client',$search_from,$search_to);
                                                }
                                                else
                                                {
                                                    $balance = fetch_account_balance($row['uid'],$cur_row['uid'],'client',$search_from,$search_to);
                                                }
                                                if($balance >= 0)
                                                {
                                                    if(array_key_exists($cur_row['uid'],$for_totals)){ $for_totals[$cur_row['uid']]['debit'][] = abs($balance); }
                                                    $balance = amount_number_format(abs($balance)).' DR';
                                                    $balance_color = '#2B9C1B';
                                                }
                                                else
                                                {
                                                    if(array_key_exists($cur_row['uid'],$for_totals)){ $for_totals[$cur_row['uid']]['credit'][] = abs($balance); }
                                                    $balance = amount_number_format(abs($balance)).' CR';
                                                    $balance_color = '#ec4758';
                                                }
                                            ?>
                                                <td style="color: <?= $balance_color; ?>;"><?= $balance; ?></td>
                                            <?php } ?>

                                        </tr>
                                    <?php  } ?>

                                    <tr class="text-center">
                                        <td></td>
                                        <td><strong><?= print_lang($app_lang,'total'); ?></strong></td>
                                        <td></td>
                                        <?php
                                        foreach($for_totals as $row){
                                            $total_debit = array_sum($row['debit']);
                                            $total_credit = array_sum($row['credit']);
                                            if($total_debit >= $total_credit)
                                            {
                                                $total = amount_number_format( $total_debit - $total_credit ).' DR';
                                                $balance_color = '#2B9C1B';
                                            }
                                            else
                                            {
                                                $total = amount_number_format( $total_credit - $total_debit ).' CR';
                                                $balance_color = '#ec4758';
                                            }
                                        ?>
                                            <td style="color: <?= $balance_color; ?>;"><strong><?= $total; ?></strong></td>
                                        <?php } ?>
                                    </tr>

                                <?php } ?>

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

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