<?php include 'inc/header.php';?>
<?php include 'inc/sidebar.php';?>
<?php include 'inc/navbar.php';?>


<?php
if (isset($_POST['ekle'])) {  
  $name_surname = $_POST['name_surname'];
  $email = $_POST['email'];
  $phone = $_POST['phone'];
  $username = $_POST['username'];
  $password = $_POST['password'];
  if ($name_surname <> "" && $email <> "" && $phone <> "" && $username <> "" && $password <> "" ) {

    $satir = [ 
      'name_surname' => $name_surname,
      'email' => $email,
      'phone' => $phone, 
      'username' => $username, 
      'password' => $password, 
  ];
  $sql = "INSERT INTO admin SET name_surname=:name_surname, email=:email, phone=:phone, username=:username, password=:password;";
  /** @var PDO $dbh */
  $durum = ($dbh->prepare($sql))->execute($satir);
  if ($durum) {
    $sonId = $dbh->lastInsertId();
    echo "<script>
    const Toast = Swal.mixin({
        toast: true,
        position: 'top-end',
        showConfirmButton: false,
        timer: 6000,
        timerProgressBar: true,
        didOpen: (toast) => {
            toast.addEventListener('mouseenter', Swal.stopTimer)
            toast.addEventListener('mouseleave', Swal.resumeTimer)
        }
        })

        Toast.fire({
            icon: 'success',
            title: 'Başarı ile kayıt edildi.'
            })
            </script>";
        }
    }else{
        echo "Tüm alanları doldurunuz";

    }
}
?>


<?php
if (isset($_POST['duzenle'])) {  
  $id = $_POST['id'];
  $name_surname = $_POST['name_surname'];
  $email = $_POST['email'];
  $phone = $_POST['phone'];
  $username = $_POST['username'];
  $password = $_POST['password'];
  if ($name_surname <> "" && $email <> "" && $phone <> "" && $username <> "" && $password <> "" ) {

    $satir = [ 
      'name_surname' => $name_surname,
      'email' => $email,
      'phone' => $phone, 
      'username' => $username, 
      'password' => $password, 
      'id' => $id, 
  ];
  $sql = "UPDATE admin SET name_surname=:name_surname, email=:email, phone=:phone, username=:username, password=:password where id=:id;";
  /** @var PDO $dbh */



  $durum = ($dbh->prepare($sql))->execute($satir);
  if ($durum) {
    $sonId = $dbh->lastInsertId();
    echo "<script>
    const Toast = Swal.mixin({
        toast: true,
        position: 'top-end',
        showConfirmButton: false,
        timer: 6000,
        timerProgressBar: true,
        didOpen: (toast) => {
            toast.addEventListener('mouseenter', Swal.stopTimer)
            toast.addEventListener('mouseleave', Swal.resumeTimer)
        }
        })

        Toast.fire({
            icon: 'success',
            title: 'Başarı ile kayıt edildi.'
            })
            </script>";
        }
    }else{
        echo "Tüm alanları doldurunuz";

    }
}
?>

