<?php require 'ust.php'; ?>
<main id="main" class="main">
  <section class="section dashboard">
    <div class="row">
      <div class="col-12">
        <div class="card">
          <div class="card-body">
            <h5 class="card-title"> Paket Listesi Paneli </h5>
            <div class="table-responsive">
            <table class="table table-striped datatable">
              <thead>
                <tr>
                  <th scope="col"> Paket Adı </th>
                  <th scope="col"> Paket Kredi Miktarı </th>
                  <th scope="col"> Paket Fiyatı </th>
                  <th scope="col"> Düzenle </th>
                  <th scope="col"> Sil </th>
                </tr>
              </thead>
              <tbody>
                <?php
                $getir = mysqli_query($db, "SELECT * FROM paketler");
                while ($listele = mysqli_fetch_array($getir)) {
                 ?>
                <tr>
                  <td><?php echo $listele["paket_adi"] ?></td>
                  <td><?php echo $listele["paket_kredi"]; ?></td>
                  <td><?php echo $listele["paket_fiyat"]; ?> ₺</td>
                  <td><button type="button" class="btn btn-success btn-sm" data-bs-toggle="modal" data-bs-target="#exampleModal_<?php echo $listele["id"]; ?>"><i class="bi bi-gear"></i> Düzenle</button></td>
                  <td><button onclick="uyesil_<?php echo $listele["id"]; ?>()" type="button" class="btn btn-danger btnl-sm">Sil</button></td>
                </tr>
                <div class="modal fade" id="exampleModal_<?php echo $listele["id"]; ?>" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
                  <div class="modal-dialog modal-lg">
                    <div class="modal-content">
                      <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel"><?php echo $listele["paket_adi"]; ?> Adlı Paketi Düzenleme Paneli </h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                      </div>
                      <div class="modal-body">
                        <form method="post">
                          <div class="row my-3">
                            <div class="col-lg-4">
                              <label class="form-label">Paket Adı</label>
                              <input name="paket_adi" type="text" class="form-control" value="<?php echo $listele["paket_adi"]; ?>" required>
                              <input name="paketid" type="hidden" class="form-control" value="<?php echo $listele["id"]; ?>">
                            </div>
                            <div class="col-lg-4">
                              <label class="form-label">Paket Kredi Miktarı</label>
                              <input name="paket_kredi" type="text" class="form-control" value="<?php echo $listele["paket_kredi"]; ?>" required>
                            </div>
                            <div class="col-lg-4">
                              <label class="form-label">Paket Fiyat ₺</label>
                              <input name="paket_fiyat" type="text" class="form-control" value="<?php echo $listele["paket_fiyat"]; ?>" required>
                            </div>
                          </div>
                            <button type="submit" name="paketislem" class="btn btn-primary">Uygula</button>
                          </form>
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Pencereyi Kapat</button>
                      </div>
                    </div>
                  </div>
                </div>
                <script>
                function uyesil_<?php echo $listele["id"]; ?>() {
                    swal({
                  title: "<?php echo $listele["paket_adi"]; ?> Adlı Paketi Silmek Üzeresiniz",
                  text: "Bu İşlem Geri Alınamaz Paket Silinir.",
                  icon: "warning",
                  buttons: true,
                  dangerMode: true,
                })
                .then((willDelete) => {
                  if (willDelete) {
                    swal("Paket Silindi.", {
                      icon: "success",
                    });
                    window.location.href = "postlar/get.php?paketsil=<?php echo $listele['id'] ?>";
                  } else {
                    swal("İşlem İptal Edildi.");
                  }
                });
                	}
                </script>
              <?php } ?>
              </tbody>
            </table>
          </div>
          </div>
        </div>
      </div>
    </div>
  </section>
</main>
<?php require 'footer.php'; ?>
<?php
if ($_POST) {
  $paketid = $_POST["paketid"];
  $paket_adi = $_POST["paket_adi"];
  $paket_kredi = $_POST["paket_kredi"];
  $paket_fiyat = $_POST["paket_fiyat"];

  $duzenle = mysqli_query($db, "UPDATE paketler SET paket_adi = '$paket_adi', paket_kredi = '$paket_kredi', paket_fiyat = '$paket_fiyat' WHERE id = '$paketid'");
  if ($duzenle) {
    echo '<script>swal("Başarılı", "Paket Başarılı Bir Şekilde Düzenlendi", "success");setTimeout(function(){ window.location.assign("paketliste.php");}, 1500);</script>';
  }else {
    echo '<script>swal("Hata", "Beklenemdik Bir Hata Meydana Geldi Lütfen Tekrar Deneyin.", "error");</script>';
  }
}
 ?>
