<?php
include_once("../include/main.inc.php");
check_session('MyCpanel','index.php');
?>
<?php
################ Paging Starts From Here ##################
@extract($_REQUEST);
$start = (intval($start)<0 or $start=="")?0:$start;
$pagesize = intval($pagesize)==0?$pagesize=20:$pagesize;

$heading="Manage FAQ";
$record_type="FAQ";
$script_name="manage_faq.php";
$table_name="faq_tbl";
####################################################

if($id=="add_record") {
		$sql=db_query("insert into $table_name set question='$question', answer='$answer', category_id='$category_id'");
		$_SESSION['session_message']='New '.$record_type.' has been added successfully';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?category_id=".$category_id."&start=".$start."&pagesize=".$pagesize);
		exit;
}

if($id=="update_record") {
		updateTable($table_name," question='$question', answer='$answer' where slno='$recordID'");
		$_SESSION['session_message']=$record_type.' has been updated successfully';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?category_id=".$category_id."&start=".$start."&pagesize=".$pagesize);
		exit;
}

if($id=="delete_record") {
	db_query("delete from $table_name where slno='$recordID'");
	$_SESSION['session_message']=$record_type.' has been deleted successfully';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?category_id=".$category_id."&start=".$start."&pagesize=".$pagesize);
	exit;
}

admin_header("");
?>
<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="pgimg_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">
<?php
$qry1= db_query("select * from $table_name");
$reccnt = mysqli_num_rows($qry1);
$qry= db_query("select * from $table_name where category_id='$category_id' order by slno desc limit $start, $pagesize");
echo pageNavigation($reccnt,$pagesize,$start);
print_message();
?>
<table width="99%" border="0" cellpadding="2" cellspacing="2" class="lightBorder">
 <?php
 if($reccnt>0) {
	 ?>
	  <tr height="25" bgcolor="#EAEAEA">
	  <td width="5%" align="center"><strong>S.No.</strong></td>
	  <?php
		  if($category_id != 0){  ?>
		   <td width="20%" align="left"><strong>Category</strong></td><?php } ?>
	 
	  <td width="20%" align="left"><strong>Ques</strong></td>
	  <td width="20%" align="left"><strong>Ans</strong></td>
	  <td width="10%" align="left"><strong>Options</strong></td>
	  </tr>
	 <?php
	 $sl=$start+1;
	 while($arr=mysqli_fetch_array($qry)) {
		//  $arr=ms_htmlentities_decode($arr);
		 if($sl%2!=0) {
			 $color="#f9f9f9";
		 }
		 else{
			 $color="#f0f0f0";
		 }
		 ?>		 
		  <tr bgcolor="<?=$color;?>">
		  <td align="center"><?=$sl;?></td>
		  <?php
		  if($category_id != 0){  ?>
		  <td align="left"><?= get_category_name($category_id)?></td><?php } ?>
		  <td align="left"><?=$arr['question'];?></td>
		  <td align="left"><?=$arr['answer'];?></td>
		  
		  <td align="center">
		  	&nbsp;<a href="<?=$script_name;?>?category_id=<?= $category_id?>&id=edit_record&start=<?=$start;?>&pagesize=<?=$pagesize?>&recordID=<?=$arr['slno'];?>#bot"><img src="<?=SITE_ADMIN_URL?>/images/edit.gif" alt="Edit This <?=$record_type;?>" title="Edit This <?=$record_type;?>" border="0"></a>&nbsp;<a href="<?=$script_name;?>?category_id=<?= $category_id?>&id=delete_record&start=<?=$start;?>&pagesize=<?=$pagesize?>&recordID=<?=$arr['slno'];?>" onClick="return confirm('Are you sure to delete this record...');"><img src="<?=SITE_ADMIN_URL?>/images/cancel.gif" alt="Delete This <?=$record_type;?>" border="0"></td>
		 </tr>
		 <?php
		 $sl++;
	 }
 }
 else{
	 ?>
 <tr><td><b>No Record Found........</b></td></tr>
 <?php
 }
 ?>
</table><br />

<form name="bannerFrm" method=post action="" enctype="multipart/form-data" onSubmit="return validate('bannerFrm') && chk_valid_date();">
 <?php
 if($_GET['recordID']=="" || $id!="edit_record") {
	 ?>
	 <div class="text-center myTitle">Add New <?=$record_type;?></div>
	 <?php
 }
 else {
	 ?>
	<div class="text-left"><b>Edit <?=$record_type;?></b></div>
	 <div class="text-right"><a href="<?=$script_name;?>"><b>Add New <?=$record_type;?></b></a>
	 </div>
	 <?php
 }
 $res=getResult($table_name," where slno='$_GET[recordID]'");
 if($category_id != '0'){
 ?>
<div class="form-group">
    <label>Category<span>*</span></label>
    <input size='60' type="text" name="category_name" readonly value="<?= get_category_name($category_id)?>" class="form-control">
</div>	
<?php } ?>
<div class="form-group">
    <label>Question<span>*</span></label>
    <input type="text" name="question" size="60" value="<?=$res['question'];?>" class="form-control" placeholder="Enter Question" required/>
</div>	
 <div class="form-group">
    <label>Answer<span>*</span></label>
   <textarea name="answer" cols="60" class="form-control" placeholder="Enter Answer" required><?= $res['answer']?></textarea>
</div>
 <div class="form-group">
 <?php
   if($recordID !="") {
	   ?>
	   <input type="submit" name="submit" value="Update <?=$record_type;?>" class="button">
	   <input type="hidden" name="recordID" value="<?=$res['slno'];?>">
	   <input type="hidden" name="id" value="update_record">
	   <?php	   
   }
   else{
	   ?>
	   <input type="submit" name="submit" value="Add New <?=$record_type;?>" class="button">
	   <input type="hidden" name="id" value="add_record">
	   <?php
   }
   ?>
   <input type="hidden" name="start" value="<?=$start;?>">
   <input type="hidden" name="pagesize" value="<?=$pagesize;?>">
</div>
</form>
<script src="jquery.min.js"></script>
<?php
admin_footer();
exit;
?>