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

<!-- 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">Laporan Registrasi</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['par1'])) {
              $par1 = isset($_POST['par1']) ? date('Y-m-d', strtotime($_POST['par1'])) : "";
              $par2 = isset($_POST['par2']) ? date('Y-m-d', strtotime($_POST['par2'])) : "";
              $id = $_POST['p_rekening'];
              
              if ($_POST['p_rekening'] !== '')
                $where = "where a.id like '%" . $id . "%' OR a.nama like '%" . $id . "%' OR a.alamat like '%" . $id . "%' OR b.desa like '%" . $id . "%' OR b.kec like '%" . $id . "%' OR b.kab like '%" . $id . "%' AND a.kode='$kode'";
              else
                $where = "where a.tgl>='$par1' and a.tgl<='$par2' AND a.kode='$kode' GROUP by a.id ";
            } else {
              $id = "";
              $where = "where a.id='$id' ";
            }
            ?>
            <form role="form" id="quickForm" action="home.php?home=<?php echo md5("lap_register");?>" 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="text" 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="form-group row" style="margin-left: 5px;">
                      <button type="submit" class="btn btn-info"><i class="fas fa-search" ></i></button>
                    </div>
                  </div>
                </div>

                <?php if (isset($_POST['par1'])) { ?>
                  <div class="form-group col-2">
                    <?php
                    $_SESSION['pinjaman']['periode'] = date('d-m-Y', strtotime($_POST['par1'])) . " S/d " . date('d-m-Y', strtotime($_POST['par2']));
                    $_SESSION['registrasi']['kantor'] = $_SESSION['kantor'];
                    ?>
                  </div>

                  <div style="position: relative; width: 100%; margin-top: 10px;">
                  <div style="position: absolute; right: 0; top: 0; display: flex; gap: 10px; align-items: center;">
                      <!-- Icon Excel -->
                      <a href="exportexcell/registrasi_murid.php" target="_blank" title="Download Excel">
                          <img src="img/excell.png" alt="Excel" style="width: 32px; height: 32px;">
                      </a>
                      <!-- Icon PDF -->
                      <a href="exportexcell/registrasi_murid.php" target="_blank" title="Download PDF">
                          <img src="img/pdf.png" alt="PDF" style="width: 32px; height: 32px;">
                      </a>
                  </div>
              </div>

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

            <table id="sortTable" width="100%" class="table table-bordered table-hover">
              <thead>
                <tr align="center">
                  <th>No</th>
                  <th>Tgl</th>
                  <th>Register</th>
                  <th>NIK</th>
                  <th>Nama</th>
                  <th>Kls</th>
                  <th>TTL</th>
                  <th>Tgl Lhr</th>
                  <th>No HP</th>
                  <th>Alamat</th>
                  <th>RT/RW</th>
                  <th>Desa</th>
                  <th>Kecamatan</th>
                  <th>Kabupaten</th>
                  <th>UserName</th>
                </tr>
              </thead>
              <tbody>
                <?php
                include 'koneksi.php';
                $queryp = "SELECT a.tgl as tgl, a.id as id,a.nim, a.nik as nik, a.ttl as ttl, a.tgll as tgll, 
                          a.nama as nama, a.alamat as alamat, a.rtrw as rtrw, b.desa as desa, 
                          b.kec as kec, b.kab as kab, a.nohp as nohp,a.klsNow,a.username as usersys 
                          FROM registrasi a 
                          LEFT JOIN alamat b ON a.desa=b.k_desa AND a.kec=b.k_kec AND a.kab=b.k_kab $where";
                
                $querya = mysqli_query($koneksi, $queryp);
                if (mysqli_num_rows($querya)) {
                  $_SESSION['registrasi']['kode']['sqlr'] = $queryp;
                  $no = 0;
                  while ($data = mysqli_fetch_assoc($querya)) {
                    extract($data);
                    $no++;
                ?>
                    <tr>
                      <td align="center"><?php echo $no; ?></td>
                      <td><?php echo date('d-m-Y', strtotime($tgl)); ?></td>
                      <td><?php echo $id; ?></td>
                      <td><?php echo $nik; ?></td>
                      <td><?php echo $nama; ?></td>
                      <td><?php echo $klsNow; ?></td>
                      <td><?php echo $ttl; ?></td>
                      <td><?php echo $tgll; ?></td>
                      <td><?php echo $nohp; ?></td>
                      <td><?php echo $alamat; ?></td>
                      <td><?php echo $rtrw; ?></td>
                      <td><?php echo $desa; ?></td>
                      <td><?php echo $kec; ?></td>
                      <td><?php echo $kab; ?></td>
                      <td><?php echo $usersys; ?></td>
                    </tr>
                <?php 
                  }
                } ?>
              </tbody>
            </table>

          </div>
          <!-- /.card-body -->
        </div>
        <!-- /.card -->
      </div>
      <!-- /.col -->
    </div>
    <!-- /.row -->
</section>
<!-- /.content -->

<script type="text/javascript" charset="utf8" src="./assets/jquery.dataTables.js"></script>
<!-- <script type="text/javascript" charset="utf8" src="./assets/jquery-1.11.0.min"></script> -->
<link rel="stylesheet" type="text/css" href="./assets/jquery.dataTables.css">
 <script>
$('#sortTable').DataTable(); //https://www.sahretech.com/2020/04/cara-membuat-pencarian-sorting-dan.html?m=1/ cara buat sorter table
</script>

<link rel="stylesheet" href="calendar.css"/>
<script type="text/javascript" src="hijri-date.js"></script>
<script type="text/javascript" src="calendar.js"></script>

<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';
  }

  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';
  }
</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>

