<?session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
$branch=$_SESSION['branch'];
if(strlen($user)<=0)
{
 print "<script> alert('Invalid Login'); window.close();opener.document.location='ind.php';</script>";
 return;
}
$code=$_REQUEST['code'];
print "User: $user <br> Branch: <font color=red>".$branch."<br><br>";
include("../opendb.php");
$result=mysqli_query($db,"select * from bruser where sno='$code'") or die(mysqli_error($db));
$row=mysqli_fetch_array($result);
//print "select * from bruser where sno='$code'";
//print "select brname,brcode from branch where brcode='$row[brcode]'";
$res=mysqli_query($db,"select brname,brcode from branch where brcode='$row[brcode]'") or die(mysqli_error($db));
$r=mysqli_fetch_array($res);

if(isset($_POST['submit']))
{
$yf=$_REQUEST['yf'];
$mf=$_REQUEST['mf'];
$df=$_REQUEST['df'];
$username=$_REQUEST['username'];
$level=$_REQUEST['level'];
$desig=$_REQUEST['desig'];
$addr=$_REQUEST['addr'];
$ph=$_REQUEST['ph'];
$pswd1=$_REQUEST['pswd1'];
$utype=$_REQUEST['utype'];
$dept=$_REQUEST['dept'];
$br=$_REQUEST['br'];
$code=$_REQUEST['code'];


$dt=$yf.'-'.$mf.'-'.$df;
 $sql="update bruser set username='$username',level='$level',desig='$desig',".
"addr='$addr',ph='$ph',pswd='$pswd1',utype='$utype',dept='$dept',brcode='$br',passdate='$dt' where sno='$code'";
mysqli_query($db,$sql) or die(mysqli_error($db));
$sql2="insert into bruserupd values('$br','$username','$level','$dt')";
mysqli_query($db,$sql2) or die(mysqli_error($db));

print "<script>window.close(); opener.document.location='listuser.php';</script>";
}
?>
<script>
window.resizeTo(500,600);
</script>

<style>
td{font-family:'arial narrow';letter-spacing:1}
th{background-color:c1d799}
body{font-family:'arial narrow';letter-spacing:1;}
</style>

<link rel="stylesheet" href="style.css" type="text/css" />

<script>
function validate()
{
var p1=document.f1.pswd1.value
var p2=document.f1.pswd2.value

if(document.f1.br.value=='')
{

alert('Invalid Branch Name');
return false;
}
 submit="true";
 if(document.f1.username.value.length<3)
  {submit="false"; alert('Invalid User Name');}

 if(document.f1.pswd1.value.length<=0)
  {submit="false"; alert('Invalid Password');}

 if(document.f1.pswd2.value.length<=0)
  {submit="false"; alert('Invalid Password2');}
 if(p1!=p2)
  {submit="false"; alert('Passwords does not match');}

 if(submit=="false")
  return false;
}
</script>



<body bgcolor=fcfff1>
<font color=green size=4> <b>User Creation</font>
<hr bgcolor=red>
<center>
<form name='f1' action='<?print $PHP_SELF?>' method='post' onSubmit="return validate()">
<input type=hidden name='code' value='<?Print $row['sno']?>'>
<table border=0  cellspacing=2 cellpadding=5>
<tr><td>Branch </td><td><select name='br'>
<option value=<?print $r[1]?>><?print $r[0]?></option>
<?

$re=mysqli_query($db,"select brcode,brname from branch order by brname");
print "select brcode,brname from branch order by brname";
while($ro=mysqli_fetch_array($re))
{
 print "<option value='$ro[0]'>$ro[1]</option>";
}
?>
</select></td></tr>
<tr><td>User Name</td><td><input type=text readonly name='username' size=40 value='<?print $row['username']?>'></td></tr>
<tr><td>User Level</td><td><select name='level'>
<option><?print $row['level']?></option>
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>11</option>

</select>
</td></tr>

<tr><td>Designation</td><td><input type=text name='desig' size=40 value='<?print $row['desig']?>'></td></tr>
<tr><td>Address</td><td><input type=text name='addr' size=40 value='<?print $row['addr']?>'></td></tr>
<tr><td>Phone/Mobile</td><td><input type=text name='ph' size=40 value='<?print $row['ph']?>'></td></tr>
<tr><td>Password</td><td><input type=password name='pswd1' size=40 value='<?print $row['pswd']?>'></td></tr>
<tr><td>Confirm Password</td><td><input type=password name='pswd2' size=40 value='<?print $row['pswd']?>'></td></tr>
<tr><td>User Type</td><td><select name='utype'>
<option><?print $row['utype']?></option>
<option>Administrator</option>
<option>Normal User</option>
</select>
</td></tr>
<tr><td>Admin to dept</td><td>
<select name='dept'>
<option><?print $row['dept']?></option>
<option>None</option>
<option>Accounts</option>
<option>Service</option>
<option>Sales</option>
<option>Spares</option>
<option>H.R</option>
</select> </td><td>
Date </td><td>dd<input type=text name='df' size=2 readonly value='<?print date('d',time())?>'>
	mm<input type=text name='mf' size=2 readonly value='<?print date('m',time())?>'>
	yy<input type=text name='yf' size=4 readonly value='<?print date('Y',time())?>'>
 </td></tr>
<tr><td colspan=2 align=center><input type=submit name='submit' value='      Save      '> 
<input type=button value='     Cancel    ' onClick=window.close();>
</td></tr>
</table>
</form>
