
<?php $__env->startSection('title'); ?> Tracking Sheets <?php $__env->stopSection(); ?>
<?php $__env->startSection('body'); ?>

<div class="row">
	<div class="form-group col-md-3">
		<label class="control-label">From</label>
		<input type="text" class="form-control fromDate" name="fromDate">
	</div>
	<div class="form-group col-md-3">
		<label class="control-label">To</label>
		<input type="text" class="form-control toDate" name="toDate">
	</div>
	<div class="form-group col-md-3">
		<button class="btn btn-primary generate" style="margin-top: 25px;">Generate</button>
		<button class="btn btn-primary export pull-right" style="margin-top: 25px;">Export</button>
	</div>
</div>

<div class="row">
	<div class="col-md-12">
		<div class="table-responsive">
			<table class="table table-bordered table-condensed report">
				<thead>
					<tr>
						<th>ID</th>
						<th>TMS TRIP NUMBER</th>
						<th>GATEPASS NUMBER</th>
						<th>PLANNED DATE</th>
						<th>DESPATCH DATE</th>
						<th>TRANSPORTER</th>
						<th>VEHICLE NUMBER</th>
						<th>TRIPS TYPE</th>
						<th>LOCATIONS</th>
						<th>DISTRIBUTOR NAMES</th>
						<th>TOTAL INVOICE VALUES</th>
						<th>DROP POINTS</th>
						<th>VEHICLE VOLUME (CBM)</th>
						<th>VEHICLE WEIGHT (KG)</th>
						<th>ACTUAL VOLUME (CBM)</th>
						<th>ACTUAL WEIGHT (KG)</th>
						<th>DISTANCE ( KM'S)</th>
						<th>LOADING OFFICER</th>
						<th>LOADING BAY</th>
						<th>LOADING TIME</th>
					</tr>
				</thead>
				<tbody>
					<th colspan="20" class="text-center">Generate Report</th>
				</tbody>
			</table>
		</div>
	</div>
</div>

<?php $__env->stopSection(); ?>
<?php $__env->startSection('script'); ?>
<script type="text/javascript">

$(function(){
	var _token = $('meta[name="_token"]').attr('content');
	var errors = JSON.parse('<?php echo json_encode($errors->toArray()) ?>');
	$('body').highlight({errors: errors.errors});

	$(document).ajaxStart(function () {
        $('button').data('loading-text', '...');
        $('button').button('loading');
    }).ajaxStop(function () {
        $('button').button('reset');
    });

    $(".export").click(function(){
		exportReport();
	});
	
	$('.fromDate').datepicker({
		dateFormat: "yy-mm-dd",
		onSelect: function(){
			$(".toDate").datepicker( "option", "minDate", new Date($('.fromDate').val()) );
		}
	});
	$(".toDate").datepicker({
		dateFormat: "yy-mm-dd",
		onSelect: function(){
			$(".fromDate").datepicker( "option", "maxDate", new Date($('.toDate').val()) );
		}
	});

	$(".generate").click(function(){
		generate();
	});

	function highlightOrThrow(e, callback){
		$.clearHighlights();
		if(e != undefined && typeof(e) == 'object'){
			if(e.hasOwnProperty('errors')){
				$.each(e.errors, function(i, k){
					$('.'+i).highlightElement({msg: k});
				});
			}else if(e.hasOwnProperty('ok') && e.hasOwnProperty('msg') && e.ok == 0){
				$.notify.error({msg: e.msg});
			}else if(e.hasOwnProperty('ok') && e.hasOwnProperty('msg')  && e.ok == 1){
				$.notify.success({msg: e.msg});
				if(callback != undefinded && typeof(callback) == 'function'){
					callback();
				}
			}else{
				callback();
			}
		}else{
			console.log('object expected.');
		}
	}


	function generate(){
		var data = {
			fromDate: $('.fromDate').val(),
			toDate: $('.toDate').val()
		};

		$.post("<?php echo e(url('/report6/generate')); ?>", {_token: _token, data: data}, function(e){

			highlightOrThrow(e, function(){
				report(e);
			});
			
		}, 'json');
	}

	function exportReport(){
		var data = {
			fromDate: $('.fromDate').val(),
			toDate: $('.toDate').val()
		};

		var dom = '<input type="hidden" name="_token" value="'+ _token +'"/>';
		$.each(data, function(k, v){
			if(v == null){
				v = '';
			}
			if(typeof(v) == "object"){
				$.each(v, function(i, j){
					dom += '<input type="hidden" name="'+ k +'['+ i +']" value="'+ j +'"/>';
				});
			}else{
				dom += '<input type="hidden" name="'+ k +'" value="'+ v +'"/>';
			}
		});
		$('<form action="<?php echo e(url("/report6/export")); ?>" method="post">'+ dom +'</form>').appendTo('body').submit();

		$.clearHighlights();
	}

	function report(e){
		$tbody = $(".report tbody");
		var dom = '';
		if(typeof(e) == "object" && Object.keys(e).length > 0){

			$.each(e, function(i, v){
				dom += '<tr>';

				dom += '	<td>'+ (++i) +'</td>';
				dom += '	<td>'+ v.trip_id +'</td>';
				dom += '	<td>'+ v.gatepass_number +'</td>';
				dom += '	<td>'+ v.takeoff_date +'</td>';
				dom += '	<td>'+ v.gatepass_date +'</td>';
				dom += '	<td>'+ v.name +'</td>';
				dom += '	<td>'+ v.number +'</td>';
				dom += '	<td>'+ v.trip_type +'</td>';
				dom += '	<td>'+ v.points +'</td>';
				dom += '	<td>'+ v.distributors +'</td>';
				dom += '	<td>'+ v.goodsCost +'</td>';
				dom += '	<td>'+ v.pointsCount +'</td>';
				dom += '	<td>'+ v.volume +'</td>';
				dom += '	<td>'+ v.weight +'</td>';
				dom += '	<td>'+ v.actualVolume +'</td>';
				dom += '	<td>'+ v.actualWeight +'</td>';
				dom += '	<td>'+ v.distance +'</td>';
				dom += '	<td>'+ v.loading_officer +'</td>';
				dom += '	<td>'+ v.loading_bay +'</td>';
				dom += '	<td>'+ v.loading_duration +'</td>';

				dom += '</tr>';
			});

		}else{
			dom = '<th colspan="20" class="text-center">Sorry, No results found</th>';
		}
		$tbody.html(dom);
	}

});

</script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('layouts.master', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>