@extends('layouts.master')

@section('content')
<div class="content">
    <div class="row" style="min-height: 500px">
        <form action="{{ url('admin/payroll_structure/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_structure') }}" 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" 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 }}<br/><br/>
                        </div>
                        <table class="table table-bordered ">
                            <tbody>
                                <?php $newhead=array(); $old_amount=0;$new_amount=0;?>
                                @foreach($single->psHead as $data)
                                <tr>
                                    <td>{{$data->head->name}} <input type="didden" name="head[]" class="hidden" value="{{$data->head->id}}"></td>
                                    <td> 
                                        <input type="text" class="form-control text-right get_amount" name="amount[]" value="{{ $data->amount }}" placeholder="Amount">
                                    </td>
                                </tr>
                                <?php
                                    $newhead[] = $data->head->id;
                                    $old_amount = $old_amount + $data->amount; 
                                ?>
                                @endforeach
                                @foreach($head as $row)
                                @if(!in_array($row->head_id,$newhead))
                                <tr style="background: #eee">
                                    <td>{{$row->head->name}} <input type="didden" name="head[]" class="hidden" value="{{$row->head_id}}"></td>
                                    <td> 
                                        <input type="text" class="form-control text-right get_amount" name="amount[]" value="{{ $row->amount }}" placeholder="Amount">
                                    </td>
                                </tr>
                                @php $new_amount = $new_amount + $row->amount  @endphp
                                @endif
                                @endforeach
                                <tr>
                                    <th colspan="2" class="text-right"> Total Salary : <span id="total">{{ number_format($old_amount + $new_amount) }}</span> TK.</th>
                                </tr>
                            </tbody>
                        </table>
                    </div>

                    <div class="col-md-10 col-md-offset-1" style="margin-top: 30px;">
                        <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-save"></i>
                            </span>
                            Update
                        </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').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