<?php


include 'inc/function.php';

?>
<div class="col-12">
  <div class="card">
             <div class="card-header">
               <h3 class="card-title">DATA COVER</h3>
               <button type="button" class="btn btn-info float-right" data-toggle="modal" data-target="#tambah-asosiasi">
                 <i class="nav-icon fas fa-plus"></i> Data
               </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>CRO</th>
                     <th>TYPE COVER</th>
                     <th>KONDISI</th>
                     <th></th>
                   </tr>
                 </thead>
                 <tbody>
                   <?php
                   include 'inc/koneksi.php';
                   $query = $link->query("SELECT * FROM tb_asosiasi ORDER BY id_asosiasi DESC");
                   $no =1;
                   while($data=mysqli_fetch_array($query)){
                   ?>
                   <tr>
                     <td><?php echo $no++; ?></td>
                     <td><?php echo $data['nama_asosiasi']; ?></td>
                     <td>
                       <center>
                        <button class="btn-xs btn-danger btn-sm" data-toggle="modal" data-target="#delete-asosiasi<?php echo $data['id_asosiasi']; ?>"><i class="fas fa-trash nav-icon"></i></button>
                      </center>
                     </td>
                     <!-- Modal Hapus -->
                <div class="modal fade" id="delete-asosiasi<?php echo $data['id_asosiasi']; ?>" 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_asosiasi" value="<?php echo $data['id_asosiasi']; ?>">
                       <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-asosiasi<?php echo $data['id_asosiasi']; ?>" tabindex="-1">
                <div class="modal-dialog">
                  <div class="modal-content bg-warning">
                    <div class="modal-header">
                      <h4 class="modal-title">Edit Data Asosiasi</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 asosiasi</label>
                              <input type="hidden" class="form-control" name="id_asosiasi" required value="<?php echo $data['id_asosiasi']; ?>">
                              <select class="select form-control" name='id_asosiasi'>
                                <option selected><?php echo $data['nama_asosiasi']; ?></option>
                                <?php
                                $qs="select * from tb_asosiasi";
                                $rs=mysqli_query($link,$qs);
                                while ($data_asosiasi=mysqli_fetch_array($rs)){
                                ?>
                                <option value="<?php echo $data_asosiasi['id_asosiasi'];?>"><?php echo $data_asosiasi['nama_asosiasi'];?></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-asosiasi" tabindex="-1">
            <div class="modal-dialog">
              <div class="modal-content">
                <div class="modal-header">
                  <h4 class="modal-title">Tambah Data Cover</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>
                          <div class="form-group">
                          <label for="exampleInputNasabah">TID</label>
                          <input type="text" class="form-control" name="nama_nasabah" placeholder="" required>
                        </div>
                         <div class="form-group">
                          <label for="exampleInputNasabah">KANCA</label>
                          <input type="text" class="form-control" name="nama_nasabah" placeholder="" required>
                        </div>
                        <div class="form-group">
                          <label for="exampleInputNasabah">LOKASI</label>
                          <input type="text" class="form-control" name="nama_nasabah" placeholder="" required>
                        </div>
                        <div class="form-group">
                          <label for="exampleInputNasabah">CRO</label>
                          <select class="select form-control" name='id_cro'>
                            <option disabled selected>=== PILIH CRO ===</option>
                            <?php
                            $qcro="select * from tb_cro";
                            $rcro=mysqli_query($link,$qcro);
                            while ($data_kanca=mysqli_fetch_array($rcro)){
                            ?>
                            <option value="<?php echo $data_kanca['id_cro'];?>"><?php echo $data_kanca['nama_cro'];?></option>
                          <?php }?>
                          <select>
                        </div>
                        <div class="form-group">
                          <label for="exampleInputNasabah">TYPE COVER</label>
                          <input type="text" class="form-control" name="nama_nasabah" placeholder="" required>
                        </div>
                        <div class="form-group">
                          <label for="formFile" class="form-label">KONDISI - UPLOAD FOTO</label>
                          <input class="form-control" type="file" id="formFile">
                        </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_asosiasi = $_POST['id_asosiasi'];
          $nama_asosiasi = $_POST['nama_asosiasi'];

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

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

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

      if(isset($_POST['edit'])){
        $id_asosiasi = $_POST['id_asosiasi'];
        $nama_asosiasi = $_POST['nama_asosiasi'];


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


      ?>
      </table>
