<?php
include_once("../include/main.inc.php");
check_session('MyCpanel','index.php');

################ Paging Starts From Here ##################
@extract($_REQUEST);
$start = (intval($start)==0 or $start=="")?$start=0:$start;
$pagesize = intval($pagesize)==0?$pagesize=20:$pagesize;

$heading="Manage Courses";
$record_type="Product";
$script_name="manage_courses.php";
$table_name="product_tbl";
######################################################
if($id=="View"){
	adminheader_popup("View $record_type");
	echo view_product($recordID);
	adminfooter_popup();
	exit;
}

if($_POST[action]=="Delete"){
	if(sizeof($_POST[cid])==0) {
		$_SESSION[session_message]='Please select a record first.';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
		exit;
	}
	for ($x=0; $x < sizeof($_POST[cid]); $x++){
		$arr=$_POST[cid];
		updateTable($table_name," status='D', featuredProduct='N', expressProduct='N' where productID='$arr[$x]'");		
	}
	$_SESSION[session_message]='Record has been deleted from the admin panel.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	exit;
}

if($_POST[action]=="Activate"){
	if(sizeof($_POST[cid])==0) {
		$_SESSION[session_message]='Please select a record first.';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=$start&pagesize=$pagesize");
		exit;
	}
	for ($x=0; $x < sizeof($_POST[cid]); $x++){
		$arr=$_POST[cid];
		updateTable($table_name," status='Y' where productID='$arr[$x]'");
	}
	$_SESSION[session_message]='Selected record(s) activated successfully.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	exit;
}

if($_POST[action]=="Deactivate"){
	if(sizeof($_POST[cid])==0) {
		$_SESSION[session_message]='Please select a record first.';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=$start&pagesize=$pagesize");
		exit;
	}
	for ($x=0; $x < sizeof($_POST[cid]); $x++){
		$arr=$_POST[cid];
		updateTable($table_name," status='N', featuredProduct='N', expressProduct='N' where productID='$arr[$x]'");
	}
	$_SESSION[session_message]='Selected record(s) deactivated successfully.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	exit;
}


if($id=="deactivate") {
	updateTable($table_name," status='N', featuredProduct='N', expressProduct='N' where productID='$recordID' and status='Y' ");	
	$_SESSION[session_message]=$record_type.' has been deactivated successfully.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	exit;
}

if($id=="activate") {
	updateTable($table_name," status='Y' where productID='$recordID' and status='N' ");
	$_SESSION[session_message]=$record_type.' has been activated successfully.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	exit;
}

if ($id=="delete") {
	updateTable($table_name," status='D', featuredProduct='N', expressProduct='N' where productID='$recordID'");
	$_SESSION[session_message]=$record_type.' has been deleted from the admin panel.';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	exit;
}

admin_header("Manage $record_type");
?>
<table width="99%" border="0" align="center" cellpadding="0" cellspacing="0" valign="top">
 <tr>
	<td align="left" width="50%" class="myTitle"><?=$heading;?></td>
	<td align="right">
	 <form name="pgcolor_form" action="" method="post" style="margin:0px;padding:0px;">
	 <table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" valign="top">
	  <tr>
	   <td align="right">Records Per Page:
	    <select name="pagesize" onchange="this.form.submit();">
	     <option value="0">Select</option>
	     <?php
	     foreach($_GLOBALS[pgsize_arr] as $val) {
		     if(trim($pagesize)==trim($val)) {
			     echo '<option value="'.$val.'" selected>'.$val.'</option>';
		     }
		     else{
			     echo '<option value="'.$val.'">'.$val.'</option>';
		     }
	     }
	     ?>
	    </select>	    
		 </td>
		</tr>
	 </table>
	 </form>
	</td>
 </tr>
</table>
<hr size="2" width="99%" color="#DA251C">
 
