<?php
include '../../config/conn.php';
if ($_GET['aksi'] == '') {
  $jenis = $_GET['type'];
  //saldo aman
  $saldoaman = mysqli_query($conn, "SELECT * FROM pengaturan_emoney WHERE id='1'");
  $aman = mysqli_fetch_array($saldoaman);

?>
  <!-- page content -->
  <div class="col" role="main">
    <div class="">
      <div class="clearfix"></div>

      <div class="row">
        <div class="col-md-12 col-sm-12 col-xs-12">
          <div class="x_panel">
            <div class="x_title" style="text-transform: capitalize;">

              <h2>PILIH DATA PEKERJA <small></small></h2>

              <div class="clearfix"></div>






            </div>
            <?php
       
              $link = "?module=transaksi-piutang";
            
            ?>




            <div class="x_content">

              <table id="datatable" class="table table-bordered">
                <thead>
                  <tr style="color: black;">
                    <th width="15px">NO.</th>
                    <th>Id Pekerja</th>
                    <th>Nama </th>
                    <th>Alamat</th>
                    <th>Jumlah Piutang</th>
                    <th width="100">Pilih</th>
                  </tr>
                </thead>
                <tbody>
                  <?php
                  $no = 0;
                  $count = 1;
                  $total_rekap = 0;

                  $hari_ini = "$thn-$bln-01";
                  $tgl_pertama = date('Y-m-01', strtotime($hari_ini));
                  $tgl = date('Y-m-t', strtotime($hari_ini));


                  //sorting data per jenis status saldo

                    $query = mysqli_query($conn, "SELECT *,pekerja.nama_pekerja, 
                   COALESCE(SUM(CASE WHEN transaksi.id_jenis = 24 THEN transaksi.kredit - transaksi.debit ELSE 0 END), 0) AS jumlah_piutang
            FROM pekerja 
            LEFT OUTER JOIN transaksi ON pekerja.id_pekerja = transaksi.id_piutang
            GROUP BY pekerja.id_pekerja, pekerja.nama_pekerja
            ORDER BY pekerja.nama_pekerja ASC");
            
                  $ro = 0;

                   

                    $akumulasi_hutang=0;
                  while ($row = mysqli_fetch_array($query)) {
                    $total_hutang=$row['jumlah_piutang']+$row['hutang_sebelum'];
                    $akumulasi_hutang=$akumulasi_hutang+$total_hutang;
                    $no++;
                  ?>

                    <tr style="background: <?php if ($no % 2 == 1) { ?>
                          #ececec;
                          <?php } else { ?>
                            white;
                            <?php } ?>">

                      <!--<td><?php //echo $row['id_transaksi'];
                              ?></td>-->
                      <td style=" color:black"><?php echo $no; ?></td>
                      <td style=" color:black"><?php echo $row['id_pekerja']; ?></td>
                      <td style=" color:black"><?php echo $row['nama_pekerja']; ?></td>
                      <td style=" color:black"><?php echo $row['alamat']; ?></td>
                      <td style="font-weight: bold; color:black;font-size:medium"><?php echo rupiah($total_hutang); ?></td>

                      <td>
                        <a class='open_modal btn  btn-primary btn-xs' href="?module=proses-piutang&id=<?php echo $row['id_pekerja']; ?>"><i class="glyphicon glyphicon-edit"></i> Pilih</a>
                      </td>




                      <?php
                      $count++

                      ?>

                    </tr>

                  <?php } ?>
                  
                  
                





                </tbody>
                <tr style="color: RED;">
                    <th width="15px">---</th>
                    <th>---</th>
                    <th>---</th>
                    <th>TOTAL</th>
                    <th><?php echo rupiah($akumulasi_hutang); ?></th>
                    <th width="100">---</th>
                  </tr>
              </table>
            </div>
          </div>
        </div>

      </div>
    </div>
  </div>
  <!-- /page content --><br>





<?php } ?>