<?php
require './kalenderhijriah.php';
error_reporting(E_ALL^(E_NOTICE|E_WARNING));
ini_set('display_errors',1);
error_reporting(0);
?>

<!-- Content Header (Page header) -->
<div class="content-header">
  <div class="container-fluid">
    <div class="row mb-1">
      <div class="col-sm-12">
        <!-- <h1 class="m-0 text-dark">Laporan Neraca []</h1> -->
      </div><!-- /.col -->
    </div><!-- /.row -->
  </div><!-- /.container-fluid -->
</div>
<!-- /.content-header -->

<!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-12">
            <!-- Default box -->
            <div class="card">
              <div class="card-header bg-info">
                <h3 class="card-title ">Pecahan Uang Tabungan</h3>
                <div class="card-tools">
                  <button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
                    <i class="fas fa-minus"></i>
                  </button>
                  <button type="button" class="btn btn-tool" data-card-widget="remove" title="Remove">
                    <i class="fas fa-times"></i>
                  </button>
                </div>
              </div>

              <!-- /.card-header -->
        <div class="card-body">
          <?php
          
          
          $username =$_SESSION['username'];
          $kode     =$_SESSION['kode']; 
          if(isset($_POST['p_rekening'])){
            $par1= date('Y-m-d',strtotime($_POST['par1']));
            $par2= date('Y-m-d',strtotime($_POST['par2']));
            $id = $_POST['p_rekening'];


            if($_POST['p_rekening']!=='')
             $where="WHERE  a.rekening like '%".$id."%' OR b.nama like '%".$id."%' GROUP by a.rekening ";
            else
               $where="WHERE a.tgl<='$par2'  GROUP by a.rekening ";

          }else{
            
            $id="";

            $where="where a.rekening='$id' ";
           
          }
          ?>
          <form role="form" id="quickForm" action="home.php?home=<?php echo md5("pecahanUang_tabungan");?>" method="post">
             <div class="row">
              <div class="col-sm-10">
            <div class="form-group row">
              <label for="nama" class="col-2 m-3">Cari Data</label>
              <input type="text" name="p_rekening" id="p_rekening"  class="form-control col-sm-2" placeholder="ketik Register/Nama/Alamat/No Register/Ayah">
                            <!-- Input tanggal pertama -->
                <div id="datepicker1" class="ml-2 " >
                    <input id="par1" name="par1" type="hidden" class="form-control d-inline-block" style="width: 100px; display: inline;" value="<?php if(isset($_POST['par1'])){ echo $_POST['par1']; }else{ echo sprintf('%02d', $hijri[1]) . '-' . $calendar->monthName($hijri[0]) . '-' . $hijri[2];} ?>"  />
                    <!-- <img id="pick-button1" src="kalender.png" alt="Pick Date" style="cursor: pointer; width: 24px; height: 24px;" onclick="pickADate1();" class="d-inline-block"/> -->
                    <div id="calendar-popup1" class="datepicker" style="display:none;"></div>
                </div>

                <!-- Input tanggal kedua -->
                <div id="datepicker2" class="ml-2">
                    <input id="par2" name="par2" type="text" class="form-control d-inline-block" style="width: 100px; display: inline;"  value="<?php if(isset($_POST['par2'])){ echo $_POST['par2']; }else{ echo sprintf('%02d', $hijri[1]) . '-' . $calendar->monthName($hijri[0]) . '-' . $hijri[2];} ?>" />
                    <img id="pick-button2" src="kalender.png" alt="Pick Date" style="cursor: pointer; width: 24px; height: 24px;" onclick="pickADate2();" class="d-inline-block"/>
                    <div id="calendar-popup2" class="datepicker" style="display:none;"></div>
                </div>
            
                <div class="col">
                    <button type="submit" class="btn btn-info">
                        <i class="fas fa-search"></i>
                    </button>
                </div>
                <?php if(isset($_POST['par1'])){?>
                  <div class="form-group col-2">
                <?php
               $_SESSION['tabungan']['periode'] = date('d-m-Y',strtotime($_POST['par2']));
                $_SESSION['tabungan']['kantor']=$_SESSION['kantor'];
                ?>
                <a href="pdf/lap_pecahanUang_tabunganpdf.php" target="_blank" style="float: right;margin-top: 10px;" class="btn btn-success"><i class="fa fa-print"></i>PDF</a>

              <?php } ?>
               </div>
            </div>    
          </form>                   
          <hr>

          <table class="table table-bordered table-hover w-100" style="width: 100%;">
    <thead>
        <tr align="center">
            <th>No</th>
            <th>Rekening</th>
            <th>Nama</th>
            <th >Kls</th>
            <th>Saldo</th>
            <th>100.000</th>
            <th>50.000</th>
            <th>20.000</th>
            <th>10.000</th>
            <th>5.000</th>
            <th>2.000</th>
            <th>1.000</th>
        </tr>
    </thead>
    <tbody>
        <?php
        include 'koneksi.php';
        $queryp = "SELECT b.klsNow as kls,a.no_bukti AS no_bukti,a.rekening as id, a.rekening as rekening, a.tgl as tgl, b.nama as nama, SUM(kredit-debet) AS saldotab, a.keterangan AS keterangan, a.username as username FROM mutasitabungan a LEFT JOIN registrasi b ON a.id=b.id $where ORDER BY b.klsNow, a.tgl";
        $querya = mysqli_query($koneksi, $queryp);
        
        if (mysqli_num_rows($querya)) {
            $_SESSION['pecahanUang_tabungan']['kode']['sqlp'] = $queryp;
            $no = 0;
            $current_kls = '';
            $kls_subtotal_kredit = 0;
            $t_saldotab = 0;
            $t_100 = 0;
            $t_50 = 0;
            $t_20 = 0;
            $t_10 = 0;
            $t_5 = 0;
            $t_2 = 0;
            $t_1 = 0;
            while ($data = mysqli_fetch_assoc($querya)) {
                
                $kls = $data['kls'];   
                $rekening = $data['rekening'];
                $nama = $data['nama'];
                $saldotab = $data['saldotab'];
                $keterangan = $data['keterangan'];
                /*https://carawawan.home.blog/2019/03/18/cara-php-menghitung-banyak-pecahan/*/
                $dataa = $saldotab % 100000;
                $a100 = ($saldotab - $dataa) / 100000;

                $datab = $dataa % 50000;
                $a50 = ($dataa - $datab) / 50000;

                $datac = $datab % 20000;
                $a20 = ($datab - $datac) / 20000;

                $datad = $datac % 10000;
                $a10 = ($datac - $datad) / 10000;

                $datae = $datad % 5000;
                $a5 = ($datad- $datae) / 5000;

                $dataf = $datae % 2000;
                $a2 = ($datae - $dataf) / 2000;
                
                $datag = $dataf % 1000;
                $a1 = ($dataf - $datag) / 1000;
                
                // Jika kelas berubah, tampilkan subtotal untuk kelas sebelumnya
                if ($current_kls != '' && $current_kls != $kls) {
                    ?>
                    <tr style="background-color: #f0f0f0;">
                        <td colspan="4" align="center"><b>Subtotal Kelas <?php echo $current_kls; ?></b></td>
                        <td><b><?php echo number_format($kls_subtotal_kredit, 0); ?></b></td>
                        <td><b><?php echo number_format($kls_subtotal_kredit1, 0); ?></b></td>
                        <td><b><?php echo number_format($kls_subtotal_kredit2, 0); ?></b></td>
                        <td><b><?php echo number_format($kls_subtotal_kredit3, 0); ?></b></td>
                        <td><b><?php echo number_format($kls_subtotal_kredit4, 0); ?></b></td>
                        <td><b><?php echo number_format($kls_subtotal_kredit5, 0); ?></b></td>
                        <td><b><?php echo number_format($kls_subtotal_kredit6, 0); ?></b></td>
                        <td><b><?php echo number_format($kls_subtotal_kredit7, 0); ?></b></td>
                        
                        <td colspan="2"></td>
                    </tr>
                    <?php
                    // Reset subtotal untuk kelas baru
                    $kls_subtotal_kredit = 0;
                    $kls_subtotal_kredit1 = 0;
                    $kls_subtotal_kredit2 = 0;
                    $kls_subtotal_kredit3 = 0;
                    $kls_subtotal_kredit4 = 0;
                    $kls_subtotal_kredit5 = 0;
                    $kls_subtotal_kredit6 = 0;
                    $kls_subtotal_kredit7 = 0;

                }
                
                // Update current class
                $current_kls = $kls;
                
                // Menambahkan ke subtotal
                $kls_subtotal_kredit += $saldotab;
                $kls_subtotal_kredit1 += $a100;
                $kls_subtotal_kredit2 += $a50;
                $kls_subtotal_kredit3 += $a20;
                $kls_subtotal_kredit4 += $a10;
                $kls_subtotal_kredit5 += $a5;
                $kls_subtotal_kredit6 += $a2;
                $kls_subtotal_kredit7 += $a1;

                
                // Menambahkan ke total keseluruhan
                $t_saldotab += $saldotab;
                $t_saldotab1 += $a100;
                $t_saldotab2 += $a50;
                $t_saldotab3 += $a20;
                $t_saldotab4 += $a10;
                $t_saldotab5 += $a5;
                $t_saldotab6 += $a2;
                $t_saldotab7 += $a1;

                
                $no++;
        ?>
                <tr>
                    <td align="center"><?php echo $no; ?></td>
                    
                    <td><?php echo $rekening; ?></td>
                    <td><?php echo $nama; ?></td>
                    <td><?php echo $kls; ?></td>
                    <td><?php echo number_format($saldotab, 0); ?></td>
                    <td><?php echo number_format($a100, 0); ?></td>
                    <td><?php echo number_format($a50, 0); ?></td>
                    <td><?php echo number_format($a20, 0); ?></td>
                    <td><?php echo number_format($a10, 0); ?></td>
                    <td><?php echo number_format($a5, 0); ?></td>
                    <td><?php echo number_format($a2, 0); ?></td>
                    <td><?php echo number_format($a1, 0); ?></td>
                </tr>
        <?php
            }
            
            // Tampilkan subtotal untuk kelas terakhir
            if ($current_kls != '') {
                ?>
                <tr style="background-color: #f0f0f0;">
                    <td colspan="4" align="center"><b>Subtotal Kelas <?php echo $current_kls; ?></b></td>
                    <td><b><?php echo number_format($kls_subtotal_kredit, 0); ?></b></td>
                    <td><b><?php echo number_format($kls_subtotal_kredit1, 0); ?></b></td>
                    <td><b><?php echo number_format($kls_subtotal_kredit2, 0); ?></b></td>
                    <td><b><?php echo number_format($kls_subtotal_kredit3, 0); ?></b></td>
                    <td><b><?php echo number_format($kls_subtotal_kredit4, 0); ?></b></td>
                    <td><b><?php echo number_format($kls_subtotal_kredit5, 0); ?></b></td>
                    <td><b><?php echo number_format($kls_subtotal_kredit6, 0); ?></b></td>
                    <td><b><?php echo number_format($kls_subtotal_kredit7, 0); ?></b></td>
                    
                    <td colspan="2"></td>
                </tr>
                <?php
            }
        }
        ?>
    </tbody>
    <tfoot>
        <tr style="background-color: #d9edf7;">
            <td colspan="4" align="center"><b>TOTAL KESELURUHAN</b></td>
            <td><b><?php echo number_format($t_saldotab, 0); ?></b></td>
            <td><b><?php echo number_format($t_saldotab1, 0); ?></b></td>
            <td><b><?php echo number_format($t_saldotab2, 0); ?></b></td>
            <td><b><?php echo number_format($t_saldotab3, 0); ?></b></td>
            <td><b><?php echo number_format($t_saldotab4, 0); ?></b></td>
            <td><b><?php echo number_format($t_saldotab5, 0); ?></b></td>
            <td><b><?php echo number_format($t_saldotab6, 0); ?></b></td>
            <td><b><?php echo number_format($t_saldotab7, 0); ?></b></td>

           
        </tr>
    </tfoot>
