<!-- Begin Page Content -->
<div class="container-fluid">
    
    <!-- Page Heading -->
    <h1 class="h3 mb-4 text-gray-800"><?= $title;  ?></h1>
    
    <div class="row mt-3 mb-4">
        <div class="col-sm">
            <div class="card border-bottom-primary shadow">
                <div class="col-sm">
                    <div class="mt-2 mb-2">
                        <div class="card-body">
                            <form action="<?php base_url('') ?>" method="post" class="form-inline">
                                <div class="form-group col-sm-2">
                                    <select name="pilih_range" id="pilih_range" class="bootstrap-select" data-width="100%" title="-- Pilih --">
                                       <option value="1" <?php echo ($pilih_range == 1) ? 'selected' : ''; ?>>Perbulan</option>
                                       <option value="2" <?php echo ($pilih_range == 2) ? 'selected' : ''; ?>>Pertahun</option>
                                        
                                    </select>
                                </div>
                                <div class="form-group col-sm-4">
                                    <label for="bln" class="col-sm-5 col-form-label">Pilih Bulan</label>
                                    <div class="col-sm-7">
                                        
                                        <select name="bln" id="bln" class="bootstrap-select" data-width="100%" title="-- Pilih Bulan --">
                                            <?php foreach($bulan as $row) : 
                                                if($bulan_sekarang == $row['id']) {
                                                    $selected = 'selected';
                                                } else { $selected = null; }
                                            ?>
                                            <option value="<?= $row['id']; ?>" <?= $selected; ?> ><?= $row['nm']; ?></option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group col-sm-3">
                                    <label for="tahun" class="col-sm-5 col-form-label">Pilih Tahun</label>
                                    <div class="col-sm-7">
                                        <select name="tahun" id="tahun" class="bootstrap-select" data-width="100%" title="-- Pilih Tahun --">
                                            <?php foreach($tahun as $row) : 
                                                if($tahun_sekarang == $row) {
                                                    $selected = 'selected';
                                                } else { $selected = null; }
                                            ?>
                                            <option value="<?= $row; ?>" <?= $selected; ?> ><?= $row; ?></option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group col-sm-3 justify-content-center">
                                    <button type="submit" class="btn btn-success mr-3">
                                        <span class="icon text-white-50">
                                            <i class="fa fa-fw fa-filter"></i>
                                        </span>
                                        <span class="text">Filter</span>
                                    </button>
                                    <a href="" class="btn btn-secondary" data-toggle="modal" data-target="#cetakTrialbalance">
                                        <span class="icon text-white-50">
                                            <i class="fa fa-fw fa-print"></i>
                                        </span>
                                        <span class="text">Cetak</span>
                                    </a>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
	
	<div class="row mt-3 mb-4">
		<div class="col-sm">
			<div class="card border-bottom-primary shadow mb-4">
	            <div class="card-header py-3">
                    <h6 class="m-0 font-weight-bold text-primary">Tabel Neraca Saldo</h6>
                </div>
			  	<div class="card-body">
				  	<div class="row">
				  		<div class="col-sm">
				  			<div class="table-responsive">
					  			<table class="table table-striped table-sm" style="width:100%" id="tabel-data">
                                    <thead>
                                        <tr>
                                            <th scope="col" width="120px">No. Akun</th>
                                            <th scope="col">Nama Akun</th>
                                            <th class="text-right" width="250px">Debit</th>
                                            <th class="text-right" width="250px">Kredit</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php if($status == 1) : ?>
                                        <!-- Aset Lancar -->
                                        <?php 
                                            $sum_aset_lancar_debit = 0;
                                            $sum_aset_lancar_kredit = 0;
                                            if(isset($neraca_saldo_data[0][110]))
                                            {
                                                foreach ($neraca_saldo_data[0][110] as $key => $row)
                                                {
                                                ?>
                                                <tr>
                                                    <td align="center"><?= $row['noakun'] ?></td>
                                                    <td><?= $row['nama'] ?></td>
                                                    <?php if($row['saldo'] >= 0) { ?>
                                                        <td align="right"><?= 'Rp. '.number_format($row['saldo'],0,',','.') ?></td>
                                                        <td></td>
                                                        <?php $sum_aset_lancar_debit += $row['saldo']; ?>
                                                        <?php } else { ?>
                                                        <td></td>
                                                        <td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
                                                        <?php $sum_aset_lancar_kredit += $row['saldo']; ?>
                                                    <?php } ?>
                                                </tr>
                                                <?php 
                                                }
                                            }
                                        ?>
                                        <!-- Tutup Aset Lancar -->
                                        <!-- Aset Tetap -->
                                        <?php 
                                            $sum_aset_tetap_debit = 0;
                                            $sum_aset_tetap_kredit = 0;
                                            if(isset($neraca_saldo_data[0][120]))
                                            {
                                                foreach ($neraca_saldo_data[0][120] as $key => $row)
                                                {
                                                ?>
                                                <tr>
                                                    <td align="center"><?= $row['noakun'] ?></td>
                                                    <td><?= $row['nama'] ?></td>
                                                    <?php if($row['saldo'] >= 0) { ?>
                                                        <td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
                                                        <td></td>
                                                        <?php $sum_aset_tetap_debit += $row['saldo']; ?>
                                                        <?php } else { ?>
                                                        <td></td>
                                                        <td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
                                                        <?php $sum_aset_tetap_kredit += $row['saldo']; ?>
                                                    <?php } ?>
                                                </tr>
                                                <?php 
                                                }
                                            }
                                        ?>
                                        <!-- tutup aset tetap -->
                                        <!-- liabilitas jangka pendek -->
                                        <?php 
                                            $sum_liabilitas_jangka_pendek_debit = 0;
                                            $sum_liabilitas_jangka_pendek_kredit = 0;
                                            if(isset($neraca_saldo_data[0][210]))
                                            {
                                                foreach ($neraca_saldo_data[0][210] as $key => $row)
                                                {
                                                ?>
                                                <tr>
                                                    <td align="center"><?= $row['noakun'] ?></td>
                                                    <td><?= $row['nama'] ?></td>
                                                    <?php if($row['saldo'] <= 0) { ?>
                                                        <td></td>
                                                        <td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
                                                        <?php $sum_liabilitas_jangka_pendek_kredit += $row['saldo']; ?>
                                                        <?php } else { ?>
                                                        <td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
                                                        <td></td>
                                                        <?php $sum_liabilitas_jangka_pendek_debit += $row['saldo']; ?>
                                                    <?php } ?>
                                                </tr>
                                                <?php 
                                                }
                                            }
                                        ?>
                                        <!-- tutup liabilitas jangka pendek -->
                                        <!-- liabilitas jangka panjang -->
                                        <?php 
                                            $sum_liabilitas_jangka_panjang_debit = 0;
                                            $sum_liabilitas_jangka_panjang_kredit = 0;
                                            if(isset($neraca_saldo_data[0][220]))
                                            {
                                                foreach ($neraca_saldo_data[0][220] as $key => $row)
                                                {
                                                ?>
                                                <tr>
                                                    <td align="center"><?= $row['noakun'] ?></td>
                                                    <td><?= $row['nama'] ?></td>
                                                    <?php if($row['saldo'] >= 0) { ?>
                                                        <td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
                                                        <td></td>
                                                        <?php $sum_liabilitas_jangka_panjang_debit += $row['saldo']; ?>
                                                        <?php } else { ?>
                                                        <td></td>
                                                        <td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
                                                        <?php $sum_liabilitas_jangka_panjang_kredit += $row['saldo']; ?>
                                                    <?php } ?>
                                                </tr>
                                                <?php 
                                                }
                                            }
                                        ?>
                                        <!-- tutup liabilitas jngaka panjang -->
                                        <!-- ekuitas -->
                                        <?php 
                                            $sum_ekuitas_debit = 0;
                                            $sum_ekuitas_kredit = 0;
                                            if(isset($neraca_saldo_data[0][310]))
                                            {
                                                foreach ($neraca_saldo_data[0][310] as $key => $row)
                                                {
                                                ?>
                                                <tr>
                                                    <td align="center"><?= $row['noakun'] ?></td>
                                                    <td><?= $row['nama'] ?></td>
                                                    <?php if($row['saldo'] >= 0) { ?>
                                                        <td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
                                                        <td></td>
                                                        <?php $sum_ekuitas_debit += $row['saldo']; ?>
                                                        <?php } else { ?>
                                                        <td></td>
                                                        <td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
                                                        <?php $sum_ekuitas_kredit += $row['saldo']; ?>
                                                    <?php } ?>
                                                </tr>
                                                <?php
                                                }
                                            }
                                        ?>
                                        <!-- tutup ekuitas -->
                                        <!-- pendapatan operasional -->
                                        <?php 
                                            $sum_pendapatan_operasional_debit = 0;
                                            $sum_pendapatan_operasional_kredit = 0;
                                            if(isset($neraca_saldo_data[0][410]))
                                            {
                                                foreach ($neraca_saldo_data[0][410] as $key => $row)
                                                {
                                                ?>
                                                <tr>
                                                    <td align="center"><?= $row['noakun'] ?></td>
                                                    <td><?= $row['nama'] ?></td>
                                                    <?php if($row['saldo'] >= 0) { ?>
                                                        <td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
                                                        <td></td>
                                                        <?php $sum_pendapatan_operasional_debit += $row['saldo']; ?>
                                                        <?php } else { ?>
                                                        <td></td>
                                                        <td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
                                                        <?php $sum_pendapatan_operasional_kredit += $row['saldo']; ?>
                                                    <?php } ?>
                                                </tr>
                                                <?php 
                                                }
                                            }
                                        ?>
                                        <!-- tutup pendapatan operasional -->
                                        <!-- pendapatan non-operasional -->
                                        <?php 
                                            $sum_pendapatan_nonoperasional_debit = 0;
                                            $sum_pendapatan_nonoperasional_kredit = 0;
                                            if(isset($neraca_saldo_data[0][420]))
                                            {
                                                foreach ($neraca_saldo_data[0][420] as $key => $row)
                                                {
                                                ?>
                                                <tr>
                                                    <td align="center"><?= $row['noakun'] ?></td>
                                                    <td><?= $row['nama'] ?></td>
                                                    <?php if($row['saldo'] >= 0) { ?>
                                                        <td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
                                                        <td></td>
                                                        <?php $sum_pendapatan_nonoperasional_debit += $row['saldo']; ?>
                                                        <?php } else { ?>
                                                        <td></td>
                                                        <td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
                                                        <?php $sum_pendapatan_nonoperasional_kredit += $row['saldo']; ?>
                                                    <?php } ?>
                                                </tr>
                                                <?php 
                                                }
                                            }
                                        ?>
                                        <!-- tutup pendapatan non-operasional -->
                                        <!-- beban operasional -->
                                        <?php 
                                            $sum_beban_operasional_debit = 0;
                                            $sum_beban_operasional_kredit = 0;
                                            if(isset($neraca_saldo_data[0][510]))
                                            {
                                                foreach ($neraca_saldo_data[0][510] as $key => $row)
                                                {
                                                ?>
                                                <tr>
                                                    <td align="center"><?= $row['noakun'] ?></td>
                                                    <td><?= $row['nama'] ?></td>
                                                    <?php if($row['saldo'] >= 0) { ?>
                                                        <td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
                                                        <td></td>
                                                        <?php $sum_beban_operasional_debit += $row['saldo']; ?>
                                                        <?php } else { ?>
                                                        <td></td>
                                                        <td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
                                                        <?php $sum_beban_operasional_kredit += $row['saldo']; ?>
                                                    <?php } ?>
                                                </tr>
                                                <?php 
                                                }
                                            }
                                        ?>
                                        <!-- tutup beban operasional -->
                                        <!-- beban non operasional -->
                                        <?php 
                                            $sum_beban_nonoperasional_debit = 0;
                                            $sum_beban_nonoperasional_kredit = 0;
                                            if(isset($neraca_saldo_data[0][520]))
                                            {
                                                foreach ($neraca_saldo_data[0][520] as $key => $row)
                                                {
                                                ?>
                                                <tr>
                                                    <td align="center"><?= $row['noakun'] ?></td>
                                                    <td><?= $row['nama'] ?></td>
                                                    <?php if($row['saldo'] >= 0) { ?>
                                                        <td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
                                                        <td></td>
                                                        <?php $sum_beban_nonoperasional_debit += $row['saldo']; ?>
                                                        <?php } else { ?>
                                                        <td></td>
                                                        <td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
                                                        <?php $sum_beban_nonoperasional_kredit += $row['saldo']; ?>
                                                    <?php } ?>
                                                </tr>
                                                <?php 
                                                }
                                            }
                                        ?>
                                        <!-- tutup beban non operasional -->
                                        <?php endif; ?>
                                    </tbody>
                                    <tfoot>
                                        <?php if($status == 1) : ?>
                                        <!-- jumlah -->
                                        <?php $sum_debit = $sum_aset_lancar_debit + $sum_aset_tetap_debit + $sum_liabilitas_jangka_pendek_debit + $sum_liabilitas_jangka_panjang_debit + $sum_ekuitas_debit + $sum_pendapatan_operasional_debit + $sum_pendapatan_nonoperasional_debit + $sum_beban_operasional_debit + $sum_beban_nonoperasional_debit; ?>
                                        <?php $sum_kredit = $sum_aset_lancar_kredit + $sum_aset_tetap_kredit + $sum_liabilitas_jangka_pendek_kredit + $sum_liabilitas_jangka_panjang_kredit + $sum_ekuitas_kredit + $sum_pendapatan_operasional_kredit + $sum_pendapatan_nonoperasional_kredit + $sum_beban_operasional_kredit + $sum_beban_nonoperasional_kredit; ?>
                                        <tr style="font-weight:bold;">
                                            <td colspan="2" align="center">Jumlah</td>
                                            <td class="text-success" align="right"><?= 'Rp. '.number_format($sum_debit,0,',','.') ?></td>
                                            <td class="text-success" align="right"><?= 'Rp. '.number_format(abs($sum_kredit),0,',','.') ?></td>
                                        </tr>
                                        <?php endif; ?>
                                        
                                    </tfoot>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
