<div class="card">
    @if(count($courses) > 0)
    <table class="table table-basic">
        <thead>
            <tr>
                <th style="width: 5%;">#</th>
                <th>Course Details</th>
                <th>Institute Details</th>
                <th>Intakes</th>
                <th>Tution Fees</th>
                <th>Requirements</th>
                <th class="text-center">Actions</th>
            </tr>
        </thead>
        <tbody>
            @php
            $serial = $courses->perPage() * ($courses->currentPage() - 1);
            @endphp
            @foreach($courses as $key => $row)
            <tr>
                <td>{{ ++$serial }}</td>
                <td>
                    {{ $row->name }}
                    <br>
                    <small>{{ $row->department }}, {{ $row->level->name }}</small>
                    <br>
                    <span class="label label-default">{{ $row->length }} <small>months</small></span>
                </td>
                <td>
                    {{ $row->institute->name }}
                    <br>
                    <small>
                        @foreach($row->campuses as $single)
                        {{str_replace(' Campus', '', $single->campus->name)}}
                        @if(!$loop->last) , @endif
                        @endforeach
                    </small>
                    <br>
                    <small>{{ $row->destination->name }}</small>
                </td>
                <td> 
                    <small>
                        @foreach($row->intakes as $intake)
                        {{date("M", mktime(0, 0, 0, $intake->intake, 1))}}
                        @if(!$loop->last) , @endif
                        @endforeach
                    </small>
                </td>
                <td>
                    {{$row->destination->currency}}{{ $row->tution_fee }}
                </td>
                <td>
                    @if($row->ielts)
                        <span class="label label-default"><small>IELTS : {{$row->ielts}}</small></span>
                    @endif
                    @if($row->toefl)
                        <span class="label label-default"><small>TOEFL : {{$row->toefl}}</small></span>
                    @endif
                    @if($row->pte)
                        <span class="label label-default"><small>PTE : {{$row->pte}}</small></span>
                    @endif
                    @if($row->duolingo)
                        <span class="label label-default"><small>Duolingo : {{$row->duolingo}}</small></span>
                    @endif
                </td>
                <td class="text-center">
                    <div class="list-icons">
                        <div class="dropdown">
                            <a href="#" class="list-icons-item" data-toggle="dropdown">
                                <i class="icon-menu9"></i>
                            </a>

                            <div class="dropdown-menu dropdown-menu-right">
                                <a href="{{ url('admin/courses/view') }}/{{ $row->id }}" class="dropdown-item">
                                    <i class="icon-eye"></i> View
                                </a>
                                <a href="{{ url('admin/courses/edit') }}/{{ $row->id }}" class="dropdown-item">
                                    <i class="icon-pencil7"></i> Edit
                                </a>
                                {!! Form::model($courses, ['method' => 'delete', 'url' => ['admin/courses/delete', $row->id], 'class' =>'dropdown-item form-delete']) !!}
                                {!! Form::hidden('id', $row->id) !!}
                                    <i class="icon-trash"></i> Delete
                                {!! Form::close() !!}
                            </div>
                        </div>
                    </div>
                </td>
            </tr>
            @endforeach
        </tbody>
    </table>
    <div class="col-md-12 mt-1 mb-2">
        {{ $courses->appends(request()->query())->links('paginator')}}
    </div> 
    @else
    <p class="text-center text-danger mt-1">No Course Found. Please try again.</p>
    @endif
</div>

<script type="text/javascript">
    $(document).ready(function(){
        $('[data-toggle="tooltip"]').tooltip();
        var elem = document.querySelector('.switchery');
        var init = new Switchery(elem);
    });

    $('.form-delete').click(function(e){
        e.preventDefault();
        $form = $(this);

        swal({
            title: 'Are you sure?',
            text: "You won't be able to revert this!",
            type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Yes, delete it!'
        }).then((result) => {
            if (result.value) {
                $form.submit();
            }
        });
    });

    $("input[type='checkbox']").on('click', function(){
        var id = $(this).data('id');
        var urll="{{ url('admin/courses/status') }}/"+id;
        var msg_success = 'Status Updated Successfully.';
        var msg_error = 'Something went wrong!';
        $.ajax({
            url:urll,
            success: function(data){
               notify('success',msg_success);
            },
            error: function (data) {
                notify('error',msg_error);
            }
        });
    });

    $('.page-link').on('click',function() {
        $('#loadData').load($(this).attr('href'));
    });
</script>