<?php 
include ("kfunctions.php");
adminall("overadmin","priaccount","secaccount");
?>
<html>
<head>
<?php kheader(); ?>

<link href="css/bootstrap-datetimepicker.min.css" rel="stylesheet" media="screen">
<script src="js/sorttable.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.js"></script>
</head>
<body>


    <div id="wrapper">

        <!-- Navigation -->
        <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
            <!-- Brand and toggle get grouped for better mobile display -->
            <?php ktopbar(); ?>
            <!-- Sidebar Menu Items - These collapse to the responsive navigation menu on small screens -->
            <?php kleftbar(); ?>
            <!-- /.navbar-collapse -->
        </nav>

        <div id="page-wrapper">

            <div class="container-fluid">

                <!-- Page Heading -->
                
                <!-- /.row -->

                <div class="row">
                    <div class="col-lg-12">
                        <div class="panel panel-default">
                            <div class="panel-body">
                            
                            
<div class="panel panel-yellow"><!--Initiative-->
<div class="panel-heading"><h3 class="panel-title">Enter your expenses</h3></div>
<div class="panel-body">

    <?php

if(isset($_POST["submit120"])){

$max = $_POST["max"];


include ("connect.php");
$ouruser = $_SESSION["rolenumber"];

for($y=1; $y<=$max; $y++){

$datez = $_POST["datez"];
$speid = $_POST["spend".$y.""];


$cost = $_POST["cost".$y.""];
$requestfrom = $_POST["requestfrom".$y.""];
$comment = $_POST["comment".$y.""];

$rolenumber = $_SESSION["rolenumber"];
$result3 = $dbh->query("select * from users where rolenumber='$rolenumber'");
$row3 = $result3->fetchObject();

$result4 = $dbh->query("select * from users where adminid='$rolenumber'");
$row4 = $result4->fetchObject();

$yy=date("Y");
$fyy=substr($yy,2,2);
$mm=date("m");
$dd=date("d");
$hi=date("h");
$mi=date("i");
$sa=date("sa");
$fsa=substr($sa,0,2);   
$expid=$row3->autoid.$y.$fyy.$mm.$dd.$hi.$mi.$fsa.$row4->autoid;

if($speid==""){

}else{
useractivity("(date ".$datez.")(cost ".$cost.")(requestfrom ". $requestfrom.")(comment ".$comment.") expenses been submitted","5");


$insertexpenses = $dbh->exec("insert into expenses (expid,speid,cost,remain,requestfrom,comment,ouruser,datez) values('$expid','$speid', '$cost','$cost','$requestfrom', '$comment', '$ouruser','$datez')");
echo "<div class='alert alert-success'>Success: Item ".$y." has been submitted. </div>";
}
}
}


?>


  <form method="post" onsubmit="return expa1();">

<div class='input-append date form_date col-lg-3' data-date='' data-date-format='yyyy-mm-dd' data-link-field='dtp_input2' data-link-format='yyyy-mm-dd'>
<input size='16' type='text' name='datez' style='cursor:pointer' placeholder='Select date' class='form-control readonly' required>
<span class='add-on'><i class='icon-th'></i></span>
</div>

    <input type='submit' style='float:right;' class='btn btn-sm btn-success' value='Submit expenses' name='submit120'>
<table class="table table-bordered table-hover table-striped sortable">
<thead>
  <tr>
    <th>No</th>
    <th>Spend by</th>
    <th>Amount Given out</th>
  <th>Comment</th>
  <th>Authorized by</th>
  </tr>
</thead>
<tbody>
  
  <?php

$max = "10";
  for($x=1; $x<=$max; $x++){
 echo "
<tr>
<input type='hidden' name='max' value='".$max."'>
  <td>".$x."</td>
  <td>
<select class='form-control' name='spend".$x."' id='spend".$x."'>
<option value=''>Select spender</option>
  ";
include ("connect.php");
$rolenumber = $_SESSION["rolenumber"];
$result2 = $dbh->query("select * from users where title='expense'");
$row2 = $result2->fetchObject();
$count2 = $result2->rowCount();

if($count2>0){
do{
echo "
<option value='".$row2->rolenumber."'>".$row2->firstname." ".$row2->lastname."</option>
";
}while($row2 = $result2->fetchObject());
}else{
  echo "<option value=''>Sorry please contact your IT department</option>";
}
echo "
  </select>
  </td>
  <td>
<input type='number' id='cost".$x."' min='0' max='900000000'  class='form-control' name='cost".$x."' placeholder='Total Amount'>
  </td>
  <td>
<textarea name='comment".$x."' placeholder='Comment' class='form-control'></textarea>
  </td>
  <td>
  <input type='text' id='requestfrom".$x."' class='form-control' name='requestfrom".$x."'>
  </td>
  
  </tr>

  ";
}
  ?>
 
  </tbody>
</table>

<input type='submit' style='float:right;' class='btn btn-sm btn-success' value='Submit expenses' name='submit120'>
</form>

</div></div>

                            </div> </div>
                    </div>
                </div>
                <!-- /.row -->

               <?php useractivity("on page","5"); ?>

            </div>
            <!-- /.container-fluid -->

        </div>
        <!-- /#page-wrapper -->

    </div>
    <!-- /#wrapper -->
<script>
function expa1(){
<?php
$max = "10";
for($h=1; $h<=$max; $h++){
?>
var cost<?php echo $h; ?> = document.getElementById("cost<?php echo $h; ?>").value;
var requestfrom<?php echo $h; ?> = document.getElementById("requestfrom<?php echo $h; ?>").value;
var spend<?php echo $h; ?> = document.getElementById("spend<?php echo $h; ?>").value;


if((spend<?php echo $h; ?>!="")&&(cost<?php echo $h; ?> == "")){

  alert(" Cost is missing");

  document.getElementById("cost<?php echo $h; ?>").style.borderColor = "red";

  return false;
}else{
document.getElementById("cost<?php echo $h; ?>").style.borderColor = "green";

}


if((spend<?php echo $h; ?>!="")&&(requestfrom<?php echo $h; ?> == "")){

  alert("Request from is missing");

  document.getElementById("requestfrom<?php echo $h; ?>").style.borderColor = "red";

  return false;
}else{

  document.getElementById("cost<?php echo $h; ?>").style.borderColor = "green";
}


<?php
}
?>
}
</script>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.js"></script>

    <!-- Bootstrap Core JavaScript -->
    <script type="text/javascript" src="js/bootstrap.min.js"></script>
    <script type="text/javascript" src="js/jquery-1.8.3.min.js" charset="UTF-8"></script>
<script type="text/javascript" src="js/bootstrap-datetimepicker.js" charset="UTF-8"></script>
<script type="text/javascript">
    $('.form_datetime').datetimepicker({
        //language:  'fr',
        weekStart: 1,
        todayBtn:  1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        forceParse: 0,
        showMeridian: 1
    });
    $('.form_date').datetimepicker({
        language:  'en',
        weekStart: 1,
        todayBtn:  1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        minView: 2,
        forceParse: 0
    });
    $('.form_time').datetimepicker({
        language:  'en',
        weekStart: 1,
        todayBtn:  1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 1,
        minView: 0,
        maxView: 1,
        forceParse: 0
    });
</script>
</body>

</html>