@extends('layouts.master')

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

    <div class="row">
        <div class="col-md-6">
            <div class="panel panel-default panel-button">
                <div class="panel-heading">
                    Create a Course
                </div>
                <div class="panel-body">

                    {{ Form::open(['action' => 'CoursesController@create','enctype'=>"multipart/form-data"]) }}
                    <div class="form-group">
                        {{ Form::label("name",'Name')}}
                        {{ Form::text('name', Input::old('name'), ['class'=>'form-control', 'placeholder' => "Introducción a la ..."])}}
                    </div>
                    <div class="form-group">
                        {{ Form::label('code', 'Code') }}
                        {{ Form::text('code', Input::old('code'), ['class' => 'form-control', 'placeholder' => 'TEST', 'maxLength' => 4]) }}
                    </div>

                    <div class="form-group">
                        {{ Form::label('number', 'Number') }}
                        {{ Form::text('number', Input::old('number'), ['class' => 'form-control', 'placeholder' => '3001', 'maxLength' => 4]) }}
                    </div>

                    <div class="form-group">
                        {{ Form::label('section', 'Section') }}
                        {{ Form::text('section', Input::old('section'), ['class' => 'form-control', 'placeholder' => '001', 'maxLength' => 3]) }}
                    </div>

                    <!-- Program -->
                    <div class="form-group">
                        {{ Form::label('program', 'Program') }}
                        <select id="program" name="program" class="form-control selectpicker">
                            
                            @foreach ($programs as $program)
                                @if (Input::old('program') != $program->id)
                                    <option value="{{ $program->id }}">{{ $program->name }}
                                        ({{ $program->school->name }})</option>
                                @else
                                    <option selected value="{{ $program->id }}">{{ $program->name }}
                                        ({{ $program->school->name }})</option>
                                @endif
                            @endforeach
                        </select>
                    </div>

                    <div class="form-group">
                        {{ Form::label('user_id', 'Professor') }}
                        <select id="professor_id" name="professor_id" class="form-control selectpicker">
                             @foreach ($professors as $professor)
                                 @if (Input::old('professor_id') != $professor->id )
                                    <option value="{{$professor->id}}">{{ $professor->surnames }}, {{$professor->first_name}}
                                        {{-- $professor->program->name --}}</option>
                                @else
                                    <option selected value="{{ $professor->id }}">{{ $professor->name }}
                                        {{-- $professor->program->name --}}</option>
                               

                                    
                                @endif
                            @endforeach
                        </select>
                    </div>

                    <div class ="form-group">
                        {{ Form::label("semester_id",'Semester')}}

                        <select id ="semester_id" name="semester_id" class="form-control selectpicker">
                            @foreach ($semesters as $semester)
                                @if (Input::old('semester_id') != $semester->id)
                                    <option value="{{ $semester->id }}">{{ $semester->name }}</option>
                                @else
                                    <option selected value="{{ $semester->id }}">{{ $semester->name }}</option>
                                @endif
                            @endforeach
                        </select>
                    </div>

                    
                    <div class = "form-group">
                        {{Form::label('modality','Course Modality')}}

                        <select id="modality" name = "modality" class= "form-control selectpicker">
                            @foreach($modalities as $modality)
                                @if (Input::old('modality') != $modality)
                                    <option value="{{ $modality }}">{{ $modality }}</option>
                                @else
                                    <option selected value="{{ $modality }}">{{ $modality }}</option>
                                @endif
                                
                            @endforeach
                        </select>
                    </div>

                    <div class ="form-group">
                        <label for="file">Choose student text file</label>
                        <input type="file"
                        id="students" class="form-control" name="students"
                        accept=".txt">
                    </div>

                    <br>

                    {{ Form::submit('Create Course', ['class' => 'btn btn-primary btn-block', 'name' => 'create_course']) }}
                    {{ Form::close() }}

                    <br>

                    @if (Session::has('courses'))
                        <p><strong>The following courses were updated:</strong></p>
                        <ul>
                            @foreach (json_decode(Session::get('courses')) as $course)
                                <li>

                                    @if (Session::has('show_sections'))
                                        {{ $course->code }}{{ $course->number }}-{{ $course->section }}
                                    @else
                                        {{ $course->code }}{{ $course->number }}
                                    @endif
                                </li>
                            @endforeach
                        </ul>
                    @endif
                </div>
            </div>
        </div>

        <div class="col-md-6">
            <div class="panel panel-default panel-button">
                <div class="panel-heading">
                    Edit Course
                </div>
                <div class="panel-body">



                    <p>You can enroll students in this setting, by choosing the course and uploading a .txt file with the students numbers</p>

                    {{ Form::open(['action' => 'CoursesController@updateCourseInfo', 'enctype'=>"multipart/form-data"]) }}
                    <button class="btn btn-md btn-secondary filterButton">
                        <span class="glyphicon glyphicon-minus">
                        </span>
                        Filters
                    </button>
                    <div class="filterSection">
                        <br>
                        <div class="form-group">
                            {{ Form::label('program_id2', 'Associated Program') }}
                            <select id='select-program' class="form-control selectpicker"
                            onchange='fetchCourses("#select-program", "#select-semester")'>

                                @foreach ($programs as $program)
                                    <option value='{{ $program->id }}' data-subtext="{{ $program->code }}">
                                        {{ $program->name }}</option>
                                @endforeach

                            </select>
                        </div>
                        
                        <div class="form-group">
                            {{ Form::label('semester_assoc', 'Course Semester') }}
                            <select id='select-semester' class="form-control selectpicker"
                                onchange='fetchCourses("#select-program", "#select-semester")'>

                                @foreach ($semesters as $semester)
                                    <option value='{{ $semester->id }}' data-subtext="{{ $semester->code }}">
                                        {{ $semester->name }}</option>
                                @endforeach

                            </select>
                        </div>
                        
                    </div>
                    <hr>
                    <div class = "form-group">
                        {{Form::label('select_course', "Course")}}
                        <select id = "select-course" name = "select-course" class ='form-control selectpicker'
                        onchange = "fetchCourseInfo('#select-course')">
                            

                        </select>
                    </div>


                    <div class="form-group">
                        {{ Form::label("name",'Name')}}
                        {{ Form::text('name', Input::old('name'), ['class'=>'form-control', 'placeholder' => "Introducción a la ...", 'id'=> 'edit_name'])}}
                    </div>
                    <div class="form-group">
                        {{ Form::label('code', 'Code') }}
                        {{ Form::text('code', Input::old('code'), ['class' => 'form-control', 'placeholder' => 'TEST', 'maxLength' => 4, 'id'=> 'edit_code']) }}
                    </div>

                    <div class="form-group">
                        {{ Form::label('number', 'Number') }}
                        {{ Form::text('number', Input::old('number'), ['class' => 'form-control', 'placeholder' => '3001', 'maxLength' => 4, 'id'=> 'edit_number']) }}
                    </div>

                    <div class="form-group">
                        {{ Form::label('section', 'Section') }}
                        {{ Form::text('section', Input::old('section'), ['class' => 'form-control', 'placeholder' => '001', 'maxLength' => 3, 'id'=> 'edit_section']) }}
                    </div>

                    <!-- Program -->
                    <div class="form-group">
                        {{ Form::label('program', 'Program') }}
                        <select id="edit_program" name="program" class="form-control selectpicker">
                            
                            @foreach ($programs as $program)
                                @if (Input::old('program') != $program->id)
                                    <option value="{{ $program->id }}">{{ $program->name }}
                                        ({{ $program->school->name }})</option>
                                @else
                                    <option selected value="{{ $program->id }}">{{ $program->name }}
                                        ({{ $program->school->name }})</option>
                                @endif
                            @endforeach
                        </select>
                    </div>

                    <div class="form-group">
                        {{ Form::label('user_id', 'Professor') }}
                        <select id="edit_professor_id" name="professor_id" class="form-control selectpicker">
                             @foreach ($professors as $professor)
                                 @if (Input::old('professor_id') != $professor->id )
                                    <option value="{{$professor->id}}">{{ $professor->surnames }}, {{$professor->first_name}}
                                        {{-- $professor->program->name --}}</option>
                                @else
                                    <option selected value="{{ $professor->id }}">{{ $professor->name }}
                                        {{-- $professor->program->name --}}</option>
                               

                                    
                                @endif
                            @endforeach
                        </select>
                    </div>

                    <div class ="form-group">
                        {{ Form::label("semester_id",'Semester')}}

                        <select id ="edit_semester_id" name="semester_id" class="form-control selectpicker">
                            @foreach ($semesters as $semester)
                                @if (Input::old('semester_prof') != $semester->id)
                                    <option value="{{ $semester->id }}">{{ $semester->name }}</option>
                                @else
                                    <option selected value="{{ $semester->id }}">{{ $semester->name }}</option>
                                @endif
                            @endforeach
                        </select>
                    </div>

                    <div class = "form-group">
                        {{Form::label('modality','Course Modality')}}

                        <select id="edit_modality" name = "modality" class= "form-control selectpicker">
                            @foreach($modalities as $modality)
                                @if (Input::old('modality') != $modality)
                                    <option value="{{ $modality }}">{{ $modality }}</option>
                                @else
                                    <option selected value="{{ $modality }}">{{ $modality }}</option>
                                @endif
                                
                            @endforeach
                        </select>
                    </div>


                    <div class ="form-group">
                        <label for="file">Choose student text file</label>
                        <input type="file"
                        id="edit_students" class="form-control" name="students"
                        accept=".txt">
                    </div>

                    <br>

                    {{ Form::submit('Edit Course', ['class' => 'btn btn-primary btn-block', 'name' => 'edit_course_info']) }}
                    {{ Form::close() }}

                </div>
            </div>
        </div>
    </div>

    <script>
        $('.filterSection').show();
        $('.filterButton').on('click', function() {
            var span = $(this).find('span');
            if (span.attr('class') == 'glyphicon glyphicon-plus') {
                span.attr('class', 'glyphicon glyphicon-minus');
            } else {
                span.attr('class', 'glyphicon glyphicon-plus');
            }
            $('.filterSection').toggle(533);
        });
        $('.filterButton').on('click', function(e) {
        // Prevent the default action of the clicked item. In this case that is submit
            e.preventDefault();


        return false;
        });

        function fetchCourses(select, semester){

            

            program_id = $(select).val();
            semester_id = $(semester).val();
            $.post(
                "{{URL::action('CoursesController@fetchCourses')}}",
                {
                    program_id:program_id,
                    semester_id:semester_id
                },
                function(courses){
                    $('#select-course').html(
                        " "
                    )
                    $.each(courses, function(ind, course){

                        
                        option =  "<option value= '"+course.id+"' data-subtext ='"+course.code+course.number+"'"+
                        "data-code='"+course.code+"' data-number='"+course.number+"' data-program-id='"+course.program_id+"'"+
                        "data-section='"+course.section+"' data-user-id='"+course.user_id+"' data-semester-id = '"+course.semester_id+"'"+
                        "data-modality='"+course.modality+"' "+
                        ">"+course.name+"</option>";
                        $("#select-course").append(option);
                    });
                    $("#select-course").trigger('change');
                    $("#select-course").selectpicker('refresh');
                }
            )
        }

        function fetchCourseInfo(course_select){

            course_option = $(course_select).find(':selected');

            $('#edit_name').val($(course_option).html())
            $('#edit_code').val($(course_option).data('code'))
            $('#edit_number').val($(course_option).data('number'))
            $('#edit_section').val($(course_option).data('section'))
            $('#edit_program').val($(course_option).data('program-id'))
            $("#edit_program").selectpicker('refresh');
            $('#edit_professor_id').val($(course_option).data('user-id'))
            $("#edit_professor_id").selectpicker('refresh');
            
            $('#edit_semester_id').val($(course_option).data('semester-id'))
            $("#edit_semester_id").selectpicker('refresh');

            $('#edit_modality').val($(course_option).data('modality'))
            $("#edit_modality").selectpicker('refresh');
            $(course_select).selectpicker('refresh');

        }


    </script>
@stop

@section('javascript')

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


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



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

@stop