<?
if ($featuredProduct!="") {
	if ($featuredProduct=="Y") {
		updateTable($table_name," status='Y', featuredProduct='$featuredProduct' where productID='$recordID'");
	}
	else {
		updateTable($table_name," featuredProduct='$featuredProduct' where productID='$recordID'");
	}
}


if ($expressProduct!="") {
	if ($expressProduct=="Y") {
		updateTable($table_name," status='Y', expressProduct='$expressProduct' where productID='$recordID'");
	}
	else {
		updateTable($table_name," expressProduct='$expressProduct' where productID='$recordID'");
	}
}


if ($status!="") {
	$cond="where status='$status' and prostatus='3'";
	$link="&status=$status";
}
else {
	$cond="where prostatus='3'";
}
$qry="select * from $table_name $cond";
$keyword=trim($keyword);
if($keyword!=""){
  $qry.=" and (productName LIKE '%$keyword%' or completeDescription LIKE '%$keyword%' )"; 
}

if($sortName!=''){
	$qry.= " order by $sortName $sortBy";
}
else {
	$qry.= " order by productID desc";
}

$qry1= db_query($qry);
$reccnt = NumRows($qry1);
$qry.= " limit $start, $pagesize";
$qry= db_query($qry);
echo pageNavigation($reccnt,$pagesize,$start,$link);
print_message();
?>

<table width="100%"  border="0" cellpadding="6" cellspacing="2" class="lightBorder">
 <tr>
  <td class="lightGrayBg" align="left"><strong>Search Products</strong> by name & description<br>
  <form name="search2" method="POST" action="">
   Keyword: <input name="keyword" type="text" class="input" size="25" value="<?=$keyword;?>">
   <input type="hidden"  class="button" name="id" value="Search2">
   <input type="submit"  class="button" name="search" value="Search">	
  </form>
  </td>
  <td class="lightGrayBg">
  <strong>Sort Products<strong><br>
  <form name="search3" method="POST" action="">
   <select name="sortName" size=1>
    <option value=''>--------Select---------</option>
    <option value='recvDate' <? if($sortName=="recvDate"){ echo "selected";}?>>Receive Date</option>
    <option value='productName' <? if($sortName=="productName"){ echo "selected";}?>>Product Name</option>    
   </select>
   <select name="sortBy">
    <option value=''>-----Sort By-----</option>
    <option value="asc" <? if($sortBy=="asc"){ echo "selected";}?>>Ascending</option>
    <option value="desc" <? if($sortBy=="desc"){ echo "selected";}?>>Descending</option>
   </select>
   <input type="hidden"  class="button" name="id" value="Search3">
   <input type="submit"  class="button" name="search" value="Sort">
  </form>
  </td>
  <td class="lightGrayBg">
   <strong>Select Status</strong><br/>
   <form name="sts_form" action="" method="post" style="margin:0px;padding:0px;">
	 <table width="100%" border="0" align="left" cellpadding="0" cellspacing="0" valign="top">
	  <tr>
	   <td align="left">
	    <select name="status" onchange="this.form.submit();">
	     <option value="">All Products</option>
	     <option value="Y" <? if($status=="Y"){ echo "selected";}?>>Active</option>
	     <option value="N" <? if($status=="N"){ echo "selected";}?>>InActive</option>
	     <option value="D" <? if($status=="D"){ echo "selected";}?>>Deleted</option>
	    </select>
	    <input type="hidden" name="keyword" value="<?=$keyword;?>" />
	    <input type="hidden" name="pagesize" value="<?=$pagesize;?>" />
		 </td>
		</tr>
	 </table>
	 </form>
	</td> 
 </tr>
</table><br />

