@extends('admin.layout.index')
@push('css-head')
@endpush
@section('content')
<!-- Page header -->
<div class="page-header page-header-light">
    <div class="breadcrumb-line breadcrumb-line-light header-elements-md-inline">
        <div class="d-flex">
            <div class="breadcrumb">
                <a href="{{url('/admin')}}" class="breadcrumb-item"><i class="icon-home2 mr-2"></i> Dashboard</a>
                <a href="{{url('/admin/institutes')}}" class="breadcrumb-item"> Institutes</a>
                <span class="breadcrumb-item active">{{$course->institute->name}} ({{$serial}}/{{$total_courses}})</span>
            </div>
        </div>
    </div>
</div>
<!-- /page header -->

<!-- Content area -->
<div class="content">
    <div class="card">
        <div class="card-body">
            @if (count($errors) > 0)
                <div class="alert alert-danger alert-no-border alert-txt-colored alert-close alert-dismissible fade show" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <strong>Whoops!</strong> There were some problems with your input.
                    <ul>
                      @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                      @endforeach
                    </ul>
                </div>
            @endif

            {!!Form::model($course,['method'=>'PETCH','url'=>['admin/institutes/update_entry_req',$course->id]])!!}
                <div class="row">
                    <div class="col-md-8">
                        <div class="form-group">
                            <label>Course Name:</label>
                            <input type="text" class="form-control" name="courseName" value="{{$course->name}}" id="courseName" disabled>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="form-group">
                            <label>Course Length (month):</label>
                            <input type="number" class="form-control" min="0" name="courseLength" value="{{$course->length}}" id="courseLength" disabled>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-md-12">
                        <div class="form-group">
                            <label>Keywords</label>
                            <input type="text" class="form-control tokenfield" name="tags" value="@foreach($selected_tags as $tag){{$tag}}@if(!$loop->last) , @endif @endforeach">
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="col-md-12">
                        <div class="form-group">
                            <label>Entry Requirements</label>
                            <textarea class="form-control summernote" name="entry_requirement">{!! $course->entry_requirement !!}</textarea>
                        </div>
                    </div>
                </div>
                <div class="text-right">
                    <a href="{{ url()->current() }}" class="btn btn-secondary pull-left">
                        Refresh
                         <i class="icon-reload-alt ml-2"></i>
                    </a>
                    <button type="submit" class="btn btn-primary">Submit <i class="icon-paperplane ml-2"></i></button>
                </div>
            {!! Form::close() !!}
        </div>
    </div>
</div>
<!-- /content area -->
@endsection

@push('scripts')

<script type="text/javascript" src="{{ asset('assets//global/js/plugins/forms/selects/select2.min.js')}}"></script>
<script type="text/javascript" src="{{ asset('assets/global/js/plugins/forms/tags/tagsinput.min.js')}}"></script>
<script type="text/javascript" src="{{ asset('assets/global/js/plugins/forms/tags/tokenfield.min.js')}}"></script>
<script type="text/javascript" src="{{ asset('assets/global/js/plugins/editors/summernote/summernote.min.js')}}"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $('.select2').select2();
        $('.tokenfield').tokenfield();
        $('.summernote').summernote();
    });

    $('.destination').on('change', function(){
        var destination = $('.destination option:selected').val();
        var select_tag = $('.institute');

        $.get("{{ route('admin.ajax-get', ['name'=>'sams_institutes']) }}", {
            destination : destination, type : 'all' 
        }).done(function(res){ 
            select_tag.empty().append('<option disabled="" selected="" value="">Choose One..</option>');
                
            res.forEach(function(row){   
                select_tag.append(
                    $('<option></option>').val(row.institute_id+'::'+row.institute_name).html(row.institute_name)
                ); 
            }); 
        });

        $('.institute').empty().append('<option disabled="" selected="" value="">Choose One..</option>');
        $('.level').empty().append('<option disabled="" selected="" value="">Choose One..</option>');
        $('.campus').empty().append('<option disabled="">Choose One..</option>');
        $('.intake').empty().append('<option disabled="">Choose One..</option>');
    });

    $('.institute').on('change', function(){
        var institute = $('.institute option:selected').val();
        var select_tag = $('.level'); 

        $.get("{{ route('admin.ajax-get', ['name'=>'sams_levels']) }}", {
            institute : institute, group : 0
        }).done(function(res){ 
            select_tag.empty().append('<option disabled="" selected="" value="">Choose One..</option>');
                
            res.forEach(function(row){   
                select_tag.append(
                    $('<option></option>').val(row.id+'::'+row.name).html(row.name)
                ); 
            }); 
        });

        $.get("{{ route('admin.ajax-get', ['name'=>'sams_campuses']) }}", {
            institute : institute
        }).done(function(campuses){ 
            var select_tag = $('.campus');
            select_tag.empty().append('<option disabled="">Choose One..</option>');
            
            if(campuses.length > 0){
                campuses.forEach(function(row){   
                    select_tag.append(
                        $('<option></option>').val(row.id+'::'+row.name).html(row.name)
                    ); 
                }); 

                $('#instituteDiv').removeClass('col-md-8');
                $('#instituteDiv').addClass('col-md-4');
                $('#campusDiv').removeClass('d-none');
            }else{
                $('#instituteDiv').addClass('col-md-8');
                $('#instituteDiv').removeClass('col-md-4');
                $('#campusDiv').addClass('d-none');
            }
        });

        $.get("{{ route('admin.ajax-get', ['name'=>'sams_intakes']) }}", {
            institute : institute
        }).done(function(intakes){ 
            var select_tag = $('.intake');
            select_tag.empty().append('<option disabled="">Choose One..</option>');
            if(intakes.length > 0){   
                intakes.forEach(function(row){   
                    select_tag.append(
                        $('<option></option>').val(row.intake).html(getMonthName(row.intake))
                    ); 
                }); 
            }else{
                select_tag.empty().append('<option disabled="">Choose One..</option>');
            }
        });
    });

    $('.level').on('change', function(){
        var institute = $('.institute option:selected').val();
        var level = $('.level option:selected').val();
        var select_tag = $('.intake');

        $.get("{{ route('admin.ajax-get', ['name'=>'sams_intakes']) }}", {
            institute : institute, level : level
        }).done(function(intakes){ 
            select_tag.empty().append('<option disabled="">Choose One..</option>');
            if(intakes.length > 0){   
                intakes.forEach(function(row){   
                    select_tag.append(
                        $('<option></option>').val(row.intake).html(getMonthName(row.intake))
                    ); 
                }); 
            }else{
                select_tag.empty().append('<option disabled="">Choose One..</option>');
            }
        });
    });

    function getMonthName(monthNumber) {
        var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
        return months[monthNumber - 1];
    }
</script>
@endpush