<?php
include("../include/config.php");
include("../include/functions.php"); 
include("../include/simpleimage.php");
  validate_admin();
  $cat_id=mysql_real_escape_string($_POST['cat_id']);
  $subcat_id=mysql_real_escape_string($_POST['subcat_id']);
  $childsubcat_id=mysql_real_escape_string($_POST['childsubcat_id']);
  $gchildsubcat_id=mysql_real_escape_string($_POST['gchildsubcat_id']);
  $brand_id=mysql_real_escape_string($_POST['brand_id']);
  $product_name=mysql_real_escape_string($_POST['product_name']);
  $description=mysql_real_escape_string($_POST['description']);
  $ingredients=mysql_real_escape_string($_POST['ingredients']);
  $how_to_use=mysql_real_escape_string($_POST['how_to_use']);
  $nutritional_facts=mysql_real_escape_string($_POST['nutritional_facts']);
  $meta_title=mysql_real_escape_string($_POST['meta_title']);
  $meta_keywords=mysql_real_escape_string($_POST['meta_keywords']);
  $meta_description=mysql_real_escape_string($_POST['meta_description']);
 
  #################################################### Product Price ######################################
  $product_id=ucfirst(mysql_real_escape_string($_POST['product_id']));
  $size=mysql_real_escape_string($_REQUEST['size']);
  $actual_price=mysql_real_escape_string($_REQUEST['actual_price']);
  $mrp_price=mysql_real_escape_string($_REQUEST['mrp_price']);
  $discount=mysql_real_escape_string($_REQUEST['discount']);
  $sell_price=mysql_real_escape_string($_REQUEST['sell_price']);
  $in_stock=mysql_real_escape_string($_REQUEST['in_stock']);
  $video=mysql_real_escape_string($_REQUEST['video']);
  $barcode_number=mysql_real_escape_string($_REQUEST['barcode_number']);
    #################################################### Product Price End ######################################
   
  if($_REQUEST['submitForm']=='yes'){
  if($_FILES['photo']['size']>0 && $_FILES['photo']['error']==''){
  $Image= new SimpleImage();
  $img=time().substr($_FILES['photo']['name'],-5);
  move_uploaded_file($_FILES['photo']['tmp_name'],"../upload_images/product/".$img);
  copy("../upload_images/product/".$img,"../upload_images/product/thumb/".$img);
  copy("../upload_images/product/".$img,"../upload_images/product/tiny/".$img);
  $Image->load("../upload_images/product/thumb/".$img);	  
  $Image->resize(124,85);	  
  $Image->save("../upload_images/product/thumb/".$img);	 
  $Image->load("../upload_images/product/tiny/".$img);	  
  $Image->resize(300,300);	  
  $Image->save("../upload_images/product/tiny/".$img);	 
  }
  
  if($_REQUEST['id']==''){
	  $obj->query("insert into $tbl_product set cat_id='$cat_id',subcat_id='$subcat_id',childsubcat_id='$childsubcat_id',brand_id='$brand_id',product_name='$product_name',nutritional_facts='$nutritional_facts',ingredients='$ingredients',how_to_use='$how_to_use',photo='$img',description='$description',gchildsubcat_id='$gchildsubcat_id', meta_title='$meta_title',meta_keywords='$meta_keywords',meta_description='$meta_description',posted_date=now(),status=1 ");
	    $product_id=mysql_insert_id();
	    $sql="insert into $tbl_productprice set product_id='$product_id',size='$size',actual_price='$actual_price',mrp_price='$mrp_price',discount='$discount',sell_price='$sell_price',in_stock='$in_stock' ,barcode='$barcodeimg',barcode_number='$barcode_number',pphoto='$img',status=1";
		$obj->query($sql);
		if(count($_REQUEST['att'])>0){
		   foreach($_REQUEST['att'] as $key=>$val){
			$obj->query("insert into $tbl_features set product_id='$product_id',attribute_id='$key',feature_value='$val' ");   
		   }
		}
		
	  $_SESSION['sess_msg']='Product added sucessfully';  
	  
       }else{ 
	 
	   $sql=" update $tbl_product set cat_id='$cat_id',subcat_id='$subcat_id',brand_id='$brand_id',childsubcat_id='$childsubcat_id',gchildsubcat_id='$gchildsubcat_id',product_name='$product_name',nutritional_facts='$nutritional_facts',ingredients='$ingredients',how_to_use='$how_to_use',description='$description', meta_title='$meta_title',meta_keywords='$meta_keywords',meta_description='$meta_description' ";
	  if($img){
		$imageArr=$obj->query("select photo from $tbl_product where id='".$_REQUEST['id']."' "); 
		$resultImage=$obj->fetchNextObject($imageArr);
		@unlink("../upload_images/product/".$resultImage->photo); 
		@unlink("../upload_images/product/tiny/".$resultImage->photo);
		@unlink("../upload_images/product/thumb/".$resultImage->photo);
		$sql.=" ,photo='$img' ";
	  }
	  $sql.=" where id='".$_REQUEST['id']."'";
	  $obj->query($sql);
	  $_SESSION['sess_msg']='Product updated sucessfully';   
        }
   header("location:product-list.php");
   exit();
  }      
	   
	   
