<?php
include '../connection.php';
if(!isset($_SESSION)){session_start();}
if(!isset($_SESSION["ID"])){header('Location: index.php');}
if($_SESSION["role"] != 2){header('Location: index.php');}

if(isset($_GET["ID"]) && $_GET["status"])
{
    $id = $_GET["ID"];
    switch($_GET["status"])
    {
        case 'deactive':
            $s = 0;
        break;
        case 'active':
            $s = 1;
        break;
    }
    $updateSql = "update admin set status={$s} where ID={$id}";
    $executeSql = mysqli_query($connection,$updateSql);
    $_SESSION["StatusAdmin"] = 1;
}
$Data = "";
$sqlRetrive = 'select * from admin';
$executeSql = mysqli_query($connection,$sqlRetrive);

while($retriveData = mysqli_fetch_row($executeSql))
{
    if($retriveData[4] == 1)
    {
        $action = "<a href='show-admin.php?ID=$retriveData[0]&status=deactive' class='btn btn-danger'>تعطيل</a>";
    }
    elseif($retriveData[4] == 0)
    {
        $action = "<a href='show-admin.php?ID=$retriveData[0]&status=active' class='btn btn-success'>تفعيل</a>";
    }
    $Data .="
    <tr>
    <td>{$retriveData[1]}</td>
    <td>{$retriveData[2]}</td>
    <td>{$action} <a href='edit-password.php?ID={$retriveData[0]}' class='btn btn-info'>تعديل كلمة المرور</a></td>

    </tr>
    ";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>خذ فرصة</title>
    <?php include 'cssFile.php'; ?>
    <style>
    .blockquote-footer
    {
        font-size:16px;
    }
    </style>
</head>
<body dir='rtl'>
    <?php include 'navbar.php';?>

    <div class="content-body">
        <div class='container' style='margin-top:30px;'>
        <div class="row justify-content-center">
            <div class="col-md-3 text-center col-family">
                <h3>المسؤولين</h3>
            </div>
        </div>
        <!-- End row -->
        <div class='col-md text-right' style='margin:0 10px 10px 0px;'>
        <a class='btn btn-yellow' href='add-admin.php'>إضافة مسؤول جديد</a>
      </div>
      <!-- End col -->

      <div class='row justify-content-center text-right'>
            <div class="col-md-10">
            <input class="form-control" id="myInput" type="text" placeholder="بحث...">
            </div>
            <div class="col-md-10">
            <table class="table table-bordered table-striped">
    <thead>
      <tr>
        <th>الإسم</th>
        <th>إسم المستخدم</th>
        <th>الحدث</th>
      </tr>
    </thead>
    <tbody id="myTable">
     <?php
        echo $Data;
     ?>
    </tbody>
  </table>
            </div>

    </div>
    <!-- End row -->


        </div>
        <!-- End container -->
    </div>
    <!-- End content-body -->

    <?php include 'jsFile.php'; ?>
    <script>
$(document).ready(function(){
  $("#myInput").on("keyup", function() {
    var value = $(this).val().toLowerCase();
    $("#myTable tr").filter(function() {
      $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
    });
  });
});
</script>
    <script>
    <?php
if(!isset($_SESSION))
  session_start();
if(isset($_SESSION["StatusAdmin"]) && $_SESSION["StatusAdmin"] == 1)
{ 
?>
Swal.fire(
  'تم التعديل بنجاح',
  '',
  'success'
)
<?php
$_SESSION["StatusAdmin"] = 0;
}
?>

  </script>
      <script>
    <?php
if(!isset($_SESSION))
  session_start();
if(isset($_SESSION["editPSuccess"]) && $_SESSION["editPSuccess"] == 1)
{ 
?>
Swal.fire(
  'تم تعديل كلمة المرور بنجاح',
  '',
  'success'
)
<?php
$_SESSION["editPSuccess"] = 0;
}
?>

  </script>
        <script>
    <?php
if(!isset($_SESSION))
  session_start();
if(isset($_SESSION["addSuccess"]) && $_SESSION["addSuccess"] == 1)
{ 
?>
Swal.fire(
  'تم إضافة مسؤول جديد بنجاح',
  '',
  'success'
)
<?php
$_SESSION["addSuccess"] = 0;
}
?>

  </script>
</body>
</html>