<?php
ob_start();
session_start();
ini_set('display_errors', 1);
 require('../includes/cmain.inc.php');
if($_SESSION['logged_in'] != true) {
header("Location: index.php");
    exit();	
	  }
	  if($_REQUEST['action']=='edit'){
		  $id=$_GET['id'];
		  $listQuery="SELECT * FROM tbl_blog WHERE id='".$id."'";
	      $result=mysqli_query($conn,$listQuery);
	      $row=mysqli_fetch_assoc($result);
		 
		}
	if($_POST['Update']){
		$blogTagIm=implode(",", $_POST['cat_name']);
		//echo $_POST['id'];exit();
				########### Uploading Image ################
		if($_FILES['sv_image']['name']!='')
		{
			/*if(strlen($sv_image) && file_exists(UP_FILES_FS_PATH."/blog/thumb/".$sv_image)){
				@unlink(UP_FILES_FS_PATH."/blog/thumb/".$sv_image);
			}*/
			$sv_image_1 = md5(uniqid(rand(), true)).'.'.file_ext($_FILES['sv_image']['name']);
				copy($_FILES['sv_image']['tmp_name'], UP_FILES_FS_PATH."/blog/thumb/".$sv_image_1) or die("Image is not uploaded");
				copy($_FILES['sv_image']['tmp_name'], UP_FILES_FS_PATH."/blog/original/".$sv_image_1) or die("Image is not uploaded");
			$n_width=225; // Fix the width of the thumb nail images
			$n_height=168; // Fix the height of the thumb nail imaage
			
			$tsrc=UP_FILES_FS_PATH."/blog/thumb/".$sv_image_1;
			$add=UP_FILES_FS_PATH."/blog/original/".$sv_image_1; // Path where thumb nail image will be stored
			
			//echo $tsrc;
			if (!($_FILES['sv_image']['type'] =="image/pjpeg" OR $_FILES['sv_image']['type']=="image/jpeg" OR $_FILES['sv_image']['type']=="image/png")){echo "Your uploaded file must be of JPG or GIF or PNG. Other file types are not allowed<BR>";
			exit;}
				///////// starting of JPG thumb nail creation//////////
			if($_FILES['sv_image']['type']=="image/pjpeg" OR $_FILES['sv_image']['type']=="image/jpeg")
			{
				$im=ImageCreateFromJPEG($add);
				$width=ImageSx($im); // Original picture width is stored
				$height=ImageSy($im); // Original picture height is stored
				$newimage=imagecreatetruecolor($n_width,$n_height);
				imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
				ImageJpeg($newimage,$tsrc);
				chmod("$tsrc",0777);
			}
			else{
				$im=ImageCreateFromPNG($add);
				$width=ImageSx($im); // Original picture width is stored
				$height=ImageSy($im); // Original picture height is stored
				$newimage=imagecreatetruecolor($n_width,$n_height);
				imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
				ImagePng($newimage,$tsrc);
				chmod("$tsrc",0777);
			}
			
		}
		else{
			$sv_image_1=$_POST['sv_image2'];
		}
		
		
		
	$id=$_POST['id'];
	$heading=$_POST['heading'];
	$post_writer=$_POST['post_writer'];
	$cat_name=$blogTagIm;
	$status=$_POST['status'];
	$title=$_POST['title'];
	$description=$_POST['description'];
	$keyword=$_POST['keyword'];
	$blog_banner=$sv_image_1;
	$content=$_POST['content'];
	$blogQuery= "update `tbl_blog` set heading='".addslashes($heading)."',cat_name='".addslashes($cat_name)."', content='".addslashes($content)."',blog_banner='".$blog_banner."',title='".addslashes($title)."',description='".addslashes($description)."', keyword='".addslashes($keyword)."', status='".addslashes($status)."', post_writer='".addslashes($post_writer)."', post_date=now() where id = $id ";
	//print_r($blogQuery);exit();
	$result=mysqli_query($conn,$blogQuery);
	//print_r($result);exit();
	header("Location: service_list.php");
	exit;
	}
     if($_POST['Add']){
	 $blogTagIm=implode(",", $_POST['cat_name']);
	
	   ########### Uploading Image ################
		if($_FILES['sv_image']['name']!='')
		{
			
			/*if(strlen($sv_image) && file_exists(UP_FILES_WS_PATH."/blog/thumb/".$sv_image)){
				 echo $_FILES['sv_image']['name']."ifnot"; exit();
				@unlink(UP_FILES_WS_PATH."/blog/thumb/".$sv_image);
			}*/
			$sv_image_1 = md5(uniqid(rand(), true)).'.'.file_ext($_FILES['sv_image']['name']);
			
				copy($_FILES['sv_image']['tmp_name'], UP_FILES_FS_PATH."/blog/thumb/".$sv_image_1) or die("Image is not uploaded");
				copy($_FILES['sv_image']['tmp_name'], UP_FILES_FS_PATH."/blog/original/".$sv_image_1) or die("Image is not uploaded");
			$n_width=225; // Fix the width of the thumb nail images
			$n_height=168; // Fix the height of the thumb nail imaage
			
			$tsrc=UP_FILES_FS_PATH."/blog/thumb/".$sv_image_1;
			$add=UP_FILES_FS_PATH."/blog/original/".$sv_image_1; // Path where thumb nail image will be stored
			// echo $add."thumb"; exit();
			//echo $tsrc;
			if (!($_FILES['sv_image']['type'] =="image/jpeg" OR $_FILES['sv_image']['type']=="image/jpeg" OR $_FILES['sv_image']['type']=="image/png")){echo "Your uploaded file must be of JPG or GIF or PNG. Other file types are not allowed<BR>";
			exit;}
				///////// starting of JPG thumb nail creation//////////
			if($_FILES['sv_image']['type']=="image/jpeg" OR $_FILES['sv_image']['type']=="image/jpeg")
			{
               // echo $add; exit();
				$im=ImageCreateFromJPEG($add);
				$width=ImageSx($im); // Original picture width is stored
				$height=ImageSy($im); // Original picture height is stored
				$newimage=imagecreatetruecolor($n_width,$n_height);
				imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
				ImageJpeg($newimage,$tsrc);
				chmod("$tsrc",0777);
			}
			else{
			    // echo $add."png"; exit();

				$im=ImageCreateFromPNG($add);
				$width=ImageSx($im); // Original picture width is stored
				$height=ImageSy($im); // Original picture height is stored
				$newimage=imagecreatetruecolor($n_width,$n_height);
				imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
				ImagePng($newimage,$tsrc);
				chmod("$tsrc",0777);
			}
			
		}
	 
	 
	//echo $_POST['Add'];
	$heading=$_POST['heading'];
	$post_writer=$_POST['post_writer'];
	$cat_name=$blogTagIm;
	$status=$_POST['status'];
	$title=$_POST['title'];
	$description=$_POST['description'];
	$keyword=$_POST['keyword'];
	$blog_banner=$sv_image_1;
	$content=$_POST['content'];
	$blogQuery= "INSERT INTO  tbl_blog(heading,content,post_writer,cat_name,blog_banner,title,description,keyword,status) VALUES('$heading','$content','$post_writer','$cat_name', '$blog_banner','$title','$description','$keyword','$status')" ;
	//print_r($blogQuery);exit();
	$result=mysqli_query($conn,$blogQuery);
	//print_r($result);exit();
	header("Location: service_list.php");
	exit;
}
ob_end_flush();
?>

