<?php
include '../../config/conn.php';
if ($_GET['aksi'] == '') { ?>
  <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>Data Pekerja <small></small></h2>

              <div class="clearfix"></div>
            </div>
            <a class="btn btn-success btn-sm" data-toggle="modal" data-target="#modalAdd"><i class="glyphicon glyphicon-plus"></i>Tambah</a>
            <div class="divider-dashed"></div>
            <div class="x_content">

              <table id="datatable" class="table table-striped table-bordered">
                <thead>
                  <tr>
                    <th width="50">No</th>
                    <th>ID Akun</th>
                    <th>Nama</th>
                    <th>Alamat</th>
                    <th>Hutang Sebelum</th>
                    <th width="50">Aksi</th>
                  </tr>
                </thead>
                <tbody>
                  <?php
                  $no = 0;
                  $query = mysqli_query($conn, "SELECT * FROM pekerja ORDER BY nama_pekerja asc");
                  while ($row = mysqli_fetch_array($query)) {
                    $no++;
                  ?>
                    <tr>
                      <td><?php echo $no; ?></td>
                      <td><?php echo $row['id_pekerja']; ?></td>
                      <td><?php echo $row['nama_pekerja']; ?></td>
                      <td><?php echo $row['alamat']; ?></td>
                      <td><?php echo rupiah($row['hutang_sebelum']); ?></td>
                      <td><a class='open_modal btn  btn-default btn-xs' href="?module=<?php echo $_GET['module']; ?>&aksi=edit&id=<?php echo $row[id_pekerja]; ?>"><i class="glyphicon glyphicon-edit"></i> Edit</a> </td>
                    </tr>
                  <?php } ?>
                </tbody>
              </table>
            </div>
          </div>
        </div>

      </div>
    </div>
  </div>
  </div>
  </div>
  </div><br>

  <!-- Modal -->
  <div class="modal fade" id="modalAdd" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title" id="myModalLabel">Tambah Data Pekerja</h4>
        </div>
        <div class="modal-body">
          <!-- start form for validation -->
          <form action="?module=<?php echo $_GET['module']; ?>&aksi=simpan" enctype="multipart/form-data" method="POST">

            <label for="nama">Nama * :</label>
            <input type="text" class="form-control" name="nama_pekerja" autocomplete="off" required />

            <label for="alamat">Alamat * :</label>
            <textarea class="form-control" name="alamat" autocomplete="off" requerid> </textarea>

            <label for="telephone">Hutang Sebelumnya:</label>
            <input type="number" class="form-control" name="hutang_sebelum" autocomplete="off" />
          
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default btn-sm" data-dismiss="modal">Batal</button>
          <button type="submit" class="btn btn-success btn-sm">Simpan</button>
        </div>
        </form>
      </div>
    </div>
  </div>


<?php  } elseif ($_GET['aksi'] == 'edit') {

  $id = $_GET['id'];
  $query = mysqli_query($conn, "SELECT * FROM pekerja WHERE id_pekerja='$id'");
  $r = mysqli_fetch_array($query);

?>

  <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>Edit Data <?php echo $_GET['module']; ?></h2>
              <div class="clearfix"></div>
            </div>
            <div class="x_content">
              <form action="?module=pekerja&aksi=simpan_edit" enctype="multipart/form-data" method="POST">
                <div class="row">

                  <div class="col-md-6">
                    <label for="id">ID Pekerja :</label>
                    <input type="text" class="form-control" disabled value="<?php echo $r['id_pekerja']; ?>" />
                    <input type="hidden" class="form-control" name="id_pekerja" value="<?php echo $r['id_pekerja']; ?>" />

                      <label for="nama">Nama * :</label>
                        <input type="text" class="form-control" name="nama_pekerja" autocomplete="off" value="<?php echo $r['nama_pekerja']; ?>" required />
            
                        <label for="alamat">Alamat * :</label>
                        <textarea class="form-control" name="alamat" autocomplete="off"><?php echo $r['alamat']; ?></textarea>
            
                        <label for="telephone">Hutang Sebelumnya:</label>
                        <input type="number" class="form-control" name="hutang_sebelum" value="<?php echo $r['hutang_sebelum']; ?>" autocomplete="off" />
                  </div>

                </div>
                <div class="ln_solid"></div>
                <div class="form-group">
                  <button type="button" class="btn btn-default btn-sm" onclick=self.history.back()>Batal</button>
                  <button type="submit" class="btn btn-success btn-sm">Simpan</button><br>
                </div>
              </form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

<?php } elseif ($_GET['aksi'] == 'simpan_edit') {
  $module = $_GET['module'];
  $password   = md5($_POST[password]);

    mysqli_query($conn, "UPDATE pekerja SET nama_pekerja = '$_POST[nama_pekerja]',
                                    alamat = '$_POST[alamat]',
                                    hutang_sebelum = '$_POST[hutang_sebelum]' 
                                    WHERE id_pekerja = '$_POST[id_pekerja]'");
  
  echo "<script language='javascript'>document.location='?module=" . $module . "';</script>";
} elseif ($_GET['aksi'] == 'simpan') {
  $module = $_GET['module'];
  mysqli_query($conn, "INSERT INTO pekerja(
                                  nama_pekerja,
                                  alamat,
                                  hutang_sebelum) VALUES(
                                                 '$_POST[nama_pekerja]',
                                                 '$_POST[alamat]',
                                                 '$_POST[hutang_sebelum]')");

  echo "<script language='javascript'>document.location='?module=" . $module . "';</script>";
} elseif ($_GET['aksi'] == 'hapus') {
  $module = $_GET['module'];
  $id = $_GET['id'];
  $query = mysqli_query($conn, "Delete FROM pekerja WHERE id_pekerja='$id'");
  echo "<script language='javascript'>document.location='?module=" . $module . "';</script>";
}
?>

<div class="modal fade" id="modal_delete">
  <div class="modal-dialog">
    <div class="modal-content" style="margin-top:100px;">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" style="text-align:center;">Apakah anda yakin menghapus data ini ?</h4>
      </div>
      <div class="modal-footer" style="margin:0px; border-top:0px; text-align:center;">
        <a href="#" class="btn btn-danger btn-sm" id="delete_link">Hapus</a>
        <button type="button" class="btn btn-success btn-sm" data-dismiss="modal">Batal</button>
      </div>
    </div>
  </div>
</div>