</table>


<link rel="stylesheet" href="calendar.css"/>
<script type="text/javascript" src="hijri-date.js"></script>
<script type="text/javascript" src="calendar.js"></script>
<!-- https://zulns.github.io/libs/calendar-api-doc.html -->



<script type="text/javascript">
    var pickedTxt1 = document.getElementById('par1'),
        datepicker1 = new Calendar(true, 0, false, true);
    document.getElementById('datepicker1').appendChild(datepicker1.getElement());
    datepicker1.getElement().style.marginTop = '10px';
    
    datepicker1.callback = function() {
        var selectedDate = datepicker1.getDate();
        var day = selectedDate.getDate().toString().padStart(2, '0');
        var month = (selectedDate.getMonth() + 1).toString().padStart(2, '0');
        var year = selectedDate.getFullYear();
        pickedTxt1.value = day + '-' + month + '-' + year;
        pickedTxt1.selectionStart = 0;
        pickedTxt1.selectionEnd = pickedTxt1.value.length;
        pickedTxt1.focus();
        datepicker1.hide();
    };

    function pickADate1() {
        datepicker1.show();
        datepicker1.getElement().style.position = 'absolute'; // Pastikan posisi kalender tidak mengganggu layout
    }

    var pickedTxt2 = document.getElementById('par2'),
        datepicker2 = new Calendar(true, 0, false, true);
    document.getElementById('datepicker2').appendChild(datepicker2.getElement());
    datepicker2.getElement().style.marginTop = '10px';
    
    datepicker2.callback = function() {
        var selectedDate = datepicker2.getDate();
        var day = selectedDate.getDate().toString().padStart(2, '0');
        var month = (selectedDate.getMonth() + 1).toString().padStart(2, '0');
        var year = selectedDate.getFullYear();
        pickedTxt2.value = day + '-' + month + '-' + year;
        pickedTxt2.selectionStart = 0;
        pickedTxt2.selectionEnd = pickedTxt2.value.length;
        pickedTxt2.focus();
        datepicker2.hide();
    };

    function pickADate2() {
        datepicker2.show();
        datepicker2.getElement().style.position = 'absolute'; // Pastikan posisi kalender tidak mengganggu layout
    }
</script>
<style>
    /* Mengatur posisi datepicker agar tidak mempengaruhi layout lain */
    .datepicker {
        position: absolute; /* Mencegah pergeseran elemen lain */
        z-index: 1000; /* Agar kalender muncul di atas elemen lain */
    }
</style>
<script type="text/javascript">
function addClickOutsideListener(datepicker, popupId) {
        function onClick(event) {
            var popup = document.getElementById(popupId);
            if (popup && !popup.contains(event.target) && !datepicker.getElement().contains(event.target)) {
               
                datepicker.hide(); // Menyembunyikan kalender
                document.removeEventListener('click', onClick); // Menghapus event listener setelah digunakan
            }
        }
        
        // Menambahkan event listener untuk mendeteksi klik di luar kalender
        document.addEventListener('click', onClick);
    }
</script>

             
            </div>
            <!-- /.card-body -->
          </div>
          <!-- /.card -->
        </div>
        <!-- /.col -->
      </div>

      <!-- /.row -->
    </section>
    <!-- /.content -->