<!DOCTYPE html>
<html lang="en">
<head>
<title>Welcome Admin</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
  
<style>
.ck-editor__editable {
    height: 350px;
    width: 600px;
}
* {
  box-sizing: border-box;
}

body {
  font-family: Arial, Helvetica, sans-serif;
}

/* Style the header */
header {
  background-color: #888888;
  padding: 30px;
  text-align: center;
  font-size: 20px;
  color: white;
}

/* Create two columns/boxes that floats next to each other */
nav {
  float: left;
  width: 20%;
  height: 500px; /* only for demonstration, should be removed */
  background-color:#DCDCDC;
  padding: 20px;
}

/* Style the list inside the menu */
nav ul {
  list-style-type: none;
  padding: 0;
}

article {
  float: left;
  padding: 20px;
  width: 80%;
  font-size:10px;
  
  height: 500px; /* only for demonstration, should be removed */
}

/* Clear floats after the columns */
section::after {
  content: "";
  display: table;
  clear: both;
}

/* Style the footer */
footer {
  
  padding: 10px;
  text-align: center;
  color: white;
}


/* Responsive layout - makes the two columns/boxes stack on top of each other instead of next to each other, on small screens */
@media (max-width: 600px) {
  nav, article {
    width: 100%;
    height: auto;
  }
}
</style>

</head>
<body>

<header>
  <h2>Welcome Admin</h2>
</header>

