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

if(isset($_GET["Type"]))
{
    $_SESSION["Type"] = $_GET["Type"];
}
elseif(!isset($_GET["Type"]))
{
    if(!isset($_SESSION["Type"]))
    {
        header( 'Location: show-app.php');  
    }
}
$type = "";
$label = "";
switch($_SESSION["Type"])
{
    case 'our':
    $type = "about_image";
    $label = "عن التطبيق";
    break;
    case 'image':
    $type = "image_image";
    $label = "صور التطبيق";
    break;
    case 'download':
    $type = "download_image";
    $label = "حمل التطبيق";
    break;
    default:
    header( 'Location: show-app.php');  
}
$flag = true;
$errors = array();
$errorList = "";
$image = "";
$direction = "../assets/img/app/";

if(isset($_POST["save"]))
{
  //check method used
  if ($_SERVER['REQUEST_METHOD'] === 'POST')
  { 
    //nothing
  }
  else
  {
    header( 'Location: show-app.php');  
  }
  $image = basename($_FILES["image"]["name"]);
          $image = date('Y-M-D').$image;
  $target_file = $direction.$image;
  $image_Type = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if(file_exists($target_file))
  {
    $errors["fileExists"] = "يرجى إختيار الصورة";
    $flag = false;
  }//check file exists
  elseif($image_Type != "jpg" && $image_Type != "png" && $image_Type != "jpeg" && $image_Type != "gif")
  {
    $errors["imageType"] = "يجب أن تكون الصورة بأمتداد (jpg,jpeg,png,gif)";
    $flag = false;
  }
  elseif($_FILES["image"]["size"] > 5000000)
  {
    $errors["imageSize"] = "يجب أن يكون حجم الصورة أقل من 5 ميجا بايت";
    $flag = false;
  }
  if($flag == true)
  {
    if(!move_uploaded_file($_FILES["image"]["tmp_name"],$target_file))
    {
      $errors["imageUpload"] = "يرجى إعادة رفع الصورة";
    }
    else
    {
        $sqlUpdate = "update app_ar set {$type}='{$image}'";
        $executeSql = mysqli_query($connection,$sqlUpdate);
        $sqlUpdate = "update app_en set {$type}='{$image}'";
        $executeSql = mysqli_query($connection,$sqlUpdate);
        $_SESSION["updatesuccessappimage"] = 1;
        header( 'Location: show-app.php');
    }
  }
  else
  {
    foreach($errors as $error)
    {
      $errorList .= $error."<br>";
    }
  }
}



$sqlretriveAR = "select {$type} from app_ar";
$executeSqlAR = mysqli_query($connection,$sqlretriveAR);
$retriveDataAR = mysqli_fetch_row($executeSqlAR);
$sqlretriveEN = "select {$type} from app_en";
$executeSqlEN = mysqli_query($connection,$sqlretriveEN);
$retriveDataEN = mysqli_fetch_row($executeSqlEN);
?>
<!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>
    .drag-text h1
    {
        padding: 30px 0;
    }
    </style>
  </head>
  <body dir="rtl">

<?php include 'navbar.php';?>
<div class="content-body">
<div class="container" style='margin-top:50px;'>
    <div class='row justify-content-center'>
        <div class='col-8'>
            <?php
            if($flag === false)
            {
            echo "
            <div class='alert alert-danger text-right'>
            $errorList
            </div>";
            }
        ?>
            <div class="card text-right" id="add-info">
                <div class="card-header"><h3><?php echo $label;?></h3></div>
                <div class="card-body">
                    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post' enctype="multipart/form-data">
                    <div class='form-row'>
                        <div class='form-group col'>
                        <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><img src='<?php echo $direction.$retriveDataAR[0];?>' style='max-width:200px;max-height:200px;'></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" class="save-image">حفظ</button>
                                <button type="button" onclick="removeUpload()" class="remove-image">حذف</button>
                            </div>
                        </div>
                        </div>
                        <!-- End form group -->
                    </div>
                    <!-- End form row -->
                    </form>
                    <!-- End form -->
                </div>
                <!-- End card-body -->
            </div>
            <!-- End Card -->
        </div>
        <!-- end col -->
    </div>
    <!-- end row -->
</div>
<!-- end container -->
</div>
<!-- End content body -->
<!-- assets / js -->
    <?php include 'jsFile.php'; ?>

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