@extends('layouts.master')

@section('navigation')
                @include('local.managers.admins._navigation')
@stop
@section('main')

    <div class="row">
        <div class="col-md-6">
                <!-- Form to add a new criterion -->
                <div class="panel panel-default panel-button">
                    <div class="panel-heading">
                        Create
                    </div>
                    <div class="panel-body">
                        {{ Form::open(array('action' => 'CriteriaController@create')) }}

                            <div class="form-group">
                                {{ Form::label('outcome_id', 'Associated Learning Outcome') }}
                                <select name="outcome_id" class="form-control selectpicker">
                                @foreach ($outcomes as $outcome)
                                    @if(Input::old('outcome_id')==$outcome->id)
                                        <option selected="selected" value="{{ $outcome->id }}">{{ $outcome->name }}</option>
                                    @else
                                        <option value="{{ $outcome->id }}">{{ $outcome->name }}</option>
                                    @endif
                                @endforeach
                                </select>
                            </div>

                            <!-- Associated Program -->
                            <div class="form-group">
                                {{ Form::label('program_id', 'Associated Program') }}
                                <select id="program_id" name="program_id" class="form-control selectpicker">
                                    <option value="0">All</option>
                                    @foreach ($programs as $program)
                                        <option value="{{ $program->id }}">{{ $program->name }} [{{ $program->school->name }}]</option>
                                    @endforeach
                                </select>
                            </div>

                            <div class="form-group">
                                {{ Form::label('name', 'Name') }}
                                {{ Form::text('name', '', array('class' => 'form-control')) }}
                            </div>

                            <div class="form-group">
                                {{ Form::label('subcriteria', 'Subcriteria') }}
                                <p class="help-block"><strong>Manually add</strong> bullets or numbering.</p>
                                {{ Form::textarea('subcriteria', '', array('class' => 'form-control', 'rows'=>3, 'aria-labelledby'=>'subcriteria')) }}
                            </div>

                            <div class="form-group">
                                {{ Form::label('description12', 'Beginning (1-2)') }}
                                {{ Form::textarea('description12', '', array('class' => 'form-control', 'rows'=>2, 'aria-labelledby'=>'description12')) }}
                            </div>

                            <div class="form-group">
                                {{ Form::label('description34', 'In Progress (3-4)') }}
                                {{ Form::textarea('description34', '', array('class' => 'form-control', 'rows'=>2, 'aria-labelledby'=>'description34')) }}
                            </div>

                            <div class="form-group">
                                {{ Form::label('description56', 'Satisfactory (5-6)') }}
                                {{ Form::textarea('description56', '', array('class' => 'form-control', 'rows'=>2, 'aria-labelledby'=>'description56')) }}
                            </div>

                            <div class="form-group">
                                {{ Form::label('description78', 'Excellent (7-8)') }}
                                {{ Form::textarea('description78', '', array('class' => 'form-control', 'rows'=>2, 'aria-labelledby'=>'description78')) }}
                            </div>

                            <div class="form-group">
                                {{ Form::label('copyright', 'Copyright') }}
                                {{ Form::textarea('copyright', '', array('class' => 'form-control', 'rows'=>2, 'placeholder'=>'(optional)', 'aria-labelledby'=>'copyright')) }}
                            </div>

                            <div class="form-group">
                                {{ Form::label('notes', 'Notes') }}
                                {{ Form::textarea('notes', '', array('class' => 'form-control', 'rows'=>2, 'placeholder'=>'(optional)', 'aria-labelledby'=>'notes')) }}
                            </div>

                            {{ Form::submit('Create', array('class' => 'btn btn-primary btn-block')) }}
                        {{ Form::close() }}
                    </div>
                </div>
        </div>

        <div class="col-md-6">
                <div class="panel panel-default panel-button">
                    <div class="panel-heading">
                        Edit
                    </div>
                    <div class="panel-body">
                        {{ Form::open(array('action' => 'CriteriaController@update')) }}

                            <div class="form-group">
                                {{ Form::label('criterion_id', 'Criterion') }}
                                <select id="select-criterion" name="id" class="form-control selectpicker">
                                @foreach ($criteria as $criterion)
                                    <option value="{{ $criterion->id }}"



                                        data-subtext="
                                        @if($criterion->program)
                                            &nbsp;&nbsp;&nbsp;[{{ $criterion->program->name }}]
                                        @endif
                                        "
                                    >
                                        {{ $criterion->name }}



                                    </option>
                                @endforeach
                                </select>
                            </div>

                            <!-- Associated Outcome -->
                            <div class="form-group">
                                {{ Form::label('assoc_outcome_id', 'Associated Learning Outcome') }}
                                <select id="assoc_outcome_id" name="outcome_id" class="form-control selectpicker">
                                    @foreach ($outcomes as $outcome)
                                        <option value="{{ $outcome->id }}">{{ $outcome->name }}</option>
                                    @endforeach
                                </select>
                            </div>

                            <!-- Associated Program -->
                            <div class="form-group">
                                {{ Form::label('program_id2', 'Associated Program') }}
                                <select id="program_id2" name="program_id" class="form-control selectpicker">
                                    <option value="0">All</option>
                                    @foreach ($programs as $program)
                                        <option value="{{ $program->id }}">{{ $program->name }} [{{ $program->school->name }}]</option>
                                    @endforeach
                                </select>
                            </div>

                            <!-- Status -->
                            <div class="form-group">
                                {{ Form::label('status', 'Status') }}
                                <span data-toggle="tooltip" data-placement="top" title="Use this option to deactivate or reactivate criteria. Inactive criteria will stay in the system, but will not be available to use in new rubrics." class="glyphicon glyphicon-question-sign"></span>

                                <select id="status" name="status" class="form-control">
                                        <option value="1">Active</option>
                                        <option value="0">Inactive</option>
                                </select>
                            </div>

                            <div class="form-group">
                                {{ Form::label('name', 'Name') }}
                                {{ Form::text('name', Input::old('name'), array('class' => 'form-control', 'id'=>'criterion_name')) }}
                            </div>

                            <div class="form-group">
                                {{ Form::label('subcriteria', 'Subcriteria') }}
                                <p class="help-block"><strong>Manually add</strong> bullets or numbering.</p>
                                {{ Form::textarea('subcriteria', '', array('class' => 'form-control', 'rows'=>3, 'id' => 'criterion_subcriteria')) }}
                            </div>

                            <div class="form-group">
                                {{ Form::label('description12', 'Beginning (1-2)') }}
                                {{ Form::textarea('description12', Input::old('description12'), array('class' => 'form-control', 'rows'=>2, 'id'=>'criterion_description12')) }}
                            </div>

                            <div class="form-group">
                                {{ Form::label('description34', 'In Progress (3-4)') }}
                                {{ Form::textarea('description34', Input::old('description34'), array('class' => 'form-control', 'rows'=>2, 'id'=>'criterion_description34')) }}
                            </div>

                            <div class="form-group">
                                {{ Form::label('description56', 'Satisfactory (5-6)') }}
                                {{ Form::textarea('description56', Input::old('description56'), array('class' => 'form-control', 'rows'=>2, 'id'=>'criterion_description56')) }}
                            </div>

                            <div class="form-group">
                                {{ Form::label('description78', 'Excellent (7-8)') }}
                                {{ Form::textarea('description78', Input::old('description78'), array('class' => 'form-control', 'rows'=>2, 'id'=>'criterion_description78')) }}
                            </div>

                            <div class="form-group">
                                {{ Form::label('copyright', 'Copyright Information') }}
                                {{ Form::textarea('copyright', Input::old('copyright'), array('class' => 'form-control', 'rows'=>2, 'id'=>'criterion_copyright', 'placeholder'=>'(optional)')) }}
                            </div>

                            <div class="form-group">
                                {{ Form::label('notes', 'Additional Notes') }}
                                {{ Form::textarea('notes', Input::old('notes'), array('class' => 'form-control', 'rows'=>2, 'id'=>'criterion_notes', 'placeholder'=>'(optional)')) }}
                            </div>

                            {{ Form::submit('Update', array('class' => 'btn btn-primary btn-block')) }}
                        {{ Form::close() }}
                    </div>
                </div>
        </div>
    </div>
