<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();
                                    $for_totals_previous = array();
                                    foreach($currencies as $row){
                                        $for_totals[$row['uid']] = array('debit' => array(), 'credit' => array());
                                        $for_totals_previous[$row['uid']] = array('debit' => array(), 'credit' => array());
                                    ?>
                                        <th class="text-center"><?= print_lang($app_lang,'client_balance_summary_client_previous_balance'); ?> (<?= $row['name']; ?>)</th>
                                        <th class="text-center"><?= print_lang($app_lang,'balance'); ?> (<?= $row['name']; ?>)</th>
                                    <?php } ?>
                                </tr>
                            </thead>
                            <tbody>
                                
                                <?php 
                                $previous_balance = '#ec4758'; 
                                $previous_balance_color = '#ec4758';
                                if(count($accounts) > 0){ ?>
                                    <?php $i = 1; foreach($accounts as $row){
                                        $pre_customer_row_total = []; $customer_row_total = [];
                                        foreach($currencies as $curr_row)
                                        {
                                            $row_total = 0;
                                            $_balance = isset($_data_currency_array[$curr_row['uid']][$row['uid']]) ? $_data_currency_array[$curr_row['uid']][$row['uid']]['balance'] : 0;
                                            $_previous_balance = isset($_data_previous_currency_array[$curr_row['uid']][$row['uid']]) ? $_data_previous_currency_array[$curr_row['uid']][$row['uid']]['balance'] : 0;

                                            if($search_amount_type == 'negative_amount')
                                            {
                                                if($_balance < 0){ $customer_row_total[] = abs($_balance); }
                                                if($_previous_balance < 0){ $pre_customer_row_total[] = abs($_previous_balance); }
                                            }
                                            else if($search_amount_type == 'positive_amount')
                                            {
                                                if($_balance >= 0){ $customer_row_total[] = abs($_balance);}
                                                if($_previous_balance >= 0){ $pre_customer_row_total[] = abs($_previous_balance);}
                                            }
                                            else
                                            {
                                                $customer_row_total[] = abs($_balance);
                                                $pre_customer_row_total[] = abs($_previous_balance);
                                            }
                                        }

                                        $row_total = array_sum($pre_customer_row_total) + array_sum($customer_row_total);
                                        if($row_total == 0){continue;}
                                    ?>
                                        <tr class="text-center">
                                            <td><?= $i++; ?></td>
                                            <td><?= $row['name']; ?></td>
                                            <td><?= $row['mobile']; ?></td>
                                            
                                            <?php
                                            foreach($currencies as $cur_row){
                                                $balance = isset($_data_currency_array[$cur_row['uid']][$row['uid']]) ? $_data_currency_array[$cur_row['uid']][$row['uid']]['balance'] : 0;
                                                $previous_balance = isset($_data_previous_currency_array[$cur_row['uid']][$row['uid']]) ? $_data_previous_currency_array[$cur_row['uid']][$row['uid']]['balance'] : 0;

                                                if($balance >= 0)
                                                {
                                                    if($search_amount_type == 'negative_amount'){$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';
                                                    if($search_amount_type == 'negative_amount' OR $balance == 0){$balance = null;}
                                                    $balance_color = '#2B9C1B';
                                                }
                                                else
                                                {
                                                    if($search_amount_type == 'positive_amount'){$balance = 0;}
                                                    if(array_key_exists($cur_row['uid'],$for_totals)){ $for_totals[$cur_row['uid']]['credit'][] = abs($balance); }
                                                    $balance = amount_number_format(abs($balance)).' CR';
                                                    if($search_amount_type == 'positive_amount' OR $balance == 0){$balance = null;}
                                                    $balance_color = '#ec4758';
                                                }
                                                if($previous_balance >= 0)
                                                {
                                                    if($search_amount_type == 'negative_amount'){$previous_balance = 0;}
                                                    if(array_key_exists($cur_row['uid'],$for_totals_previous)){ $for_totals_previous[$cur_row['uid']]['debit'][] = abs($previous_balance); }
                                                    $previous_balance = amount_number_format(abs($previous_balance)).' DR';
                                                    if($search_amount_type == 'negative_amount' OR $previous_balance == 0){$previous_balance = null;}
                                                    $previous_balance_color = '#2B9C1B';
                                                }
                                                else
                                                {
                                                    if($search_amount_type == 'positive_amount'){$previous_balance = 0;}
                                                    if(array_key_exists($cur_row['uid'],$for_totals_previous)){ $for_totals_previous[$cur_row['uid']]['credit'][] = abs($previous_balance); }
                                                    $previous_balance = amount_number_format(abs($previous_balance)).' CR';
                                                    if($search_amount_type == 'positive_amount' OR $previous_balance == 0){$previous_balance = null;}
                                                    $previous_balance_color = '#ec4758';
                                                }

                                            ?>
                                                <td style="color: <?= $previous_balance_color; ?>;"><?= $previous_balance; ?></td>
                                                <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 $key => $row){
                                            $total_debit = array_sum($row['debit']);
                                            $total_credit = array_sum($row['credit']);

                                            $total_previous_debit = array_sum($for_totals_previous[$key]['debit']);
                                            $total_previous_credit = array_sum($for_totals_previous[$key]['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';
                                            }

                                            if($total_previous_debit >= $total_previous_credit)
                                            {
                                                $total_previous = amount_number_format( $total_previous_debit - $total_previous_credit ).' DR';
                                                $balance_color_previous = '#2B9C1B';
                                            }
                                            else
                                            {
                                                $total_previous = amount_number_format( $total_previous_credit - $total_previous_debit ).' CR';
                                                $balance_color_previous = '#ec4758';
                                            }
                                        ?>
                                            <td style="color: <?= $balance_color_previous; ?>;"><strong><?= $total_previous; ?></strong></td>
                                            <td style="color: <?= $balance_color; ?>;"><strong><?= $total; ?></strong></td>
                                        <?php } ?>
                                    </tr>

                                <?php } ?>

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

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