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

<?php

$account_db = fetch_table_where('accounts','uid',$search_type,true);
$account_db_type_name = isset($account_db['name']) ? $account_db['name'] : print_lang($app_lang,'builtin_account_all_accounts_statement');

$account_db_child = fetch_table_where('accounts','uid',$search_account,true);
$account_db_type_name_child = isset($account_db_child['name']) ? $account_db_child['name'] : NULL;


$currency_symbol = array_key_exists($search_currency, $currencies) ? $currencies[$search_currency]['symbol'] : NULL;
$account_name = array_key_exists($search_account, $accounts) ? $accounts[$search_account]['name'] : NULL;
if(!empty($search_from)){$from = true_date_for_inputs($search_from);}else{$from = '';}
if(!empty($search_to)){$to = true_date_for_inputs($search_to);}else{$to = '';}
// print_rr($to);

?>

<table style="width: 100%; margin: 15px 0px; background: #fff;">
    <tr style="margin: 10px 0px;">
        <td style="width: 25%;"><span style="color: #bb0d0d !important; padding: 4px;"><strong style="color: #bb0d0d !important; font-size: 15px;"><?= strtoupper(print_lang($app_lang,'type')); ?>:</strong></span> <strong style="font-size: 14px;"><?php if(!empty($account_db_type_name)){echo ucfirst($account_db_type_name).' '.print_lang($app_lang,'statement');} ?></strong></td>
        <td style="width: 25%;"><span style="color: #bb0d0d !important; padding: 4px;"><strong style="color: #bb0d0d !important; font-size: 15px;"><?= strtoupper(print_lang($app_lang,'currency')); ?>:</strong></span> <strong style="font-size: 14px;"><?php if(!empty($search_currency)){echo ucfirst($currency_symbol);} ?></strong></td>
        <td style="width: 25%;"><span style="color: #bb0d0d !important; padding: 4px;"><strong style="color: #bb0d0d !important; font-size: 15px;"><?= strtoupper(print_lang($app_lang,'account')); ?>:</strong></span> <strong style="font-size: 14px;"><?php if(!empty($account_db_type_name_child)){echo ucfirst($account_db_type_name_child);} ?></strong></td>
        <td style="width: 25%;"><span style="color: #bb0d0d !important; padding: 4px;"><strong style="color: #bb0d0d !important; font-size: 15px;"><?= strtoupper(print_lang($app_lang,'date')); ?>:</strong></span> <strong style="font-size: 14px;"><?php if(!empty($from)){echo $from."  -  ".$to;}else{echo print_lang($app_lang,'overall');} ?></strong></td>
    </tr>
</table>

<table style="width: 100%; margin: 20px 0px 5px; background: #fff;">
    <tr style="margin: 5px 0px;">
        <td style="width: 33.3%; text-align: center;"><span style="color: #bb0d0d !important; padding: 4px;"><strong style="color: #bb0d0d !important; font-size: 15px;"><?= print_lang($app_lang,'ledger_overall_debit'); ?>:</strong></span> <strong style="font-size: 14px;"><?php echo amount_number_format( $totals_Data['debit'] ); ?></strong></td>
        <td style="width: 33.3%; text-align: center;"><span style="color: #bb0d0d !important; padding: 4px;"><strong style="color: #bb0d0d !important; font-size: 15px;"><?= print_lang($app_lang,'ledger_overall_credit'); ?>:</strong></span> <strong style="font-size: 14px;"><?php echo amount_number_format( $totals_Data['credit'] ); ?></strong></td>
        <td style="width: 33.3%; text-align: center;"><span style="color: #bb0d0d !important; padding: 4px;"><strong style="color: #bb0d0d !important; font-size: 15px;"><?= print_lang($app_lang,'ledger_overall_balance'); ?>:</strong></span> <strong style="font-size: 14px;"><?php echo amount_number_format( $totals_Data['debit'] - $totals_Data['credit'] ); ?></strong></td>
    </tr>
</table>

<hr>

