<?php
{

function UserRoletka(){
require "build/db_connect.inc.php"  ;
    $query="SELECT idUSER,jmeno,prijmeni
            FROM user U
            WHERE U.validity=1
            AND idUSER<>1
            ORDER BY prijmeni,jmeno ASC
                ";
    $result = mysql_query($query);
    if (!$result) {
         die('Roletku uživatelů nelze načíst:' . mysql_error());
    }
    echo "<select name=\"USERS\" size=1 id=\"acrol\" OnChange=\"RefreshForm(this);\">";
    echo "<option value=\"\" >vyberte...</option>";
    while(list($idUSER,$jmeno,$prijmeni)=mysql_fetch_row($result)){
        if ($_POST['USERS']==$idUSER){
            echo "<option value=".$idUSER." selected>".$prijmeni." ".$jmeno."</option>";
        }else{
            echo "<option value=".$idUSER.">".$prijmeni." ".$jmeno."</option>";
        }
    }
    echo "</select>";


}



function AccountRoletka(){
require "build/db_connect.inc.php"  ;
    $query="SELECT UA.id,SB.name
            FROM user_account UA
            JOIN sip_buddies SB ON SB.id=UA.id AND UA.validity=1
            WHERE UA.idTECHNOLOGY=1
            AND idUSER=".$_POST['USERS']."
                ";
    $result = mysql_query($query);
    if (!$result) {
         die('Roletku u6ivatelů nelze načíst:' . mysql_error());
    }
    echo "<select name=\"ACCOUNT\" size=1 id=\"acrol\" OnChange=\"RefreshForm(this);\">";
    echo "<option value=\"\" >vyberte...</option>";
    while(list($idACC,$name)=mysql_fetch_row($result)){
        if ($_POST['ACCOUNT']==$idACC){
            echo "<option value=".$idACC." selected>".$name."</option>";
        }else{
            echo "<option value=".$idACC.">".$name."</option>";
        }
    }
    echo "</select>";


}
###############################################################################
if(IsSet($_POST['CHARGE'])){
$query="UPDATE bill_credit SET value=value + ".$_POST['CREDIT']." 
        WHERE idBILL=(SELECT idBILL FROM user_account 
                      WHERE id=".$_POST['ACCOUNT']." AND idTECHNOLOGY=1)
        ";
 $result = mysql_query($query);
    if (!$result) {
         die('Kredit nebyl doplněn:' . mysql_error());
    }


}


###############################################################################
if (IsSet($_POST['ACCOUNT']) && $_POST['ACCOUNT']!='' && IsSet($_POST['USERS']) && $_POST['USERS']!=''){
  $query="SELECT value
          FROM bill_credit BC
          JOIN user_account UA ON UA.idBILL=BC.idBILL
          WHERE UA.id=".$_POST['ACCOUNT']."
          AND UA.idTECHNOLOGY=1 ";
   $result = mysql_query($query);
    if (!$result) {
         die('Hodnota kreditu nelze nacist:' . mysql_error());
    }
    list($creditvalue)=mysql_fetch_row($result);

}else{$creditvalue=0;}
?>
<script language="JavaScript">
function RefreshForm(theForm){
  //    document.form1.TGE_SUBMIT.value="FALSE";
      
          document.forms.form1.submit();
      }

</script>
<center>
<div class="forms">
<h3>Dobití kreditu</h3>
    <fieldset>
        <legend>Parametry dobití</legend>
    <form name="form1" action="admin_page.php" method="post" >
      <table align="center">
        <tr>
              <td class="tablehead">Úživatel</td>
              <td class="tablehead">Účet</td>
              <td class="tablehead">Aktuální částka v kč</td>
              <td class="tablehead">Doplnit částku v kč</td>
              <td class="tablehead"></td>
        </tr><tr>
             <td class="tableculombhead"> <?php UserRoletka();?> </td>  
             <td class="tableculombhead"> <?php if($_POST['USERS']!=''){AccountRoletka();}?> </td>
             <td class="tableculombhead"><input size=4 type="text" value="<?php echo $creditvalue;  ?>" disabled>
            <td class="tableculombhead"> <input size=4 type="text" name="CREDIT" value="0" /> </td>
            <td class="tableculombhead"><?php if($_POST['ACCOUNT']!='') echo " <input type=\"submit\" name=\"CHARGE\" value=\"Doplnit\">";?> </td>
        </tr>
    
      </table>
    </form>
  </fieldset>  







</div>
</center>




<?php
}
?>