@extends('layouts.master')

@section('content')
<section class="content">
	 <div class="row">
	 <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
	 	<div class="panel panel-primary">
	 		<div class="panel-heading">
	 			<h3 class="panel-title">Journal</h3>
	 		</div>
	 		<div class="panel-body" style='min-height: 350px;'>
	 			@include('layouts.message')
						<a href="{{ url('account/journal/new') }}" class="btn btn-primary pull-right"><i class="fa fa-plus"></i> Add Expense</a>  
					
	 					<a href="{{ url('account/revenue') }}" class="btn btn-success pull-right" style='margin-right: 10px;'>
	 					<i class="fa fa-plus"></i> Add Revenue</a>
	 					
	 			
	 			<br><br>
	 			<table class="table table-striped table-bordered">
	 				<thead>
	 					<tr>
	 						<th>Sl.</th>
	 						<th>Purpose</th>
	 						<th>Description</th>
	 						<th>Requested At</th>
	 						<th>Request By</th>
	 						<th>Due Date</th>
	 						<th>Amount</th>
	 						<th>Approval Status</th>
	 					</tr>
	 				</thead>
	 				<tbody>
	 				<?php $indx=1; ?>
	 				@foreach($approved_funds as $fund)
	 					<tr>
	 						<td>{{ $indx }}</td>
	 						<td>{{ $fund->acc_head?$fund->acc_head->name:'' }}</td>
	 						<td>{{ $fund->description }}</td>
	 						<td>{{ date('d M, Y',strtotime($fund->created_at)) }}</td>
	 						<td>{{ $fund->user?$fund->user->name:'' }}</td>
	 						<td>{{ date('Y M, Y',strtotime($fund->due_date)) }}</td>
	 						<td style='text-align: right;'>{{ $fund->amount }}</td>
	 						<td width="13%">
	 							<a href="{{ url('account/journal/payment') }}/{{ $fund->id }}" class="btn btn-info btn-xs">
	 								Make Payment
	 							</a>
	 						</td>
	 					</tr>
	 				<?php $indx++; ?>
	 				@endforeach
	 				</tbody>
	 			</table>
	 			<table class="table table-striped table-bordered hide">
	 				<thead>
	 					<tr>
	 						<th>Sl.</th>
	 						<th>Account Head</th>
	 						<th style='text-align: right'>Balance(Tk.)</th>
	 						<th width="20%">Approval Status</th>
	 						<th width="20%">Payment</th>
	 					</tr>
	 				</thead>
	 				<tbody>
	 				<?php $summery=0; ?>
	 				@if($journals)
	 				<?php $indx=1; ?>
	 				@foreach($journals as $journal)
	 				<?php 
	 				$balance=$journal->debit-$journal->credit; 
	 				$summery+=$balance;
	 				?>
	 				@if($balance>0)
	 					<tr>
	 						<td>{{ $indx }}</td>
	 						<td><a data-toggle='modal' data-target='#modalJournalDetails' href="{{ url('account/journal/details') }}/{{ $journal->debit_account }}">{{ $journal->name }}</a></td>
	 						<td style='text-align: right;'>{{ $balance }}</td>
	 						<td width="20%">
	 							<?php
	 							$app_remarks=App\Models\JournalComment::where('approaval_status',1)->where('debit_account',$journal->debit_account)->first();
	 							?>
	 							<label class="checkbox">
		 							@if(!$app_remarks)
		 							@if(\Auth::user()->id==2)
		 							<input type="checkbox" class="ck_approaval" data-balance='{{ $balance }}' data-debit_account='{{ $journal->debit_account }}' value="1">
		 							@endif
		 							<span>Not Approved</span>
		 							@else
		 							<span>{{ $app_remarks->approaval_status?'Approaved':'Not Approaved' }}</span>
		 							@endif
	 							</label>
	 						</td>
	 						<td>
	 							@if($app_remarks)
	 							<a href="{{ url('account/journal/payment') }}/{{ $journal->debit_account }}" class="btn btn-info btn-xs">
	 								Make Payment
	 							</a>
	 							@else
	 							<span class="label label-danger">Need Approval</span>
	 							@endif
	 						</td>
	 					</tr>
	 				<?php $indx++; ?>
					@endif
	 				@endforeach
	 				@endif
	 				</tbody>
	 				<tfoot>
	 					<tr>
	 						<th></th>
	 						<th></th>
	 						<th style='text-align: right;'>{{ $summery }}</th>
	 						<th></th>
	 						<th></th>
	 					</tr>
	 				</tfoot>
	 			</table>

 				@if(!$funds->isEmpty())
	 			 	<h3>Fund Requests</h3>
		 			<table class="table table-bordered table-striped">
		 				<thead>
		 					<tr>
		 						<th>Sl.</th>
		 						<th>Purpose</th>
		 						<th>Description</th>
		 						<th>Requested At</th>
		 						<th>Requested By</th>
		 						<th>Due Date</th>
		 						<th style='text-align: right;'>Amount</th>
		 						<th>Approval Status</th>
		 					</tr>
		 				</thead>
		 				<tbody>
		 				<?php $indx=1; ?>
		 				@foreach($funds as $fund)
		 					<tr>
		 						<td>{{ $indx }}</td>
		 						<td>{{ $fund->acc_head?$fund->acc_head->name:'' }}</td>
		 						<td>{{ $fund->description }}</td>
		 						<td>{{ date('d M, Y',strtotime($fund->created_at)) }}</td>
		 						<td>{{ $fund->user?$fund->user->name:'' }}</td>
		 						<td>{{ date('Y M, Y',strtotime($fund->due_date)) }}</td>
		 						<td style='text-align: right;'>{{ $fund->amount }}</td>
		 						<td width="13%">
		 							<div class="btn-group">
		 							  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><i class="fa fa-cog"></i>  
		 							    Action <span class="caret"></span>
		 							  </button>
		 							  <ul class="dropdown-menu">
		 							    <li><a class='btnReqApprove' data-id='{{ $fund->id }}' data-purpose='{{ $fund->purpose }}' data-description='{{ $fund->description }}' data-amount='{{ $fund->amount }}' href="#">Approve</a></li>
		 							    <li><a class='btnReqRevoke' data-id='{{ $fund->id }}' href="#">Revoke</a></li>
		 							  </ul>
		 							</div>
		 						</td>
		 					</tr>
		 				<?php $indx++; ?>
		 				@endforeach
		 				</tbody>
		 			</table>
 			 	@endif
	 		</div>
	 	</div>

		
	 </div>
	 </div>
