{{ csrf_field() }}
<div class="box box-primary">
    <div class="box-header with-border">
        <h3 class="box-title"></h3>
    </div>
    <input type="hidden" name="name" value=" ">
    <div class="box-body">
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    {!! Form::label('type', 'Tipologia:') !!}
                    {!! Form::select('type', ['nation' => 'Nazionale', 'region' => 'Regionale'] , null, array('class' => 'form-control')) !!}
                </div>                
            </div>
            <div class="col-md-6">
                <div class="form-group">
                    {!! Form::label('season_id', 'Stagione:') !!}
                    {!! Form::select('season_id', [''=>''] + $seasons, null, ['class' => 'form-control']) !!}
                </div>                
            </div>
        </div>
        <div class="row">
            <div class="col-md-6">
                <div class="form-group">
                    {!! Form::label('category_id', 'Categoria:') !!}
                    <select name="category_id" class="form-control"></select>
                </div>                
            </div>

            <div class="col-md-6">
                <div class="form-group">
                    {!! Form::label('group_id', 'Girone:') !!}
                    <select name="group_id" class="form-control"></select>
                </div>                
            </div>
        </div>
        <div class="row">
            <div class="col-md-6">    
                <div class="form-group">
                    {!! Form::label('file', 'File da importare:') !!}<br>
                    @if(isset($calendar))
                        <small>Attenzione, se viene importato un file viene sovrascritto il calendario</small><br>
                    @endif
                    <div class="custom-file-upload">{!! Form::file('file', null, array('class' => 'form-control')) !!}</div>
                </div>            
            </div>
        </div>
    
    </div>

    <div class="box-footer">
        @if(Auth::user()->only_read == false)
            {!! Form::submit($submit_text, ['class'=>'btn btn-success']) !!}
        @endif
        {!! link_to_route('calendars.index', 'Annulla', null, array('class' => 'btn btn-info')) !!}
    </div>
</div>

@section('extra_js')

    <script>

        function loadCategories()
        {
            var type = $('select[name="type"] option:selected').val();
            $.getJSON( '/admin/load_categories/' + type, {
                format: "json"
              })
            .done(function( data ) {
                $('select[name="category_id"]').empty();
                jQuery.each(data, function(i, val) {
                    var o = new Option(val.name, val.id);
                    $(o).html(val.name);
                    $('select[name="category_id"]').append(o);
                });
                @if(isset($calendar) && $calendar->category_id > 0)
                    $('select[name="category_id"]').val('{{$calendar->category_id}}');
                @endif
                loadGroups();
            });
        }

        function loadGroups()
        {
            var category = $('select[name="category_id"] option:selected').val();
            $.getJSON( '/admin/load_groups/' + category, {
                format: "json"
              })
            .done(function( data ) {
                $('select[name="group_id"]').empty();
                jQuery.each(data, function(i, val) {
                    var o = new Option(val.name, val.id);
                    $(o).html(val.name);
                    $('select[name="group_id"]').append(o);
                });
                @if(isset($calendar) && $calendar->group_id > 0)
                    $('select[name="group_id"]').val('{{$calendar->group_id}}');
                @endif
            });
        }

        loadCategories();

        $( document ).ready(function() {
            $('select[name="type"]').change(function() {
                loadCategories();
            });
            $('select[name="category_id"]').change(function() {
                loadGroups();
            });
        });
        
    </script>
    
@stop