<?php include("links/top.php");

$action=$_GET['action'];

if(isset($_GET['pid'])){$pid=$_GET['pid'];}

$pg=25;

if(isset($_GET['page'])){$page=$_GET['page'];}else{$page=1;}

$lt=($page-1)*$pg;

?>

<?php

if($action=='delete'){

$id=$_GET['id'];



$del=executeQuery("delete from products where id='$id'");



header('Location:products.php?action=list');

}

?>

	<?php

	if($action=='addnew' || $action== 'edit'){

	if($action=='edit'){

	$id=$_GET['id'];

	}

	if(isset($_POST['submit']) || isset($_POST['update'])){



	extract($_POST);

	

	//$seo=str_replace('"','',str_replace("'","",str_replace($special_array,$special_replace,strtolower(trim($title)))));

	

	if(totalRecords($sel)>0){$error="URL already Exists";}

	if(!isset($error)){

	$today=date('Y-m-d H:i:s');

	if($action=='addnew'){

		$arr=array('title' 	=> mysql_real_escape_string($title),'description' 	=> mysql_real_escape_string($description),'now' 	=> $today,);

		$DB->insert('products',$arr);

		$id=getRecordId();

	}

	if($action=='edit'){

		$arr=array('title' 	=> mysql_real_escape_string($title),'description' 	=> mysql_real_escape_string($description));

		$DB->update('products',$arr,"id='$id'");

		

	}

	

if($_FILES['file']['name']!='')

{

//$code=time();

$filepath="uploads/images/";

$extension = strtolower(strrchr($_FILES['file']['name'], '.'));

if($extension=='.jpg' || $extension=='.jpeg' || $extension=='.png' || $extension=='.gif' || $extension=='.JPG' || $extension=='.JPEG' || $extension=='.PNG' || $extension=='.GIF' || $extension=='.bmp' || $extension=='.BMP'){

$image = $filepath.$_FILES['file']['name'].$extension;

move_uploaded_file($_FILES['file']['tmp_name'],'../'.$image);

$arr=array( 'image' 	=> $image);

$DB->update('products',$arr,"id='$id'");

}}

header('Location: products.php?action=list');

	}}

	}

	?>

		<div id="content">

			<div class="container">

				<div class="page-header">

					<div class="page-title">

						<h3>Products Management</h3>

					</div>

				</div>

				<div class="row">



					<div class="col-md-12">

                    <?php if($action=='list'){?> <a href="products.php?action=addnew"><button class="btn btn-sm btn-info">Add Product</button></a> <?php }else if($action=='imglist'){ ?>

                    <a href="products.php?action=addimage&pid=<?php echo $pid;?>"><button class="btn btn-sm btn-info">Add Image</button></a>

                 <a href="products.php?action=list"><button class="btn btn-sm btn-info pull-right">Go to Products</button></a>

                 <?php } ?>

						<div class="widget box">

                        <div class="widget-header">

						<?php if($action=='list'){?>

                            <h4>Products List</h4>

                            <?php }else if($action=='addnew'){ ?>

                            <h4>Add Product</h4>

                            <?php }?>

							</div>

                        <div class="widget-content">

                             <?php if($action=='list'){?>

								  

								<table class="table table-striped table-bordered table-hover table-checkable datatable">

									<thead>

										<tr>

											<!-- <th class="checkbox-column"><input type="checkbox" class="uniform"></th> -->

                                            <th>S.No</th>

                                             <th>Home</th>
                                             
                                             <th>Title</th>

                                             <th>Image</th>

                                             <th>Status</th>

                                             <th>Action</th>

										</tr>

									</thead>

									<tbody>

                         <?php

                         $sel=executeQuery("select * from products limit $lt,$pg");

								  $sno=1;

								 while($rec=getAssoc($sel)){?>

										<tr>

											<!-- <td class="checkbox-column"><input type="checkbox" id="" class="uniform"></td> -->

                                 <td><?php echo $sno;?></td>

                                

                                 <td class="checkbox-column"><input type="checkbox" onclick="checkhome('products','<?php echo $rec['id'];?>','home')" class="uniform" value="1" id="home<?php echo $rec['id'];?>" <?php if($rec['home']==1){echo 'checked="checked"';}?>></td>

                               
                                 <td><?php echo $rec['title'];?></td>

                                  <td><img src="<?php if($rec['image']!=''){echo '../'.$rec['image'];}else{echo 'assets/img/no-image.gif';}?>" style="height:40px;" /></td>

                                 
                                 <td><a href="javascript:void(0)" id="status<?php echo $rec['id'];?>" onClick="return status(<?php echo $rec['id'];?>,'products')"><?php if($rec['status']==1){echo 'Active';}else{echo 'Inactive';}?></a></td>

											<td>

                                             <a href="products.php?action=edit&id=<?php echo $rec['id'];?>" title="Edit"><i class="icol-pencil"></i></a> &nbsp;&nbsp;

										 <a href="products.php?action=delete&id=<?php echo $rec['id'];?>" onClick="return confirm('Are you sure you want to delete ?')" title="Delete"><i class="icol-cross"></i></a>

                                            </td>

										</tr>

                                        <?php $sno++;}?>

									</tbody>

								</table>

								<div class="pull-right"><?php echo Pages('products',$pg,'id!=0','products.php?action=list&');?></div>

                            <?php }else if($action=='addnew' || $action=='edit'){?>



							<?php if($action=='edit'){

                            $id=$_GET['id'];

                            $e="select * from products where id='$id'";

                            $ed=$DB->executeQuery($e);

                            $edi=$DB->getAssoc($ed);

                            }?>



								<form class="form-horizontal" id="validate-1" method="post" novalidate="novalidate" enctype="multipart/form-data">

									

									<div class="form-group">

										<label class="col-md-2 control-label">Title <span class="required">*</span></label>

										<div class="col-md-4">

											<input type="text" name="title" class="form-control required" value="<?php if(isset($_POST['title'])){echo $title;}else if($action=='edit'){echo $edi['title'];}?>">

										</div>
                                        
										</div>

									 

                          <div class="form-group">

                           <label class="col-md-2 control-label">Image <span class="required">*</span></label>

                           <div class="col-md-2 fileupload fileupload-new" data-provides="fileupload">

                                   <div class="fileupload-new thumbnail" style="width: 180px;">

                                  <img src="<?php if($action=='edit'){if($edi['image']!=''){ echo '../'.$edi['image'];}else{echo 'assets/img/no-image.gif';}}else{echo 'assets/img/no-image.gif';}?>" style="width:170px;" />

                                   </div>

                                   <div class="fileupload-preview fileupload-exists thumbnail"></div>

                                   <div>

                                       <span class="btn btn-file"><span class="fileupload-new">Select image</span><span class="fileupload-exists" style="padding:6px;">Change</span><input type="file" name='file' accept="image/*"  /></span><br />
                                               <span style="color:#F00;"> Image Size[360 X 195];</span>


                                       <a href="#" class="btn fileupload-exists" style="padding:4px 7px!important;" data-dismiss="fileupload">Remove</a>

                                   </div>

                               </div>

										
									</div>


                                    <div class="form-group">

                                    <label class="col-md-2 control-label">Description</label>

										<div class="col-md-12" style="clear:both;margin-top:40px;">

											<textarea id="description" name="description" class="ckeditor" rows="6"><?php if($action=='edit'){echo $edi['description'];}?></textarea>

										</div>

									</div>
                                    
									<div class="form-actions">

                                    <label class="col-md-2">&nbsp;</label>

									<?php if($action=='addnew'){?>

									<input class="btn btn-info pull-left" type="submit" id="submit" name="submit" value="Submit" />

									<?php }else if($action=='edit'){?>

									<input class="btn btn-info pull-left" type="submit" id="submit" name="update" value="Update" />

									<?php }?>

									</div>

								</form>

						<?php }?>

                        </div>

                    </div>

                </div>

            </div>

        </div>

        </div>

 <script>

 function geteggless(val) {

 	if(val==1){document.getElementById("eggless_price").disabled=false;}

 	else {document.getElementById("eggless_price").disabled=true;}

 }

	function add_imgs()

	{

		var fcount  = document.getElementById('fm').value;

		var sid		= parseInt(fcount)+1;

		document.getElementById('fm').value=parseInt(fcount)+1;

		$.ajax({

				   type: "POST",

				   url: "image_add.php",

				   data: "fmcount="+sid,

				   success: function(msg){

					 $('#mydiv').append(msg);

					   }

				 });

	}

	function delete_img(delid)

	{

		var str="products_img";

		$.ajax({

				   type: "POST",

				   url: "image_delete.php",

				   data: "delid="+delid+"&str="+str,

				   success: function(msg){

				 	 document.getElementById('del'+delid).style.display="none";

					   }

				 });

	}



