<?php include ("../core/kfunctions.php"); adminall("overadmin","priaccount2","secaccount2");
?>
<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">
                            
                            <?php
include ("../core/connect.php");
$expid = $_GET["expid"];

$result = $dbh->query("select * from expenses where expid='$expid'");
$row = $result->fetchObject();
$count = $result->rowCount();

if($row->remain == 0){
  $cost = $row->cost;

}else{
  $cost = $row->remain;
}
                            ?>
<div class="panel panel-yellow"><!--Initiative-->
<div class="panel-heading"><h3 class="panel-title">Enter your expenses<?php if($count>0){echo ", Total amount : ".number_format($row->cost)."";}else{} ?></h3></div>
<div class="panel-body">

    <?php

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

$max = $_POST["max"];
$remain = $_POST["remain"];

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

for($y=1; $y<=$max; $y++){
$item = $_POST["item".$y.""];
$item2 = $_POST["item2".$y.""];
$datez = $_POST["datez"];
$expid = $_POST["expid"];

if($item == ""){
  $fitem = $item2;
  $exptim = "2";

}elseif($item2 == ""){
  $fitem = $item;
  $exptim = "1";

}
$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();


if(($fitem=="")&&($requestfrom == "")){

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

$insertexpenses = $dbh->exec("insert into expenses2 (expid,item,cost,requestfrom,comment,ouruser,datez,exptim) values('$expid','$fitem', '$cost', '$requestfrom', '$comment', '$ouruser','$datez','$exptim')");

if($remain == 0){
  $status = ", status='2'";
}else{
  $status = "";
}

$updateexpenses = $dbh->query("update expenses set remain='$remain'$status where expid='$expid'");

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-4' 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>
<div class='col-lg-4'>
  <textarea id='remain' class='form-control' rows='1' style='font-size:35px;text-align:center;color:red;' name='remain'><?php if($count>0){ echo $cost;}else{} ?></textarea>
  </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 colspan='2'>Item</th>
    <th>Cost</th>
  <th>Comment</th>
  <th>Received by</th>
  </tr>
</thead>
<tbody>
  
  <?php
$max = "10";
  for($x=1; $x<=$max; $x++){
 echo "
<tr>
<input type='hidden' name='max' value='".$max."'>
<input type='hidden' name='expid' value='".$_GET['expid']."'>
  <td>".$x."</td>
  <td><input type='text' id='item".$x."'  class='form-control' name='item".$x."' placeholder='Item'></td>
  <td>
  <select class='form-control' name='item2".$x."' id='item2".$x."'>
<option value=''>Item</option>
  ";

  $rolenumber = $_SESSION["rolenumber"];
  $role = $_SESSION["role"];
  
  if($role=="overadmin") {
   $all = "";
  
  }else{
    $all = "ouruser='$rolenumber' and";
  }

  include ("../core/connect.php");
  $result = $dbh->query("select item from expenses2 where $all exptim='1'");
$row = $result->fetchObject();
$count = $result->rowCount();

if($count>0){
  do{
echo "
<option value='".$row->item."'>".$row->item."</option>
";
  }while($row = $result->fetchObject());

}else{echo "<option value=''>So far, No expense has been submitted</option>";}

  echo "
  </select>
  </td>
<td>
<input type='number' id='cost".$x."' min='0' max='9000000'  class='form-control' onkeyup='exp12()' name='cost".$x."' placeholder='Cost'>
  </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 item<?php echo $h; ?> = document.getElementById("item<?php echo $h; ?>").value;
var item2<?php echo $h; ?> = document.getElementById("item2<?php echo $h; ?>").value;
var cost<?php echo $h; ?> = document.getElementById("cost<?php echo $h; ?>").value;
var requestfrom<?php echo $h; ?> = document.getElementById("requestfrom<?php echo $h; ?>").value;



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

alert("Fill only one item NOT BOTH");

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


if(((item<?php echo $h; ?> != "")||(item2<?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(((item<?php echo $h; ?> != "")||(item2<?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>

<?php
include ("../core/connect.php");
$expid = $_GET["expid"];
$result2 = $dbh->query("select * from expenses where expid='$expid'");
$row2 = $result2->fetchObject();
$count2 = $result2->rowCount();

$cost = $row2->remain;


?>

<script>
function exp12(){
<?php 
$max = 10;
for($d=1; $d<=$max; $d++){
?>

if(document.getElementById("cost<?php echo $d; ?>").value == ""){
var cost<?php echo $d; ?> = 0;
}else{
  var cost<?php echo $d; ?> = document.getElementById("cost<?php echo $d; ?>").value;
}


<?php 
}
?>


var totalcost = 0 <?php for($d=1; $d<=10; $d++){ ?> + parseInt(cost<?php echo $d; ?>) <?php } ?> ;

if(totalcost>parseInt("<?php echo $cost; ?>")){
  alert("Oops..!! Cost is greater than total amount");
  return false;
 }

 document.getElementById("remain").innerHTML = parseInt("<?php echo $cost; ?>") - totalcost;

 
}

function numbaz(){
var numbar = document.getElementById("numba").value;
document.getElementById("shown").innerHTML = numbar;
}
</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>