if($_REQUEST['id']!=''){
$sql=$obj->query("select * from $tbl_product where id=".$_REQUEST['id']);
$result=$obj->fetchNextObject($sql);
}
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo SITE_TITLE; ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/admin.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" language="javascript">
function validate(obj)
{
if(obj.product_name.value==''){
alert("Please enter product name");
obj.product_name.focus();
return false;
}
if(obj.cat_id.value==''){
alert("Please select category");
obj.cat_id.focus();
return false;
}
if(obj.print_price.value==''){
alert("Please enter price");
obj.print_price.focus();
return false;
}

}
</script>
<?php /*?>
<script type="text/javascript" language="javascript" src="ajax.js"></script>
<script type="text/javascript" language="javascript">
function callSubcategory(cat_id){
url="callSubcat.php?cat_id="+cat_id;
//alert(url);
xmlhttpPost(url,cat_id,"getResponseSubcategory");

}
function getResponseSubcategory(str){
//alert(str);
document.getElementById("subcat1").innerHTML=str;
document.getElementById("childsubcat1").innerHTML='<select name="" style="width:250px;"><option value="">Select Child Subcategory</option></select>';
}
</script>
<script type="text/javascript" language="javascript">
function callChildSubcategory(subcat_id){
url="callchildSubcat.php?subcat_id="+subcat_id;
//alert(url);
xmlhttpPost(url,subcat_id,"getResponseCSubcategory");

}
function getResponseCSubcategory(str){
//alert(str);
document.getElementById("childsubcat1").innerHTML=str;
}
</script>

<?php */?>
<script type="text/javascript" src="../include/ckeditor/ckeditor.js"></script>
</head>
<body>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<?php include("header.php");?>
<script type="text/javascript">
$(document).ready(function(){
	$("#cat_id").change(function(){
		var cat_id=$(this).val();	
		
		$.ajax({
			url:"getSubcategory.php",
			data:{cat_id:cat_id},
			beforeSend:function(){
				$("#childsubcat_id").html("");
                $("#subcatfield").hide();
				$("#childsubcatfield").hide();
				$("#gchildsubcatfield").hide();
				$("#subcat_id").html('<option value="">Select Subcategory</option>');
				$("#gchildsubcat_id").html('');
				
				},
			success:function(data){
				$("#subcat_id").append(data);
				if(data!=''){
				$("#subcatfield").show();
				}
				$("#childsubcat_id").html("<option value=''>Select Child Subcategory</option>");
				$("#gchildsubcat_id").html("<option value=''>Select Grand Child Subcategory</option>");
				}
			
			})
			
			$.ajax({
			url:"getBrand.php",
			data:{cat_id:cat_id},
			beforeSend:function(){
				$("#brand_id").html('<option value="">Select Brand</option>');
				},
			success:function(data){
				$("#brand_id").append(data);
				}
			
			})
			$.ajax({			
			url:"getAttribute.php",
			data:{cat_id:cat_id},
			beforeSend:function(){
				$("#features").html('');
				},
			success:function(data){
				$("#features").html(data);
				}
			})
		
	})
	 $("#subcat_id").change(function(){
	  var cat_id=$('#cat_id').val();	
	  var subcat_id=$(this).val();
		$.ajax({
			url:"getChildSubcat.php",
			data:{subcat_id:subcat_id},
			beforeSend:function(){
				$("#childsubcatfield").hide();
				$("#gchildsubcatfield").hide();
				$("#chsubcat_id").html("<option value=''>Select Child Subcategory</option>");
				$("#gchsubcat_id").html("<option value=''>Select Grand Child Subcategory</option>");
				},
			success:function(data){
			   //alert(data);
			   if(data!=''){
				$("#childsubcat_id").append(data);
				$("#childsubcatfield").show();
			   }
				}
			
			})
		$.ajax({
			url:"getBrand.php",
			data:{cat_id:cat_id,subcat_id:subcat_id},
			beforeSend:function(){
				$("#brand_id").html('<option value="">Select Brand</option>');
				},
			success:function(data){
				$("#brand_id").append(data);
				}
			
			})
		
		})
		$("#childsubcat_id").change(function(){
	  var childsubcat_id=$(this).val();
		$.ajax({
			url:"getGChildSubcat.php",
			data:{childsubcat_id:childsubcat_id},
			beforeSend:function(){
			   $("#gchildsubcatfield").hide();
				$("#gchildsubcat_id").html("<option value=''>Select Grand Child Subcategory</option>");
				},
			success:function(data){
			  // alert(data);
			  if(data!=''){
				$("#gchildsubcat_id").append(data);
				$("#gchildsubcatfield").show();
			  }
				}
			
			})
		
		
		})
	
	})
