@extends('layouts.master')

@section('content')
<div class="content">
    <div class="row" style="min-height: 500px">
        <form action="{{ url('admin/payroll/update/'.$single->id) }}" enctype="multipart/form-data" method="POST" role="form">
        <div class="col-md-8 col-md-offset-2">
            <header class="panel-heading" style="height:50px;">
                <div class="pull-left">
                    {{$title}}
                </div>
                <div>
                     @if(\Auth::user()->utype==1)
                        <a style="margin-left: 3px" href="{{ url('admin/payroll') }}" class="btn btn-sm btn-info pull-right"><i class="fa fa-arrow-left"></i> Back </a>
                    @endif
                </div>
            </header>
        </div>
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-body">
                    @if ($errors->any())
                        <div class="alert alert-danger">
                            <ol>
                                @foreach ($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ol>
                        </div>
                    @endif

                    {{ csrf_field() }}
                    <div class="col-md-10 col-md-offset-1">
                        <div class="col-md-6 text-left" style="padding: 0px">
                            <b>Name : </b> {{ $single->employee->name }}
                        </div>
                        <div class="col-md-6 text-right" style="padding: 0px">
                            <b>Grade : </b> {{ $single->grade->name }}
                        </div><br/><br/>
                        <table class="table table-bordered">
                            <tbody>
                                @php $total=0 @endphp
                                @foreach($single->psHead as $data)
                                <tr>
                                    <th>{{$data->head->name}} </th>
                                    <td class="text-right"> {{ number_format($data->amount,2) }}</td>
                                </tr>
                                @php $total = $total + $data->amount @endphp
                                @endforeach 
                                <tr>
                                    <th>Total Salary </th>
                                    <th class="text-right"> {{ number_format($total,2) }} TK. </th>
                                </tr>
                            </tbody>
                        </table>

                        <table class="table table-bordered">
                            <h4>Pay Now :</h4>
                            <tbody>
                                @php $total_due=0 @endphp
                                @foreach($single->payableDue as $payableDue)
                                 @if($payableDue->gross_amount > $payableDue->paid_amount)
                                 @php $due= $payableDue->gross_amount - $payableDue->paid_amount; $total_due = $total_due + $due @endphp
                                 <input type="hidden" name="prh_id[]" value="{{ $payableDue->id }}" class="hidden">
                                <tr>
                                    <td>
                                        
                                        {{ date('d/m/Y', strtotime($payableDue->date)) }}
                                        
                                    </td>
                                    <td><input type="text" name="dues[]" id="dues" class="form-control text-right get_amount" value="{{$due}}" required></td>
                                </tr>
                                @endif
                                @endforeach
                                <tr>
                                    <th>Payable Amount</th>
                                    <th class="text-right"><span class="total_amount">{{ number_format($total_due) }}</span> TK.</th>
                                </tr>
                                <tr>
                                    <th>Media</th>
                                    <td> 
                                        <select class="form-control" name="media" required="">
                                            <option disabled="" selected="" value="">Choice One..</option>
                                            @foreach($media as $row)
                                                <option value="{{$row->id}}"> {{$row->name}} </option>
                                            @endforeach
                                        </select>
                                    </td>
                                </tr>
                                <tr>
                                    <th>Description</th>
                                    <td> 
                                        <textarea name="description" class="form-control" rows="3" placeholder="Description"></textarea>
                                    </td>
                                </tr>
                                <input type="hidden" name="emp_id" value="{{ $single->employee->id }}" class="hidden">
                            </tbody>
                        </table>
                    </div>

                    <div class="col-md-10 col-md-offset-1">
                        {{-- <a href="{{ url('admin/payroll_structure') }}" class="btn btn-default pull-left">
                            <span class="btn-icon"> 
                                <i class="fa fa-times"></i>
                            </span>
                            Cancel
                        </a> --}}
                        <button type="submit" class="btn btn-inline btn-success pull-right">
                            <span class="btn-icon"> 
                                <i class="fa fa-credit-card"></i>
                            </span>
                            Pay
                        </button>
                    </div>
                </div>
            </div>
        </div>

        </form>
    </div>
</div>
@stop


@section('script')
<script type="text/javascript">
    $('.get_amount').on('keyup', function(){
    var total = 0;
    // on every keyup, loop all the elements and add all the results
    $('.get_amount').each(function(index, element) {
        var val = parseFloat($(element).val());
        if( !isNaN( val )){
           total += val;
        }
    });
    $('.total_amount').html(total);
});
</script>
@if(Session::has('msg'))
    <div class="alert alert-success center-block msg_alt" >
        <a class="close" data-dismiss="alert" href="#">&times;</a>
        <strong><i class="fa fa-check"></i></strong>&nbsp;
    {{Session::get('msg')}}
    </div>
@endif
@stop