<?php
error_reporting(0);
session_start();
include '../include/connection.php';
$conn     = db();
@extract($_REQUEST);
if($_FILES['image']['name'])
{
$folder='../upload/category/';
$folder2='../upload/product/';
$tm=time();
$u_userimage = "";
$file=time().@str_replace(' ','-',$_FILES['image']['name']);
if(move_uploaded_file(@$_FILES['image']['tmp_name'],$folder.$file))
{
 move_uploaded_file(@$_FILES['image']['tmp_name'],$folder2.$file);   
$image = $file;



if ($file)
{
$filename = stripslashes(@$_FILES['image']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg")
&& ($extension != "png") && ($extension != "gif"))
{
echo ' Unknown Image extension ';
$errors=1;
}
else
{
$size=filesize($folder.$file);

if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile =$folder.$file;
$src = imagecreatefromjpeg($uploadedfile);
}
else if($extension=="png")
{
$uploadedfile =$folder.$file;
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}

list($width,$height)=getimagesize($uploadedfile);
//This code is done for small img
$newwidth=300;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = "../upload/category/small/". $file;
imagejpeg($tmp,$filename,100);
$filename2 = "../upload/product/small/". $file;
imagejpeg($tmp,$filename2,100);
//imagedestroy($src);
imagedestroy($tmp);

//This code is done for small img

$newwidth1=200;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
$filename1 = "../upload/category/thumb/". $file;
imagejpeg($tmp1,$filename1,100);
imagedestroy($src);
imagedestroy($tmp1);

}


}

}
}

function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
if($_REQUEST['id'])
{
$parentid=$_REQUEST['id'];
}
else
{
$parentid=0;
}

$bannerfolder='../upload/banner/';
$banerfile=time().@str_replace(' ','-',$_FILES['banner']['name']);
if(move_uploaded_file(@$_FILES['banner']['tmp_name'],$bannerfolder.$banerfile))
{
$banner=$banerfile;
}

$page_hindi_url= strtolower(str_replace('-',' ',$_REQUEST['hindi_url']));
$page_hindi_url_link =  preg_replace('/\s+/', '-', strtolower(str_replace('-',' ',$_REQUEST['hindi_url'])));

$ProductColour = implode(',', $_REQUEST['ProductColour']);
$Product_home_pg_showing_status = implode(',', $_REQUEST['home_pg_showing_status']);	
$ProductSku  = $_REQUEST['ProductSku'] ?? '';

if (empty($ProductSku)) {
    $prefix = 'PRD';
    $date   = date('Ymd'); 
    $random = strtoupper(substr(md5(uniqid(rand(), true)), 0, 6)); 
    $ProductSku = $prefix . '-' . $date . '-' . $random;
}

$ProductMrp = $_REQUEST['ProductMrp'];
$ProductSellingPrice = $_REQUEST['ProductSellingPrice'];
$discount = 0;
if (!empty($ProductMrp) && $ProductMrp > 0 && $ProductSellingPrice < $ProductMrp) {
    $discount = (($ProductMrp - $ProductSellingPrice) / $ProductMrp) * 100;
}
$discount = round($discount, 2);

$pg_meta_title_location = str_replace("'",'^',$_REQUEST['pg_meta_title_location']);
$pg_meta_keyword_location = str_replace("'",'^',$_REQUEST['pg_meta_keyword_location']);
$pg_meta_desc_location = str_replace("'",'^',$_REQUEST['pg_meta_desc_location']);
$page_url= strtolower(str_replace('-',' ',$_REQUEST['pg_url']));

$page_url_link =  preg_replace('/\s+/', '-', strtolower(str_replace('-',' ',$_REQUEST['pg_url'])));
$prodsel=mysqli_query($conn,"select * from tbl_product where pg_url='".$_REQUEST['pg_url']."'");