</script>
<link rel="stylesheet" type="text/css" href="css/jquery.autocomplete.css" />
<script type="text/javascript" src="js/jquery.autocomplete.js"></script>
<script>
 $(document).ready(function(){
  $("#size").autocomplete("sizesSource.php", {
        //selectFirst: true
  });
 });
</script>
<script type="text/javascript">
function calcPrice(){
var mrp_price=document.getElementById('mrp_price').value;	
var dis=document.getElementById('discount').value;
if(mrp_price!='' && dis!='' ){
document.getElementById('sell_price').value=mrp_price-(mrp_price*dis/100);
}
}
</script>
<tr>
	<td align="right" class="paddRtLt70" valign="top">
		<table width="99%" border="0" cellspacing="0" cellpadding="0">
			<tr>
			
				<td align="right" valign="top">
					<table width="100%" border="0" cellspacing="0" cellpadding="0">
						<tr>
							<td align="left" valign="middle" class="headingbg bodr text14">
					<em><img src="images/arrow2.gif" width="21" height="21" hspace="10" align="absmiddle" /></em>Admin: Add Product 
					<span  style="float:right; padding-right:10px;">
					<input type="button" name="add" value="View Products"  class="button" onclick="location.href='product-list.php'" /></span></td>
						</tr>
						
						<tr>
							<td height="100" align="left" valign="top" bgcolor="#f3f4f6" class="bodr">
                            <form name="frm" method="POST" enctype="multipart/form-data" action="" onsubmit="return validate(this)">
						<input type="hidden" name="submitForm" value="yes" />
						<input type="hidden" name="id" value="<?php echo $_REQUEST['id'];?>" />
							<table width="100%" cellpadding="0" cellspacing="0">
									<tr>
										<td align="center" colspan="2" class="paddRt14 paddBot11">
										<font color="#FF0000"><strong><?php echo $_SESSION['sess_msg']; $_SESSION['sess_msg']='';?></strong></font></td>
									</tr>
									<tr>
                                      <td align="right" class="paddBot11 paddRt14"><h2>Product Details</h2></td>
									  <td align="left" class="paddBot11">&nbsp;</td>
							       </tr>
									<tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Product Name:</strong></td>
									  <td align="left" class="paddBot11"><input name="product_name" type="text" id="product_name" size="36" value="<?php echo stripslashes($result->product_name);?>" /></td>
							       </tr>
                                   	<tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Category :</strong></td>
									  <td align="left" class="paddBot11"><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="32%"><select name="cat_id" style="width:250px;" id="cat_id">
                                      <option value="">Select Category</option>
                                      <?php $catArr=$obj->query("select * from $tbl_category where status=1 order by id ",$debug=-1); 
									  while($resultCat=$obj->fetchNextObject($catArr)){
									  ?>
                                      <option value="<?php echo $resultCat->id; ?>"<?php if($resultCat->id==$result->cat_id){?>selected<?php } ?>><?php echo stripslashes($resultCat->category); ?></option>
                                      <?php } ?>
                                      </select></td>
    <td width="68%"><a href="category-addf.php" target="_blank">Add New Category</a></td>
  </tr>
