<?php
include_once("../include/main.inc.php");
check_session('MyCpanel','index.php');
?>
<script type="text/javascript">
function chk_valid_date() {
	if (document.bannerFrm.validFrom.value==0) {
		alert("Please select Validity Period");
		return false;
	}
	if (document.bannerFrm.validTo.value==0) {
		alert("Please select Validity Period");
		return false;
	}
}
</script>
<?php
################ Paging Starts From Here ##################
@extract($_REQUEST);
$start = (intval($start)<0 or $start=="")?0:$start;
$pagesize = intval($pagesize)==0?$pagesize=20:$pagesize;

$heading="Manage Coupons";
$record_type="Coupon";
$script_name="manage_coupons.php";
$table_name="coupon_tbl";
####################################################
if($id=="add_record") {
	
		$sql=db_query("insert into $table_name set coupon_title='$ctitle', coupon_desc='$cdesc', coupon_date=now(), coupon_status='Y', coupon_cat='$cat', coupon_subcat='$subcat', coupon_type='$ctype', coupon_code='$ccode', coupon_link='$clink', coupon_brand='$store', coupon_exdate='$expdate'");
		$_SESSION[session_message]='New '.$record_type.' has been added successfully';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
		exit;
	}
	else {
		$msg=html_head();
		$msg.=$form_check_alert;
		$msg.=html_foot();
	}
if($id=="update_record") {
		updateTable($table_name,"coupon_title='$ctitle', coupon_desc='$cdesc', coupon_cat='$cat', coupon_subcat='$subcat', coupon_type='$ctype', coupon_code='$ccode', coupon_link='$clink', coupon_brand='$store', coupon_exdate='$expdate' where coupon_id='$recordID'");
		$_SESSION[session_message]=$record_type.' has been updated successfully';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
		exit;
	}
	else {
		$msg=html_head();
		$msg.=$form_check_alert;
		$msg.=html_foot();
	}	
