<!-- Begin Page Content -->
<div class="container-fluid">
	
  	<!-- Page Heading -->
    <h1 class="h3 mb-4 text-gray-800"><?= $title;  ?></h1>
    
	<div class="card border-bottom-primary shadow">
		<div class="row mt-3 mb-4">
			<div class="col-sm">
                <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="#cetakIncomeStatement">
                                        <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">Laporan Laba Rugi</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%">
									<thead>
										<tr>
											<th scope="col"></th>
											<th scope="col"></th>
											<th scope="col"></th>
											<th scope="col"></th>
										</tr>
									</thead>
									<tbody>
										
										<tr style="font-weight:bold;">
											<td scope="row" colspan="4">Pendapatan</td>
										</tr>
								  		<!-- pendapatan operasional -->
										<?php 
											$sum_pendapatan_operasional_debit = 0;
											$sum_pendapatan_operasional_kredit = 0;
                                            if(isset($laba_rugi_data[0][410]))
                                            {
                                                foreach ($laba_rugi_data[0][410] as $key => $row)
                                                {
												?>
												<?php if($row['saldo'] > 0) { ?>
													<tr>
														<td align="center"><?= $row['noakun'] ?></td>
														<td><?= $row['nama'] ?></td>
														<td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
														<td></td>
													</tr>
													<?php $sum_pendapatan_operasional_debit += $row['saldo']; ?>
													<?php } else if($row['saldo'] < 0) { ?>
													<tr>
														<td align="center"><?= $row['noakun'] ?></td>
														<td><?= $row['nama'] ?></td>
														<td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
														<td></td>
													</tr>
													<?php $sum_pendapatan_operasional_kredit += $row['saldo']; ?>
												<?php } ?>
												<?php 
												}
											}
										?>
										<!-- tutup pendapatan operasional -->
										<!-- pendapatan non-operasional -->
										<?php 
											$sum_pendapatan_nonoperasional_debit = 0;
											$sum_pendapatan_nonoperasional_kredit = 0;
                                            if(isset($laba_rugi_data[0][420]))
                                            {
                                                foreach ($laba_rugi_data[0][420] as $key => $row)
                                                {
												?>
												<?php if($row['saldo'] > 0) { ?>
													<tr>
														<td align="center"><?= $row['noakun'] ?></td>
														<td><?= $row['nama'] ?></td>
														<td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
														<td></td>
													</tr>
													<?php $sum_pendapatan_nonoperasional_debit += $row['saldo']; ?>
													<?php } else if($row['saldo'] < 0) { ?>
													<tr>
														<td align="center"><?= $row['noakun'] ?></td>
														<td><?= $row['nama'] ?></td>
														<td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
														<td></td>
													</tr>
													<?php $sum_pendapatan_nonoperasional_kredit += $row['saldo']; ?>
												<?php } ?>
												<?php 
												}
											}
										?>
										<!-- tutup pendapatan non-operasional -->
										<?php $total_pendapatan = -(($sum_pendapatan_operasional_debit + $sum_pendapatan_nonoperasional_debit) + ($sum_pendapatan_operasional_kredit + $sum_pendapatan_nonoperasional_kredit)); ?>
										<!-- Total Pendapatan -->
										<tr style="font-weight:bold;">
											<td colspan="3" style="text-indent: 0.3in;">Total Pendapatan</td>
											<td align="right"><?= 'Rp. '.number_format(abs($total_pendapatan),0,',','.') ?></td>
										</tr>
										<!-- End Looping Data Pendapatan -->
										
										<!-- Looping Data Beban -->
										<tr style="font-weight:bold;">
											<td scope="row" colspan="4">Beban</td>
										</tr>
										<!-- beban operasional -->
										<?php 
                                            $sum_beban_operasional_debit = 0;
                                            $sum_beban_operasional_kredit = 0;
                                            if(isset($laba_rugi_data[0][510]))
                                            {
                                                foreach ($laba_rugi_data[0][510] as $key => $row)
                                                {
												?>
												<?php if($row['saldo'] > 0) { ?>
													<tr>
														<td align="center"><?= $row['noakun'] ?></td>
														<td><?= $row['nama'] ?></td>
														<td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
														<td></td>
													</tr>
													<?php $sum_beban_operasional_debit += $row['saldo']; ?>
													<?php } else if($row['saldo'] < 0) { ?>
													<tr>
														<td align="center"><?= $row['noakun'] ?></td>
														<td><?= $row['nama'] ?></td>
														<td></td>
														<td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
													</tr>
													<?php $sum_beban_operasional_kredit += $row['saldo']; ?>
												<?php } ?>
												<?php 
												}
											}
										?>
										<!-- tutup beban operasional -->
										<!-- beban non operasional -->
										<?php 
											$sum_beban_nonoperasional_debit = 0;
											$sum_beban_nonoperasional_kredit = 0;
                                            if(isset($laba_rugi_data[0][520]))
                                            {
                                                foreach ($laba_rugi_data[0][520] as $key => $row)
                                                {
												?>
												<?php if($row['saldo'] > 0) { ?>
													<tr>
														<td align="center"><?= $row['noakun'] ?></td>
														<td><?= $row['nama'] ?></td>
														<td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
														<td></td>
													</tr>
													<?php $sum_beban_nonoperasional_debit += $row['saldo']; ?>
													<?php } else if($row['saldo'] < 0) { ?>
													<tr>
														<td align="center"><?= $row['noakun'] ?></td>
														<td><?= $row['nama'] ?></td>
														<td></td>
														<td align="right"><?= 'Rp. '.number_format(abs($row['saldo']),0,',','.') ?></td>
													</tr>
													<?php $sum_beban_nonoperasional_kredit += $row['saldo']; ?>
												<?php } ?>
												<?php 
												}
											}
										?>
										<!-- tutup beban non operasional -->
										<?php $total_beban = ($sum_beban_operasional_debit + $sum_beban_nonoperasional_debit) + ($sum_beban_operasional_kredit + $sum_beban_nonoperasional_kredit); ?>
										<!-- End Looping Data Beban -->
										<!-- Total Beban -->
										<tr style="font-weight:bold;">
											<td colspan="3" style="text-indent: 0.3in;">Total Beban</td>
											<td align="right"><?= 'Rp. '.number_format($total_beban,0,',','.') ?></td>
										</tr>
										
									</tbody>
									<tfoot>
										<?php if($status == 0) : ?>
								  		<tr style="font-weight:bold;">
							                <td colspan="3" style="text-indent: 0.3in;">Laba/Rugi Bersih</td>
							                <td>Rp. 0</td>
										</tr>
										<?php endif; ?>
										<?php if($status == 1) : ?>
										<!-- Laba /Rugi -->
										<?php $total_laba_rugi = $total_pendapatan - $total_beban; ?>
										<?php if($total_laba_rugi >= 0) { ?>
											<tr style="font-weight:bold;">
												<td colspan="3" style="text-indent: 0.3in;">Laba Bersih</td>
												<td align="right"><?= 'Rp. '.number_format($total_laba_rugi,0,',','.') ?></td>
											</tr>
											<?php } else { ?>
											<tr style="font-weight:bold;">
												<td colspan="3" style="text-indent: 0.3in;">Rugi Bersih</td>
												<td align="right"><?= 'Rp. '.number_format(abs($total_laba_rugi),0,',','.') ?></td>
											</tr>
										<?php } ?>
										<!-- tutup laba rugi -->
										<?php endif; ?>
									</tfoot>
								</table>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	
</div>
<!-- /.container-fluid -->

<!-- Modal -->
<div class="modal fade" id="cetakIncomeStatement" tabindex="-1" role="dialog" aria-labelledby="cetakIncomeStatementLabel" aria-hidden="true" >
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<h5 class="modal-title" id="cetakIncomeStatementLabel"></h5>
			</div>
			<form action="<?= base_url('report/printincomestatement'); ?>" 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" 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" 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>
	$("#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>
