<?php


include 'inc/function.php';

?>
<div class="col-12">
  <div class="card">
             <div class="card-header">
               <h3 class="card-title">MANAGEMENT DATA DEVELOPER</h3>
               <button type="button" class="btn btn-info float-right" data-toggle="modal" data-target="#tambah-developer">
                 <i class="nav-icon fas fa-plus"></i> DEVELOPER
               </button>
             </div>
             <!-- /.card-header -->
             <div class="card-body">
               <table id="example1" class="table table-bordered table-striped">
                 <thead>
                   <tr>
                     <th>NO</th>
                     <th>NAMA DEVELOPER</th>
                     <th></th>
                   </tr>
                 </thead>
                 <tbody>
                   <?php
                   include 'inc/koneksi.php';
                   $query = $link->query("SELECT * FROM tb_developer ORDER BY id_developer DESC");
                   $no =1;
                   while($data=mysqli_fetch_array($query)){
                   ?>
                   <tr>
                     <td><?php echo $no++; ?></td>
                     <td><?php echo $data['nama_developer']; ?></td>
                     <td>
                       <center>
                        <button class="btn-xs btn-danger btn-sm" data-toggle="modal" data-target="#delete-developer<?php echo $data['id_developer']; ?>"><i class="fas fa-trash nav-icon"></i></button>
                      </center>
                     </td>
                     <!-- Modal Hapus -->
                <div class="modal fade" id="delete-developer<?php echo $data['id_developer']; ?>" tabindex="-1">
                 <div class="modal-dialog">
                   <div class="modal-content bg-danger">
                     <div class="modal-header">
                       <h4 class="modal-title">Konfirmasi Hapus Data</h4>
                       <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                         <span aria-hidden="true">&times;</span>
                       </button>
                     </div>
                     <div class="modal-body">
                       <p>Apakah anda yakin ingin menghapus data ini ?</p>
                     </div>
                     <form action="" method="post">
                     <div class="modal-footer justify-content-between">
                       <input type="hidden" name="id_developer" value="<?php echo $data['id_developer']; ?>">
                       <button type="button" class="btn btn-outline-light" data-dismiss="modal">Batal</button>
                       <button type="submit" name="delete" class="btn btn-outline-light">Hapus</button>
                    </form>
                     </div>
                   </div>
                   <!-- /.modal-content -->
                 </div>
                 <!-- /.modal-dialog -->
               </div>
               <!-- /.modal -->

               <div class="modal fade" id="edit-developer<?php echo $data['id_developer']; ?>" tabindex="-1">
                <div class="modal-dialog">
                  <div class="modal-content bg-warning">
                    <div class="modal-header">
                      <h4 class="modal-title">Edit Data developer</h4>
                      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                      </button>
                    </div>
                    <div class="modal-body">
                      <div class="col-md-12">
                      <!-- general form elements -->
                        <form method="POST">
                          <div class="card-body">
                            <div class="form-group">
                              <label for="exampleInputDeveloper">Nama developer</label>
                              <input type="hidden" class="form-control" name="id_developer" required value="<?php echo $data['id_developer']; ?>">
                              <select class="select form-control" name='id_developer'>
                                <option selected><?php echo $data['nama_developer']; ?></option>
                                <?php
                                $qs="select * from tb_developer";
                                $rs=mysqli_query($link,$qs);
                                while ($data_developer=mysqli_fetch_array($rs)){
                                ?>
                                <option value="<?php echo $data_developer['id_developer'];?>"><?php echo $data_developer['nama_developer'];?></option>
                              <?php }?>
                              <select>
                          </div>
                              </div>
                              <div class="modal-footer justify-content-between">
                                <button type="button" class="btn btn-outline-dark" data-dismiss="modal">Batal</button>
                                <button type="submit" name="edit" class="btn btn-outline-dark">Update</button>
                              </div>
                                </form>
                            </div>
                            <!-- /.modal-content -->
                          </div>
                          <!-- /.modal-dialog -->
                        </div>
                        <!-- /.modal -->

                   </tr>
                   <?php } ?>
                 </tbody>
               </table>

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

           <!-- Modal Tambah -->
           <div class="modal fade" id="tambah-developer" tabindex="-1">
            <div class="modal-dialog">
              <div class="modal-content">
                <div class="modal-header">
                  <h4 class="modal-title">Tambah Data developer</h4>
                  <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                  </button>
                </div>
                <div class="modal-body">
                  <div class="col-md-12">
                  <!-- general form elements -->
                      <form method="POST">
                      <div class="card-body">
                        <div class="form-group">
                          <label for="exampleInput">developer</label>
                          <input type="text" class="form-control" name="nama_developer" placeholder="" required>
                        </div>
                      </div>
                          </div>
                          <div class="modal-footer justify-content-between">
                            <button type="button" class="btn btn-outline-dark" data-dismiss="modal">Batal</button>
                            <button type="submit" name="submit" class="btn btn-outline-dark">Simpan</button>
                          </div>
                            </form>
                        </div>
                        <!-- /.modal-content -->
                      </div>
                      <!-- /.modal-dialog -->
                    </div>
                    <!-- /.modal -->


           <?php
      if(isset($_POST['submit'])){
          $id_developer = $_POST['id_developer'];
          $nama_developer = $_POST['nama_developer'];

          $query = $link->query("INSERT INTO tb_developer VALUES('','$nama_developer')")or die(mysqli_error($link));
          if($query){
              echo "<script>alert('Berhasil Menambah!'); window.location.href='?page=developer';</script>";
          }
      }

      if(isset($_POST['delete'])){
          $id_developer = $_POST['id_developer'];

          $query = $link->query("DELETE FROM tb_developer WHERE id_developer='$id_developer'")or die(mysqli_error($link));
          if($query){
              echo "<script>alert('Berhasil Menghapus!'); window.location.href='?page=developer';</script>";
          }
      }

      if(isset($_POST['edit'])){
        $id_developer = $_POST['id_developer'];
        $nama_developer = $_POST['nama_developer'];


          $query = $link->query("UPDATE tb_developer SET nama_developer='$nama_developer' WHERE id_developer='$id_developer'")or die(mysqli_error($link));
          if($query){
              echo "<script>alert('Berhasil Mengubah Data!'); window.location.href='?page=developer';</script>";
          }
      }


      ?>
      </table>