@stop

@section('javascript')

    // --------------------------------------------------------------------------
    // Page load
    // --------------------------------------------------------------------------

    // Hide accordion panel contents by default
    $('.panel-group .panel-body').hide();

    $('#outcome-display').parent().hide();

    fetchCriterionForEditing();
    // setCriterionStatus();




    // --------------------------------------------------------------------------
    // Functions
    // --------------------------------------------------------------------------

    // Fetch criterion info for editing
    function fetchCriterionForEditing()
    {
        var id = $('#select-criterion').find(':selected').val();

        $.post(
        "{{ URL::action('CriteriaController@fetchCriterionWithTrashed') }}",
        { id: id },
        function(json)
        {
            var name = json.name;

            var subcriteria = '';
            if(json.subcriteria){
                subcriteria= JSON.parse(json.subcriteria).join('\n');
            }

            var description12 = json.description12;
            var description34 = json.description34;
            var description56 = json.description56;
            var description78 = json.description78;
            var copyright = json.copyright;
            var notes = json.notes;

            // Display info
            $('#criterion_name').val(name);
            $('#criterion_subcriteria').text(subcriteria);
            $('#criterion_description12').text(description12);
            $('#criterion_description34').text(description34);
            $('#criterion_description56').text(description56);
            $('#criterion_description78').text(description78);

            // If copyright or notes aren't empty, load them
            if(copyright){
                $('#criterion_copyright').text(copyright);
            }else{
                $('#criterion_copyright').text('');
            }
            if(notes){
                $('#criterion_notes').text(notes);
            }else{
                $('#criterion_notes').text('');
            }
            // Select associated outcome
            $('#assoc_outcome_id').val(json.outcome_id);
            $('#assoc_outcome_id').selectpicker('refresh');



            // Select associated program
            if(json.program_id)
            {
                $('#program_id2').val(json.program_id);

            }
            else
            {
                $('#program_id2').val(0);
            }

            $('#program_id2').selectpicker('refresh');


            // Select status
            if(json.deleted_at)
                $('#status').val(0);
            else
                $('#status').val(1);
        },
        'json'
        );
    }

    // --------------------------------------------------------------------------
    // Events
    // --------------------------------------------------------------------------

    // When panel heading is clicked, toggle it
    $('.panel-group .panel-heading').on('click', function()
    {
        $(this).next().stop().slideToggle();
    })


    // When list item is clicked, load corresponding info
    $('#select-criterion').on('change', function()
    {
        fetchCriterionForEditing();
        $('.selectpicker').selectpicker('refresh');
    });

    // When list item is clicked, load corresponding info
    $('.selectpicker').on('change', function()
    {
        //alert($(this).find(':selected').val());
        $('.selectpicker').selectpicker('refresh');
    });

@stop