<!-- Page Content-->
<div class="page-content">
    <div class="container-fluid">
        <!-- Page-Title -->
        <div class="row">
            <div class="col-sm-12">
                <div class="page-title-box">
                    <div class="row">
                        <div class="col">
                            <h4 class="page-title">Yönetici Listesi</h4>
                            <ol class="breadcrumb">
                                <li class="breadcrumb-item"><a href="anasayfa">Anasayfa</a></li> 
                                <li class="breadcrumb-item active">Yönetici Listesi</li>
                            </ol>

                        </div><!--end col--> 
                        <div class="col-auto align-self-center"> 
                            <button type="button" class="btn btn-outline-primary waves-effect waves-light" data-toggle="modal" data-target="#ekle"> <i data-feather="plus-square"></i></i> Yönetici Ekle</button>   
                        </div><!--end col-->  
                    </div><!--end row-->                                                              
                </div><!--end page-title-box-->
            </div><!--end col-->
        </div><!--end row-->
        <!-- end page title end breadcrumb -->
        <style type="text/css">
            i { 
                font-size: 16px;
            }
        </style>
        <div class="row"> 
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-header">
                        <h4 class="card-title">Yönetici Listesi</h4>
                        <p class="text-muted mb-0">Değerli kullanıcı, bu alan sayesinde sisteme kayıtlı yöneticilerin listesini görebilirsiniz.(Sadece ana adminler görebilir)
                        </p>
                    </div><!--end card-header-->
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-bordered mb-0 table-centered">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>İsim Soyisim</th>
                                        <th>Kullanıcı Adı</th>
                                        <th>Mail Adresi</th>
                                        <th>Telefon</th>
                                        <th>Tarih</th> 
                                        <th>İşlemler</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $ebultensorgu  = $dbh->prepare("SELECT * FROM admin ");
                                    $ebultensorgu-> execute(array());
                                    $ebultensorgu  = $ebultensorgu->fetchAll(PDO::FETCH_ASSOC);

                                    foreach ($ebultensorgu as $ebultensonuc) {

                                        $sube  = $dbh->prepare("SELECT * FROM branches where id=? ");
                                        $sube-> execute(array($ebultensonuc['branch']));
                                        $sube  = $sube->fetch(PDO::FETCH_ASSOC);

                                        $id = $ebultensonuc['id'];
                                        $name = $ebultensonuc['name_surname']; 
                                        $username = $ebultensonuc['username']; 
                                        $mail = $ebultensonuc['email'];  
                                        $branch = $ebultensonuc['branch'];  
                                        $phone = $ebultensonuc['phone']; 
                                        $tarih = $ebultensonuc['date'];
                                        ?>
                                        <tr>
                                            <td style="width: 10px; height: 10px;">#<?=$id;?></td>
                                            <td><?=$name;?></td>
                                            <td><?=$username;?></td>
                                            <td><?=$mail;?></td>
                                            <td><?=$phone;?></td>
                                            <td><?=$tarih?></td>

                                            <td style="width: 150px; ">
                                                <button type="button" class="btn btn-outline-primary btn-icon-circle-sm" data-toggle="modal" data-target="#exampleModalDanger1<?=$ebultensonuc['id'];?>"><i class="ti-settings"></i></button>

                                                <button type="button" class="btn btn-outline-danger btn-icon-circle-sm" data-toggle="modal" data-target="#exampleModalDanger<?=$ebultensonuc['id'];?>" ><i class="ti-trash"></i></button> 

                                            </td> 
                                        </tr>

                                        <div class="modal fade" id="exampleModalDanger1<?=$ebultensonuc['id'];?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalDanger1<?=$ebultensonuc['id'];?>" aria-hidden="true">
                                            <div class="modal-dialog" role="document">
                                                <div class="modal-content">
                                                    <div class="modal-header bg-primary">
                                                        <h6 class="modal-title m-0 text-white" id="exampleModalDanger1<?=$ebultensonuc['id'];?>">Mesaj Detayı</h6>
                                                        <button type="button" class="close " data-dismiss="modal" aria-label="Close">
                                                            <span aria-hidden="true"><i class="la la-times text-white"></i></span>
                                                        </button>
                                                    </div><!--end modal-header-->
                                                    <form method="post">
                                                        <div class="modal-body">
                                                            <div class="row">
                                                                <div class="col-xl-6"> 
                                                                    <div class="form-group">
                                                                        <input type="hidden" name="id" value="<?=$id?>">
                                                                        <label for="exampleInputEmail1">İsim Soyisim: * </label>
                                                                        <input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="name_surname" required="" value="<?=$name?>"> 
                                                                    </div>
                                                                </div>

                                                                <div class="col-xl-6"> 
                                                                    <div class="form-group">
                                                                        <label for="exampleInputEmail1">Mail Adresi * </label>
                                                                        <input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="email" required="" value="<?=$mail?>">  
                                                                    </div>
                                                                </div>

                                                                <div class="col-xl-6"> 
                                                                    <div class="form-group">
                                                                        <label for="exampleInputEmail1">Telefon No * </label>
                                                                        <input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="phone" required="" value="<?=$phone?>">  
                                                                    </div>
                                                                </div>

                                                                <div class="col-xl-6"> 
                                                                    <div class="form-group">
                                                                        <label for="exampleInputEmail1">Kullanıcı Adı* </label>
                                                                        <input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="username" required="" value="<?=$username?>">   
                                                                    </div>
                                                                </div>
                                                                <div class="col-xl-12"> 
                                                                    <div class="form-group">
                                                                        <label for="exampleInputEmail1">Şifre* </label>
                                                                        <input type="password" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="password" required="" >
                                                                    </div>
                                                                </div>

                                                            </div>  

                                                        </div><!--end modal-body-->
                                                        <div class="modal-footer">
                                                            <button type="button" class="btn btn-secondary btn-sm" data-dismiss="modal">Kapat</button>
                                                            <button type="submit" class="btn btn-danger btn-sm" name="duzenle">Kaydet</button>



                                                        </div><!--end modal-footer-->
                                                    </form>
                                                </div><!--end modal-content-->
                                            </div><!--end modal-dialog-->
                                        </div><!--end modal-->

                                        <!-- silme başlangıçı -->

                                        <div class="modal fade" id="exampleModalDanger<?=$ebultensonuc['id'];?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalDanger<?=$ebultensonuc['id'];?>" aria-hidden="true">
                                            <div class="modal-dialog" role="document">
                                                <div class="modal-content">
                                                    <div class="modal-header bg-danger">
                                                        <h6 class="modal-title m-0 text-white" id="exampleModalDanger<?=$ebultensonuc['id'];?>"><?=$ebultensonuc['title'];?></h6>
                                                        <button type="button" class="close " data-dismiss="modal" aria-label="Close">
                                                            <span aria-hidden="true"><i class="la la-times text-white"></i></span>
                                                        </button>
                                                    </div><!--end modal-header-->
                                                    <div class="modal-body">
                                                        <div class="row"> 
                                                            <div class="col-lg-12">
                                                                <h5>Bu veriyi silmek istiyormusunuz ?</h5>
                                                                <span class="badge bg-soft-secondary">Eklenme Tarihi:</span>
                                                                <small class="text-muted ml-2"> <?=$mysqldate?></small>
                                                                <ul class="mt-3 mb-0">
                                                                    <li>Bu veri silinirse geri döndürülemez.</li> 
                                                                </ul>
                                                            </div><!--end col-->
                                                        </div><!--end row-->                                                     
                                                    </div><!--end modal-body-->
                                                    <div class="modal-footer">
                                                        <button type="button" class="btn btn-secondary btn-sm" data-dismiss="modal">Kapat</button>
                                                        <a href="islem.php?id=<?=$ebultensonuc['id']; ?>&islem=yonetici-sil"><button type="button" class="btn btn-danger btn-sm">Evet, Sil</button></a>
                                                    </div><!--end modal-footer-->
                                                </div><!--end modal-content-->
                                            </div><!--end modal-dialog-->
                                        </div><!--end modal-->
                                    <?php } ?>
                                    <?php 
                                    if(count($ebultensorgu)==0){
                                        echo "<tr><td colspan='7'><center><br>Herhangi bir veri bulunamadı <br> <img src='assets/images/sad.png' width='30px;' ></center></td></tr>";
                                    }

                                    ?>

                                </tbody>
                            </table><!--end /table-->
                        </div><!--end /tableresponsive-->
                    </div><!--end card-body-->
                </div><!--end card-->
            </div> <!-- end col -->
        </div> <!-- end row -->


        <div class="modal fade" id="ekle" tabindex="-1" role="dialog" aria-labelledby="ekle" aria-hidden="true">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header bg-danger">
                        <h6 class="modal-title m-0 text-white" id="exampleModalDanger11">Yeni Yönetici Ekle</h6>
                        <button type="button" class="close " data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true"><i class="la la-times text-white"></i></span>
                        </button>
                    </div><!--end modal-header-->
                    <form method="post">

                        <div class="modal-body">
                            <div class="row">
                                <div class="col-xl-6"> 
                                    <div class="form-group">
                                        <label for="exampleInputEmail1">İsim Soyisim: * </label>
                                        <input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="name_surname" required=""> 
                                    </div>
                                </div>

                                <div class="col-xl-6"> 
                                    <div class="form-group">
                                        <label for="exampleInputEmail1">Mail Adresi * </label>
                                        <input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="email" required=""> 
                                    </div>
                                </div>

                                <div class="col-xl-6"> 
                                    <div class="form-group">
                                        <label for="exampleInputEmail1">Telefon No * </label>
                                        <input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="phone" required=""> 
                                    </div>
                                </div>

                                <div class="col-xl-6"> 
                                    <div class="form-group">
                                        <label for="exampleInputEmail1">Kullanıcı Adı* </label>
                                        <input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="username" required=""> 
                                    </div>
                                </div>
                                <div class="col-xl-12"> 
                                    <div class="form-group">
                                        <label for="exampleInputEmail1">Şifre* </label>
                                        <input type="password" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" name="password" required=""> 
                                    </div>
                                </div>

                            </div>  

                        </div><!--end modal-body-->
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary btn-sm" data-dismiss="modal">Kapat</button>
                            <button type="submit" name="ekle" class="btn btn-danger btn-sm">Oluştur</button>
                        </div><!--end modal-footer-->
                    </form> 
                </div><!--end modal-content-->
            </div><!--end modal-dialog-->
        </div><!--end modal-->


    </div><!-- container -->

    <?php include 'inc/footer.php';?>