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

$direction = "../assets/img/our-program/";
$ar_name = isset($_POST["ar_name"]) ? $_POST["ar_name"] : "";
$en_name = isset($_POST["en_name"]) ? $_POST["en_name"] : "";
$ar_paragraph = isset($_POST["ar_paragraph"]) ? $_POST["ar_paragraph"] : "";
$en_paragraph = isset($_POST["en_paragraph"]) ? $_POST["en_paragraph"] : "";
$active = isset($_POST["switch"]) ? $_POST["switch"] : 1;
$flag = true;
$errors = array();
$errorList = "";
$image = "";
$flagImage = false;
$flagImage2 = false;

//check if click save
if(isset($_POST["save"]))
{
  //check method used
  if ($_SERVER['REQUEST_METHOD'] === 'POST')
  { 
    //nothing
  }
  else
  {
    
     header( 'Location: show-program.php');
  }
    $image = basename($_FILES["image"]["name"]);
    $image2 = basename($_FILES["image2"]["name"]);
  if(!empty($image))
  {
    $image = date('Y-M-D').$image;
    $target_file = $direction.$image;
    $image_Type = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
    if(!file_exists($target_file))
    {
      if($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;
    }
    $flagImage = true;
    }
  }
  if(!empty($image2))
  {
  $image2 = date('Y-M-D').$image2;
  $target_file2 = $direction.$image2;
  $image_Type2 = strtolower(pathinfo($target_file2,PATHINFO_EXTENSION));
    if(!file_exists($target_file2))
    {
      if($image_Type2 != "jpg" && $image_Type2 != "png" && $image_Type2 != "jpeg" && $image_Type2 != "gif")
    {
      $errors["imageType"] = "يجب أن تكون الصورة بأمتداد (jpg,jpeg,png,gif)";
      $flag = false;
    }

    elseif($_FILES["image2"]["size"] > 5000000)
    {
           $errors["imageSize"] = "يجب أن يكون حجم الصورة أقل من 5 ميجا بايت";
      $flag = false;   
    }
    $flagImage2 = true;
    }
  }

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


  if($flag === true)
  {
      if($flagImage === true && $flagImage2 === true)
      {
        if(!move_uploaded_file($_FILES["image"]["tmp_name"],$target_file) || !move_uploaded_file($_FILES["image2"]["tmp_name"],$target_file2))
        {
        $errors["imageUpload"] = "يرجى إعادة رفع الصورة";
        }
        else
        {
        $date = date("Y-m-d");
        $updateSql = "UPDATE our_program_ar SET name='{$ar_name}',paragraph='{$ar_paragraph}',status=$active,updateDate='{$date}',updateUsername='{$_SESSION["username"]}',image='{$image}' WHERE ID={$_SESSION["updateID"]}";
        $executeSql = mysqli_query($connection,$updateSql);
        $updateSql = "UPDATE our_program_en SET name='{$en_name}',paragraph='{$en_paragraph}',status=$active,updateDate='{$date}',updateUsername='{$_SESSION["username"]}',image='{$image2}' WHERE ID={$_SESSION["updateID"]}";
        $executeSql = mysqli_query($connection,$updateSql);
        $_SESSION["updateinfo"] = 1;
        header( 'Location: show-program.php');  
        }//if check store image or not
      }
      elseif($flagImage === true && $flagImage2 === false)
      {
          if(!move_uploaded_file($_FILES["image"]["tmp_name"],$target_file))
                {
                    $errors["imageUpload"] = "يرجى إعادة رفع الصورة";
                }
          else
                {
                    $date = date("Y-m-d");
                    $updateSql = "UPDATE our_program_ar SET name='{$ar_name}',paragraph='{$ar_paragraph}',status=$active,updateDate='{$date}',updateUsername='{$_SESSION["username"]}',image='{$image}' WHERE ID={$_SESSION["updateID"]}";
                    $executeSql = mysqli_query($connection,$updateSql);
                    $updateSql = "UPDATE our_program_en SET name='{$en_name}',paragraph='{$en_paragraph}',status=$active,updateDate='{$date}',updateUsername='{$_SESSION["username"]}' WHERE ID={$_SESSION["updateID"]}";
                    $executeSql = mysqli_query($connection,$updateSql);
                    $_SESSION["updateinfo"] = 1;
                    header( 'Location: show-program.php');   
                }
      }
      elseif($flagImage === false && $flagImage2 === true)
        {
            if(!move_uploaded_file($_FILES["image2"]["tmp_name"],$target_file2))
                {
                    $errors["imageUpload"] = "يرجى إعادة رفع الصورة";
                }
            else
                {
                    $date = date("Y-m-d");
                    $updateSql = "UPDATE our_program_ar SET name='{$ar_name}',paragraph='{$ar_paragraph}',status=$active,updateDate='{$date}',updateUsername='{$_SESSION["username"]}' WHERE ID={$_SESSION["updateID"]}";
                    $executeSql = mysqli_query($connection,$updateSql);
                    $updateSql = "UPDATE our_program_en SET name='{$en_name}',paragraph='{$en_paragraph}',status=$active,updateDate='{$date}',updateUsername='{$_SESSION["username"]}',image='{$image2}' WHERE ID={$_SESSION["updateID"]}";
                    $executeSql = mysqli_query($connection,$updateSql);
                    $_SESSION["updateinfo"] = 1;
                    header( 'Location: show-program.php');
                }
        }
        elseif($flagImage === false)
        {
            $date = date("Y-m-d");
            $updateSql = "UPDATE our_program_ar SET name='{$ar_name}',paragraph='{$ar_paragraph}',status=$active,updateDate='{$date}',updateUsername='{$_SESSION["username"]}' WHERE ID={$_SESSION["updateID"]}";
            $executeSql = mysqli_query($connection,$updateSql);
            $updateSql = "UPDATE our_program_en SET name='{$en_name}',paragraph='{$en_paragraph}',status=$active,updateDate='{$date}',updateUsername='{$_SESSION["username"]}' WHERE ID={$_SESSION["updateID"]}";
            $executeSql = mysqli_query($connection,$updateSql);    
            $_SESSION["updateinfo"] = 1; 
            header( 'Location: show-program.php');  
        }
  }//if for store image in file
  else
  {
    foreach($errors as $error)
    {
      $errorList .= $error."<br>";
    }
  }
}//end if click save
if(isset($_GET["ID"]) || isset($_SESSION["updateID"]))
{
    if(isset($_GET["ID"]))
    {
    $ID = $_GET["ID"];        
    }
    else
    {
        $ID = $_SESSION["updateID"];
    }
    $programSql = "
    SELECT our_program_ar.ID, our_program_ar.name, our_program_ar.paragraph, our_program_ar.image, our_program_ar.insertDate,
    our_program_ar.insertUsername, our_program_ar.updateDate, our_program_ar.updateUsername, our_program_ar.status ,
    our_program_en.ID, our_program_en.name, our_program_en.paragraph, our_program_en.image, our_program_en.insertDate,
    our_program_en.insertUsername, our_program_en.updateDate, our_program_en.updateUsername, our_program_en.status
    FROM our_program_ar ,our_program_en where our_program_ar.ID = {$ID} and our_program_en.ID = {$ID}";
    $executeSql = mysqli_query($connection,$programSql);
    $retriveData = mysqli_fetch_row($executeSql);
    $_SESSION["updateID"] = $ID;
}
elseif(!isset($_GET["ID"]) && isset($_SESSION["updateID"]))
{
    header('Location: show-program.php');
}


