@extends('admin.layouts.main-layout')
@section('title', 'Manage Subjects')

@section('content')
    <!-- Content wrapper -->
    <div class="content-wrapper">
        <!-- Content -->
        <div class="container-xxl flex-grow-1 container-p-y">
            @if (session('success'))
                <div class="alert alert-success alert-dismissible" role="alert">
                    {{ session('success') }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            @elseif (session('error'))
                <div class="alert alert-danger alert-dismissible" role="alert">
                    {{ session('error') }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
            @endif
            <!-- Hoverable Table rows -->
            <div class="card">
                <h5 class="card-header">Manage Subjects
                    <span class="float-end">
                        <a href="{{ route('add.subject') }}"
                            class="btn rounded-pill btn-icon btn-outline-primary float-end ms-2"
                            style="font-size: 1.7em;">+</a>
                        {{-- <a href="{{ route('manage.courses') }}"
                            class="btn btn-icon rounded-pill btn-outline-secondary float-end">
                            <i class='bx bx-left-arrow-alt' style="font-size: 1.7rem;"></i>
                        </a> --}}
                    </span>
                </h5>
                <div class="table-responsive text-nowrap px-4 pb-4">
                    <table id="subject-table" class="table table-hover">
                        <thead>
                            <tr>
                                <th>S. No.</th>
                                {{-- <th>Course Code(s) (Course Name(s))</th> --}}
                                <th>Course Code(s)</th>
                                <th>Subject Code</th>
                                <th>Subject Name</th>
                                <th>Subject Position</th>
                                <th>Status</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody class="table-border-bottom-0">
                            @php $sno = 1; @endphp

                            @foreach ($subjects as $subject)
                                <tr>
                                    <td>{{ $sno }}.</td>

                                    <td>
                                        {{-- @foreach ($subject->courses as $course)
                                            {{ $course->course_code ." (". $course->course_name.")" }}, <br>
                                        @endforeach --}}
                                        {!! collect($subject->courses)->pluck('course_code')->implode(', <br>') !!}
                                    </td>

                                    <td><strong>{{ $subject->subject_code }}</strong></td>

                                    <td>{{ $subject->subject_name }}</td>

                                    <td>{{ $subject->position_order }}</td>

                                    <td>
                                        <div class="form-check form-switch mb-2">
                                            <input class="form-check-input status_button"
                                                onclick="change_status('{{ $model }}', {{ $sno }}, '{{ $subject->encrypted_id }}');"
                                                type="checkbox" id="status{{ $sno }}"
                                                {{ $subject->status == 'active' ? 'checked' : '' }} />
                                        </div>
                                    </td>

                                    <td>
                                        <div class="dropdown">
                                            <button type="button" class="btn p-0 dropdown-toggle hide-arrow"
                                                data-bs-toggle="dropdown">
                                                <i class="bx bx-dots-vertical-rounded"></i>
                                            </button>

                                            <div class="dropdown-menu">
                                                <a class="dropdown-item"
                                                    href="{{ route('edit.subject', $subject->encrypted_id) }}">
                                                    <i class="bx bx-edit-alt me-1"></i> Edit
                                                </a>

                                                <button class="dropdown-item"
                                                    onclick="delete_item('{{ $model }}', '{{ $subject->encrypted_id }}');">
                                                    <i class="bx bx-trash me-1"></i> Delete
                                                </button>
                                            </div>
                                        </div>
                                    </td>
                                </tr>

                                @php $sno++; @endphp
                            @endforeach
                        </tbody>

                    </table>
                </div>
            </div>
            <!--/ Hoverable Table rows -->
        </div>
    </div>
@endsection

@section('admin-custom-js')
    <script>
        $("#subject-table").DataTable();

        {{--
    // function getOnBanner(sno, encrypted_id) {
    //     let token = '{{ csrf_token() }}';
    //     $("#blog_on_banner" + sno).attr('disabled', true);
    //     $.ajax({
    //         url: "{{ route('blog.on.banner') }}",
    //         data: {'_token': token, 'encrypted_id': encrypted_id},
    //         type: "POST",
    //         dataType: 'json',
    //         success: function(result) {
    //             if(result.status == 'done') {
    //                 Swal.fire({
    //                     icon: "success",
    //                     title: result.message,
    //                     showConfirmButton: false,
    //                     timer: 1500
    //                 });
    //                 setTimeout(function() {
    //                     $("#blog_on_banner" + sno).attr('disabled', false);
    //                     location.reload();
    //                 }, 1500);
    //             }
    //             else {
    //                 Swal.fire({
    //                     icon: "error",
    //                     title: result.message,
    //                     showConfirmButton: false,
    //                     timer: 1500
    //                 });
    //             }
    //         }
    //     });
    // }
    --}}
    </script>
@endsection
