<?php 
   session_start();
   include "config.php";

      
	       if(isset($_POST['submit'])){
             
          if(isset($_FILES['upload_image'])){
            $errors = array();
            
            $file_name = $_FILES['upload_image']['name'];
            $file_size = $_FILES['upload_image']['size'];
            $file_tmp = $_FILES['upload_image']['tmp_name'];
            $file_type = $_FILES['upload_image']['type'];
            $file_ext = end(explode(".",$file_name));
            $extensions = array("jpeg","jpg","png");
            if(in_array($file_ext,$extensions) === false)
             {
            $errors[] = "This extension file not allowed, Please choose a JPG or PNG file.";
            header("location:{$hostname}/admin/client_banner.php");
             }
            if($file_size > 2097152){
              $rerrors[] = "File size must be 2mb or lower.";
              $_SESSION['status'] = "File size must be 10mb or lower!";
              $_SESSION['status_code'] = "error";
              header("location:{$hostname}/admin/client_banner.php");
              }
              if(empty($errors) == true){
                move_uploaded_file($file_tmp,"all_image/client_banner/".$file_name);
              }else{
              print_r($errors);
              header("location:{$hostname}/admin/client_banner.php");
              die();
              }
              
        $banner_tag_line = mysqli_real_escape_string($conn,$_POST['banner_tag_line']);

        date_default_timezone_set("Asia/Kolkata");
        $image_posting_date = Date('Y-m-d H:i:s');


            $sql1 = "INSERT INTO client_banner(banner_tagline, banner_name, created_date, updated_date) VALUES('$banner_tag_line','$file_name','$image_posting_date','$image_posting_date')";
            if(mysqli_query($conn,$sql1)){
                $_SESSION['status'] = "Add Banner Successfully";
                $_SESSION['status_code'] = "success";
                header("location:{$hostname}/admin/client_banner.php");
            }
        }
        // end of file upload funtion

      }


    // Edit & Update Image Details 

      if(isset($_POST['hidden_banner_id'])){
 
        if(empty($_FILES['update_upload_image']['name'])){
          $file_name = $_POST['old_image'];
        }else{
          $errors = array();
          
          $file_name = $_FILES['update_upload_image']['name'];
          $file_size = $_FILES['update_upload_image']['size'];
          $file_tmp = $_FILES['update_upload_image']['tmp_name'];
          $file_type = $_FILES['update_upload_image']['type'];
          $file_ext = end(explode(".",$file_name));
          $extensions = array("jpeg","jpg","png");
            if(in_array($file_ext,$extensions) === false)
             {
            $errors[] = "This extension file not allowed, Please choose a JPG or PNG file.";
             }
            if($file_size > 2097152){
            $rerrors[] = "File size must be 2mb or lower.";
            $_SESSION['status'] = "File size must be 2mb or lower!";
            $_SESSION['status_code'] = "error";
            header("location:{$hostname}/admin/client_banner.php");
            }
            if(empty($errors) == true){
              move_uploaded_file($file_tmp,"all_image/client_banner/".$file_name);
            }else{
            print_r($errors);
            header("location:{$hostname}/admin/client_banner.php");
            die();
            }
          }
      
        $hidden_banner_id = mysqli_real_escape_string($conn,$_POST['hidden_banner_id']);
        $update_banner_tag_line = mysqli_real_escape_string($conn,$_POST['update_banner_tag_line']); 


        date_default_timezone_set("Asia/Kolkata");
        $image_update_date = Date('Y-m-d H:i:s');

      //Update old class name with the new one in this id...
      $query = "UPDATE client_banner SET banner_tagline = '$update_banner_tag_line', banner_name ='$file_name', updated_date = '$image_update_date' WHERE id = '$hidden_banner_id'";
      $update = mysqli_query($conn,$query);
      if($conn->affected_rows == 1){
        $_SESSION['status'] = "Data Update Successfully";
        $_SESSION['status_code'] = "success";
        echo "Data Updated Successfully";
        header("location:{$hostname}/admin/client_banner.php");
      }else{
        // $_SESSION['status'] = "Data not updated";
        // $_SESSION['status_code'] = "error";
        header("location:{$hostname}/admin/client_banner.php");
      }

    }
    
?>