<?php
require_once('../config/config.php');
require_once(CLASS_DIR.'dbClass.php');
$obj = new DBClass();
$dat = date('Y-m-d');
$headin = 'Add New Admin User';
$buton = 'Add User';

$sql1 = "SELECT * FROM ".TBL_ADMIN;
$rs1 = $obj->executeQry($sql1,$link);
$row1 = $obj->fetchrow();	 

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

if(isset($_POST['submit'])){
        $valid = validmess($_POST['uname'], "Username"); if(isset($valid) && $valid != ''){ $error[] = $valid;}
        $valid = checkEmail($_POST['email'], "Email"); if(isset($valid) && $valid != ''){ $error[] = $valid;}
	if(!isset($_GET['action'])){
        $valid = validmess($_POST['password'], "Password"); if(isset($valid) && $valid != ''){ $error[] = $valid;}
	}
	if($_POST['password'] != $_POST['cpassword']){
		$error[] = 'Password and confirm password should be same';
	}

	if($error == ''){
		if($_POST['submit'] == 'Add User'){
			if($row1['email'] == $_POST['email']){
				$error[] = 'email already registerd';
			} else {
				$sqll = "INSERT INTO ".TBL_ADMIN." (uname,email,pass,added_date) VALUES ('".trim($_POST['uname'])."','".trim($_POST['email'])."','".trim($_POST['password'])."','".$dat."') ";
				$rs2 = $obj->executeQry($sqll,$link);
				if($rs2){
					@header('Location: admin.php?msg=add');
				}
			}
		}
	
		if($_POST['submit'] == 'Edit User'){
			if($_POST['password'] != ''){
				if($_POST['currpassword'] != $row['pass']){
					$error[] = "Current password is wrong";
					}
				}
				if($error == ''){
					if($_POST['password'] != ''){
						$pass = $_POST['password'];
						} else {
						$pass = $row['pass'];
						}
					$sqll = "UPDATE ".TBL_ADMIN." SET uname = '".$_POST['uname']."', email = '".$_POST['email']."', pass = '".$pass."' WHERE id = '".$_GET['id']."'";
					$rs2 = $obj->executeQry($sqll,$link);
					if($rs2){
						@header('Location: admin.php?msg=edit');
					}
                                }	
		}
	}	
}

require_once(INCLUDES_DIR.ADMHEADER); 
?>
<table border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="29" align="center" class="submenu"><a href="admin.php">Manage Admin Users</a></td>
    <td align="center" class="submenu">Add New Admin User</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">Name and email</td>
                </tr>
              <tr>
                <td align="left" valign="top" class="form_bg"><table width="500" border="0" cellpadding="0" cellspacing="0" class="form_txt1">
                  <tr>
                    <td align="right" width="40%">Username: </td>
                    <td align="left" width="60%"><input type="text" name="uname" id="uname" value="<?php echo ($_POST['uname'] == '') ? $row['uname'] : $_POST['uname']; ?>" /></td>
                    </tr>
                  <tr>
                    <td align="right">Email:</td>
                    <td><input type="text" name="email" id="email" value="<?php echo ($_POST['email'] == '') ? $row['email'] : $_POST['email']; ?>" /></td>
                  </tr>
                  </table></td>
              </tr>
              <tr>
                <td bgcolor="#990066" class="form_head">Password</td>
              </tr>
              <tr>
                <td align="left" valign="top" class="form_bg"><table width="500" border="0" cellpadding="0" cellspacing="0" class="form_txt1">
<?php if(isset($_GET['action']) && $_GET['action'] == 'edit'){ ?>
                  <tr>
                    <td align="right" width="40%">Current Password: </td>
                    <td align="left" width="60%"><input type="password" name="currpassword" id="currpassword" /></td>
                  </tr>
<? } ?>
                  <tr>
                    <td align="right" width="40%">Password: </td>
                    <td align="left" width="60%"><input type="password" name="password" id="password" /></td>
                  </tr>
                  <tr>
                    <td valign="top" align="right">Confirm Password: </td>
                    <td><input type="password" name="cpassword" id="cpassword" /></td>
                  </tr>
                  </table></td>
              </tr>
              <tr>
                <td align="right" valign="top" class="form_bg"><table width="500" border="0" cellpadding="0" cellspacing="0">
                  <tr>
                    <td align="center"><input name="submit" type="submit" class="bg_bt" id="button" value="<?php echo $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); ?>