<?php $__env->startSection('content'); ?>
<?php 
		$url_date = date('Y-m-d', strtotime($date));
		$thisWeek = strtotime('last Sunday');
		$startWeek = strtotime('2017-02-26');
?>
	<div class="row">
    	<div class="col-lg-12">
    		<h1 class="page-header">Timecard</h1> 
    		<select class="notes" onchange="window.location = '<?php echo e(url("uadmin/timecard/report")); ?>' + '/' + this.value;" style="width: 100%; margin-bottom: 1em;">
    			<option>Select Week</option>
    			<?php while($thisWeek != $startWeek): ?>
    				<option value="<?php echo e(date('Y-m-d', $thisWeek)); ?>"><?php echo e(strtoupper(date('d-M-Y', $thisWeek)).' through '.strtoupper(date('d-M-Y', strtotime('+6 days', $thisWeek)))); ?></option>
    				<?php $thisWeek = strtotime('-1 week', $thisWeek);?>
    			<?php endwhile; ?>
    		</select>
    	</div>
    </div>
        <div class="row">
                <div class="col-lg-12">
                    <div class="panel panel-default">
	                    <?php if(session('status')): ?>
							<div class="alert alert-success">
								<?php echo e(session('status')); ?>

							</div>
						<?php endif; ?>
			
						<?php if(count($errors) > 0): ?>
							<div class="alert alert-danger">
								<strong>Whoops!</strong> There were some problems with your input.<br><br>
								<ul>
									<?php foreach($errors->all() as $error): ?>
										<li><?php echo e($error); ?></li>
									<?php endforeach; ?>
								</ul>
							</div>
						<?php endif; ?>
                        <div class="panel-heading">
                            Report: <?php echo e(strtoupper(date('d-M-Y', strtotime($date)))); ?> through <?php echo e(strtoupper(date('d-M-Y', strtotime('+6 days', strtotime($date))))); ?> 
                            <i class="fa fa-eye" style="float: right; cursor: pointer;" onclick="openChildren()"></i>

                            <form class="form-horizonal" method="post" action="<?php echo e(url('admin/timecard/macc')); ?>" style="display: inline; ">
                            	<input type="hidden" name="_token" value="<?php echo e(csrf_token()); ?>">
                            	<input type="hidden" name="date" value="<?php echo e($url_date); ?>">
                            	<button type="submit" style="float: right; outline: none; border: none; background-color: transparent;">
                            		<i type="submit" class="fa fa-file-excel-o" style="float: right; cursor: pointer; padding-left: 10px; padding-right: 10px;"></i>
                            	</button>
                            	
                            </form>

                        </div>
                        <!-- /.panel-heading -->
                        	<div class="panel-body">
								<table id="dt-timecards" class="table table-striped table-bordered" cellspacing="0" width="100%">
							        <thead>
							            <tr>
							                <th>&nbsp;</th>
							                <th>Name</th>
							                <th>#</th>
							                <th>Total Hours</th>
							                <th>Employee</th>
							                <th>Manager</th>
							            </tr>
							        </thead>
							        <tbody>
							        <?php foreach($timecards as $timecard): ?>
							        	<?php 
                            			$employee = $timecard->employee;//\App\Employee::find($timecard->employee); 
                            			$user = $timecard->user;//\App\User::find($employee->user);
                            			?>
							        	<tr>
							        		<td></td>
							        		<td>
							        			<?php echo e($user->name); ?> <?php echo $timecard->total <= 40 ? '' : "<span style='display: none;'> (overtime)</span>"; ?>

							        			<a href="<?php echo e(url('uadmin/timecard/accountant-editor/'.$timecard->id.'/'.$employee->id)); ?>" target="_blank" class="edit-icon" style="float: right; display: none;"><i class="fa fa-edit"></i></a>
							        		</td>
							        		<td><?php echo e($employee->number); ?></td>
							        		<td><?php echo $timecard->total <= 40 ? $timecard->total : "<span style='color: red; font-weight: 600;'>".$timecard->total."</span>"; ?></td>
							        		<td>
							        			<?php if($timecard->approve_self != ''): ?>
							        				<i class="fa fa-check-circle" style="color: green;"></i> &nbsp; <?php echo e($timecard->approve_self); ?>

							        			<?php else: ?>
							        				<i class="fa fa-exclamation-circle" style="color: red;"></i> &nbsp; <span style="display: none;">Uncompleted</span>
							        			<?php endif; ?>
							        			<?php if($timecard->approve_self == '' && $timecard->updated_by != Auth::user()->name): ?>
							        				&nbsp;
							        				<div class="manualA">
		                                                <form method="POST" action="<?php echo e(url('admin/timecard/manual-approval')); ?>" 
		                                                onsubmit="return confirm('Are you sure you want to manually complete employee approval for <?php echo e($user->name); ?>?')" 
		                                                style="display: inline;">
		                                                    <input type="hidden" name="_token" value="<?php echo e(csrf_token()); ?>">
		                                                    <input type="hidden" name="timecardID" value="<?php echo e($timecard->id); ?>">
		                                                    <button type="submit" style="border: none; background-color: transparent; padding: 0; color: red; display: inline;">
		                                                        &nbsp;<i class="fa fa-thumbs-o-up" style="display: inline; cursor: help;" title="Manually Approve Timecard (employee)"></i>
		                                                    </button>
		                                                </form>
		                                            </div>
							        			<?php endif; ?>
							        			
							        		</td>
							        		<td>
							        			<?php if($timecard->approve_manager != ''): ?>
							        				<i class="fa fa-check-circle" style="color: green;"></i> &nbsp; <?php echo e($timecard->approve_manager); ?> &nbsp; <span style="display: none;">Approved</span>
							        			<?php else: ?>
							        				<i class="fa fa-exclamation-circle" style="color: red;"></i> &nbsp; <span style="display: none;">Uncompleted</span>
							        			<?php endif; ?>
							        			<?php if($timecard->approve_self != '' && $timecard->approve_manager == '' && $timecard->updated_by != Auth::user()->name): ?>
							        				&nbsp;
							        				<div class="manualA">
		                                                <form method="POST" action="<?php echo e(url('admin/timecard/manual-approval')); ?>" 
		                                                onsubmit="return confirm('Are you sure you want to manually complete manager approval for <?php echo e($user->name); ?>?')" 
		                                                style="display: inline;">
		                                                    <input type="hidden" name="_token" value="<?php echo e(csrf_token()); ?>">
		                                                    <input type="hidden" name="timecardID" value="<?php echo e($timecard->id); ?>">
		                                                    <button type="submit" style="border: none; background-color: transparent; padding: 0; color: red; display: inline;">
		                                                        &nbsp;<i class="fa fa-thumbs-o-up" style="display: inline; cursor: help;" title="Manually Approve Timecard (manager)"></i>
		                                                    </button>
		                                                </form>
		                                            </div>
							        			<?php endif; ?>
							        		</td>
							        	</tr>
							        <?php endforeach; ?>
							        </tbody>
							    </table>
                             </div>
                         </div>
                     </div>
               </div>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('extras'); ?>
	<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/t/bs/pdfmake-0.1.18,dt-1.10.11,b-1.1.2,b-flash-1.1.2,b-html5-1.1.2,b-print-1.1.2/datatables.min.css"/>
   
    <style>
    	a:hover{
    		text-decoration: none;
    	}
    	.odd {
    		background-color: #fff;
    	}
    	.even {
    		background-color: #eee;
    	}
    	.table-striped>tbody>tr:nth-of-type(odd) {
    		
    		background-color: #eee;
    	}
    	.table-striped>tbody>tr:nth-of-type(even) {
    		background-color: #fff;
    	}
    	.floatLeft
    	{
    		float: left;
    	}
    	th {
    		padding: 5px;
    	}
    	td {
    		padding: 5px;
    	}
		span, strong {
		-webkit-print-color-adjust: exact;
		}
		td.details-control {
		    background: url('/images/details_close2.png') no-repeat center center;
		    cursor: pointer;
		    background-size: 10px 10px;
		}
		tr.shown td.details-control {
		    background: url('/images/details_open2.png') no-repeat center center;
		    background-size: 10px 10px;
		}
		.manualA {
			display: none;
		}
		
    </style>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('bottom'); ?>

	<script type="text/javascript" src="https://cdn.datatables.net/t/bs/pdfmake-0.1.18,dt-1.10.11,b-1.1.2,b-flash-1.1.2,b-html5-1.1.2,b-print-1.1.2/datatables.min.js"></script>
	
	<script>
	var table;
	var open_state = false;
	$(document).ready(function() {
	    table = $('#dt-timecards').DataTable( {

	        "order": [[2, 'asc']],
	        "paging": false,
	        "scrollX" : window.innerWidth < 800 ? true : false,
			"columnDefs": [
			               {
			                   "targets": [ 0 ],
			                   "visible": true,
			                   "searchable": false,
			                   "className": 'details-control',
			                   "orderable": false
			               },
			           ],

	    } );
	    $('#dt-timecards tbody').on('click', 'td.details-control', function () {
	        var tr = $(this).closest('tr');
	        //console.log(tr);
	        var row = table.row( tr );
	 
	        if ( row.child.isShown() ) {
	            // This row is already open - close it
	            row.child.hide();
	            tr.removeClass('shown');
	        }
	        else {
	            // Open this row
	            row.child( format(row.data()) ).show();
	            tr.addClass('shown');
	        }
	    } );
	   
	} );

	function openChildren()
	{
		table.rows().every( function(rowIdx, tableLoop, rowLoop) {
			var tr = this.node();
			var row = this;
			//console.log(tr.className);
			if(!open_state)
			{
				if ( row.child.isShown() ) {
		            // This row is already open - close it
		            //row.child.hide();
		            //tr.removeClass('shown');
		        }
		        else {
		            // Open this row
		            row.child( format(row.data()) ).show();
		            //tr.addClass('shown');
		            tr.className = tr.className + " shown";
		        }
		        
			}
			else
			{
				if ( row.child.isShown() ) {
		            // This row is already open - close it
		            row.child.hide();
		            //tr.removeClass('shown');
		            tr.className = '';
		        }
		        else {
		            // Open this row
		            //row.child( format(row.data()) ).show();
		            //tr.addClass('shown');
		            //tr.className = tr.className + " shown";
		        }
		        
			}
			
			
		} );
		if(open_state)
		{
			 open_state = false;
		}
		else
		{
			open_state = true;
		}
	}
	
	function format(data)
	{
		var timecard = timecards[data[2]];
		var account = accounts[data[2]];
		var allocation = allocations[data[2]];
		var allocation_detail = allocation_details[data[2]];
		var hasAllocations = false;
		var wd_total = 0;
		var we_total = 0;
		var week_total = 0;
		var regular_total = 0;
		var text = "";
		
		text += "<table width='100%' class='table table-striped table-bordered'>"+
					"<thead>"+
						"<th>Account</th>"+
						"<th>Company</th>"+
						"<th>Description</th>"+
						"<th>Weekday</th>"+
						"<th>Weekend</th>"+
						"<th>Total</th>"+
					"</thead>"+
					"<tbody>";
					for(var key in timecard.accounts)
					{
						var total = timecard.accounts[key].weekday + timecard.accounts[key].weekend;
						week_total += total;
						wd_total += timecard.accounts[key].weekday;
						we_total += timecard.accounts[key].weekend;
						if(account[key].regular)
						{
							regular_total += total;
						}
						text +=	"<tr>"+
									"<td>"+account[key].number+"</td>"+
									"<td>"+account[key].company+"</td>"+
									"<td>"+account[key].description+"</td>"+
									"<td>"+timecard.accounts[key].weekday+"</td>"+
									"<td>"+timecard.accounts[key].weekend+"</td>"+
									"<td>"+total+"</td>"+
								"</tr>";
					}	
					for(var key in timecard.allocations)
					{
						hasAllocations = true;
						var total = timecard.allocations[key].weekday + timecard.allocations[key].weekend;
						week_total += total;
						wd_total += timecard.allocations[key].weekday;
						we_total += timecard.allocations[key].weekend;
						regular_total += total;
						text +=	"<tr>"+
									"<td>"+"-"+"</td>"+
									"<td>"+"-"+"</td>"+
									"<td>"+allocation[key].description+"</td>"+
									"<td>"+timecard.allocations[key].weekday+"</td>"+
									"<td>"+timecard.allocations[key].weekend+"</td>"+
									"<td>"+total+"</td>"+
								"</tr>";
					}	
					for(var key in timecard.work_orders)
					{
						var total = timecard.work_orders[key].weekday + timecard.work_orders[key].weekend;
						week_total += total;
						wd_total += timecard.work_orders[key].weekday;
						we_total += timecard.work_orders[key].weekend;
						regular_total += total;
						text +=	"<tr>"+
									"<td>"+timecard.work_orders[key].number+"</td>"+
									"<td>"+"-"+"</td>"+
									"<td>"+timecard.work_orders[key].description+"</td>"+
									"<td>"+timecard.work_orders[key].weekday+"</td>"+
									"<td>"+timecard.work_orders[key].weekend+"</td>"+
									"<td>"+total+"</td>"+
								"</tr>";
					}
					text +=	"<tr>"+
								"<td colspan='3'>"+"Regular Total: "+regular_total+"</td>"+
								"<td>"+wd_total+"</td>"+
								"<td>"+we_total+"</td>"+
								"<td><strong>"+week_total+"</strong></td>"+
							"</tr>";	
		text +=		"</tbody>"+
				"</table>";
		if(timecard.notes != '')
		{	
		text += "<table width='100%' class='table table-striped table-bordered'>"+
					"<thead>"+
						"<th>Notes</th>"+
					"</thead>"+
					"<tbody>"+
						"<tr>"+
							"<td>"+timecard.notes+"</td>"+
						"</tr>"+
					"</tbody>"+
				"</table>";
		}
		if(hasAllocations)
		{
			text += "<h5 style='text-decoration: underline;'>Allocation Details</h5>";
			for(var key in timecard.allocations)
			{
			text += "<h6>"+allocation[key].description+"</h6>";
			text += "<table width='100%' class='table table-striped table-bordered'>"+
						"<thead>"+
							"<th>Account</th>"+
							"<th>Company</th>"+
							"<th>Description</th>"+
							"<th>Total</th>"+
						"</thead>"+
						"<tbody>";
						var temp_total = 0;
						for(var k in allocation_detail[key])
						{ 
							temp_total += Number(allocation_detail[key][k].value);
					text +=	"<tr>"+
								"<td>"+allocation_detail[key][k].number+"</td>"+
								"<td>"+allocation_detail[key][k].company+"</td>"+
								"<td>"+allocation_detail[key][k].description+"</td>"+
								"<td>"+allocation_detail[key][k].value+"</td>"+
							"</tr>";
						}
					text +=	"<tr>"+
								"<td colspan='3'></td>"+
								"<td><strong>"+temp_total+"</strong></td>"+
							"</tr>"+
						"</tbody>"+
					"</table>";
			}
			
		}
			return text;
	}
	
    if ( typeof $.fn.dataTable == "function" && typeof $.fn.dataTableExt.fnVersionCheck == "function" && $.fn.dataTableExt.fnVersionCheck('1.9.2')/*older versions should work too*/ )
    {
        $.fn.dataTableExt.oApi.clearSearch = function ( oSettings )
        {
     
            var table = this;
             
            //any browser, must include your own file
            //var clearSearch = $('<img src="/images/delete.png" style="vertical-align:text-bottom;cursor:pointer;" alt="Delete" title="Delete"/>');
             
            //no image file needed, css embedding must be supported by browser
            var clearSearch = $('<i class="fa fa-times-circle-o fa-fw" style="vertical-align:text-bottom;cursor:pointer;color: gray;"></i>');
            $(clearSearch).click( function ()
                    {
                          table.fnFilter('');
                    });
            $(oSettings.nTableWrapper).find('div.dataTables_filter').append(clearSearch);
            $(oSettings.nTableWrapper).find('div.dataTables_filter label').css('margin-right', '-16px');//16px the image width
            $(oSettings.nTableWrapper).find('div.dataTables_filter input').css('padding-right', '16px');
        }
     
        //auto-execute, no code needs to be added
        $.fn.dataTable.models.oSettings['aoInitComplete'].push( {
            "fn": $.fn.dataTableExt.oApi.clearSearch,
            "sName": 'whatever'
        } );
    }

	var timecards = [];
	var accounts = [];
	var allocations = [];
	var work_orders = [];
	var allocation_details = [];
	
	<?php foreach($timecards as $timecard): ?>
	<?php 
    	$employee = $timecard->employee;//\App\Employee::find($timecard->employee); 
    	$user = $timecard->user;//\App\User::find($employee->user);
    	
    ?>
   		timecards[<?php echo e($employee->number); ?>] = 
    	{
			"accounts": <?php echo json_encode($timecard->accounts); ?>,
			"allocations": <?php echo json_encode($timecard->allocations); ?>,
			"work_orders": <?php echo json_encode($timecard->work_orders); ?>,
			"notes": <?php echo json_encode(nl2br($timecard->notes)); ?>,
    	};
		tAccounts = [];
		<?php foreach($timecard->accounts as $key => $value): ?>
			<?php 
				$account = $allAccounts[$key];//\App\Account::find($key); 
			?>
			tAccounts[<?php echo e($key); ?>] = 
			{
				"number": '<?php echo e($account->number); ?>',
				"company": '<?php echo e($account->company); ?>',
				"description": '<?php echo e($account->description); ?>',
				"regular": <?php echo e($account->regular ? 'true' : 'false'); ?>,
			};
		<?php endforeach; ?>
		tAllocations = [];
		tDetails = [];
		<?php foreach($timecard->allocations as $key => $value): ?>
			<?php 
				$account = $allAllocations[$key];//\App\Allocation::find($key); 
				$tempEntries = array(); //\App\Entry::where('timecard','=',$timecard->id)->where('type','=','allocation')->where('account','=',$key)->get();
				foreach($timecard->entries as $ent)
				{
					if($ent->type == 'allocation' && $ent->account == $key)
					{
						$tempEntries[$ent->id] = $ent;
					}
				}
			?>
			tAllocations[<?php echo e($key); ?>] = 
			{
				"description": '<?php echo e($account->description); ?>',
			};
			<?php if(count($tempEntries) > 0): ?>
				<?php 
					$allocation = $account;//\App\Allocation::find($key);
				?> 
				<?php $a_total = 0;?>
				<?php $a_cal = array(); ?>
				<?php $a_time = \App\Allocation_link::where('allocation','=',$allocation->id)->where('time','!=',0.00)->first() ? true : false;?>
				<?php foreach($tempEntries as $a_entry): ?>
					<?php $a_total += $a_entry->value;?>
					<?php if($a_time): ?>
					<?php $a_temp = $allocation->calculate($a_entry->value); ?>
					<?php foreach($a_temp as $kk => $vv): ?>
						<?php if(array_key_exists($kk, $a_cal)): ?>
							<?php $a_cal[$kk] += $vv;	?>
						<?php else: ?>
							<?php $a_cal[$kk] = $vv; ?>
						<?php endif; ?>
					<?php endforeach; ?>
					<?php endif; ?>	
				<?php endforeach; ?>
				
				<?php if(!$a_time): ?>
				<?php $a_cal = $allocation->calculate($a_total); ?>
				<?php endif; ?>
				var a_d_temp = [];
				<?php foreach($a_cal as $k => $v): ?>
					<?php 
						$ind_acc = $allAccounts[$k];//\App\Account::find($k);
					?>

					
					a_d_temp[<?php echo e($k); ?>] = {
						"number": '<?php echo e($ind_acc->number); ?>',
						"company": '<?php echo e($ind_acc->company); ?>',
						"description": '<?php echo e($ind_acc->description); ?>',
						"value": '<?php echo e($v); ?>',
					};
					
					

				<?php endforeach; ?>
				tDetails[<?php echo e($key); ?>] = a_d_temp;
			<?php endif; ?>
		<?php endforeach; ?>	
	
		accounts[<?php echo e($employee->number); ?>] = tAccounts;
		allocations[<?php echo e($employee->number); ?>] = tAllocations;
		allocation_details[<?php echo e($employee->number); ?>] = tDetails;
	<?php endforeach; ?>

	var keys = [];
	
	document.onkeydown = function(evt){    
	   var key = evt.keyCode;
	   keys[key] = true;
	    if(keys[17] && keys[18] && keys[69]){
	    	
			$('.edit-icon').css('display', 'inline');
			
			keys[17] = false;
			keys[18] = false;
			keys[69] = false;
	    } else if(keys[17] && keys[18] && keys[65]){
	    	
			$('.manualA').css('display', 'inline');
			
			keys[17] = false;
			keys[18] = false;
			keys[65] = false;
	    }


	}
	document.onkeyup = function(evt){    
	   var key = evt.keyCode;
	   keys[key] = false;
	}
    
    </script>

<?php $__env->stopSection(); ?>

<?php echo $__env->make('residential.templates.uadmin', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>