</div>
<!-- /.container-fluid -->

<!-- Modal -->
<div class="modal fade" id="cetakTrialbalance" tabindex="-1" role="dialog" aria-labelledby="cetakTrialbalanceLabel" aria-hidden="true" >
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="cetakTrialbalanceLabel"></h5>
            </div>
            <form action="<?= base_url('report/printtrialbalance'); ?>" method="post" target="_BLANK">
                <div class="modal-body">
                    <div class="form-group row">
                        <label for="bln" class="col-sm-5 col-form-label">Pilih</label>
                        <div class="col-sm-7">
                            <select name="pilih_range_print" id="pilih_range_print" class="bootstrap-select" data-width="100%" title="-- Pilih --">
                                <option value="1" selected>Perbulan</option>
                                <option value="2">Pertahun</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="bln" class="col-sm-5 col-form-label">Pilih Bulan</label>
                        <div class="col-sm-7">
                            <select name="bln" id="bln_print" class="bootstrap-select" data-width="100%" title="-- Pilih Bulan --">
                                <?php foreach($bulan as $row) : 
                                    if($bulan_sekarang == $row['id']) {
                                        $selected = 'selected';
                                    } else { $selected = null; }
                                ?>
                                <option value="<?= $row['id']; ?>" <?= $selected; ?> ><?= $row['nm']; ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="tahun" class="col-sm-5 col-form-label">Pilih Tahun</label>
                        <div class="col-sm-7">
                            <select name="tahun" id="tahun_print" class="bootstrap-select" data-width="100%" title="-- Pilih Tahun --">
                                <?php foreach($tahun as $row) : 
                                    if($tahun_sekarang == $row) {
                                        $selected = 'selected';
                                    } else { $selected = null; }
                                ?>
                                <option value="<?= $row; ?>" <?= $selected; ?> ><?= $row; ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-success">Cetak</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    $(document).ready(function() {
        // $('#tabel-data').DataTable();
        
        $(".bootstrap-select").selectpicker();
        
    });
    
    $("#pilih_range").change(function() {
        var id = $("#pilih_range").val();
        console.log(id)
        if(id==1)
        {
            $("#bln").prop('disabled',false);
            $('#bln').selectpicker('refresh');
            }else{
            $("#bln").prop('disabled',true);
            $('#bln').selectpicker('refresh');
        }
    });
    $("#pilih_range_print").change(function() {
        var id = $("#pilih_range_print").val();
        console.log(id)
        if(id==1)
        {
            $("#bln_print").prop('disabled',false);
            $('#bln_print').selectpicker('refresh');
            }else{
            $("#bln_print").prop('disabled',true);
            $('#bln_print').selectpicker('refresh');
        }
    });
    $("#pilih_range").val("<?=$pilih_range;?>").change();
    
</script>