<?php
if($reccnt>0){
	?>
	<form name="formn" method="post" action="" enctype="multipart/form-data" onsubmit="return validate(this);">
	<input type="hidden" name="recordID" value="" />
	<input type="hidden" name="featuredProduct" value="" />
	<input type="hidden" name="expressProduct" value="" />	
	<table width="99%" border="0" cellpadding="2" cellspacing="2" class="lightBorder">
	 <tr height="25" bgcolor="#EAEAEA">
	  <td width="4%"><input type="checkbox" name="chk" onclick="CheckAll()" class="NoStyle"></td>
	  <td width="25%"><strong>Member Details</strong></td>
	  <td width="30%"><strong>Product Details</strong></td>
	  <td width="7%" align="center"><strong>Image</strong></td>
	  <td width="11%"><strong>Added On</strong></td>
	  <td width="8%" align="center"><strong>Status</strong></td>
	  <td width="15%"><strong>Options</strong></td>
	 </tr>
	 <?php
	 $sl=$start+1;
	 while ($arr=mysql_fetch_array($qry)) {
		 $arr=ms_htmlentities_decode($arr);
		 $res=getResult("member_tbl"," where memberID='$arr[memberID]'");
		 if($sl%2==0){
			 $color="f0f0f0";
		 }
		 else{
			 $color="f9f9f9";
		 }
		 ?>
		 <tr bgcolor="<?=$color;?>">
		  <td valign="top"><input type=checkbox name="cid[]" value="<?=$arr[productID]?>" class="NoStyle"></td>
		  <td valign="top">
		   Name: <strong><?=ucwords($res[memberName]);?></strong>
		   <br/>Email: <strong><?=$res[email];?></strong>
		   <br/>Membership Type: <strong><?=getMembershipPlanName($res['membershipType']);?></strong>
		  </td>
		  <td valign='top'>
		   <strong>Product Name</strong>: <?=$arr[productName];?>
		   <br/><strong>Product Code</strong>: <?=$arr[productCode];?>
		   <br/><strong>Product Price</strong>: <?php echo $arr[productPrice];
		   if ($arr[discountedPrice] > 0) {
			   ?>
			   <br/><strong>Discounted Price</strong>: <?php echo $arr[discountedPrice];
		   }
		   if ($arr[brandName] != "") {
			   ?>
			   <br/><strong>Brand Name</strong>: <?php echo $arr[brandName];
		   }
		   if ($arr[modelType] != "") {
			   ?>
			   <br/><strong>Model Type</strong>: <?php echo $arr[modelType];
		   }
		   if ($arr[availability] == "Y") {
			   ?>
			   <br/><strong>Availability</strong>: Yes
			   <?php
		   }
		   else {
			   ?>
			   <br/><strong>Availability</strong>: No
			   <?php
		   }
		   ?>
		   <br/><strong>Main Category</strong>: <?=getCategoryName($arr[mainCategory]);?>
		   <br/><strong>Sub Categories</strong>: <?=getIndustryType($arr[industryType]);?>		   
		  </td>
		  <td valign="top" align="center">
		   <?php
		   if($arr['image1']!="") {
			   $file1=SITE_DIR_PATH."/uploaded_files/product_images/".$arr['image1'];
			   if(file_exists($file1)) {
				   $dimens=GetImageSize($file1);
				   if($dimens[0]>130) {
					   $dimens[0]=$dimens[0]+30;
					   $dimens[1]=$dimens[1]+80;
				   }
				   else{
					   $dimens[0]=200;
					   $dimens[1]=200;
				   }
				   ?>
				   <a href="javascript:void(0);" onClick="openwin('<?=SITE_ADMIN_URL;?>/popup.php?img=product_images/<?=$arr['image1'];?>', '<?=$dimens[0];?>', '<?=$dimens[1];?>')" class="linkgray fontbold"><strong>View</strong>
				   <?php
			   }
		   }
		   else {
			   ?>
			   <strong>No<br/>Image</strong>
			   <?php
		   }		   
		   ?>
		  </td>
		  <td valign='top'><?=show_date($arr[recvDate]);?></td>
		  <td valign='top' align="center">
		   <?php
		   if($arr[status]=='Y') {
			   ?>
			   <strong>Active</strong>
			   <?php
		   }
		   else if($arr[status]=='D') {
			   ?>
			   <strong>Deleted</strong>
			   <?php
		   }
		   else {
			   ?>
			   <strong>Inactive</strong>
			   <?php
		   }
		   ?>		   
		  </td>
		  <td valign='top'>
		   <?php		   
		   if($arr[status]=='Y') {
			   ?>
			   &nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=deactivate&recordID=<?=$arr[productID];?>&start=<?=$start;?>&pagesize=<?=$pagesize?>"><img src="<?=SITE_ADMIN_URL?>/images/active.gif" alt="Deactivate This <?=$record_type;?>" title="Deactivate This <?=$record_type;?>" border="0"></a>
			   <?php
		   }
		   if($arr[status]=='N') {
			   ?>
			   &nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=activate&recordID=<?=$arr[productID];?>&start=<?=$start;?>&pagesize=<?=$pagesize?>"><img src="<?=SITE_ADMIN_URL?>/images/deactive.gif" alt="Activate This <?=$record_type;?>" title="Activate This <?=$record_type;?>" border="0"></a>
			   <?php
		   }
		   if($arr[status] != "D") {
			   ?>
			   &nbsp;<a href="<?=SITE_URL_PATH;?>/product_details.php?product_id=<?=$arr['productID'];?>" target="_blank"><img src="<?=SITE_ADMIN_URL?>/images/preview.gif" alt="View This <?=$record_type;?> Details" title="View This <?=$record_type;?> Details" border="0"></a>
			   <? if($arr[status]!='D') { ?>
			   &nbsp;<a href="<?=$script_name;?>?id=delete&recordID=<?=$arr[productID]?>&start=<?=$start?>&pagesize=<?=$pagesize?>" onClick="return confirm('Are you sure you want to delete this record')"><img src="<?=SITE_ADMIN_URL?>/images/cancel.gif" alt="Delete This <?=$record_type;?>" title="Delete This <?=$record_type;?>" border="0"></a>
			   <?php } ?>
			   
			   <br/><br/>
			   <table width="100%" border="0" align="left" cellpadding="0" cellspacing="0" valign="top">
			    <tr>
			     <td align="center">
			      <select name="featuredProduct<?=$arr[productID];?>" onchange="return submit_form2(this.value,'<?=$arr[productID];?>');">
			       <option value="">Set Product As</option>
			       <option value="Y" <? if($arr[featuredProduct]=="Y"){ echo "selected";}?>>Featured</option>
			       <option value="N">Unset Featured</option>
			      </select>			      			      
			     </td>
			    </tr>
			   </table>
			   
			   <br/><br/>
			   <table width="100%" border="0" align="left" cellpadding="0" cellspacing="0" valign="top">
			    <tr>
			     <td align="center">
			      <select name="expressProduct<?=$arr[productID];?>" onchange="return submit_form3(this.value,'<?=$arr[productID];?>');">
			       <option value="">Set Product As</option>
			       <option value="Y" <? if($arr[expressProduct]=="Y"){ echo "selected";}?>>Express</option>
			       <option value="N">Unset Express</option>
			      </select>			      			      
			     </td>
			    </tr>
			   </table>
			   <?php
		   }
		   ?>
		  </td>
		 </tr>
		 <?php
		 $sl++;
	 }
	 ?>
	 <tr>
	  <td colspan="5">
	   <input type="submit"  class="button" name="action" value="Delete"  onclick="return chkdelete();">
	   <input type="submit"  class="button" name="action" value="Activate"  onclick="return chkactivate();">
	   <input type="submit"  class="button" name="action" value="Deactivate"  onclick="return chkactivate();">
	   <input type="hidden" name="start" value="<?=$start;?>">
	   <input type="hidden" name="pagesize" value="<?=$pagesize;?>">
	  </td>
	 </tr>
	</table>
	</form><br>
	<?php
}
else {
	?>
	<table width="100%"  border="0" cellpadding="6" cellspacing="2" class="lightBorder">
	 <tr><td><strong>No Record Found.............</strong></td></tr>
	</table>
	<?php
}

admin_footer();
exit;
?>