<?php
define("_VALID_PHP", true);
require_once("../../init.php");
$page = basename($_SERVER["PHP_SELF"]);
$heder = "Lista";
$mali = " > za naručivanje";
$korisnik = $user->username;
$lokal = $user->fname;
$level = $user->userlevel;
$prezime = $user->lname;
$koje = $user->name;
//echo $koje;
$ip = $_SERVER['REMOTE_ADDR']; 
$kome = $user->sendto;
$id_user = $user->uid;
$dept = $user->todept;
//echo $id_user;
$gdeje = "(".$dept.")";
//echo $gdeje;
$firma = $user->country;
//load the database configuration file
include 'dbConfig.php';
?>
<h3 class="page-title">
<?php echo $heder;?><small><?php echo $mali;?></small>
</h3>
<div class="page-bar">
	<ul class="page-breadcrumb">
		<li>
			<i class="fa fa-home"></i>
			<a href="index.html">Home</a>
			<i class="fa fa-angle-right"></i>
		</li>
		<li>
			<a href="#"><?php echo $mali;?></a>
		</li>
	</ul>
</div>
<!-- END PAGE HEADER-->
<!-- BEGIN PAGE CONTENT-->
<div class="row">
<form class="form-horizontal" role="form" method="post" action="pages/magacin.php">
    Izaberite željeni lokal
	<div class="form-group">
	<div  class="col-sm-4">	
	<select class="form-control col-sm-4" name="lokal">
	<?php
	$query1 = $db->query("SELECT id,fname FROM users WHERE FIND_IN_SET($id_user, send_to) AND userlevel=1;");
    while($row1 = $query1->fetch_assoc()){
	?>	
	<option value="<?php echo $row1['id']; ?>"><?php echo $row1['fname']; ?></option>
	<?php 
	}
	?>
	</select>
    </div>
    </div>
	<hr>
			<!-- BEGIN ACCORDION PORTLET-->

	<div class="portlet box purple">
                                    <div class="portlet-title">
                                        <div class="caption">
                                            <i class="fa fa-gift"></i>Trebovanje po kategorijama </div>
										</div>
    <?php
	//get rows query
    $query3 = $db->query("SELECT * FROM category_products ORDER BY category_id ASC");
    if($query3->num_rows > 0){ 
    while($row3 = $query3->fetch_assoc()){
		$kategorija = $row3['category_id'];
		//echo $kategorija;
    ?>
	<div class="portlet-body">
                                        <div class="panel-group accordion" id="accordion2">
                                            <div class="panel panel-default">
                                                <div class="panel-heading">
                                                    <h4 class="panel-title">
                                                        <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapse_2_<?php echo $row3['category_id']; ?>"> <?php echo $row3['name']; ?> </a>
                                                    </h4>
                                                </div>
                                                <div id="collapse_2_<?php echo $row3['category_id']; ?>" class="panel-collapse collapse">
                                                    <div class="panel-body">
    <?php
    //get rows query
    $query = $db->query("SELECT * FROM products where status = 1 AND category_id =". $kategorija." ORDER BY id ASC");
    if($query->num_rows > 0){ 
    while($row = $query->fetch_assoc()){
    ?>
	<label for="name" class="col-sm-2"><?php echo $row['name']; ?></label>
	<div class="form-group" >
	<div  class="col-sm-2">	
    <div class="input-group" class="col-sm-2">
          <span class="input-group-btn">
              <button type="button" class="btn btn-danger btn-number"  data-type="minus" data-field="quant[<?php echo $row['id']; ?>]">
                <span class="glyphicon glyphicon-minus"></span>
              </button>
          </span>
          <input type="number" name="quant[<?php echo $row['id']; ?>]" class="form-control input-number" value="0" min="0" max="100">
          <span class="input-group-btn">
              <button type="button" class="btn btn-success btn-number" data-type="plus" data-field="quant[<?php echo $row['id']; ?>]">
                  <span class="glyphicon glyphicon-plus"></span>
              </button>
          </span>
      </div>
	</div>			
	</div>
	<?php } ?>
	<?php } ?>
                                                    </div>
                                                </div>
                                            </div>
										</div>
                                   
	
	<?php } ?>
	<?php } ?>
	</div>
            <!-- END ACCORDION PORTLET-->

  	<div class="form-group">
		<div class="col-sm-4 col-sm-offset-2">
			<input id="submit" name="submit" type="submit" value="Posalji" class="btn btn-primary">
		</div>
	</div>
</form>
</div>
<script>
//plugin bootstrap minus and plus
//http://jsfiddle.net/laelitenetwork/puJ6G/
$( document ).ready(function() {
    $('.btn-number').click(function(e){
        e.preventDefault();
        
        var fieldName = $(this).attr('data-field');
        var type      = $(this).attr('data-type');
        var input = $("input[name='"+fieldName+"']");
        var currentVal = parseInt(input.val());
        if (!isNaN(currentVal)) {
            if(type == 'minus') {
                var minValue = parseInt(input.attr('min')); 
                if(!minValue) minValue = 0;
                if(currentVal > minValue) {
                    input.val(currentVal - 1).change();
                } 
                if(parseInt(input.val()) == minValue) {
                    $(this).attr('disabled', true);
                }
    
            } else if(type == 'plus') {
                var maxValue = parseInt(input.attr('max'));
                if(!maxValue) maxValue = 9999999999999;
                if(currentVal < maxValue) {
                    input.val(currentVal + 1).change();
                }
                if(parseInt(input.val()) == maxValue) {
                    $(this).attr('disabled', true);
                }
    
            }
        } else {
            input.val(0);
        }
    });
    $('.input-number').focusin(function(){
       $(this).data('oldValue', $(this).val());
    });
    $('.input-number').change(function() {
        
        var minValue =  parseInt($(this).attr('min'));
        var maxValue =  parseInt($(this).attr('max'));
        if(!minValue) minValue = 0;
        if(!maxValue) maxValue = 100;
        var valueCurrent = parseInt($(this).val());
        
        var name = $(this).attr('name');
        if(valueCurrent >= minValue) {
            $(".btn-number[data-type='minus'][data-field='"+name+"']").removeAttr('disabled')
        } else {
            alert('Sorry, the minimum value was reached');
            $(this).val($(this).data('oldValue'));
        }
        if(valueCurrent <= maxValue) {
            $(".btn-number[data-type='plus'][data-field='"+name+"']").removeAttr('disabled')
        } else {
            alert('Sorry, the maximum value was reached');
            $(this).val($(this).data('oldValue'));
        }
        
        
    });
    $(".input-number").keydown(function (e) {
            // Allow: backspace, delete, tab, escape, enter and .
            if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 190]) !== -1 ||
                 // Allow: Ctrl+A
                (e.keyCode == 65 && e.ctrlKey === true) || 
                 // Allow: home, end, left, right
                (e.keyCode >= 35 && e.keyCode <= 39)) {
                     // let it happen, don't do anything
                     return;
            }
            // Ensure that it is a number and stop the keypress
            if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
                e.preventDefault();
            }
    });
});
</script>