<?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">Site Sıkca Sorulan Sorular <button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#exampleModal_soruekle">Soru Ekle</button></h5>
            <div class="table-responsive">
            <table class="table table-striped datatable">
              <thead>
                <tr>
                  <th scope="col"> Soru Başlık </th>
                  <th scope="col"> Soru Cevap </th>
                  <th scope="col"> Düzenle </th>
                  <th scope="col"> Sil </th>
                </tr>
              </thead>
              <tbody>
                <?php
                $getir = mysqli_query($db, "SELECT * FROM sikcasorulan_sorular");
                while ($listele = mysqli_fetch_array($getir)) {
                 ?>
                <tr>
                  <td><?php echo $listele["soru_baslik"] ?></td>
                  <td><?php echo $listele["soru_cevap"]; ?></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="sorusil_<?php echo $listele["id"]; ?>()" type="button" class="btn btn-danger">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">
                    <div class="modal-content">
                      <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel"> Sıkca Sorulan Sorular Düzenleme </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-12">
                              <label class="form-label">Soru Başlık</label>
                              <input name="soru_baslik" type="text" class="form-control" value="<?php echo $listele["soru_baslik"]; ?>" required>
                              <input name="soruid" type="hidden" class="form-control" value="<?php echo $listele["id"]; ?>">
                            </div>
                            <div class="col-lg-12">
                              <label class="form-label">Soru Cevap</label>
                              <input name="soru_cevap" type="text" class="form-control" value="<?php echo $listele["soru_cevap"]; ?>" required>
                            </div>
                          </div>
                            <button type="submit" name="sorud" class="btn btn-primary">Düzenle</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>
                <div class="modal fade" id="exampleModal_soruekle" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                    <div class="modal-dialog" role="document">
                      <div class="modal-content">
                        <div class="modal-header">
                          <h5 class="modal-title" id="exampleModalLabel">Sıkca Sorulan Soru Ekleme Paneli</h5>
                          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                          </button>
                        </div>
                        <div class="modal-body">
                          <form method="post">
                            <div class="form-group my-3">
                              <label>Soru Başlık</label>
                              <input name="soru_baslik" type="text" class="form-control">
                            </div>
                            <div class="form-group my-3">
                              <label>Soru Cevap</label>
                              <input name="soru_cevap" type="text" class="form-control">
                            </div>
                            <button name="soruekle" type="submit" class="btn btn-primary">Ekle</button>
                          </form>
                        </div>
                        <div class="modal-footer">
                          <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                          <button type="button" class="btn btn-primary">Save changes</button>
                        </div>
                      </div>
                    </div>
                  </div>
                <script>
                function sorusil_<?php echo $listele["id"]; ?>() {
                    swal({
                  title: "<?php echo $listele["soru_baslik"]; ?> Başlıklı Soruyu Silmek Üzeresiniz",
                  text: "Bu İşlem Geri Alınamaz Soru Silinir.",
                  icon: "warning",
                  buttons: true,
                  dangerMode: true,
                })
                .then((willDelete) => {
                  if (willDelete) {
                    swal("Soru Silindi.", {
                      icon: "success",
                    });
                    window.location.href = "postlar/get.php?sorusil=<?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 (isset($_POST["sorud"])) {
  $soru_baslik = $_POST["soru_baslik"];
  $soru_cevap = $_POST["soru_cevap"];
  $soruid = $_POST["soruid"];
  $duzenle = mysqli_query($db, "UPDATE sikcasorulan_sorular SET soru_baslik = '$soru_baslik', soru_cevap = '$soru_cevap' WHERE id = '$soruid'");
  if ($duzenle) {
    echo '<script>swal("Başarılı", "Soru Başarılı Bir Şekilde Düzenlendi", "success");setTimeout(function(){ window.location.assign("sksorular.php");}, 1500);</script>';
  }else {
    echo '<script>swal("Hata", "Beklenmedik Bir Hata Meydana Geldi Lütfen Tekrar Deneyin.", "error");</script>';
  }
}
else if(isset($_POST["soruekle"])){
  $soru_baslikekle = $_POST["soru_baslik"];
  $soru_cevapekle = $_POST["soru_cevap"];
  $ekle = mysqli_query($db, "INSERT INTO sikcasorulan_sorular(soru_baslik,soru_cevap)VALUES('$soru_baslikekle','$soru_cevapekle')");
  if ($ekle) {
    echo '<script>swal("Başarılı", "Soru Başarılı Bir Şekilde Eklendi", "success");setTimeout(function(){ window.location.assign("sksorular.php");}, 1500);</script>';
  }else {
    echo '<script>swal("Hata", "Beklenmedik Bir Hata Meydana Geldi Lütfen Tekrar Deneyin.", "error");</script>';
  }
}
 ?>
