<?php
include '../connection.php'; 
if(!isset($_SESSION)) session_start();
$direction = "../assets/img/main-image/";
$Data = "";
if(!isset($_SESSION["ID"])){header('Location: index.php');}
//if user click delete
if(isset($_GET["ID"]) && isset($_GET["Type"]))
{
    $ID = $_GET["ID"];
    $deleteSql = "update main_image set status=2 WHERE ID={$ID}";
    $executeSql = mysqli_query($connection,$deleteSql);
    $_SESSION["DeleteImage"] = 1;
}


// retrive all image from table
$imageSql = "select ID ,nameImage,orderBy from main_image where status=1";
$executeSql = mysqli_query($connection,$imageSql);
while($retriveData = mysqli_fetch_row($executeSql))
{
    if($retriveData[2] == 1 || $retriveData[2] == 2 || $retriveData[2] == 3)
    {
        $selected = "selected";
        $action = "";
    }
    else
    {
        $selected = "";
        $action = "<a href='edit-main-image.php?ID={$retriveData[0]}&Type=active' class='btn btn-success' style='width:49%'>تفعيل</a>
        <a href='show-main-images.php?ID={$retriveData[0]}&Type=delete' class='btn btn-danger' style='width:49%'>حذف</a>";

    }
    $Data .= "
    <div class='col-md-3 text-center' style='margin:10px 0;'>
        <img src='$direction{$retriveData[1]}'  class='img-fluid list-main-img {$selected}'>
        {$action}
        
    </div>
    ";
}


?>
<!doctype html>
<html lang="ar">
  <head>
    <title>خذ فرصة</title>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- assets / css -->
    <?php include 'cssFile.php'; ?>

  </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-main-image">
                <h3>الصور الرئيسية</h3>
            </div>
        </div>
        <div class='row'>
        <div class='col-md-4 text-right' style='margin:0 10px 10px 0px;'>
        <a class='btn btn-pink' href='add-main-image.php'>إضافة صورة جديد </a>
      </div>
              <div class='col-md-6 text-left' style='margin:0 0 0 0px;'>
        <a class='btn btn-pink' href='deleteMain-image.php'>عرض المحذوف</a>
      </div>
      <!-- End col -->
      </div>
        <div class='row'>

            <?php
            echo $Data;
            ?>
            
        </div>
        <!-- End row -->
    </div>
    <!-- End container -->
</div>
<!-- End content body -->
<!-- assets / js -->
    <?php include 'jsFile.php'; ?>
<script>
    <?php
if(!isset($_SESSION))
  session_start();
if(isset($_SESSION["DeleteImage"]) && $_SESSION["DeleteImage"] == 1)
{ 
?>
Swal.fire(
  'تم الحذف بنجاح',
  '',
  'error'
)
<?php
$_SESSION["DeleteImage"] = 0;
}
?>

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

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

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

  </script>
  </body>
</html>
<!-- onload="reloader()" -->
