<?php
require_once('../config/config.php');
require_once(CLASS_DIR.'dbClass.php');
$obj = new DBClass();
$obj1 = new DBClass();
$dat = date('Y-m-d');
$headin = 'Add New Customer Recipt Numbers';
$buton = 'Add CRN';
$cupage = 'crn';
$sql1 = "SELECT * FROM crn";
$rs1 = $obj->executeQry($sql1,$link);
$row1 = $obj->fetchrow();	 

if(isset($_GET['action']) && $_GET['action'] == 'edit'){
	$headin = 'Edit CRN';
	$buton = 'Edit CRN';
	$obj2 = new DBClass();
	$sql = "SELECT * FROM crn WHERE id = '".$_GET['id']."' ";
	$rs = $obj2->executeQry($sql,$link);
	$row = $obj2->fetchrow();
}

if(isset($_POST['submit'])){
	$service = ($_POST['package_id']==1)?"Free Members":"Paid Members";
	if($error == ''){
		if($_POST['submit'] == 'Add CRN'){
			$sqll = "INSERT INTO crn (crn_id,name,service,package_id,issue_date,status) VALUES ('".trim($_POST['crn_id'])."','".trim($_POST['name'])."','".trim($service)."','".trim($_POST['package_id'])."','".date("Y-m-d")."','".trim($_POST['status'])."') ";
			$rs2 = $obj->executeQry($sqll,$link);
				if($rs2){
					@header('Location: crn.php?msg=add');
					exit;
				}
		}
	
		if($_POST['submit'] == 'Edit CRN'){
			$sqll = "UPDATE crn SET crn_id = '".trim($_POST['crn_id'])."',package_id = '".trim($_POST['package_id'])."',service = '".trim($service)."', name = '".trim($_POST['name'])."' WHERE id = '".$_GET['id']."'";
			$rs2 = $obj->executeQry($sqll,$link);
			if($rs2){
				@header('Location: crn.php?msg=edit');
				exit;
			}
		}
	}	
}

require_once(INCLUDES_DIR.ADMHEADER); 
?>
<table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="29" align="center" class="submenu"><a href="crn.php">Manage Customer Recipt Numbers</a></td>
    <td align="center" class="submenu">Add New CRN</td>
  </tr>
</table>
<table width="1000" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="17"></td>
  </tr>
  <tr>
    <td><table width="910" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td class="pagehead"><table width="900" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="315" height="35"><?php echo $headin;?></td>
            </tr>
        </table></td>
      </tr>
      <tr>
        <td valign="top" bgcolor="#FFFFFF" class="innermain"><table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td height="25"><?php ShowError($error); ShowInfo($info);?></td>
          </tr>
	<form name="pageform" id="pageform" method="post" action="">
          <tr>
            <td valign="top" class="bdr4"><table width="800" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td bgcolor="#990066" class="form_head">CRN Details</td>
                </tr>
              <tr>
                <td valign="top" class="form_bg"><table width="100%" border="0" cellpadding="0" cellspacing="0" class="form_txt1">
                  <tr>
                    <td align="right" width="30%">Enter CRN Number : </td>
                    <td align="left" width="70%"><input type="text" name="crn_id" id="crn_id" value="<?php echo ($_POST['crn_id'] == '') ? $row['crn_id'] : $_POST['crn_id']; ?>" /></td>
                  </tr>
                  <tr>
                    <td align="right" width="30%">Company Name : </td>
                    <td align="left" width="70%"><input type="text" name="name" id="name" value="<?php echo ($_POST['name'] == '') ? $row['name'] : $_POST['name']; ?>" /></td>
                  </tr>
				  <tr>
                    <td align="right" width="30%">Select Package : </td>
                    <td align="left" width="70%"><select id="package_id" name="package_id" class="txt_box2">
										<option value="">--Select Package--</option>
											<?php                    
												$obj1 = new DBClass();
												$sql2 = "SELECT * FROM ".TBL_PACKAGE;
												$rs2 = $obj1->executeQry($sql2,$link);
												while($row2 = $obj1->fetchrow()){                 
											?>
                         					<option value="<?php echo $row2['id'];?>" <?php if(($_POST['package_id'] == $row2['id']) || ($row['package_id'] == $row2['id'])) echo 'selected'; ?>><?php echo $row2['package_name'];?></option>
                    	                    <?php } ?>
								</select></td>
                  </tr>
               </table>
			  </td>
              </tr>
              
                  <tr>
                    <td align="center"><input name="submit" type="submit" class="bg_bt" id="button" value="<?=$buton?>" /></td>
                  </tr>
                  </table></td>
              </tr>
</form>
              </table></td>
          </tr>
          </table></td>
      </tr>
      </table></td>
  </tr>
  <tr>
    <td height="40">&nbsp;</td>
  </tr>
</table>

<?php require_once(INCLUDES_DIR.ADMFOOTER); ?>