@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' => 'ProgramsController@addToOtherProgramTYP','enctype'=>"multipart/form-data"]) }}


                    <!-- Program -->
                    <div class="form-group">
                        {{ Form::label('program', 'Select Program to pair other Program Courses\'') }}
                        <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" id = "other_program_div">
                        {{ Form::label('program', 'Choose other program') }}
                        <select id="other_program_0" name="other_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>

                    <hr>
                    <div class ='form-group'>
                        <button id='add-other-program' class='btn btn-md btn-secondary'
                        onclick='addOtherProgram("#add-other-program", "#other_program_div")'>
                        <span class='glyphicon glyphicon-plus'>
    
                        </span>
                        Add another Program
                    </button>
                    </div>


                    


              


                    <br>

                    {{ Form::submit('Pair Other Program Courses', ['class' => 'btn btn-primary btn-block', 'name' => 'pair_other_courses']) }}
                    {{ Form::close() }}

                    <br>

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

        <div class="col-md-6">
            <div class="panel panel-default panel-button">
                <div class="panel-heading">
                    Edit pairing
                </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' => 'ProgramsController@updatePairingInfo', 'enctype'=>"multipart/form-data"]) }}


                    <div class = "form-group">
                        {{Form::label('select-program-edit', "Edit Program Courses' Pairing")}}
                        <select id = "select-program-edit" name = "program" class ='form-control selectpicker'
                        onchange = "fetchProgramPairing('#select-program-edit')">
                        @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" id = "other_program_div_edit">
                        {{ Form::label('program', 'Choose other program') }}
                        <select id="program-edit" name="other_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>
                    <hr>

                    <div class ='form-group'>
                        <button id='add-other-program-edit' class='btn btn-md btn-secondary'
                        onclick='addOtherProgram("#add-other-program-edit", "#other_program_div_edit")'>
                        <span class='glyphicon glyphicon-plus'>
    
                        </span>
                        Add another Program
                    </button>
                    </div>



                    <br>

                    {{ Form::submit("Edit Program Courses' Pairing", ['class' => 'btn btn-primary btn-block', 'name' => 'edit_pair_other_courses']) }}
                    {{ 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;
        });

        
        options = $('#other_program_0').html();

        function addOtherProgram(button, div, removable = ''){

            master_div = $("<div>",{

                'class':removable
                
            })
            div_select = $("<div>", {
                'class':'col-md-11'
            })
            select = $("<select>", {
                'name': "other_program[]",
                'class':'form-control selectpicker ' 
            }).html(options);

            select.appendTo(div_select)

            div_button = $("<div>", {
                'class':'col-md-1'
            })

            rem_button = $('<button>',{
                'type':'button',
                'class':'btn btn-primary',
                'onclick':'$(this).parent().parent().remove();'
            }).html("<span class='glyphicon glyphicon-remove'></span>")

            div_button.append(rem_button);

            master_div.append(div_select);
            master_div.append(div_button);
            master_div.prepend("<br>")
            master_div.append("<br>");



          

            master_div.appendTo($(div));
            $('.selectpicker').selectpicker('refresh');
            

        }
        

        function fetchProgramPairing(select){
            $.post(
                "{{URL::action('ProgramsController@fetchPairingInfo')}}", {
                    program_id: $(select).val()
                },
                function(paired_programs){

                    $('.removable').remove();

                    $.each(paired_programs, function(index, program_id){

                       selectPick=   $('#other_program_div_edit').find('select.selectpicker').eq(index).val(program_id);
                       selectPick.val(program_id);

                        if(paired_programs[index+1]){
                            addOtherProgram("#add-other-program-edit", "#other_program_div_edit", 'removable')
                        }

                        $('.selectpicker').selectpicker('refresh');
                    })
                }
            )
        }

        
    </script>

    
@stop

@section('javascript')

    // --------------------------------------------------------------------------
    // Page load
    // --------------------------------------------------------------------------
$("#add-other-program").on("click", function(e){
    e.preventDefault();
});

$("#add-other-program-edit").on('click', function(e){
    e.preventDefault();
})


fetchProgramPairing('#select-program-edit')

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



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

@stop
