<?
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;

$record_type="Membership Plan";
$script_name="manage_memberships.php";
$table_name="membership_plan_tbl";
####################################################

if($action=="add_record" && $planName!="") {
	$sql=db_query("select * from $table_name where planName='$planName'");
	$num=NumRows($sql);
	if($num>0) {
		$_SESSION[session_message]=$record_type.' already exist';
	}
	else{
		$sql=db_query("insert into $table_name (planName, planDuration, planCost)values('$planName', '$planDuration', '$planCost')");
		$_SESSION[session_message]='New '.$record_type.' has been added successfully';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
		exit;
	}
}

if($action=="update_record" && $planName!="" ) {
	$res=db_fetch_array(db_query("select * from $table_name where slno='$recordID'"));
	if($res[planName]!=$planName) {
		$sql=db_query("select * from $table_name where planName='$planName'");
		if(NumRows($sql)>0) {
			$_SESSION[session_message]=$record_type.' already exist';
			header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
			exit;
		}
	}
	updateTable($table_name," planName='$planName', planDuration='$planDuration', planCost='$planCost' where slno='$recordID'");
	$_SESSION[session_message]=$record_type.' has been updated successfully';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?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."?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%"><strong><b class="myTitle">Manage <?=$record_type;?> Options</b></strong></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">

<?
$qry1= db_query("select * from $table_name");
$reccnt = NumRows($qry1);
$qry= db_query("select * from $table_name 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">
 <?
 if($reccnt>0) {
	 ?>
	 <tr height="25" bgcolor="#EAEAEA">
	  <td width="5%" class="PadFive" align="center"><strong>S.No.</strong></td>
	  <td class="PadFive" width="30%" align="left"><strong>Membership Plan Name</strong></td>
	  <td class="PadFive" width="25%" align="left"><strong>Plan Duration</strong></td>
	  <td class="PadFive" width="25%" align="left"><strong>Plan Cost</strong></td>
	  <td class="PadFive" width="15%" align="center"><strong>Options</strong></td>
	 </tr>
	 <?
	 $sl=$start+1;
	 while($arr=mysql_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>
		  <td align="left"><?=$arr[planName];?></td>
		  <td align="left"><?=$arr[planDuration];?> Days</td>
		  <td align="left"><?=$arr[planCost];?> <?=CURRENCY;?></td>
		  <td align="center">&nbsp;<a href="<?=$script_name;?>?id=edit_record&start=<?=$start;?>&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;?>?id=delete_record&start=<?=$start;?>&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>
<?
	$sl++;
   }
 }
 else{
	echo '<tr><td><b>No Record Found........</b></td></tr>';	
 }
?>
</table><br />

<? if ($id=="edit_record") { ?>
<form name="cpFrm" method=post action="<?=$script_name;?>" enctype="multipart/form-data" onsubmit="return validate(this);">
<table width="99%"  border="1" cellpadding="2" cellspacing="2" class="lightBorder">
 <?
 if($recordID=="" || $id!="edit_record") {
	 ?>
	 <tr bgcolor="#EAEAEA"><td colspan="2" align="left" class="PadFive" height="20"><b><a name="bot">Add New <?=$record_type;?></a></b></td></tr>
	 <?
 }
 else {
	 ?>
	 <tr bgcolor="#EAEAEA">
	  <td align="left" class="PadFive" height="20"><a name="bot"></a><b>Edit <?=$record_type;?></b></td>
	  <td align="right" class="PadFive" height="20"> <!-- <a href="<?=$script_name;?>"><b>Add New <?=$record_type;?></b></a> !--> </td>
	 </tr>
	 <?
 }
 $res=getResult($table_name," where slno='$recordID'");
 @extract($res);
 ?>
 <tr><td colspan="2" align="right"><font color="red">*</font> fields are mandatory</td></tr>
 <tr>
  <td class="brdrbtm" width="25%"><b>Membership Plan Name <font color="red">*</font></b></td>
  <td class="brdrbtm"><input type="text" name="planName" size="60" value="<?=$planName;?>" readonly id="NOBLANK~Please enter membership plan name.~DM~" /></td>
 </tr>
 <tr>
  <td class="brdrbtm"><b>Membership Plan Duration <font color="red">*</font></b></td>
  <td class="brdrbtm"><input type="text" name="planDuration" size="46" value="<?=$planDuration;?>" id="NOBLANK~Please enter membership plan duration.~DM~NUM~Please enter numbers only.~DM~"/>&nbsp;&nbsp;<strong>In Days</strong></td>
 </tr>
 <tr>
  <td class="brdrbtm"><b>Membership Plan Cost <font color="red">*</font></b></td>
  <td class="brdrbtm"><input type="text" name="planCost" size="46" value="<?=$planCost;?>" id="NOBLANK~Please enter membership plan cost.~DM~NUM~Please enter numbers only.~DM~"/>&nbsp;&nbsp;<strong><?=CURRENCY;?></strong></td>
 </tr>
 <tr>
  <td class="brdrbtm"></td>
  <td class="brdrbtm">
   <?
   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="action" value="update_record">
	   <?	   
   }
   else{
	   ?>
	   <input type="submit" name="submit" value="Add New <?=$record_type;?>" class="button">
	   <input type="hidden" name="action" value="add_record">
	   <?
   }
   ?>   
  </td>
 </tr>
</table>
</form>
<? } 
admin_footer();
exit;
?>