<?php
// error_reporting(0);

// session_start();

// include '../include/connection.php';

// $conn     = db();

// @extract($_REQUEST);	

// if($_FILES['images']['name']){
//      $total = count($_FILES['images']['tmp_name']);
//     for($i=0; $i<$total; $i++)
//     {
//         $file_name = $_FILES['images']['name'][$i];
// 		$file_size = $_FILES['images']['size'][$i];
// 		$file_tmp  = $_FILES['images']['tmp_name'][$i];
		
// 		$t = explode(".", $file_name);
// 		$t1= end($t);
// 		$file_ext = strtolower($t1);
		
// 		$ext_boleh = array("jpg", "jpeg", "png", "gif", "webp");
		
// 		if(in_array($file_ext, $ext_boleh))
// 		{
// 		    $sumber = $file_tmp;
// 			$tujuan = $file_name;
// 			if(move_uploaded_file($sumber,"../upload/category/multi_images/".$tujuan))
// 			{
// 			     $repeat = mysqli_query($conn, "select * from tbl_product_image where product_id='".$_REQUEST['id']."'");
//                 $count=mysqli_num_rows($repeat);
//                 if($count > 5)
//                 {
//                     // echo "kabir";
//                      $sqlll=mysqli_query($conn, "update tbl_product_image set image='$tujuan' where product_id='".$_REQUEST['id']."'");
//                      if($sqlll)
//                      {
//                         echo "<script>window.location.href='manage-product-category.php?id=".$webresp['parent_id']."'</script>";
//                      }
//                 }
//                 else{
//                     $sql= mysqli_query($conn, "insert into tbl_product_image set product_id='".$_REQUEST['id']."', image='$tujuan', pg_status='Active'");
//                     if($sql)
//                     {
//                         echo "<script>window.location.href='manage-product-category.php'</script>";
//                     }
//                     // echo "insert into tbl_product_image set product_id='".$_REQUEST['id']."', image='$tujuan', pg_status='Active'";
//                 }
			   
// 			}
// 		}
// 	}
//     }
?>




<?php

if($_FILES['image']['name'])

{  

	$folder='../upload/category/';

				$tm=time();

				$u_userimage = "";

				$file=time().@str_replace(' ','-',$_FILES['image']['name']);

				if(move_uploaded_file(@$_FILES['image']['tmp_name'],$folder.$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);

									//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;

 }



?>

<?php

error_reporting(0);

session_start();

include '../include/connection.php';

$conn     = db();

@extract($_REQUEST);	

// @extract($_REQUEST);	

if($_FILES['images']['name']){
     $total = count($_FILES['images']['tmp_name']);
    for($i=0; $i<$total; $i++)
    {
        $file_name = $_FILES['images']['name'][$i];
		$file_size = $_FILES['images']['size'][$i];
		$file_tmp  = $_FILES['images']['tmp_name'][$i];
		
		$t = explode(".", $file_name);
		$t1= end($t);
		$file_ext = strtolower($t1);
		
		$ext_boleh = array("jpg", "jpeg", "png", "gif", "webp");
		
		if(in_array($file_ext, $ext_boleh))
		{
		    $sumber = $file_tmp;
			$tujuan = $file_name;
			if(move_uploaded_file($sumber,"../upload/category/multi_images/".$tujuan))
			{
			     $repeat = mysqli_query($conn, "select * from tbl_product_image where product_id='".$_REQUEST['id']."'");
                $count=mysqli_num_rows($repeat);
                if($count > 5)
                {
                    // echo "kabir";
                     $sqlll=mysqli_query($conn, "update tbl_product_image set image='$tujuan' where product_id='".$_REQUEST['id']."'");
                    //  if($sqlll)
                    //  {
                    //     echo "<script>window.location.href='manage-product-category.php?id=".$webresp['parent_id']."'</script>";
                    //  }
                }
                else{
                    $sql= mysqli_query($conn, "insert into tbl_product_image set product_id='".$_REQUEST['id']."', image='$tujuan', pg_status='Active'");
                    // if($sql)
                    // {
                    //     echo "<script>window.location.href='manage-product-category.php'</script>";
                    // }
                    // echo "insert into tbl_product_image set product_id='".$_REQUEST['id']."', image='$tujuan', pg_status='Active'";
                }
			   
			}
		}
	}
    }

$bannerfolder='../upload/banner/';

				

				if($_FILES['banner']['name'])

{



				$banerfile=time().@str_replace(' ','-',$_FILES['banner']['name']);

				if(move_uploaded_file(@$_FILES['banner']['tmp_name'],$bannerfolder.$banerfile))

				{

				$banner=$banerfile;					

				}

}

else

{



$websel=mysqli_query($conn,"select * from tbl_product_category where product_cat_id='".$_REQUEST['id']."'");

$webres=mysqli_fetch_array($websel);

$banner=$webres['banner'];

}





if(!$_FILES['image']['name'])

{

$websel=mysqli_query($conn,"select * from tbl_product_category where product_cat_id='".$_REQUEST['id']."'");

$webres=mysqli_fetch_array($websel);

$image=$webres['image'];

}



$webselp=mysqli_query($conn,"select * from tbl_product_category where product_cat_id='".$_REQUEST['id']."'");

$webresp=mysqli_fetch_array($webselp);

if($webresp['parent_id']==0)

{

	$backlink="manage-product-category.php";

}

else

{

	$backlink="manage-product-category.php?id=".$webresp['parent_id'];

}

$webselprod=mysqli_query($conn,"select * from tbl_product where product_subcat='".$_REQUEST['id']."'");

$webresprod=mysqli_fetch_array($webselprod);


$page_url= strtolower(str_replace('-',' ',$_REQUEST['pg_url']));

$page_url_link =  preg_replace('/\s+/', '-', strtolower(str_replace('-',' ',$_REQUEST['pg_url'])));

$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'];
$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']);


$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'])));



mysqli_query($conn,"update tbl_product_category set

product_category_title='".str_replace("'",'^',$_REQUEST['product_category_title'])."',

hindi_title='".str_replace("'","^",$_REQUEST['hindi_title'])."',

pg_url='".str_replace('-',' ',$_REQUEST['pg_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']."',

description_location='".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."' where product_cat_id='".$_REQUEST['id']."'");

mysqli_query($conn,"update tbl_product set
product_cat='".$webresprod['product_cat']."',
product_subcat='".$webresprod['product_subcat']."',
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."' where product_subcat='".$_REQUEST['id']."'");




$_SESSION['page-msg']="update Successfully";

?>

<script>window.location.href='<?php echo $backlink;?>'</script>