</table>
</td>
							  </tr>
                             <tr id="subcatfield" style="display:none;">
                                      <td align="right" class="paddBot11 paddRt14"><strong>Subcategory :</strong></td>
									  <td align="left" class="paddBot11" ><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="31%" id="subcat1"><select  name="subcat_id"  id="subcat_id" style="width:250px;" >
 <option value="">Select Subcategory</option>
 <?php if($_REQUEST['id']!=''){
	 $subcatArr=$obj->query("select * from $tbl_subcategory where cat_id='".$result->cat_id."' and status=1  order by subcategory",$debug=-1);
	
 if($obj->numRows($subcatArr)){
 
 while($resulSubcat=$obj->fetchNextObject($subcatArr)){?>
 <option value="<?php echo $resulSubcat->id ;?>" <?php if($result->subcat_id==$resulSubcat->id ){?>selected<?php } ?>><?php echo stripslashes($resulSubcat->subcategory);?></option>
  <?php } } else { ?>
  <option value="-1">No Record</option>
  <?php } }?>
</select></td>
    <td width="69%"><a href="subcat-addf.php" target="_blank">Add New Subcategory</a></td>
  </tr>
</table>
</td>
							  </tr>
                               <tr id="childsubcatfield" style="display:none;">
                                      <td align="right" class="paddBot11 paddRt14"><strong>Child Subcategory :</strong></td>
									  <td align="left" class="paddBot11" ><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="31%" id="childsubcat1"><select  name="childsubcat_id" id="childsubcat_id" style="width:250px;"  >
 <option value="">Select Child Subcategory</option>
  <?php if($_REQUEST['id']!=''){
	   $csubcatArr=$obj->query("select * from $tbl_childsubcategory where subcat_id='".$result->subcat_id."' and status=1  order by childsubcategory",$debug=-1);
 if($obj->numRows($csubcatArr)){
 
 while($resultCsub=$obj->fetchNextObject($csubcatArr)){?>
 <option value="<?php echo $resultCsub->id ;?>" <?php if($result->childsubcat_id==$resultCsub->id ) {?>selected<?php } ?>><?php echo stripslashes($resultCsub->childsubcategory);?></option>
  <?php } } else { ?>
  <option value="-1">No Record</option>
  <?php } }?>
</select></td>
    <td width="69%"><a href="childsubcat-addf.php" target="_blank">Add New Child Subcategory</a></td>
  </tr>
