<?php 
    require_once("../_ayarlar.php");
    require_once("../_fonksiyonlar.php");
?>
<!DOCTYPE html>
<html lang="tr">

<head>
    <?php include("_head.php") ?>
    <title>Kullanıcı Güncelle</title>
</head>

<body id="page-top">

    <!-- Page Wrapper -->
    <div id="wrapper">

        <?php include("_sidebar.php") ?>

        <!-- Content Wrapper -->
        <div id="content-wrapper" class="d-flex flex-column">

            <!-- Main Content -->
            <div id="content">

                <?php include("_topbar.php") ?>

                <!-- Sayfaya Özel İçerik -->
                <div class="container-fluid">

                    <div class="card shadow mb-4">
                        <div class="card-header py-3">
                            <h6 class="m-0 font-weight-bold text-primary">Kullanıcı Güncelle</h6>
                        </div>
                        <div class="card-body">
                        <?php

                        $id = @$_GET["id"];

                        if (isset($_POST["kullanici_guncelle"])) {

                            $yeni_ad = $_POST["ad"];
                            $yeni_soyad = $_POST["soyad"];
                            $yeni_eposta = $_POST["eposta"];
                            $yeni_dogum_yeri = $_POST["dogum_yeri"];
                            $yeni_dogum_tarihi = $_POST["dogum_tarihi"];

                            $sql = "UPDATE kullanici SET ad	= '$yeni_ad', soyad='$yeni_soyad', eposta='$yeni_eposta', dogum_yeri='$yeni_dogum_yeri', dogum_tarihi='$yeni_dogum_tarihi' WHERE id = '$id' ";
                            $sonuc = $vt->query($sql);

                            if($sonuc===TRUE){
                                echo '<div class="alert alert-success alert-dismissible fade show" role="alert">
                                Kullanıcı güncelleme işlemi başarılı.
                                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                                </div>';
                            }
                            else {
                                echo '<div class="alert alert-danger alert-dismissible fade show" role="alert">
                                Kullanıcı güncelleme işleminde hata meydana geldi.
                                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                                </div>';
                            }
                        }        
                        $sql = "SELECT * FROM kullanici WHERE id = '$id' ";
                        $sonuc = $vt->query($sql);
                        $satir = $sonuc->fetch_assoc();
                        ?>

                            <form method="post">
                                <div class="mb-3">
                                    <label for="ad" class="form-label">Adı</label>
                                    <input type="text" class="form-control" id="ad" name="ad" value="<?php echo $satir["ad"] ?>">
                                </div>
                                <div class="mb-3">
                                    <label for="soyad" class="form-label">Soyadı</label>
                                    <input type="text" class="form-control" id="soyad" name="soyad" value="<?php echo $satir["soyad"] ?>">
                                </div>
                                <div class="mb-3">
                                    <label for="eposta" class="form-label">e-Posta</label>
                                    <input type="text" class="form-control" id="eposta" name="eposta" value="<?php echo $satir["eposta"] ?>">
                                </div>
                                <div class="mb-3">
                                    <label for="dogumtarihi" class="form-label">Doğum Tarihi</label>
                                    <input type="date" class="form-control" id="dogum_tarihi" name="dogum_tarihi" value="<?php echo $satir["dogum_tarihi"] ?>">
                                </div>
                                <div class="mb-3">
                                    <label for="dogumyeri" class="form-label">Doğum Yeri</label>
                                    <select class="form-control mb-3" name="dogum_yeri">
                                        <?php

                                            $sehir_sql = "SELECT * FROM sehir";
                                            $sehir_tablo = $vt->query($sehir_sql);
                                            while ($sehir_satir = $sehir_tablo->fetch_assoc()) {
                                                if($satir["dogum_yeri"] != $sehir_satir["ad"]) echo "<option>".$sehir_satir["ad"]."</option>\n";
                                                else echo "<option selected>".$sehir_satir["ad"]."</option>\n";
                                            }

                                        ?> 
                                    </select>
                                </div>
                                <div class="form-group">
                                    <button class="btn btn-primary btn-user btn-block" name="kullanici_guncelle">Kullanıcı Güncelle</button>
                                </div>
                            </form>

                        </div>
                    </div>

                </div>
                <!-- /.container-fluid -->

            </div>
            <!-- End of Main Content -->

            <?php include("_footer.php") ?>

        </div>
        <!-- End of Content Wrapper -->

    </div>
    <!-- End of Page Wrapper -->

    <!-- Scroll to Top Button-->
    <a class="scroll-to-top rounded" href="#page-top">
        <i class="fas fa-angle-up"></i>
    </a>

    <?php include("_script.php") ?>
</body>

</html>