$('a.add_group').click(function() {

	val=parseInt(document.getElementById("group_count").value);

	document.getElementById("group_count").value=val+1;

    $(".option-group").append('<div class="form-group group'+val+'"><label class="col-md-2 control-label">Group '+(val+1)+'</label><div class="col-md-4"><input type="text" name="group[]" class="form-control" value=""></div></div><div class="options'+val+'"><input type="hidden" id="option_count'+val+'" value="0"></div><div class="form-group"><label class="col-md-2 control-label"><a href="javascript:void(0)" onclick="option('+val+')" style="color:blue;">Add Option</a></label></div>');

    return false;

});

function option(iid) {

	val=parseInt(document.getElementById("option_count"+iid).value)+1;

	document.getElementById("option_count"+iid).value=val;

    $(".options"+iid).append('<div class="form-group option"'+val+'><label class="col-md-2 control-label">Option '+val+'</label><div class="col-md-4"><input type="text" name="option['+(iid)+'][]" class="form-control" value=""></div><label class="col-md-2 control-label">Price</label><div class="col-md-4 clearfix"><div class="input-group"><span class="input-group-addon">₹</span><input type="text" name="option_price['+(iid)+'][]" class="form-control" value=""></div></div></div>');

    return false;

}

function checkhome(tab,iid,str)

{

var val=document.getElementById(str+''+iid).checked;

$.ajax({

   type: "POST",

   url: "checkhome.php",

   data: "val="+val+"&iid="+iid+"&str="+str+"&tab="+tab,

   success: function(msg){}

 });

}

function checkprod(val,str,iid,fid)

{

$.ajax({

   type: "POST",

   url: "checkprod.php",

   data: "val="+val+"&iid="+iid+"&str="+str+"&fid="+fid,

   success: function(msg){

	  document.getElementById(fid+"_err").innerHTML=msg;

	  document.getElementById(fid+"_err").focus();

	  if(msg!=''){document.getElementById("submit").disabled=true;}

	  else{document.getElementById("submit").disabled=false;}

	  }

 });

}

</script>

	<?php include("links/bottom.php");?>