<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
include('header.php');
date_default_timezone_set("Europe/Athens");
?>
<style>
	body * {
		font-size:12px;
	}
	#viewall_filter{
			float:left;
	}
	#viewall_filter>label{
			font-weight:800;
	}
	#viewall_length{
			float:right;
	}
	.qcontrols tr td:hover {
		cursor: pointer;
	}
	.qcontrols tr th {
		background-color: #414141 !important;
		color: rgb(217, 181, 31) !important;
	}
	.qcontrols td,
	.qcontrols th {
		text-align: center;
	}
	tr.blue{
		background-color:#007cc4 !important;
		color:white;
	}
	tr.red{
		background-color:#a21d1d !important;
		color:white;
	}

</style>

<script>
$(document).ready(function(){
  makeTable(hash());
  $('#filter, #limit, #today').on('change',function(){
    makeTable();
  })
});
function approve(id){
	$.ajax({
		method: 'POST',
		url: 'toDB.php',
		data: {id: id, user: '<?php echo $_SESSION['susername']; ?>', approve: 1, control: 1},
		success: function(r){
			if(r==""){
				alertify.success('Η Ενέργεια ολοκληρώθηκε επιτυχώς!');
			} else {
				alertify.error(r);
			}
      makeTable(hash());
		}
	})
}

function hash(val=0){
  if(val==0){
    if(!$.isNumeric(parseInt(parent.location.hash = parent.location.hash.replace('#', '')))){
      parent.location.hash=1;
    }
    return parseInt(parent.location.hash = parent.location.hash.replace('#', ''));
  } else {
    parent.location.hash = val;
  }
}

function makePages(max){
    var current = hash();
    var pages = "<div class='btn-group'><button class='btn btn-default"+(current==1 ? ' disabled' : '')+"' onclick='"+(current==1 ? '' : 'makeTable('+(current-1)+')')+"'>Previous</button>";
    if(max>8){
        if(current<=5){
    	    for(var i=1;i<=6;i++){
    		    pages+="<button class='btn btn-default"+(current==i ? ' active' : '')+"' onClick='makeTable("+i+")'>"+i+"</button>";
    	    }
    	    pages+="<button class='btn btn-default disabled'>...</button>";
    	    pages+="<button class='btn btn-default' onClick='makeTable("+max+")'>"+max+"</button>";
        } else if(current>5 && current<max-5){
    	    pages+="<button class='btn btn-default' onClick='makeTable(1)'>1</button>";
    	    pages+="<button class='btn btn-default disabled'>...</button>";
    	    for(var i=current-1;i<=current+1;i++){
    		    pages+="<button class='btn btn-default"+(current==i ? ' active' : '')+"' onClick='makeTable("+i+")'>"+i+"</button>";
    	    }
    	    pages+="<button class='btn btn-default disabled'>...</button>";
    	    pages+="<button class='btn btn-default' onClick='makeTable("+max+")'>"+max+"</button>";
        } else {
    	    pages+="<button class='btn btn-default' onClick='makeTable(1)'>1</button>";
    	    pages+="<button class='btn btn-default disabled'>...</button>";
    	    for(var i=max-6;i<=max;i++){
    		    pages+="<button class='btn btn-default"+(current==i ? ' active' : '')+"' onClick='makeTable("+i+")'>"+i+"</button>";
    	    }
        }
    } else {
        for(var i=1;i<=max;i++){
            pages+="<button class='btn btn-default"+(current==i ? ' active' : '')+"' onClick='makeTable("+i+")'>"+i+"</button>";
        }
    }
    
    pages += "<button class='btn btn-default"+(current==max ? ' disabled' : '')+"' onclick='"+(current==max ? '' : 'makeTable('+(current+1)+')')+"'>Next</button></div>";
    $('#pagination').html(pages);
}

function makeTable(page=1){
  $.ajax({
    method: "POST",
    url:"controlList.php",
    data: {limit: $('#limit').val(), filter:$('#filter').val(),page: page,today:$('#today').is(':checked'), origin:2},
    success: function(r){
      data = JSON.parse(r);
      $('#tbody').html(data['table']);
      hash(page);
      makePages(data['numPages']);
    },
    error: function(r){
      console.error(r);
    }
  });
}
</script>

<div id="container">
<div class="row" style="margin-bottom:1rem;">
  <div class="col-md-2">
      <input type="text" class="form-control" name="filter" id="filter" placeholder="Αναζήτηση..">
  </div>
  <div class="col-md-2 col-md-offset-3 text-center">
	<input type="checkbox" class="form-control" value="1" id="today" />Σημερινές Εγγραφές
  </div>
  <div class="col-md-2 col-md-offset-3">
    <select name="limit" class="form-control" id="limit">
      <option value="10" selected>10</option>
      <option value="25">25</option>
      <option value="50">50</option>
    </select>
  </div>
</div>
	<table id="viewall" class="table table-bordered table-responsive table-condensed table-hover">
		<thead>
			<tr>
				<th>ID</th>
				<th>Εταιρία</th>
				<th>Ελεγκτής</th>
				<th>Είδος</th>
				<th>Μονάδα Πάχυνσης</th>
				<th>Ημ. Ελέγχου</th>
				<th>Ημ. Λήξης</th>

				<th>Παρ.ID</th>
				<th>Παρ.Ημ/νία</th>
				<th>Παρ.Είδος</th>

				<th>Έχει φωτογραφίες</th>
				<th>Έγκριση Απο</th>
				<th>Επεξεργασία</th>
			</tr>
		</thead>
		<tbody id="tbody">
		</tbody>
	</table>
  <div id="pagination" class="pull-right"></div>
</div>
<?php require('footer.php'); ?>