<?php session_start();  //Developed by ITechScripts
   $mode=$_GET['mode'];
   $id=$_GET['id'];
$today=date('Y-m-d');
   require 'include/configurations.php'; // ITECHBIDS Configuration File
   require 'include/header.php';
?>

>
<?

if(isset($_POST['btn_delete']))
{	
	$id=$_POST['id'];
	$pid=$_POST['chkSub'];
	foreach($pid as $plan_id)
	{
	$sql="delete from coupons where auto=$plan_id";
	$res=mysql_query($sql);
	$message="Voucher(s) were deleted Sucessfully";
	}
}
	if($mode=="edit" || $mode=="new") {
	
	 $canSave=$_POST['canSave'];
	 if($canSave==1) {
	 	$coupon=$_POST['txtCoup'];
            $amount=$_POST['txtAmount'];

		if($mode=="edit")
		 $sql="update coupons set c_name='$coupon',amount=$amount, c_date='$today' where auto=$id";
		else 
		 $sql="insert into coupons(c_name,amount, c_date) values ('$coupon',$amount,'$today')";
		$result=mysql_query($sql);
		
		echo '<meta http-equiv="refresh" content="0;url=coupon.php">';
		exit();
	 }
		if($mode=="edit") {
			$sql="select * from coupons where auto=$id";
			$result=mysql_query($sql);
			$row=mysql_fetch_array($result);
		}
?>
<table border="0" align="center" cellpadding="5" cellspacing="2" width="70%" class="ft">
<form name="frmPlan" action="<? $_SERVER['PHP_SELF']?>" method="post" onSubmit="this.canSave.value=1;">
<tr class="ct"><td colspan="2"><b><?=ucwords($mode)?>  Voucher</b></td></tr>
<tr>
<td>Voucher ID</td>
<td><input type="text" class="bluetext"name="txtCoup" value="<?=$row['c_name']?>"></td>
</tr>
<tr>
<td> Bid Amount</td>
<td><input type="text" class="bluetext"name="txtAmount" value="<?=$row['amount']?>"></td>
</tr>

<tr><td colspan="2" align="center"><input type="hidden" name="canSave" value="0"><input type="submit" class="button"   name="btn_Add" value="<? 
if($mode=='new') echo ' Add ';else if($mode=='edit') echo ' Modify '?>" ></td></tr>
</form>
</table>
<?
	}
	else {
?>
<form name="frm1" method="post">
<table border="0" align="center" cellpadding="5" cellspacing="2" width="87%" class="ft">
<tr><td style="color:#FF0000 "><?=$message; ?></td></tr>
<tr><td>
<table border="0" align="center" cellpadding="5" cellspacing="2" width="100%">
<tr class="ct">
  <td colspan="6">You Can Manage Bid Vouchers.</td>
  </tr>
<tr >
<td width="19%"><input type="checkbox" name="chkMain" onClick="chkall();" class="check" value=1></td>
   <td width="30%"><strong>Voucher ID</strong></td>
  <td width="30%"><strong>Bid Amount</strong></td>
  <td width="15%"><strong></strong></td>
  </tr>
<?
	$sql="select * from coupons";
	$result=mysql_query($sql);
	$i=1;
	while($row=mysql_fetch_array($result)) {
		if($row['type']=='gift') $p_type='Gift';
		if($row['type']=='permanent') $p_type='Permanent';
		if($row['v_type']=='flat') $v_type='Flat';
		if($row['v_type']=='percent') $v_type='Percentage';
		?>
<tr >
<td><input name="chkSub[]" id="chkSub" type="checkbox" class="check" value="<?=$row['auto']; ?>">
 <input type="hidden" name="id[]" value="<?php echo $row['auto']; ?>"> </td>
  <td><?=$row['c_name'];?></td>
  <td><?=$row['amount'];?></td>
  <td>
  <a href="coupon.php?mode=edit&id=<?=$row['auto']?>">Edit</a> </td>
  </tr>
<?
$i++;
	}
?>
<tr><td>
<input type="submit" class="button"   name="btn_delete" value=" Delete " >
</td>
  <td colspan="6" align="left"><a href="coupon.php?mode=new">
   Add New
  </a></td>
</tr>
</table></td>
</tr>
</table>
</form>
<?
	}

?>
<script language="javascript">
function chkall() {
	len=document.frm1.chkSub.length;
	if(len > 1) {
	for(i=0;i<len;i++) {
		if(document.frm1.chkMain.checked==true) {
			document.frm1.chkSub[i].checked=true;
		}
		else {
			document.frm1.chkSub[i].checked=false;
		}
	}
	}
	else {
		if(document.frm1.chkMain.checked==true) {
			document.frm1.chkSub.checked=true;
		}
		else {
			document.frm1.chkSub.checked=false;
		}
	
	}
	}

</script>
<? require 'include/footer.php'?>