</section>


<div class="modal fade" id="modalJournalDetails">
	
</div>

<div class="modal fade" id="appModal">
	<div class="modal-dialog modal-sm">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h4 class="modal-title">Payment Approval</h4>
			</div>
			<div class="modal-body">
				<input type="hidden" name="debit_account_id" value="0">
				<div class="form-group">
				<input type="text" onkeypress="return checkNumber(event)" class="form-control" name="modalAmount" id="modalAmount" placeholder="Amount" value="0">
				</div>
				<div class="form-group">
				<textarea name="modalRemarks" id="modalRemarks" class='form-control' placeholder="Remarks"></textarea>
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
				<button type="button" id='btnApproave' class="btn btn-primary">Save changes</button>
			</div>
		</div>
	</div>
</div>
@stop



@section('style')
<style>
	#modalJournalDetails .modal-dialog  {width:75%;}
	@media screen and (min-width: 768px) {
		
		#modalJournalDetails .modal-dialog  {width:900px;}

	}
</style>
@stop


@section('script')
<script type="text/javascript">
$(document).ready(function() {

	$('.ck_approaval').click(function(){
		var that=$(this);
		var status=$(this).is(':checked')?1:0;
		if(status==1)
		{
			$('#appModal').modal('toggle');
			$('#appModal').find("input[name='debit_account_id']").val($(this).data('debit_account'));
			$('#appModal').find("#modalAmount").val($(this).data('balance'));
		}
	});

	$('#btnApproave').click(function(){
		var status=1;
		var debit_acc_id=$("input[name='debit_account_id']").val();
		var debit_amount=$('#modalAmount').val();
		var remarks=$('#modalRemarks').val();
		var token='{{ csrf_token() }}';

		$.ajax({
			url: '{{ url("account/journal/change_approaval_status") }}',
			type: 'POST',
			dataType:'JSON',
			data: {debit_account: debit_acc_id,status:status,amount:debit_amount,remarks:remarks,_token:token}
		})
		.done(function(res) {
			if(res.ok)
			{
				window.location.href='{{ url('account/journal') }}';
			}
		});

	});

	$('body').on('hide.bs.modal', '.modal', function () {
	  $(this).removeData('bs.modal');
	});

	/**
	 * approve fund request
	 */
	$('.btnReqApprove').click(function(e){
		e.preventDefault();
		var id=$(this).data('id');
		var purpose=$(this).data('purpose');
		var description=$(this).data('description');
		var amount=$(this).data('amount');
		var token='{{ csrf_token() }}';

		$.ajax({
			url: '{{ url('account/fund/approval_status') }}',
			type: 'POST',
			data: {id: id,purpose:purpose,description:description,amount:amount,status:1,_token:token}
		})
		.done(function(res) {
			alert('Fund request approved successfully !');
			location.href='{{ url('account/journal') }}';
		});
	});//end approve fund request


	/**
	 * remoke fund request
	 */
	$('.btnReqRevoke').click(function(e){
		e.preventDefault();
		var id=$(this).data('id');
		var token='{{ csrf_token() }}';

		$.ajax({
			url: '{{ url('account/fund/approval_status') }}',
			type: 'POST',
			data: {id: id,status:2,_token:token}
		})
		.done(function(res) {
			alert('Fund request revoked successfully !');
			location.href='{{ url('account/journal') }}';
		});
	});//end remoke fund request

});

function checkNumber(evt)
{
	var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode != 46 && charCode > 31 
      && (charCode < 48 || charCode > 57))
    {
    	alert('Invalid amount  !!');
       return false;
    }

    return true;
}
</script>
@stop