<?php 
   session_start();
    include "config.php";

	       if(isset($_POST['submit'])){

          if($_FILES['upload_image']['name']){
            $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/services_page.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/services_page.php");
              }
              if(empty($errors) == true){
                move_uploaded_file($file_tmp,"all_image/services_page_image/".$file_name);
              }else{
              print_r($errors);
             // header("location:{$hostname}/admin/services_page.php");
              die();
              }
            }else{
				
				
				$file_name= '';
			}

          if($_FILES['upload_image1']['name']){
            $errors = array();
            
            $file_name1 = $_FILES['upload_image1']['name'];
            $file_size1 = $_FILES['upload_image1']['size'];
            $file_tmp1 = $_FILES['upload_image1']['tmp_name'];
            $file_type1 = $_FILES['upload_image1']['type'];
            $file_ext1 = end(explode(".",$file_name1));
            $extensions1 = array("jpeg","jpg","png");
            if(in_array($file_ext1,$extensions1) === false)
              {
            $errors[] = "This extension file not allowed, Please choose a JPG or PNG file.";
          //  header("location:{$hostname}/admin/services_page.php");
              }
            if($file_size1 > 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/services_page.php");
              }
              if(empty($errors) == true){
                move_uploaded_file($file_tmp1,"all_image/services_page_image/".$file_name1);
              }else{
              print_r($errors);
             // header("location:{$hostname}/admin/services_page.php");
              die();
              }
            }else{
				
				$file_name1= '';
			}

            if($_FILES['upload_image2']['name']){
              $errors = array();
              
              $file_name2 = $_FILES['upload_image2']['name'];
              $file_size2 = $_FILES['upload_image2']['size'];
              $file_tmp2 = $_FILES['upload_image2']['tmp_name'];
              $file_type2 = $_FILES['upload_image2']['type'];
              $file_ext2 = end(explode(".",$file_name2));
              $extensions2 = array("jpeg","jpg","png");
              if(in_array($file_ext2,$extensions2) === false)
                {
              $errors[] = "This extension file not allowed, Please choose a JPG or PNG file.";
           //   header("location:{$hostname}/admin/services_page.php");
                }
              if($file_size2 > 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/services_page.php");
                }
                if(empty($errors) == true){
                  move_uploaded_file($file_tmp2,"all_image/services_page_image/".$file_name2);
                }else{
                print_r($errors);
               // header("location:{$hostname}/admin/services_page.php");
                die();
                }
              }else{
				  
				  $file_name2= '';
			  }

        $banner_tag_line = mysqli_real_escape_string($conn,$_POST['banner_tag_line']);
        $banner_tag_line1 = mysqli_real_escape_string($conn,$_POST['banner_tag_line1']);
        $banner_tag_line2 = mysqli_real_escape_string($conn,$_POST['banner_tag_line2']);
        $banner_tag_line3 = mysqli_real_escape_string($conn,$_POST['banner_tag_line3']);
        $banner_tag_line4 = mysqli_real_escape_string($conn,$_POST['banner_tag_line4']);

        $update_main_containt = str_replace("'","\'", $_POST['update_main_containt']);
        $update_main_containt1 = str_replace("'","\'", $_POST['update_main_containt1']);
        $update_main_containt2 = str_replace("'","\'", $_POST['update_main_containt2']);
        $update_main_containt3 = str_replace("'","\'", $_POST['update_main_containt3']);

        $select_category_id = $_POST['select_category_name'];
      
        $sql3 = "SELECT * FROM category WHERE category_id = '$select_category_id'";
        $result3 = mysqli_query($conn, $sql3) or die("Query Failed.");
         $row3 = mysqli_fetch_assoc($result3);
        $category_name = $row3['category_name']; 

        date_default_timezone_set("Asia/Kolkata");
        $image_posting_date = Date('Y-m-d H:i:s');


            $sql1 = "INSERT INTO services_page(main_heading, main_content, created_date, updated_date, product_img, product_img1, product_img2, banner_tag_line1, banner_tag_line2, banner_tag_line3, banner_tag_line4, update_main_containt1, update_main_containt2, update_main_containt3, category_id, category_name) VALUES('$banner_tag_line','$update_main_containt','$image_posting_date','$image_posting_date','$file_name','$file_name1','$file_name2','$banner_tag_line1','$banner_tag_line2','$banner_tag_line3','$banner_tag_line4','$update_main_containt1','$update_main_containt2','$update_main_containt3','$select_category_id','$category_name')";
            if(mysqli_query($conn, $sql1)){
                $_SESSION['status'] = "Add Banner Successfully";
                $_SESSION['status_code'] = "success";
                header("location:{$hostname}/admin/services_page.php");
            }
      }


    // Edit & Update Image Details 

      if(isset($_POST['Update'])){

        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/services_page.php");
            }
            if(empty($errors) == true){
              move_uploaded_file($file_tmp,"all_image/services_page_image/".$file_name);
            }else{
            print_r($errors);
            header("location:{$hostname}/admin/services_page.php");
            die();
            }
          }

          if(empty($_FILES['update_upload_image1']['name'])){
            $file_name1 = $_POST['old_image1'];
          }else{
            $errors = array();
            
            $file_name1 = $_FILES['update_upload_image1']['name'];
            $file_size1 = $_FILES['update_upload_image1']['size'];
            $file_tmp1 = $_FILES['update_upload_image1']['tmp_name'];
            $file_type1 = $_FILES['update_upload_image1']['type'];
            $file_ext1 = end(explode(".",$file_name1));
            $extensions1 = array("jpeg","jpg","png");
              if(in_array($file_ext1,$extensions1) === false)
               {
              $errors[] = "This extension file not allowed, Please choose a JPG or PNG file.";
               }
              if($file_size1 > 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/services_page.php");
              }
              if(empty($errors) == true){
                move_uploaded_file($file_tmp1,"all_image/services_page_image/".$file_name1);
              }else{
              print_r($errors);
              header("location:{$hostname}/admin/services_page.php");
              die();
              }
            }

            if(empty($_FILES['update_upload_image2']['name'])){
              $file_name2 = $_POST['old_image2'];
            }else{
              $errors = array();
              
              $file_name2 = $_FILES['update_upload_image2']['name'];
              $file_size2 = $_FILES['update_upload_image2']['size'];
              $file_tmp2 = $_FILES['update_upload_image2']['tmp_name'];
              $file_type2 = $_FILES['update_upload_image2']['type'];
              $file_ext2 = end(explode(".",$file_name2));
              $extensions2 = array("jpeg","jpg","png");
                if(in_array($file_ext2,$extensions2) === false)
                 {
                $errors[] = "This extension file not allowed, Please choose a JPG or PNG file.";
                 }
                if($file_size2 > 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/services_page.php");
                }
                if(empty($errors) == true){
                  move_uploaded_file($file_tmp2,"all_image/services_page_image/".$file_name2);
                }else{
                print_r($errors);
                header("location:{$hostname}/admin/services_page.php");
                die();
                }
              }

        $hidden_content_id = mysqli_real_escape_string($conn,$_POST['hidden_content_id']); 
        $update_main_containt = str_replace("'","\'", $_POST['update_main_containt']); 
        $update_banner_tag_line = mysqli_real_escape_string($conn,$_POST['update_banner_tag_line']); 


        $banner_tag_line1 = mysqli_real_escape_string($conn,$_POST['banner_tag_line1']);
        $banner_tag_line2 = mysqli_real_escape_string($conn,$_POST['banner_tag_line2']);
        $banner_tag_line3 = mysqli_real_escape_string($conn,$_POST['banner_tag_line3']);
        $banner_tag_line4 = mysqli_real_escape_string($conn,$_POST['banner_tag_line4']);

        $update_main_containt1 = str_replace("'","\'", $_POST['update_main_containt1']);
        $update_main_containt2 = str_replace("'","\'", $_POST['update_main_containt2']);
        $update_main_containt3 = str_replace("'","\'", $_POST['update_main_containt3']);

       $select_category_id = mysqli_real_escape_string($conn,$_POST['select_category_name']);
  
        $sql4 = "SELECT * FROM category WHERE category_id = '$select_category_id'";
        $result4 = mysqli_query($conn, $sql4) or die("Query Failed.");
         $row4 = mysqli_fetch_assoc($result4);
          $select_category_name = $row4['category_name'];

        date_default_timezone_set("Asia/Kolkata");
        $content_update_date = Date('Y-m-d H:i:s');

      //Update old class name with the new one in this id...
      $query = "UPDATE services_page SET  main_content ='$update_main_containt', main_heading = '$update_banner_tag_line', product_img = '$file_name', product_img1 = '$file_name1', product_img2 = '$file_name2', updated_date = '$content_update_date', banner_tag_line1 = '$banner_tag_line1', banner_tag_line2 = '$banner_tag_line2', banner_tag_line3 = '$banner_tag_line3', banner_tag_line4 = '$banner_tag_line4', update_main_containt1 = '$update_main_containt1', update_main_containt2 = '$update_main_containt2', update_main_containt3 = '$update_main_containt3', category_id = '$select_category_id', category_name = '$select_category_name'  WHERE id = '$hidden_content_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/services_page.php");
      }else{
        // $_SESSION['status'] = "Data not updated";
        // $_SESSION['status_code'] = "error";
        header("location:{$hostname}/admin/services_page.php");
      }

    }
    
?>