?>
<!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-10'>
            <?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>تعديل المعلومات</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'>
                            <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'>
                            <label for="ar-name">إسم البرنامج</label>
                            <input type="text" class='form-control' name="ar_name" value="<?php echo $retriveData[1];?>">                      
                        </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="3" name="ar_paragraph" style='resize:none;'><?php echo $retriveData[2];?></textarea>                 
                        </div>
                        <!-- end form group -->
                    </div>
                    <!-- end form row -->
                    <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.$retriveData[3]?>' style='max-width: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="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'>
                            <label class='en-info' for="en-info">المعلومات باللغة الإنجليزية</label>                        
                        </div>
                        <!-- end form group -->
                    </div>
                    <div class="form-row">
                        <div class='form-group col'>
                            <label for="en-name">إسم البرنامج</label>
                            <input type="text" class='form-control' style='direction:ltr;text-align:left' name="en_name" value="<?php echo $retriveData[10];?>">                      
                        </div>
                        <!-- end form group -->
                    </div>
                    <div class="form-row">
                        <div class='form-group col'>
                            <label for="en-paragraph">الملخص</label>
                            <textarea class='form-control' rows="3" name="en_paragraph" style='resize:none;direction:ltr;text-align:left'><?php echo $retriveData[11];?></textarea>                 
                        </div>
                        <!-- end form group -->
                    </div>
                    <div class="form-row">
                    <div class='form-group col'>
                        <button class="file-upload-btn" type="button" onclick="$('.file-upload-input2').trigger( 'click' )">إضافة صورة جديدة</button>
                        <div class="image-upload-wrap2">
                            <input class="file-upload-input2" name="image2" type='file' onchange="readURL2(this);" />
                            <div class="drag-text">
                            <h1><img src='<?php echo $direction.$retriveData[12]?>' style='max-width:200px;'></h1>
                            </div>
                        </div>
                        <div class="file-upload-content2">
                            <img class="file-upload-image2" src="#" alt="your image" />
                            <div class="image-title-wrap">
                                <button type="button" onclick="removeUpload2()" class="remove-image">حذف</button>
                            </div>
                        </div>
                        </div>
                        <!-- End form group -->
                    </div>
                    <div calss='form-row'>
                        <div class="form-group col text-center">
                            <button type="submit" name="save" class="save-image">حفظ</button>
                        </div>
                    </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()" -->