<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">
                    
                    <div class="table-responsive">
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <th class="text-center" style="width: 50px !important; min-width: 50px !important;"><?= print_lang($app_lang,'sr_#'); ?></th>
                                    <th class="text-center" style="width: 100px !important; min-width: 100px !important;"><?= print_lang($app_lang,'date'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'ref_number'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'narration'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'debit'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'credit'); ?></th>
                                    <th class="text-center"><?= print_lang($app_lang,'balance'); ?></th>
                                    <th class="text-center" style="min-width: 150px; max-width: 150px;"><?= print_lang($app_lang,'remarks'); ?></th>
                                </tr>
                            </thead>
                            <tbody>

                                <?php
                                $debits = array();
                                $credits = array();
                                    if($start_filter == 'yes'){
                                        $debits[] = $before_filter_balance['debit'];
                                        $credits[] = $before_filter_balance['credit'];
                                ?>
                                    <tr class="text-center">
                                        <td></td>
                                        <td></td>
                                        <td></td>
                                        <td><?= print_lang($app_lang,'previous_balance'); ?></td>
                                        <td><?= amount_number_format($before_filter_balance['debit']); ?></td>
                                        <td><?= amount_number_format($before_filter_balance['credit']); ?></td>

                                        <?php $row_balance = get_ledgers_balance($debits,$credits); ?>
                                        <td style="color: <?= ($row_balance >= 0) ? '#2B9C1B' : '#ec4758'; ?> !important;"><?= amount_number_format($row_balance); ?></td>
                                        <td class="accounts-report-remarks-col"><span></span></td>
                                    </tr>
                                <?php } ?>


                                <?php
                                $i = 1;
                                foreach($pageData as $row){

                                    $ref_prefix = null;
                                    $journal_invoice = null;
                                    $journal_receipt_uid = null;
                                    $journal_receipt = null;
                                    $journal_details = null;
                                    $journal_debit_details = null;
                                    $journal_credit_details = null;

                                    $row_cross_check_data = fetch_table_where('accounts_cross_checks','uid',$row['cc_uid'],true);
                                    $color = isset($row_cross_check_data['color']) ? (empty($row_cross_check_data['color']) ? '#ffffff' : $row_cross_check_data['color']) : '#ffffff';
                                    $remarks = isset($row_cross_check_data['remarks']) ? $row_cross_check_data['remarks'] : NULL;
                                    $is_checked = isset($row_cross_check_data['is_checked']) ? $row_cross_check_data['is_checked'] : 'no';

                                    // amounts
                                    $row_debit = ($_item_settings['item_measurements'] == 'get') ? $row['wim_debit'] : $row['debit'];
                                    $row_credit = ($_item_settings['item_measurements'] == 'get') ? $row['wim_credit'] : $row['credit'];
                                    if($row_debit == 0 AND $row_credit == 0){ continue; }
                                    $debits[] = $row_debit;
                                    $credits[] = $row_credit;
                                    $ref_prefix = print_lang($app_lang,'ledgers_module_wise_sr_prefix_'.$row['module_name']);

                                    // embed url
                                    $row['narration'] = str_replace('__embed_host__', url('/'), $row['narration']);

                                    // if($row['module_name'] == 'journal')
                                    // {
                                    //     $journal_debit_details = isset($journals[str_replace('ALE_', '', $row['module_uid'])]['payment_narration']) ? $journals[str_replace('ALE_', '', $row['module_uid'])]['payment_narration'] : null;
                                    //     $journal_credit_details = isset($journals[str_replace('ALE_', '', $row['module_uid'])]['receiving_narration']) ? $journals[str_replace('ALE_', '', $row['module_uid'])]['receiving_narration'] : null;
                                    //     $journal_invoice = isset($journals[str_replace('ALE_', '', $row['module_uid'])]['invoice']) ? $journals[str_replace('ALE_', '', $row['module_uid'])]['invoice'] : null;
                                    //     $journal_receipt_uid = isset($journals[str_replace('ALE_', '', $row['module_uid'])]['receipt_uid']) ? $journals[str_replace('ALE_', '', $row['module_uid'])]['receipt_uid'] : null;
                                    //     $journal_receipt = isset($money_receipts[$journal_receipt_uid]['invoice']) ? $money_receipts[$journal_receipt_uid]['invoice'] : null;
                                    // }

                                    if($row['module_name'] == 'journal')
                                    {
                                        $journal_data = fetch_table_where('journal','uid',str_replace('ALE_', '', $row['module_uid']),true);
                                        if(!empty($journal_data))
                                        {
                                            $journal_debit_details = isset($journal_data['payment_narration']) ? $journal_data['payment_narration'] : null;
                                            $journal_credit_details = isset($journal_data['receiving_narration']) ? $journal_data['receiving_narration'] : null;
                                            $journal_invoice = isset($journal_data['invoice']) ? $journal_data['invoice'] : null;
                                            $journal_receipt_uid = isset($journal_data['receipt_uid']) ? $journal_data['receipt_uid'] : null;

                                            if(!empty($journal_receipt_uid))
                                            {
                                                $money_receipt_data = fetch_table_where('money_receipts','uid',$journal_receipt_uid,true);
                                                if(!empty($money_receipt_data))
                                                {
                                                    $journal_receipt = isset($money_receipt_data['invoice']) ? $money_receipt_data['invoice'] : null;
                                                }
                                            }
                                        }
                                    }
                                    
                                    ?>

                                    <tr class="text-center" style="background: <?= $color ?> !important;">
                                        <td>
                                            <?= $i++; ?>
                                        </td>
                                        <td><?= display_true_date(date('Y-m-d h:i:s A',strtotime($row['aldate']))); ?></td>
                                        <td><?= $ref_prefix.' '.$row['serial_id']; ?></td>
                                        <td>
                                            <?php if($row['module_name'] == 'journal'){?>
                                                <?= 'Journal # '.$journal_invoice; ?><br>
                                                <?php if(!empty($journal_receipt)){?>
                                                <?= 'Receipt # '.$journal_receipt; ?><br>
                                                <?php } ?>
                                                <?php if($row['debit'] != 0){?>
                                                    <?php if(!empty($journal_debit_details)){?>
                                                    <?= $journal_debit_details; ?><br>
                                                    <?php } ?>
                                                <?php }else{?>
                                                    <?php if(!empty($journal_credit_details)){?>
                                                    <?= $journal_credit_details; ?><br>
                                                    <?php } ?>
                                                <?php } ?>


                                            <?php }else{?>
                                            <?= $row['narration']; ?><br>
                                            <?php } ?>
                                            <?php 
                                                $branch_text = isset($branches[$row['branch']]) ? ucwords($branches[$row['branch']]['branch_name']) : print_lang($app_lang,'customer_ledger_branch_not_found');
                                                $branch_name = print_lang($app_lang,'customer_ledger_branch_name').' '.$branch_text;
                                            ?>
                                            <span class="text-muted" style="font-size: 11px !important; font-weight: 600 !important;"><?php echo $row['module_details']; ?></span>
                                        </td>
                                        <td><?= amount_number_format($row_debit); ?></td>
                                        <td><?= amount_number_format($row_credit); ?></td>

                                        <?php $row_balance = get_ledgers_balance($debits,$credits); ?>
                                        <td style="color: <?= ($row_balance >= 0) ? '#2B9C1B' : '#ec4758'; ?> !important;"><?= amount_number_format($row_balance); ?></td>
                                        <td class="accounts-report-remarks-col"><span><?= $remarks; ?></span></td>
                                    </tr>

                                <?php } ?>
                                <tr class="text-center">
                                    <td><?= $i++; ?></td>
                                    <td><?= display_true_date(date('Y-m-d h:i:s A')); ?></td>
                                    <td></td>
                                    <td><?= print_lang($app_lang,'total'); ?></td>
                                    <td><?= amount_number_format(array_sum($debits)); ?></td>
                                    <td><?= amount_number_format(array_sum($credits)); ?></td>

                                    <?php $total_balance = get_ledgers_balance($debits,$credits); ?>
                                    <td style="color: <?= ($total_balance >= 0) ? '#2B9C1B' : '#ec4758'; ?> !important;"><?= amount_number_format($total_balance); ?></td>
                                    <td></td>
                                </tr>
                            </tbody>
                        </table>    
                    </div>

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

<script type="text/javascript">
    $(document).ready(function()
    {
        $('a').attr('href','#');
    });
</script>