<section>
  <nav>
    <ul>
       <li><a href="welcome.php">Home</a></li>
      <li><a href="service_list.php">Service List</a></li>
      <li><a href="logout.php">Logout</a></li>
    </ul>
  </nav>

  <article>
    <h1>Add/Edit Services</h1><h3>
	<form action="" method="POST" enctype="multipart/form-data">
	<div style="text-align:right"><a href="service_list.php" align="right">Back to Blog List</a></div>
    <script type="text/javascript" src="ckeditor/ckeditor.js"></script>
	
	<table width="100%">
	<tr><td>Heading:</td><td><input type="text" name="heading" style="width:240px" value="<?php echo isset($_GET['id'])? $row['heading']:''; ?>" /></td></tr>
	<tr><td>Writer:</td><td><input type="text" name="post_writer" style="width:240px"  value="<?php echo isset($_GET['id'])? $row['post_writer']:''; ?>" /></td></tr>
	<tr><td>Provided Category:</td><td> <select name="cat_name[]" id="cat_name" style="width:240px" multiple>	
				<?php 	
                $blogTagEx=explode(",", $row['cat_name']);				
				$indusmenusql = mysqli_query($conn,"select * from tbl_blogcatp where status='1'"); 
			    while($row1 =mysqli_fetch_array($indusmenusql)){?>				
				<option value="<?php echo ucwords($row1['catP_Name']); ?>"<?php echo in_array($row1['catP_Name'], $blogTagEx) ? 'selected' : ''; ?>><?php echo ucwords($row1['catP_Name']);?></option>
				<?php }?>		 
				</select> </td></tr>
	<tr><td>Status:</td><td>
	<select id="status" name="status" style="width:240px">
           <option value="1" <?php echo $status == '1' ? 'selected' : ''; ?>>Active</option>
           <option value="0" <?php echo $status == '0' ? 'selected' : ''; ?>>Inactive</option>
    </select></td></tr>
	<tr><td>Title:</td><td><input type="text" name="title" style="width:240px"  value="<?php echo isset($_GET['id'])? $row['title']:''; ?>" /></td></tr>
	<tr><td>Description:</td><td><textarea name="description" rows="10" cols="30"><?php echo isset($_GET['id'])? $row['description']:''; ?>
</textarea></td></tr>
	<tr><td>Keyword:</td><td><textarea name="keyword" rows="10" cols="30"><?php echo isset($_GET['id'])? $row['keyword']:''; ?>
</textarea></td></tr>
<tr><td>Blog Content:</td><td><textarea name="content" id="content" class=""><?php echo isset($_GET['id'])? stripslashes($row['content']):''; ?></textarea></td></tr>
	<tr><td><tr><td>Banner Image:</td><td><input type="file" name="sv_image" id="sv_image" /></td></tr>
	<!--<tr><td>Blog Content:</td><td><input type="text" name="content" /></td></tr>-->
	<!--<tr><td>Service Description:</td><td><textarea name="service_description" rows="10" cols="30">-->

</textarea></td></tr>

<tr><td>&nbsp;</td></tr>
        <tr>
		  <td><b>Current blog Image</b></td>
          <td>
        <? if(isset($_GET['id']))
            {
               $sv_image2 = $row['blog_banner'];
               if(strlen($sv_image2) && file_exists(UP_FILES_FS_PATH."/blog/thumb/".$sv_image2)){
			   //if(strlen($sv_image2)){
                $imgsrc=SITE_WS_PATH."/uploaded_files/blog/thumb/".$sv_image2;
                $size=getimagesize(UP_FILES_FS_PATH."/blog/thumb/".$sv_image2);
				echo '<img src="'.$imgsrc.'" alt="No Image"  />';
                }
				else{  echo 'NO Images'; }
				
				
                if(strlen($sv_image2) && file_exists(UP_FILES_FS_PATH."/blog/original/".$sv_image2)){
                $imgsrcBig=SITE_WS_PATH."/uploaded_files/blog/original/".$sv_image2;
                $size=getimagesize(UP_FILES_FS_PATH."/blog/original/".$sv_image2);
                $img=ImageCreateFromJPEG($imgsrcBig);
                $width=ImageSx($img); 
                $height=ImageSy($img); 				
			}
		}
        ?>
             
            </td>
        </tr>
        <tr><td>&nbsp;</td></tr>
	<input type="hidden" name="id" value="<?php echo isset($_GET['id'])? $_GET['id']:" "; ?>">
	<input type="hidden" name="sv_image2" value="<?php echo isset($_GET['id'])? $row['blog_banner']:" "; ?>">
	<tr><td></td><td><input type="submit" name="<?php echo isset($_GET['id'])? 'Update':'Add'; ?>" value="<?php echo isset($_GET['id'])? "Update":"Save"; ?>" id=""/></td></tr>
	</table>
	
	</form
    
  </article>
</section>
<script type="text/javascript">
  // Retrieve the textarea element
    var textarea = document.getElementById('content');

    // Retrieve the textarea value
    var textareaValue = textarea.value;

    // Find and replace the duplicated base URL in the link URL
    var modifiedTextareaValue = textareaValue.replace(/"(https:\/\/www\.metalstamping-dies\.com)(\/[^"]+)"/g, '"$2"');

    // Update the textarea value with the modified link URL
    textarea.value = modifiedTextareaValue;


    // Replace the textarea with CKEditor
    CKEDITOR.replace('content');
</script>
<script>
var submitButton = document.querySelector("#submit-button");

// Add a click event listener to the submit button
submitButton.addEventListener("click", function(event) {
    // Disable the submit button
    this.disabled = true;

    // Perform form submission or any other logic here
});
</script>

<footer>
  <p></p>
</footer>

</body>
</html>

