<?
session_start();
include("../opendb.php");
include("../test.php");
?>
<style>
tr{font-family:arial;font-size:13;background-color:white}
TH{COLOR:WHITE;BACKGROUND-COLOR:STEELBLUE}
</style>

<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<center>
<?
if($save)
{
mysqli_query($db,"create table saletarget (exec varchar(100),location varchar(100),maname varchar(100),target double(12,2),dgm varchar(100))");
mysqli_query($db,"delete from saletarget where exec='$exec'")or die(mysqli_error($db));
/*$i=0;
 foreach($maname as $a)
 {
   $maname[$i]=$a; 
   print $maname[$i].'<br>';
   $i++;
 }
*/
 $i=0;
 foreach($target as $a)
 {
   $target1[$i]=$a; 
   $i++;
 }
 $i=0;
 foreach($dgm as $a)
 {
   $dgm1[$i]=$a;
   $i++;
 }


 for($k=0;$k<$i;$k++)
  {
     if($target1[$k]>0)
      {
        $sql="insert into saletarget values('$exec','$location','$maname[$k]','$target1[$k]', '$dgm1[$k]')";
          mysqli_query($db,$sql) or die(mysqli_error($db));
      }
  }
 print "Target has been set";
}//end if save
if($view)
{
$shre=mysqli_query($db,"select * from saletarget where exec='$exec'");
 if(mysqli_num_rows($shre)<=0)
 {
   print "<font color='red'>Record does not exist for $exec</font>";
 }
else
{
print "<table cellspacing=1 cellpadding=4 bgcolor='steelblue'
><th>Location<th>Model <th>Target <th>DSM";
while($row=mysqli_fetch_array($shre))
{
 print "<tr><td>$row[1]</td><td>$row[2]</td><td align=right>$row[3]</td><td>$row[4]</td>";
}
print "</table><br>&nbsp;";
}
}// end if view
if($show)
{
?>
<table >
<tr><td>
<form name='f1' action='<?print $PHP_SELF?>' onSubmit='return valid()'>
<table cellspacing=1 cellpadding=3 bgColor='steelblue'>
<th colspan=5>Target Master</th>
<tr><td>Executive </td><td><select name='exec'>
<option><? print $exec?></option>
</select></td><td>
Location </td>
<td><select name='location'>
<?
$locres=mysqli_query($db,"select location from saletarget where exec='$exec'");
if(mysqli_num_rows($locres)>0)
{
 $locrow=mysqli_fetch_array($locres);
 print "<option>$locrow[0]</option>";
}
$re1=mysqli_query($db,"select loc from locmaster order by loc");
while($row=mysqli_fetch_array($re1))
{
 print "<option>$row[0]</option>";
}
?>
</option></td></tr>
<tr><td colspan=5><center>
  <table cellspacing=1 cellpadding=3 bgColor='steelblue'>
  <tr><th>Model<th>Target <th>DSM</tr>
<?
$re= mysqli_query($db,"select * from premodel order by model");
while($row=mysqli_fetch_array($re))
{
 $s="select * from saletarget where exec='$exec' and maname='$row[model]'";
 $pres=mysqli_query($db,$s)or die(mysqli_error($db));
 $prerow=mysqli_fetch_array($pres);
 print "\n<tr><TD>"; //<input type=checkbox name='maname[]' value='$row[model]'>";
 print " <input type=text readonly name='maname[]' value='$row[model]'</td>";
 if(mysqli_num_rows($pres)<=0)
 {
 print "\n<td><input type=text name='target[]' size=5 value=0></td>";
 print "<td><select name='dgm[]'><option>-</option>";
 
  $dgre=mysqli_query($db,"select username from bruser where level=11 order by username");
   while($dgrow=mysqli_fetch_array($dgre))
    print "<option>$dgrow[0]</option>";
 print "</select></td></tr>";
}
else
{
 print "\n<td><input type=text name='target[]' size=5 value='$prerow[target]'></td>";
 print "<td><select name='dgm[]'>";
 print "<option>$prerow[dgm]</option>";
  $dgre=mysqli_query($db,"select username from bruser where level=11 order by username");
   while($dgrow=mysqli_fetch_array($dgre))
    print "<option>$dgrow[0]</option>";
 print "<option>-</option></select></td></tr>";

}// end else prerow
}
?>
</select>
</td></tr>
</table>
<tr><th colspan=4><input type=submit value='Save' name='save'></th></tr>
</table>
</form>
</td><td bgcolor='ivory' valign=top>
<center><b>Existing Data<br>&nbsp;<br>
<?
//xx
$shre=mysqli_query($db,"select * from saletarget where exec='$exec'");
 if(mysqli_num_rows($shre)<=0)
 {
   print "<font color='red'>Record does not exist for $exec</font>";
 }
else
{
print "<table cellspacing=1 cellpadding=4 bgcolor='steelblue'
><th>Location<th>Model <th>Target <th>DSM";
while($row=mysqli_fetch_array($shre))
{
 print "<tr><td>$row[1]</td><td>$row[2]</td><td align=right>$row[3]</td><td>$row[4]</td>";
}
print "</table><br>&nbsp;";
}
?>
</td></tr>
</table>
<?
return;
} // end if show
?>
<form action='<?print $PHP_SELF ?>'>
<table cellspacing=1 cellpadding=3 bgColor='steelblue'>
<th colspan=5>Target Master</th>
<tr><td>Executive </td><td><select name='exec'>
<?
$re=mysqli_query($db,"select username from bruser where level=0 order by username");
while($row=mysqli_fetch_array($re))
{
 print "<option>$row[0]</option>";
}
?>
</select></td>
<tr><td colspan=3 align=center>
<input type=submit value='Set' name='show'>
<input type=submit value='View' name='view'></td></tr>
</table>
</form>
