@extends('layouts.master')

@section('content')
<link rel="stylesheet" href="{{ asset('/assets/') }}/css/dataTables.bootstrap.min.css">
<link rel="stylesheet" href="{{ asset('/assets/') }}/css/buttons.dataTables.min.css">

<div class="content">
    <div class="row" style="min-height: 500px">
        <form action="{{ url('admin/payroll/bill_create') }}" enctype="multipart/form-data" method="POST" role="form">
        <div class="col-md-10 col-md-offset-1">
            <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-10 col-md-offset-1">
            <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-12">
                        <table class="table table-bordered " id="example">
                            <thead>
                                <tr>
                                    <th>#</th>
                                    <th>Employee Name</th>
                                    <th>Designation</th>
                                    <th>Grade</th>
                                    <th class="text-right">Salary</th>
                                    <th class="text-right">Due</th>
                                    <th class="text-right">Payable Amount</th>
                                    <th class="text-center">Status</th>
                                </tr>
                            </thead>
                            <tbody>
                            @if($all)
                            <?php $i=1; $total=0; $total_payable=0;?>
                            @foreach($all as $row)
                                <tr>
                                    <input type="hidden" name="emp_id[]" value="{{$row->employee->id}}" class="hidden">
                                    <td>{{ $i++ }}</td>
                                    <td>{{ $row->employee->name }}</td>
                                    <td>{{ $row->designationName->designation['name'] }}</td>
                                    <td>{{ $row->grade->name }}</td>

                                    <td class="text-right"> 
                                        <?php $salary=0; ?>
                                        @foreach($row->psHead as $data)
                                            @php $salary = $salary + $data->amount @endphp

                                        @endforeach
                                        <span>{{ number_format($salary,2) }} </span>
                                        <input type="hidden" name="salary[]" value="{{$salary}}" class="hidden">

                                    </td>
                                    <td class="text-right">
                                        <?php $gross=0; $paid=0; ?>
                                        @foreach($row->due as $due)
                                            @php 
                                            $gross = $gross + $due->gross_amount;
                                            $paid = $paid + $due->paid_amount
                                            @endphp
                                        @endforeach
                                        @php 
                                        $total_due = $gross - $paid
                                        @endphp
                                        {{ number_format($total_due,2) }}
                                    </td>
                                    <td class="text-right">{{ number_format($total_payable = $salary + $total_due,2) }}</td>
                                    <td class="text-center">
                                        @if($total_due <= 0)
                                        <label class="label label-success">Paid</label>
                                        @else
                                        <label class="label label-info">Due</label>
                                        @endif
                                    </td>
                                    @php $total = $total + $total_payable @endphp
                                </tr>
                            @endforeach
                            @endif
                            </tbody>
                        </table>
                        <h5 class="text-right"><b>Total Salary : {{ number_format($total,2) }} TK. </b></h5>
                    </div>

                    <div class="col-md-12" style="margin-top: 15px">
                        <a href="{{ url('admin/payroll') }}" 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-plus"></i>
                            </span>
                            Bill Create
                        </button>
                    </div>
                </div>
            </div>
        </div>

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


@section('script')
<script src="{{ asset('/assets/') }}/js/jquery.dataTables.min.js"></script>
<script src="{{ asset('/assets/') }}/js/dataTables.bootstrap.min.js"></script>
<script src="{{ asset('/assets/') }}/js/dataTables.buttons.min.js"></script>
<script src="{{ asset('/assets/') }}/js/buttons.print.min.js"></script>
<script type="text/javascript">
    var table=$("#example").DataTable({
         "searching": true,
         "lengthChange": false,
         "ordering": false,    
         "bPaginate": false,
         "bInfo" : false,
          dom: 'Bfrtip',
        buttons: [
            'print'
        ]
    });
    
</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