<?php


include 'inc/function.php';

?>
<div class="col-12">
  <div class="card">
             <div class="card-header">
               <h3 class="card-title">MANAGEMENT DATA </h3>
               <button type="button" class="btn btn-info float-right" data-toggle="modal" data-target="#tambah-kanca">
                 <i class="nav-icon fas fa-plus"></i> TID
               </button>
             </div>
             <!-- /.card-header -->
             <div class="card-body">
               <table id="example1" class="table table-bordered table-striped">
                 <thead>
                   <tr>
                     <th>NO</th>
                     <th>TID</th>
                     <th>KANCA</th>
                     <th>LOKASI</th>
                     <th></th>
                   </tr>
                 </thead>
                 <tbody>
                   <?php
                   include 'inc/koneksi.php';
                   $query = $link->query("SELECT * FROM tb_atm ORDER BY id_atm DESC");
                   $no =1;
                   while($data=mysqli_fetch_array($query)){
                   ?>
                   <tr>
                     <td><?php echo $no++; ?></td>
                     <td><?php echo $data['TID']; ?></td>
                     <td><?php echo $data['kanca']; ?></td>
                     <td><?php echo $data['lokasi']; ?></td>
                     <td>
                       <center>
                        <button class="btn-xs btn-danger btn-sm" data-toggle="modal" data-target="#delete-kanca<?php echo $data['id_atm']; ?>"><i class="fas fa-trash nav-icon"></i></button>
                      </center>
                     </td>
                     <!-- Modal Hapus -->
                <div class="modal fade" id="delete-kanca<?php echo $data['id_kanca']; ?>" 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_kanca" value="<?php echo $data['id_kanca']; ?>">
                       <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-kanca<?php echo $data['id_kanca']; ?>" tabindex="-1">
                <div class="modal-dialog">
                  <div class="modal-content bg-warning">
                    <div class="modal-header">
                      <h4 class="modal-title">Edit Data Kanca</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 Kanca</label>
                              <input type="hidden" class="form-control" name="id_kanca" required value="<?php echo $data['id_kanca']; ?>">
                              <select class="select form-control" name='id_kanca'>
                                <option selected><?php echo $data['nama_kanca']; ?></option>
                                <?php
                                $qs="select * from tb_kanca";
                                $rs=mysqli_query($link,$qs);
                                while ($data_kanca=mysqli_fetch_array($rs)){
                                ?>
                                <option value="<?php echo $data_kanca['id_kanca'];?>"><?php echo $data_kanca['nama_kanca'];?></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-kanca" tabindex="-1">
            <div class="modal-dialog">
              <div class="modal-content">
                <div class="modal-header">
                  <h4 class="modal-title">Tambah Data TID</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">TID</label>
                          <input type="text" class="form-control" name="TID" placeholder="" required>
                        </div>
                        <div class="form-group">
                          <label for="exampleInput">KANCA</label>
                          <input type="text" class="form-control" name="kanca" placeholder="" required>
                        </div>
                        <div class="form-group">
                          <label for="exampleInput">LOKASI</label>
                          <input type="text" class="form-control" name="lokasi" 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_atm = $_POST['id_atm'];
          $TID = $_POST['TID'];
          $kanca = $_POST['kanca'];
          $lokasi = $_POST['lokasi'];



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

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

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

      if(isset($_POST['edit'])){
        $id_kanca = $_POST['id_kanca'];
        $nama_kanca = $_POST['nama_kanca'];


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


      ?>
      </table>