if($id=="delete_record") {
	db_query("delete from $table_name where coupon_id='$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%" 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 = NumRows($qry1);
$qry= db_query("select * from $table_name order by coupon_id 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>
	 <td width="10%" align="left"><strong>Coupon Type</strong></td>
	 <td width="10%" align="left"><strong>Store</strong></td>
	 <td width="20%" align="center"><strong>Coupon</strong></td>
	 <td width="10%" align="left"><strong>Category</strong></td>
	 <td width="10%" align="left"><strong>Options</strong></td>
	 </tr>
	 <?php
	 $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">
		  <?
		  $ctype=$arr[coupon_type];
		  if($ctype =='1') { echo "Coupon"; } else { echo "Offer"; } 
		  ?>
		  </td>
		  <td align="center">
		 <?
		 $store=$arr[coupon_brand];
		 $sql_store=mysql_query("select * from brand_tbl where slno='$store'");
         $res_store=mysql_fetch_array($sql_store);
		 echo $res_store['brandName'];
		 ?>
		</td>
		<td align="center">
		 <?=$arr[coupon_title];?>
		</td>
		<td align="center">
		 <?
		 $cct=$arr[coupon_cat];
		 $sql_cat=mysql_query("select * from category_tbl where catID='$cct'");
         $res_cat=mysql_fetch_array($sql_cat);
		 echo $res_cat['catName'];
         ?>
		</td>
		<td align="center">&nbsp;<a href="<?=$script_name;?>?id=edit_record&start=<?=$start;?>&pagesize=<?=$pagesize?>&recordID=<?=$arr[coupon_id];?>#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;?>&pagesize=<?=$pagesize?>&recordID=<?=$arr[coupon_id];?>" 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();">
<table width="99%"  border="1" cellpadding="2" cellspacing="2" class="lightBorder">
 <?php
 if($_GET[recordID]=="" || $id!="edit_record") {
	 ?>
	 <tr bgcolor="#EAEAEA"><td colspan="2" align="left" height="20"><b><a name="bot">Add New <?=$record_type;?></a></b></td></tr>
	 <?php
 }
 else {
	 ?>
	 <tr bgcolor="#EAEAEA">
	  <td align="left" height="20"><a name="bot"></a><b>Edit <?=$record_type;?></b></td>
	  <td align="right" height="20"><a href="<?=$script_name;?>"><b>Add New <?=$record_type;?></b></a></td>
	 </tr>
	 <?php
 }
 $res=getResult($table_name," where coupon_id='$_GET[recordID]'");
 ?>
 <tr>
<td class="brdrbtm"><b>Select Type <font color="red">*</font></b></td>
<td class="brdrbtm">
<select name="ctype">
<option value="1"  <? if ($res[coupon_type]=='1') { echo "selected"; }?>>Coupon</option>
<option value="2" <? if ($res[coupon_type]=='2') { echo "selected"; }?>>Offer</option>
</select>
</td>
</tr> 
<tr>
<td class="brdrbtm"><b>Select Store <font color="red">*</font></b></td>
<td class="brdrbtm">
<select name="store">
<option>Select Store</option>
<?php
$sql_store=mysql_query("select * from brand_tbl");
while ($res_store=mysql_fetch_array($sql_store))
{
?>
<option value="<?php echo $res_store['slno']; ?>" <? if ($res[coupon_brand]==$res_store[slno]) { echo "selected"; }?>><?php echo $res_store['brandName']; ?></option>
<?php
}
?>
</select>
</td>
</tr> 
<tr>
<td class="brdrbtm"><b>Select Category <font color="red">*</font></b></td>
<td class="brdrbtm"><select name="cat" id="quickCity">
<option>Select Category</option>
<?php
$sql_cat=mysql_query("select * from category_tbl where parentID='0'");
while ($res_cat=mysql_fetch_array($sql_cat))
{
?>
<option value="<?php echo $res_cat['catID']; ?>" <? if ($res[coupon_cat]==$res_cat[catID]) { echo "selected"; }?>><?php echo $res_cat['catName']; ?></option>
<?php
}
?></select></td>
</tr>
<tr>
<td class="brdrbtm"><b>Select Sub Category <font color="red">*</font></b></td>
<td class="brdrbtm"><select name="subcat" id="locatity">
<?php
 $sql_tl=mysql_query("select * from category_tbl where parentID='$res[coupon_cat]' and parentID!=''");
 if(mysql_num_rows($sql_tl) > 0)
 {

 while($qq_res=mysql_fetch_array($sql_tl))
 {
 ?>
 <option value="<?php echo $qq_res['catID'];?>" <? if ($res[coupon_subcat]==$qq_res[catID]) { echo "selected"; }?>><?php echo $qq_res['catName'];?></option>
 <?php
 }
 }
 ?>
</select></td>
</tr>
<tr>
<td class="brdrbtm"><b>Coupon Title <font color="red">*</font></b></td>
<td class="brdrbtm"><input type="text" name="ctitle" size="60" value="<?=$res[coupon_title];?>" id="NOBLANK~Please enter Person Name.~DM~" /></td>
</tr> 
<tr>
<td class="brdrbtm"><b>Coupon Desc<font color="red">*</font></b></td>
<td class="brdrbtm"><textarea rows="10" cols="50" name="cdesc"><?=$res[coupon_desc];?></textarea></td>
</tr>
<tr>
<td class="brdrbtm"><b>Coupon Code<font color="red"> (if you selected coupon type) </font></b> </td>
<td class="brdrbtm"><input type="text" name="ccode" size="60" value="<?=$res[coupon_code];?>" id="NOBLANK~Please enter Person Name.~DM~" /></td>
</tr>
<tr>
<td class="brdrbtm"><b>Coupon Link<font color="red">*</font></b></td>
<td class="brdrbtm"><input type="text" name="clink" size="60" value="<?=$res[coupon_link];?>" id="NOBLANK~Please enter Person Name.~DM~" /></td>
</tr>  
<tr>
<td class="brdrbtm"><b>Expiry Date <font color="red">*</font></b></td>
<td class="brdrbtm"><input name='expdate' id='txtCalendarFirst' class='inputBoxStyle' value="<?=$res[coupon_exdate];?>" readonly>
<img src='cal.gif' align='absmiddle' onMouseOver="fnInitCalendar(this, 'txtCalendarFirst', 'style=calendar.css,close=true')"> </td>
</tr> 
<tr>
<td class="brdrbtm"></td>
<td class="brdrbtm">
   <?php
   if($recordID !="") {
	   ?>
	   <input type="submit" name="submit" value="Update <?=$record_type;?>" class="button">
	   <input type="hidden" name="recordID" value="<?=$res[coupon_id];?>">
	   <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;?>">
</td>
</tr>
</table>
</form>
<link rel='stylesheet' href='calendar.css' title='calendar'>
<script language="javascript" src="calendar.js"></script>
<script src="jquery.min.js"></script>
<script type="text/javascript">

                    $("#quickCity").change(function(){	

                            var cityid  =  $('#quickCity').val();

                           $.ajax({   

                                url: "ajax99.php", //The url where the server req would we made.

                                async: false,

                                type: "POST", //The type which you want to use: GET/POST

                                data: "cityid="+cityid, //The variables which are going.

                                dataType: "html", //Return data type (what we expect).

                                //This is the function which will be called if ajax call is successful.

                                success: function(data) {

                                    //data is the html of the page where the request is made.

                                    $('#locatity').html(data);

                                }

                            })

                });

        </script>

<iframe width=174 height=189 name="gToday:normal:agenda.js" id="gToday:normal:agenda.js" src="<?=SITE_URL_PATH;?>/js/calendar/themes/Normal/ipopeng.htm" scrolling="no" frameborder="0" style="visibility:visible; z-index:999; position:absolute; top:-500px; left:-500px;">
</iframe>
<?php
admin_footer();
exit;
?>