$websel=mysqli_query($conn,"select * from tbl_product_category where pg_url='".$_REQUEST['pg_url']."' && hindi_url='".$page_hindi_url_link."'");
if(mysqli_num_rows($websel)==0)
{

$page_url= strtolower(str_replace('-',' ',$_REQUEST['pg_url']));
$page_url_link =  preg_replace('/\s+/', '-', strtolower(str_replace('-',' ',$_REQUEST['pg_url'])));
$sql="insert into tbl_product_category set
parent_id='".$parentid."',
product_category_title='".str_replace("'","^",$_REQUEST['product_category_title'])."',
hindi_title='".str_replace("'","^",$_REQUEST['hindi_title'])."',
pg_url='".$page_url."',
page_url_link='".str_replace('&','and',$page_url_link)."',
hindi_url='".str_replace('&','and',$page_hindi_url_link)."',
pg_meta_title='".str_replace("'","^",$_REQUEST['pg_meta_title'])."',
pg_hindi_meta_title='".str_replace("'","^",$_REQUEST['pg_hindi_meta_title'])."',
pg_meta_keyword='".str_replace("'","^",$_REQUEST['pg_meta_keyword'])."',
pg_meta_desc='".str_replace("'","^",$_REQUEST['pg_meta_desc'])."',
pg_meta_title_location='".str_replace("'","^",$_REQUEST['pg_meta_title_location'])."',
pg_meta_keyword_location='".str_replace("'","^",$_REQUEST['pg_meta_keyword_location'])."',
pg_meta_desc_location='".str_replace("'","^",$_REQUEST['pg_meta_desc_location'])."',
imges_alttag='".str_replace("'","^",$_REQUEST['img_alttag'])."',
sort_description='".str_replace("'","^",$_REQUEST['sort_description'])."',
hindi_video='".$_REQUEST['hindi_video']."',
product_information='".str_replace("'","^",$_REQUEST['product_information'])."',
hindi_description='".str_replace("'","^",$_REQUEST['hindi_description'])."',
description='".str_replace("'","^",$_REQUEST['description'])."',
pg_status='".$_REQUEST['pg_status']."',
home_page_visible='".$_REQUEST['home_page_visible']."',
home_page_visible_with_text='".$_REQUEST['home_page_visible_with_text']."',
banner='".$banner."',
image='".$image."'";
if(mysqli_query($conn,$sql))
{$inserted_id = mysqli_insert_id($conn);
$_SESSION['add-page-msg']="Add Successfully";
if((mysqli_num_rows($prodsel)==0)&&($parentid>0)&&(!empty($_REQUEST['ProductMrp']))&&(!empty($_REQUEST['ProductSellingPrice'])))
{
$sql="insert into tbl_product set
product_cat='".$parentid."',
product_subcat='".$inserted_id."',
product_title='".str_replace("'","^",$_REQUEST['product_category_title'])."',
pg_url='".str_replace('&','and',$page_url_link)."',
page_url_link='".str_replace('&','and',$page_url_link)."',
pg_meta_title='".str_replace("'","^",$_REQUEST['pg_meta_title'])."',
pg_meta_keyword='".str_replace("'","^",$_REQUEST['pg_meta_keyword'])."',
pg_meta_desc='".str_replace("'","^",$_REQUEST['pg_meta_desc'])."',
pg_meta_title_location='".str_replace("'","^",$_REQUEST['pg_meta_title_location'])."',
pg_meta_keyword_location='".str_replace("'","^",$_REQUEST['pg_meta_keyword_location'])."',
pg_meta_desc_location='".str_replace("'","^",$_REQUEST['pg_meta_desc_location'])."',
imges_alttag='".str_replace("'","^",$_REQUEST['img_alttag'])."',
pg_status='".$_REQUEST['pg_status']."',
description='".str_replace("'","^",$_REQUEST['description'])."',
description_location='".str_replace("'","^",$_REQUEST['product_information'])."',
home_page_visible='".$_REQUEST['home_page_visible']."',
ProductColour='".$ProductColour."',
home_pg_showing_status='".$Product_home_pg_showing_status."',
sku_code='".$ProductSku."',
mrp_price='".$ProductMrp."',
discounted_price='".$ProductSellingPrice."',
discount='".$discount."',
banner='".$banner."',
image='".$image."'";

if(mysqli_query($conn,$sql))
{
$_SESSION['add-page-msg']="Add Successfully";
echo "<script>window.location.href='add-category.php?id=".$parentid."'</script>";
}
}
if(mysqli_num_rows($prodsel)>0)
{
$_SESSION['add-page-errormsg']="Page Url already exit please take other Page Url";
echo "<script>history.back(-1)</script>";
}









IF($parentid)
{
echo "<script>window.location.href='add-category.php?id=".$parentid."'</script>";
}
else
{
echo "<script>window.location.href='add-category.php'</script>";
}

}
}
else
{
$_SESSION['add-page-errormsg']="Page Url or Page Hindi Url already exit please take other Page Url";
echo "<script>history.back(-1)</script>";
}
?>