<?php 
include '../connection.php'; 
if(!isset($_SESSION)) session_start();
if(!isset($_SESSION["ID"])){header('Location: index.php');}
$direction = "../assets/img/main-image/";
$flag_image = true;
$errors = array();
$errorList = "";
$image = "";
//check if click save
if(isset($_POST["save-image"]))
{
  //check method used
  if ($_SERVER['REQUEST_METHOD'] === 'POST')
  { 
    //nothing
  }
  else
  {
    header( 'Location: ../admin/show-main-images.php');
  }
  $imageNumber = 0;
  $active = isset($_POST["switch"]) ? $_POST["switch"] : 0;
  $image = basename($_FILES["image"]["name"]);
  $image = date("h:i:sa").$image;
  $target_file = $direction.$image;
  $image_Type = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
  if($active == 1)
  {
    $imageNumber = isset($_POST["imageNumber"]) ? $_POST["imageNumber"] : 0;
    switch($imageNumber)
    {
      case 1:
      $imageNumber = 1;
      break;
      case 2:
      $imageNumber = 2;
      break;
      case 3:
      $imageNumber = 3;
      break;
      default :
      $imageNumber = 1;
      break;
    }
  }
  //check file exists
  if(file_exists($target_file))
  {
    $errors["fileExists"] = "يرجى إختيار الصورة";
    $flag_image = false;
  }
  elseif($image_Type != "jpg" && $image_Type != "png" && $image_Type != "jpeg" && $image_Type != "gif" && $image_Type != "jfif")
  {
    $errors["imageType"] = "يجب أن تكون الصورة بأمتداد (jpg,jpeg,png,gif)";
    $flag_image = false;
  }
  elseif($_FILES["image"]["size"] > 5000000)
  {
    $errors["imageSize"] = "يجب أن يكون حجم الصورة أقل من 5 ميجا بايت";
    $flag_image = false;
  }

  if($flag_image === true)
  {
    if(!move_uploaded_file($_FILES["image"]["tmp_name"],$target_file))
    {
      $errors["imageUpload"] = "يرجى إعادة رفع الصورة";
    }
    else
    {
      if($active == 1)
      {
        $updateOrderImage = "update main_image set orderBy=0 where orderBy={$imageNumber}";
        $executeSql = mysqli_query($connection,$updateOrderImage);
      }
      $insertSql = "INSERT INTO main_image (nameImage,insertUsername,orderBy) VALUES ('{$image}','{$_SESSION["username"]}',{$imageNumber})";
      $executeSql = mysqli_query($connection,$insertSql);
      $_SESSION["insertMainImageSuccess"] = 1;
      header( 'Location: ../admin/show-main-images.php');

    }//if check store image or not
  }//if for store image in file
  else
  {
    foreach($errors as $error)
    {
      $errorList .= $error."<br>";
    }
  }
}//end if click save


?>
<!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'>
   <form  action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post' enctype="multipart/form-data" style='margin-top:50px;'>
    <div class="file-upload">
      <?php
        if($flag_image === false)
        {
          echo "
          <div class='alert alert-danger text-right'>
          $errorList
          </div>";
        }
      ?>

  <button class="file-upload-btn" type="button" onclick="$('.file-upload-input').trigger( 'click' )">إضافة صورة جديدة</button>

  <div class="image-upload-wrap">
    <input class="file-upload-input" name="image" type='file' onchange="readURL(this);" />
    <div class="drag-text">
    <h1><i class="fas fa-image"></i></h1>
    </div>
  </div>
  <div class="file-upload-content">
    <img class="file-upload-image" src="#" alt="your image" />
    <div class="image-title-wrap">
      <button type="submit" name="save-image" class="save-image">حفظ</button>
      <button type="button" onclick="removeUpload()" class="remove-image">حذف</button>
    </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' name="switch">
					<span class="el-switch-style"></span>
				</label>
			</div>
      </div>
      <!-- end form-group -->
      <div class='form-group col chooseimage'>
      <label class="my-1 mr-2" for="chooseimage">أختر رقم الصورة</label>
        <select class="custom-select my-1 mr-sm-2" id="chooseimage" name="imageNumber">
          <option value='0' selected>أختر...</option>
          <option value="1">الأولى</option>
          <option value="2">الثانية</option>
          <option value="3">الثالثة</option>
        </select>
      </div>
      <!-- end form-group -->
    </div>
    <!-- end form-row -->

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

$(document).ready(function () {
    var ckbox = $('#checkbox');

    $('input').on('click',function () {
        if (ckbox.is(':checked')) {
          $(".chooseimage").fadeIn();
        } else {
          $(".chooseimage").fadeOut();
        }
    });
});



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