@extends('layouts.master')

@section('content')
<div class="content">
    <div class="row" style="min-height: 500px">
        <form action="{{ url('admin/grade_structure/update') }}" 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/grade_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">
                    {{ csrf_field() }}
                    
                    <div class="col-md-10 col-md-offset-1">
                        <h4>{{ $grade_id->name }} :</h4>
                        <table class="table table-bordered">
                            <tbody>
                                <input type="hidden" name="grade_id" value="{{$grade_id->id}}" class="hidden">
                                <?php $newhead=array(); $old_amount=0;$new_amount=0; ?>
                                @if(!empty($single))
                                @foreach($single as $row)
                                <tr>
                                    
                                    <input type="hidden" name="head_id[]" value="{{$row->head_id}}" class="hidden">
                                  <td>{{$row->head->name}}</td>
                                  <td><input type="text" name="amount[]" value="{{$row->amount}}" class="form-control text-right get_amount" required=""></td>
                                </tr>
                                <?php
                                    $old_amount = $old_amount + $row->amount;
                                    $newhead[] = $row->head_id;
                                ?>
                                @endforeach
                                @endif
                                @foreach($heads as $head)
                                    @if(!in_array($head->id,$newhead))
                                        <tr>
                                            <input type="hidden" name="head_id[]" value="{{$head->id}}" class="hidden">
                                          <td>{{$head->name}}</td>
                                          <td><input type="text" name="amount[]" value="0" class="form-control text-right get_amount" required=""></td>
                                        </tr>

                                    @endif
                                @endforeach
                                <tr>
                                    <td>Total Amount:</td>
                                    <td class="text-right"><span class="total_amount">{{ number_format($old_amount) }}</span> TK.</td>
                                </tr>

                            </tbody>
                          </table>
                    </div>

                    <div class="col-md-10 col-md-offset-1" style="margin-top: 30px;">
                        <a href="{{ url('admin/grade_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_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