<?php

include("../include/config.php");

include("../include/functions.php"); 

include("../include/simpleimage.php");

  validate_admin();

  if($_REQUEST['id']==''){

	 header("location:wholesale-product-addf.php"); 

	 exit();

  } 

   $cat_id=mysql_real_escape_string($_POST['cat_id']);

  $catAr=getCategoryArray($cat_id,$array='');

  if(count($catAr)>0){

	$categories=implode(",",$catAr);  

  }

 /* echo "<pre>";

  print_r($_REQUEST);

  echo "</pre>";

  */

  $brand_id=mysql_real_escape_string($_POST['brand_id']);

  $product_name=mysql_real_escape_string($_POST['product_name']);

  $product_code=mysql_real_escape_string($_POST['product_code']);

  $description=mysql_real_escape_string($_POST['description']);

  $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']);

 

  if($_REQUEST['submitForm']=='yes'){

  if($_FILES['photo']['size']>0 && $_FILES['photo']['error']==''){

  $product_img_name = buildURL($product_name);

  $Image= new SimpleImage();

  $filename = $_FILES['photo']['name'];
  $ext = pathinfo($filename, PATHINFO_EXTENSION);
  
  $img=time()."-".$product_img_name.".".$ext;

  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(180,180);	  

  $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);	 

CompressImage('../upload_images/product/'.$img, '../upload_images/product/'.$img, 40);
CompressImage('../upload_images/product/thumb/'.$img, '../upload_images/product/thumb/'.$img, 40);
CompressImage('../upload_images/product/tiny/'.$img, '../upload_images/product/tiny/'.$img, 40);

  }

  

 	   $sql=" update $wholesale_product set cat_id='$cat_id',categories='$categories',brand_id='$brand_id',product_name='$product_name',product_code='$product_code',description='$description', meta_title='$meta_title',meta_keywords='$meta_keywords',meta_description='$meta_description' ";

	  if($img){

		$imageArr=$obj->query("select photo from $wholesale_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);

	  $obj->query("delete from  $tbl_features  where  product_id='".$_REQUEST['id']."' ");  

	  if(count($_REQUEST['att'])>0){		   

		   foreach($_REQUEST['att'] as $key=>$val){

			   if(is_array($val)){

				 $val=implode(",",$val);  

			   }

			   if($val!=''){

			$obj->query("insert into $tbl_features set product_id='".$_REQUEST['id']."',attribute_id='$key',feature_value='".mysql_real_escape_string($val)."' ");  

			   }

		   }

		}

	  

	  $_SESSION['sess_msg']='Product updated sucessfully';   

      

   header("location:wholesale-product-list.php");

   exit();

  }      

	   

	   

if($_REQUEST['id']!=''){

$sql=$obj->query("select * from $wholesale_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.cat_id.value==''){

alert("Please select category");

obj.cat_id.focus();

return false;

}

if(obj.product_name.value==''){

alert("Please enter product name");

obj.product_name.focus();

return false;

}







}

</script>



<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");?>



<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />

<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

<script type="text/javascript">

 $(document).ready(function(){

$('#category').autocomplete({

	 search: function () {},

    source: function (request, response)

    {

        $.ajax(

        {

            url:"wholesale-getCategoryTree.php" ,

            dataType: "json",

            data:

            {

                q: request.term,

            },

            success: function (data)

            {

                response(data);

            }

        });

    },

    minLength: 2,

   select: function( event, ui ) {

		$( "#category" ).val( ui.item.value );

		$( "#hdnId" ).val( ui.item.key );

        

		var cat_id=$( "#hdnId" ).val();	

		//alert(cat_id);

		 $.ajax({

			url:"wholesale-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:"wholesale-getAttribute.php",

			data:{cat_id:cat_id,product_id:"<?php echo $_REQUEST['id']; ?>"},

			beforeSend:function(){

				$("#features").html('');

				},

			success:function(data){

				$("#features").html(data);

				}

			})

		

	

		return false;

	  }

});

	  

 })

</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='wholesale-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>Category :</strong></td>

									  <td align="left" class="paddBot11">

                                      <input type="text" name="category"  id="category" style="width:500px;" value="<?php echo getCategoryTree($result->cat_id,$array=''); ?>"/>

                                      <input type="hidden" name="cat_id" id="hdnId" value="<?php echo $result->cat_id; ?>" />                    

                                    </td>

							  </tr>

                              <tr>

                                      <td align="right" class="paddBot11 paddRt14"><strong>Brand :</strong></td>

									  <td align="left" class="paddBot11"><select name="brand_id" id="brand_id" style="width:250px;" >

                                      <option value="">Select Brand</option>

                                      <?php $cat_id=getMainParent($result->cat_id);

									  $brandArr=$obj->query("select * from $tbl_brand where status=1 and bcat_id='".$cat_id."' 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>

							  </tr>

                              <tr>

                                      <td align="right" class="paddBot11 paddRt14"><strong>Product Code:</strong></td>

									  <td align="left" class="paddBot11"><input name="product_code" type="text" id="product_code" size="36" value="<?php echo stripslashes($result->product_code);?>" /></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>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> 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">

                              <?php ###################################### Features ##################################################

							 

	 $featArr=$obj->query("select * from  tbl_features where product_id='".$_REQUEST['id']."' ");

	 $fArray='';

	 while($resultFeature=$obj->fetchNextObject($featArr)){

		$fArray[$resultFeature->attribute_id]= stripslashes($resultFeature->feature_value);

	 }

//print_r($fArray);

if($cat_id!=''){

	$groupArr=$obj->query("select attribute_ids from $wholesale_maincategory join $tbl_attributegroup on $tbl_attributegroup.id=$wholesale_maincategory.group_id  where $wholesale_maincategory.id='".$cat_id."' ");

	if($obj->numRows($groupArr)>0){

	$rsGroup=$obj->fetchNextObject($groupArr);

	if($rsGroup->attribute_ids!=''){

$AttrbueArr=$obj->query("select * from $tbl_attribute where id in($rsGroup->attribute_ids) ");	

}

?>



<table width="100%" border="0" cellspacing="0" cellpadding="0">

                               <?php while($resultAttribute=$obj->fetchNextObject($AttrbueArr)){ ?>

                            <tr>

                         <td width="25%" align="right" class="paddBot11 paddRt14"><strong> <?php echo stripslashes($resultAttribute->attribute); ?>:</strong></td>

				         <td width="75%" align="left" class="paddBot11">

                          <?php if($resultAttribute->attribute_type=='Dropdown'){?>

                          <select name="att[<?php echo stripslashes($resultAttribute->id); ?>]" style="width:240px;">

                          <option value="">-Select-</option>

                          <?php $AttrbuteValueArr=$obj->query("select * from $tbl_attributevalue where attribute_id='".$resultAttribute->id."' ");

						  while($resultVal=$obj->fetchNextObject($AttrbuteValueArr)){?>

                           <option value="<?php  echo stripslashes($resultVal->attributevalue);?>" <?php if($fArray[$resultAttribute->id]==$resultVal->attributevalue){ ?>selected<?php } ?>><?php  echo stripslashes($resultVal->display_value);?></option>

                          <?php } ?>

                          </select>

                         <?php } ?>

                         

                         <?php if($resultAttribute->attribute_type=='MultipleSelect'){

							 $mutiArr='';

					         $mutiArr=explode(",",$fArray[$resultAttribute->id]);

							// print_r($mutiArr);

							 ?>

                          <select name="att[<?php echo stripslashes($resultAttribute->id); ?>][]"   multiple style="width:240px;">

                          <option value="">-Select-</option>

                          <?php $AttrbuteValueArr=$obj->query("select * from $tbl_attributevalue where attribute_id='".$resultAttribute->id."' ");

						  while($resultVal=$obj->fetchNextObject($AttrbuteValueArr)){?>

                           <option value="<?php  echo stripslashes($resultVal->attributevalue);?>" <?php if($mutiArr!='' && in_array($resultVal->attributevalue,$mutiArr)){ ?>selected<?php } ?> ><?php  echo stripslashes($resultVal->display_value);?></option>

                          <?php } ?>

                          </select>

                         <?php } ?>

                         

                         <?php if($resultAttribute->attribute_type=='Yes/No'){?>

                         <input type="radio" name="att[<?php echo stripslashes($resultAttribute->id); ?>]" <?php if($fArray[$resultAttribute->id]=='Yes'){ ?>checked<?php } ?>  value="Yes" />

                         <input type="radio" name="att[<?php echo stripslashes($resultAttribute->id); ?>]" <?php if($fArray[$resultAttribute->id]=='No'){ ?>checked<?php } ?>  value="No" />

                         <?php } ?>

                         

                          <?php if($resultAttribute->attribute_type=='Textbox'){?>

                         <input type="text" name="att[<?php echo stripslashes($resultAttribute->id); ?>]"   value="<?php echo $fArray[$resultAttribute->id]; ?>" />

                         <?php } ?>

                         

                         <?php if($resultAttribute->attribute_type=='Date'){?>

                         <link rel="stylesheet" href="calender/css/jquery-ui.css">

	<script src="calender/js/jquery-1.9.1.js"></script>

	<script src="calender/js/jquery-ui.js"></script>

	<script>

	$(function() {

		$( "#feature_date" ).datepicker({

			changeMonth: true,

			changeYear: true,

			yearRange:'2013:<?php echo date('Y') ?>'

		});

	});

	</script>

                         <input type="text" name="att[<?php echo stripslashes($resultAttribute->id); ?>]" id="feature_date"   value="<?php echo $fArray[$resultAttribute->id]; ?>" />

                         <?php } ?>

                         

                         <?php if($resultAttribute->attribute_type=='TextArea'){?>

                         <textarea name="att[<?php echo stripslashes($resultAttribute->id); ?>]"   rows="5" cols="40"><?php echo $fArray[$resultAttribute->id]; ?></textarea>

                         <?php } ?>

                         </td>

							  </tr>

                              <?php } ?>

</table>



<?php } } ?>



                              

                              </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>