</table>
</td>
							  </tr>
                              <tr id="gchildsubcatfield" style="display:none;">
                                      <td align="right" class="paddBot11 paddRt14"><strong>Grand Child Subcategory :</strong></td>
									  <td align="left" class="paddBot11" ><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="31%" id="childsubcat1"><select  name="gchildsubcat_id" id="gchildsubcat_id" style="width:250px;"  >
 <option value="">Select Grand Child Subcategory</option>
  <?php if($_REQUEST['id']!=''){
	   $csubcatArr=$obj->query("select * from $tbl_gchildsubcat where chsubcat_id='".$result->childsubcat_id."' and status=1  order by gchildsubcat",$debug=-1);
 if($obj->numRows($csubcatArr)){
 
 while($resultCsub=$obj->fetchNextObject($csubcatArr)){?>
 <option value="<?php echo $resultCsub->id ;?>" <?php if($result->gchildsubcat_id==$resultCsub->id){?>selected<?php } ?>><?php echo stripslashes($resultCsub->gchildsubcat);?></option>
  <?php } } else { ?>
  <option value="-1">No Record</option>
  <?php } }?>
</select></td>
    <td width="69%"><a href="gchildsubcat-addf.php" target="_blank">Add New Grand Child Subcategory</a></td>
  </tr>
</table>
</td>
							  </tr>
                              <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Brand :</strong></td>
									  <td align="left" class="paddBot11"><table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="32%"><select name="brand_id" id="brand_id" style="width:250px;" >
                                      <option value="">Select Brand</option>
                                      <?php /*$brandArr=$obj->query("select * from $tbl_brand where status=1 order by brand ",$debug=-1); 
									  while($resultBrand=$obj->fetchNextObject($brandArr)){
									  ?>
                                      <option value="<?php echo $resultBrand->id; ?>"<?php if($resultBrand->id==$result->brand_id){?>selected<?php } ?>><?php echo stripslashes($resultBrand->brand); ?></option>
                                      <?php } */?>
                                      </select></td>
    <td width="68%"><a href="brand-addf.php" target="_blank">Add New Brand</a></td>
  </tr>
