<?
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 Product Enquiries";
$record_type="Product Enquiry";
$script_name="manage_product_enquiries_catalog.php";
$table_name="product_enquiry_tbl";
######################################################
if($id=="add_record") {
	if ($name!="" && $content!="") {
		$sql=db_query("insert into $table_name (name, email, title, content, recvDate)values('$name', '$email', '$title', '$content', '$wdt')");
		$_SESSION[session_message]='New '.$record_type.' successfully added';
	}
	else {
		$_SESSION[session_message]='Person Name and Testimonial Content are required.';
	}
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize."&add_id=".$add_id);
	exit;	
}

if($id=="update_record") {
	if ($name!="" && $content!="") {
		updateTable($table_name," name='$name', email='$email', title='$title', content='$content' where slno='$recordID'");
		$_SESSION[session_message]=$record_type.' is updated successfully';
	}
	else {
		$_SESSION[session_message]='Person Name and Testimonial Content are required.';
	}
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	exit;
}

if ($id=="Sendmail"){
	for ($x=0; $x < sizeof($_POST[cid]); $x++){
		$arr=$_POST[cid];
		$qur=getResult($table_name," where slno='$arr[$x]'");
		$To=$qur[email]; 
		$from=SITE_EMAIL;
		$fromName=getAdminname();
		$mess=str_replace("\n","<br>",$message);
		sent_mail($fromName,$from,$To,$subject,$mess);
		updateTable($table_name," replyStatus='Y' where slno='$arr[$x]'");
	}
	$_SESSION[session_message]='Mail has been sent to selected member(s)';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	exit;
}

if($id=="View"){
	adminheader_popup("View Member Details");
	echo view_member_details($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' where slno='$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 slno='$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' where slno='$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' where slno='$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 slno='$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' where slno='$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="<?=$PHP_SELF?>" 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 ($status!="") {
	$cond="where status='$status' and source='catalog'";
	$link="&status=$status";
}
else {
	$cond="where source='catalog'";
}
$qry="select * from $table_name $cond";
$keyword=trim($keyword);
if($keyword!=""){
  $qry.=" and (contactName LIKE '%$keyword%' or companyName LIKE '%$keyword%' or email LIKE '%$keyword%' or subject LIKE '%$keyword%' or comments LIKE '%$keyword%' )"; 
}

if($sortName!=''){
	$qry.= " order by $sortName $sortBy";
}
else {
	$qry.= " order by slno 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"><b>Search Trade Leads</b><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">
  <b>Sort Business Classified Enquiries<b><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='memberID' <? if($sortName=="memberID"){ echo "selected";}?>>Member ID</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">
   <b>Select Status</b><br/>
   <form name="sts_form" action="<?=$PHP_SELF?>" 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 Enquiries</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 />

<?
if($reccnt>0){
	?>
	<form name="formn" method="post" action="" enctype="multipart/form-data" onsubmit="return validate(this);">
	<table width="99%" border="0" cellpadding="2" cellspacing="2" class="lightBorder">
	 <tr height="25" bgcolor="#EAEAEA">
	  <td width="5%"><input type="checkbox" name="chk" onclick="CheckAll()" class="NoStyle"></td>
	  <td width="30%"><b>Sender Details</b></td>
	  <td width="35%"><b>Enquiry Details</b></td>
	  <td width="10%"><b>Receive Date</b></td>
	  <td width="10%" align="center"><b> Status</b></td>
	  <td width="10%"><b>Options</b></td>
	 </tr>
	 <?
	 $sl=$start+1;
	 while ($r2=mysql_fetch_array($qry)) {
		 $r2=ms_htmlentities_decode($r2);		 
		 if($sl%2==0){
			 $color="f0f0f0";
		 }
		 else{
			 $color="f9f9f9";
		 }
		 ?>
		 <tr bgcolor="<?=$color;?>">
		  <td valign="top"><input type=checkbox name="cid[]" value="<?=$r2[slno]?>" class="NoStyle"></td>
		  <td valign="top">
		   <b>Name</b>: <?=ucwords($r2[contactName]);?>
		   <?
		   if ($r2[companyName]) {
			   ?>
			   <br/><b>Company Name</b>: <?=ucwords($r2[companyName]);
		   }
		   if ($r2[email]) {
			   ?>
			   <br/><b>Email</b>: <?=$r2[email];
		   }
		   if ($r2[phone]) {
			   ?>
			   <br/><b>Phone</b>: <?=$r2[phone];
		   }
		   if ($r2[fax]) {
			   ?>
			   <br/><b>Fax</b>: <?=$r2[fax];
		   }
		   if ($r2[industry]) {
			   ?>
			   <br/><b>Industry</b>: <?=$r2[industry];
		   }
		   ?>
		  </td>
		  <td valign="top">
		   <?
		   if ($r2[subject]) {
			   ?>
			   <b>Subject</b>: <?=ucfirst($r2[subject]);
		   }
		   if ($r2[comments]) {
			   ?>
			   <br/><b>Comments</b>: <?=ucfirst($r2[comments]);
		   }
		   ?>
		  </td>
		  <td valign='top'><?=show_date($r2[recvDate])?></td>
		  <td valign='top' align="center">
		   <?
		   if($r2[status]=='Y') {
			   ?>
			   <strong>Active</strong>
			   <?
		   }
		   else if($r2[status]=='D') {
			   ?>
			   <strong>Deleted</strong>
			   <?
		   }
		   else {
			   ?>
			   <strong>Inactive</strong>
			   <?
		   }
		   ?>
		  </td>
		  <td valign='top'>
		   <?
		   if($r2[status]=='Y') {
			   ?>
			   &nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=deactivate&recordID=<?=$r2[slno];?>&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>
			   <?
		   }
		   if($r2[status]=='N') {
			   ?>
			   &nbsp;<a href="<?=SITE_ADMIN_URL?>/<?=$script_name;?>?id=activate&recordID=<?=$r2[slno];?>&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>
			   <?
		   }
		   if($r2[status]!='D') {
			   ?>
			   &nbsp;<a href="<?=$script_name;?>?id=delete&recordID=<?=$r2[slno]?>&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>
			   <?
		   }
		   ?>
		  </td>
		 </tr>
		 <?
		 $sl++;
	 }
	 ?>
	 <tr>
	  <td colspan="7">
	   <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><br>
	<?
	/*
	if ($add_id!="new") {
		?>
		<table width="100%"  border="0" cellpadding="6" cellspacing="2" class="lightBorder">
		 <tr>
		  <td width="10%" align="left"><b>Subject *</b></td>
		  <td align="left"><input type="text" name="subject" value="" size=100></td>
		 </tr>
		 <tr>
		  <td valign="top" align="left"><b>Message *</b></td>
		  <td align="left"><textarea name="message" cols=100 rows=10></textarea></td>
		 </tr>
		 <tr>
		  <td class="lightGrayBg">&nbsp;</td>
		  <td align="left" class="lightGrayBg"><input class="button" type="submit" name="id" value="Sendmail" onclick="return chksendmail(this);"></td>
		 </tr>
		</table>
		</form>
		<script type="text/javascript">
		var oFCKeditor=new FCKeditor('message');
		oFCKeditor.BasePath="fckeditor/";
		oFCKeditor.Height="350px";
		oFCKeditor.ReplaceTextarea();
		</script>
		<?
	}
	*/
}
else {
	?>
	<table width="100%"  border="0" cellpadding="6" cellspacing="2" class="lightBorder">
	 <tr><td><b>No Record Found.............</b></td></tr>
	</table>
	<?
}

admin_footer();
exit;
?>