<?php 
include '../connection.php'; 
if(!isset($_SESSION)) session_start();
if(!isset($_SESSION["ID"])){header('Location: index.php');}
$direction = "../assets/img/main-image/";
$type = 0;
if(!isset($_GET["ID"]) || !isset($_GET["Type"]))
{
    if(!isset($_SESSION["idimage"]) && !isset($_SESSION["type"]))
    {
        header( 'Location: ../admin/show-main-images.php');
    }
}
else
{
    switch($_GET["Type"])
    {
        case 'deactive':
        $type = 0;
        break;
        case 'active';
        $type = 1;
        break;
        default:
        header( 'Location: ../admin/show-main-images.php');
    }
    $_SESSION["type"] = $type;
    $_SESSION["idimage"] = $_GET["ID"];
}

//check if click save
if(isset($_GET["save-image"]))
{  
    
    $imgNum = $_GET["imageNumber"];
  //check method used
  if ($_SERVER['REQUEST_METHOD'] === 'POST')
  { 
    //nothing
  }
  else
  {
    header('Location: index.php');
  }
  $sqlUpdate = "update main_image set orderBy=0 where orderBy={$imgNum}";
  $executeSql = mysqli_query($connection,$sqlUpdate);
  $sqlUpdate = "update main_image set orderBy=$imgNum where ID={$_SESSION["idimage"]}";
  $executeSql = mysqli_query($connection,$sqlUpdate);
  $_SESSION["updateSuccess"] = 1;
  header( 'Location: ../admin/show-main-images.php');
}//end if click save

$sqlretrive = "select nameImage from main_image where ID={$_SESSION["idimage"]}";
$executeSql = mysqli_query($connection,$sqlretrive);
$rowImage = mysqli_fetch_row($executeSql);
$showImage = $direction.$rowImage[0];
?>
<!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'; ?>
    <style>
  
    </style>
  </head>
  <body onload="reloader()" data-default-color="navyblue" dir="rtl">
    <div class='text-center' id="loading">
    <h2 id='loadingText'><h2>
    </div>
<?php include 'navbar.php';?>
<div class='content-body'>
   <form  action="<?php echo $_SERVER['PHP_SELF']; ?>" method='get' style='margin-top:50px;'>
    <div class="file-upload">
      <?php
        // if($flag_image === false)
        // {
        //   echo "
        //   <div class='alert alert-danger'>
        //   $errorList
        //   </div>";
        // }
      ?>


  <div class="image-upload-wrap">
    <div class="drag-text">
    <h1>
        <img src='<?php echo $showImage; ?>' style='max-width:300px;'/>
    </h1>
    </div>
  </div>

  <div>
    <div class='form-row' style='margin-top:30px;'>
      <div class='form-group col text-right'>
      <div dir='ltr' style='margin-top:10px;'>
				<span class="margin-r">تفعيل</span>
				<label class="el-switch el-switch-green">
					<input type="checkbox" id='checkbox' value='1' checked name="switch">
					<span class="el-switch-style"></span>
				</label>
			</div>
      </div>
      <!-- end form-group -->
      <div class='form-group col chooseimage' style='display:block'>
      <label class="my-1 mr-2" for="chooseimage">أختر رقم الصورة</label>
        <select class="custom-select my-1 mr-sm-2" id="chooseimage" name="imageNumber">
          <option value="1" selected>الأولى</option>
          <option value="2">الثانية</option>
          <option value="3">الثالثة</option>
        </select>
      </div>
      <!-- end form-group -->
      
    </div>
    <!-- end form-row -->
    <div class="form-row">
        <div class="form-group col text-center">
        <button type="submit" name="save-image" class="save-image">حفظ</button>

        </div>
    </div>

			</div>
</div>
</div>
<!-- End content body -->
<!-- assets / js -->
    <?php include 'jsFile.php'; ?>
<script>



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

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