</table>
</td>
							  </tr>
                           
                                     <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Image<br/>
                                      (300 X 300px)(Not more than 100 KB):</strong></td>
									  <td align="left" class="paddBot11"><input name="photo" type="file"  /><br/>
                                      <?php if(is_file("../upload_images/product/thumb/".$result->photo)) {?>
                                      <img src="../upload_images/product/thumb/<?php echo  $result->photo; ?>" />
                                      <?php } ?>
                                      </td>
							       </tr>
                                   <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Size :</strong></td>
									  <td align="left" class="paddBot11"><input name="size" type="text" id="size" size="36" value="<?php echo stripslashes($result->size);?>" /></td>
							  </tr>
                               <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Actual Price:</strong></td>
							    <td align="left" class="paddBot11"><input name="actual_price" type="text" id="actual_price" size="20" value="<?php echo stripslashes($result->actual_price);?>"  /> <strong><?php echo $website_currency_code; ?></strong></td>
							       </tr>
                              <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>MRP Price:</strong></td>
							    <td align="left" class="paddBot11"><input name="mrp_price" type="text" id="mrp_price" size="20" value="<?php echo stripslashes($result->mrp_price);?>"  onkeyup="return calcPrice()"/> <strong><?php echo $website_currency_code; ?></strong></td>
							       </tr>
                                   <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Discount:</strong></td>
								     <td align="left" class="paddBot11"><input name="discount" type="text" id="discount" size="20" value="<?php echo stripslashes($result->discount);?>"  onkeyup="return calcPrice()" />							          <strong> %</strong></td>
							       </tr> 
                                    <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Sell Price (After Discount) :</strong></td>
									  <td align="left" class="paddBot11"><input name="sell_price" type="text" id="sell_price" size="20" value="<?php echo stripslashes($result->sell_price);?>" />
								      <strong> <?php echo $website_currency_code; ?></strong></td>
							       </tr>
									 <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>IN Stock:</strong></td>
									  <td align="left" class="paddBot11">
                                      <input type="checkbox" name="in_stock" value="1" <?php if($result->in_stock==1 || $_REQUEST['id']==''){ ?>checked<?php } ?> />
								      </td>
							       </tr>
                                    
                                   
                                
                                   
                             
                                   <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Barcode Number:</strong></td>
									  <td align="left" class="paddBot11"><input name="barcode_number" type="text" id="barcode_number" size="36" value="<?php echo stripslashes($result->barcode_number);?>" /></td>
							  </tr>
                                     <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Video(YouTube URL):</strong></td>
								     <td align="left" class="paddBot11">
                                     <textarea name="video"  rows="5" cols="40"  id="video"><?php echo  $result->video; ?></textarea>						         </td>
							       </tr> 
                                
                                   
                                   
                                     <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong> Details:</strong></td>
									  <td align="left" class="paddBot11"><textarea name="description"  class="ckeditor" id="description" rows="5" cols="40"><?php echo stripslashes($result->description); ?></textarea></td>
							  </tr>
                              
                              <tr><td  colspan="2" id="features">
                              

                              
                              </td></tr>
                              <!--<tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong> Ingredients:</strong></td>
									  <td align="left" class="paddBot11"><textarea name="ingredients"  id="ingredients" rows="5" cols="40"><?php echo stripslashes($result->ingredients); ?></textarea></td>
							  </tr>
                              <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong> How To Use:</strong></td>
									  <td align="left" class="paddBot11"><textarea name="how_to_use"  id="how_to_use" rows="5" cols="40"><?php echo stripslashes($result->how_to_use); ?></textarea></td>
							  </tr>
                              <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong> Nutritional Facts:</strong></td>
									  <td align="left" class="paddBot11"><textarea name="nutritional_facts"  id="nutritional_facts" rows="5" cols="40"><?php echo stripslashes($result->nutritional_facts); ?></textarea></td>
							  </tr> -->
                              <tr>
                                      <td align="right" class="paddBot11 paddRt14"><h2>Meta Tags</h2></td>
									  <td align="left" class="paddBot11">&nbsp;</td>
							       </tr>
                             <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Meta Title:</strong></td>
									  <td align="left" class="paddBot11"><textarea name="meta_title" rows="5" cols="40"><?php echo stripslashes($result->meta_title); ?></textarea></td>
							  </tr>
                              <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Meta Keywords:</strong></td>
									  <td align="left" class="paddBot11"><textarea name="meta_keywords" rows="5" cols="40"><?php echo stripslashes($result->meta_keywords); ?></textarea></td>
							  </tr>
                              <tr>
                                      <td align="right" class="paddBot11 paddRt14"><strong>Meta Description:</strong></td>
									  <td align="left" class="paddBot11"><textarea name="meta_description" rows="5" cols="40"><?php echo stripslashes($result->meta_description); ?></textarea></td>
							  </tr> 
									<tr>
									  <td align="right" class="paddRt14 paddBot11">&nbsp;</td>
									  <td align="left" class="paddBot11">&nbsp;</td>
							  </tr>
									<tr>
										<td width="25%" align="right" class="paddRt14 paddBot11">&nbsp;</td>
										<td width="75%" align="left" class="paddBot11">
											<input type="submit" name="submit" value="Submit"  class="submit" border="0"/> 	                  		 &nbsp;&nbsp;
											<input name="Reset" type="reset" id="Reset" value="Reset" class="submit" border="0" />									  </td>
									</tr>
								</table></form>
							</td>
						</tr>
						
					</table>
				</td>
			</tr>
		</table>
	</td>
</tr>
<?php include('footer.php'); ?>
</table>

</body>
</html>


