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

$direction = "../assets/img/storySuccess/";
$flag = true;
$errors = array();
$errorList = "";
$name_ar = isset($_POST["name_ar"]) ? $_POST["name_ar"] : "";
$name_en = isset($_POST["name_en"]) ? $_POST["name_en"] : "";
$paragraph_ar = isset($_POST["paragraph_ar"]) ? $_POST["paragraph_ar"] : "";
$paragraph_en = isset($_POST["paragraph_en"]) ? $_POST["paragraph_en"] : "";
$program = isset($_POST["program"]) ? $_POST["program"] : "";

if(isset($_POST["save"]))
{
  //check method used
  if ($_SERVER['REQUEST_METHOD'] === 'POST')
  { 
    //nothing
  }
  else
  {
    header( 'Location: show-success.php');
  }
  if(!empty($_FILES["image"]["name"]))
  {
    $flagImage = true;
    $image = basename($_FILES["image"]["name"]);
    $image = date("h:i:sa").$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 != "jfif" && $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;
    }
  }
  else
  {
      $flagImage = false;
      $image = 'whiteBG.png';
  }

  

  if(empty($name_ar) || empty($name_en) || 
     empty($paragraph_ar) || empty($paragraph_en))
  {
    $errors["empty"] = "يرجى تعبئة جميع الحقول";
    $flag = false; 
  }  //check if any textbox empty

  if($flag === true)
  {
      if($flagImage == true)
      {
        if(!move_uploaded_file($_FILES["image"]["tmp_name"],$target_file))
        {
          $errors["imageUpload"] = "يرجى إعادة رفع الصورة";
        }
        else
        {
          $insertSql = "INSERT INTO success_ar(name, story, image, insertUsername,type) VALUES ('{$name_ar}','{$paragraph_ar}','{$image}','{$_SESSION["username"]}','{$program}')";
          $executeSql = mysqli_query($connection,$insertSql);
          $insertSql = "INSERT INTO success_en(name, story, image, insertUsername,type) VALUES ('{$name_en}','{$paragraph_en}','{$image}','{$_SESSION["username"]}','{$program}')";
          $executeSql = mysqli_query($connection,$insertSql);
          $_SESSION["insertStory"] = 1;
          $name_ar = $name_en = $paragraph_ar = $paragraph_en = $program = '';
        }//if check store image or not
      }
      elseif($flagImage == false)
      {
        $insertSql = "INSERT INTO success_ar(name, story, image, insertUsername,type) VALUES ('{$name_ar}','{$paragraph_ar}','{$image}','{$_SESSION["username"]}','{$program}')";
        $executeSql = mysqli_query($connection,$insertSql);
        $insertSql = "INSERT INTO success_en(name, story, image, insertUsername,type) VALUES ('{$name_en}','{$paragraph_en}','{$image}','{$_SESSION["username"]}','{$program}')";
        $executeSql = mysqli_query($connection,$insertSql);
        $_SESSION["insertStory"] = 1;
        $name_ar = $name_en = $paragraph_ar = $paragraph_en = $program = '';
      }

  }//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">
<div class='container' style='margin-top:30px;'>
    <div class='row justify-content-center'>
        <div class='col-md-10'>
        <?php
            if($flag === false)
            {
            echo "
            <div class='alert alert-danger text-right'>
            $errorList
            </div>";
            }
        ?>
    <form  action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post' enctype="multipart/form-data">
            <div class="card text-right" id="add-info">
                <div class="card-header"><h3>إضافة قصة نجاح جديد</h3></div>
                <div class="card-body">
                    <div class='form-row'>
                        <div class='form-group col-md'>
                            <label class='ar-info' for="ar-info">المعلومات باللغة العربية</label>                        
                        </div>
                        <!-- End form-group -->
                    </div>
                    <!-- End form row -->
                    <div class='form-row'>
                        <div class='form-group col-md'>
                            <label for="news_ar">إسم الكامل</label>       
                            <input type='text' class='form-control' name='name_ar' value="<?= $name_ar ?>">                 
                        </div>
                        <!-- End form-group -->
                    </div>
                    <!-- End form row -->
                    <div class='form-row'>
                        <div class='form-group col'>
                            <label for="ar-paragraph">محتوى</label>
                            <textarea class='form-control' rows="2" name="paragraph_ar" style='resize:none;'><?= $paragraph_ar ?></textarea>                 
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <div class="form-row">
                    <div class='form-group col-md'>
                            <label class='en-info' for="en-info">المعلومات باللغة الإنجليزية</label>                        
                        </div>
                        <!-- End form-group -->
                    </div>
                    <div class="form-row">
                      <div class='form-group col-md'>
                            <label for="news_en">إسم الكامل</label>       
                            <input type='text' class='form-control' name='name_en' style='direction:ltr;text-align:left' value="<?= $name_en ?>">                                        
                        </div>
                        <!-- End form-group -->
                    </div>
                    <div class="form-row">
                      <div class='form-group col'>
                            <label for="en-paragraph">محتوى</label>
                            <textarea class='form-control' rows="2" name="paragraph_en" style='resize:none;direction:ltr;text-align:left'><?= $paragraph_en?></textarea>                 
                        </div>
                        <!-- end form group -->
                    </div>
                    <div class='form-row'>
                        <div class='form-group col'>
                            <label>برنامج</label>
                            <select class='form-control custom-select' name='program'>
                                <option value='joodcom'>برنامج جودكم</option>
                                <option value='makingStars'>برنامج صناعة النجوم</option>
                            </select>
                        </div>
                    </div>
                    <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><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="button" onclick="removeUpload()" class="remove-image">حذف</button>
                            </div>
                        </div>
                        </div>
                        <!-- End form group -->
                    </div>
                    <!-- End form row -->
                    <div class="form-row">
                        <div class="form-group col text-center">
                        <button type="submit" name="save" class="save-image">حفظ</button>
                        </div>
                    </div>
                </div>
                <!-- End card-body -->
            </div>
            <!-- End card -->
    </form>
        </div>
        <!-- End col-md -->
    </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["insertStory"]) && $_SESSION["insertStory"] == 1)
{ 
?>
Swal.fire(
  'تم إضافة قصة نجاح جديد بنجاح',
  '',
  'success'
)
<?php
$_SESSION["insertStory"] = 0;
}
?>

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