<!DOCTYPE html>
<html>

@include('includes.head')


<body class="hold-transition skin-blue fixed sidebar-mini">
    <!-- Site wrapper -->
    <div class="wrapper">

        @include('includes.header')
        <!-- =============================================== -->

        <!-- Left side column. contains the sidebar -->
        @include('includes.side-nav')

        <!-- =============================================== -->

        <!-- Content Wrapper. Contains page content -->
        <div class="content-wrapper">
            <!-- Content Header (Page header) -->
            <section class="content-header">
                <h1>
                    Event
                    <small>Manage Event</small>
                </h1>
                <ol class="breadcrumb">
                    <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
                    <li><a href="#">Event</a></li>
                    <li class="active">Event</li>
                </ol>
            </section>

            <!-- Main content -->
            <section class="content">
                @include('includes.messages')
                <!-- Default box -->
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title">Event Management</h3>
                        <div class="box-tools pull-right">
                            <a href="{{ route('event.create') }}" class="btn btn-block btn-primary btn-flat"
                                data-toggle="tooltip" title="Create System User">
                                <i class="fa fa-plus"></i> Create Event </a>

                        </div>

                    </div>
                    <div class="row">
                        <div class="col-md-12">
                            <div class="box-header">
                                <div class="input-group mb-3">
                                    <div class="box-header mb-3">
                                        <form method="GET" action="{{ route('event.list') }}">
                                            <div class="row">
                                                <div class="col-md-6">
                                                <input type="text" name="search" value="{{ request('search') }}" class="form-control" placeholder="Search Event Name">
                                                </div>
                                                <div class="col-md-2">
                                                    <button type="submit" class="btn btn-primary">Search</button>
                                                </div>
                                                <div class="col-md-2">
                                                    <a href="{{ route('event.list') }}" class="btn btn-secondary">Reset</a>
                                                </div>
                                            </div>
                                        </form>
                                </div>
                            </div>
                            <div class="box-body">
                                <table class="table table-bordered table-hover">
                                    <thead>
                                        <tr>
                                            <th>Sl No</th>
                                            <th>Event Name</th>
                                            <th>Date & Time</th>
                                            <th>Location</th>
                                            <th>Image</th>

                                            <th>Online</th>
                                            <th>Show in Home</th>
                                            <th>Status</th>

                                            <th style="width: 200px">Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach ($records as $record)
                                            <tr>
                                                <td>{{ $loop->iteration }}</td>
                                                <td>{{ $record->name_en ?? '' }}</td>
                                                <td>
                                                    <span>Start Date : {{ $record->start_date }}</span><br>
                                                    <span>Start Time : {{ $record->start_time }}</span><br>
                                                    <span>End Time : {{ $record->end_time }}</span>
                                                </td>

                                                <td>{{ $record->location ?? '' }}</td>
                                                <td>

                                                    @if ($record->img)
                                                        <img src="{{ $record->img }}"
                                                            style="width: 100px;  border:2px solid #101010"
                                                            alt="">
                                                    @else
                                                        No Image
                                                    @endif
                                                </td>
                                                <!-- Toggle Switches -->
                                                <td>

                                                        <label class="switch">
                                                            <input type="checkbox" class="toggle-status"
                                                            data-id="{{ $record->id }}" data-field="is_online"
                                                            {{ $record->is_online ? 'checked' : '' }}>
                                                            <span class="slider round"></span>
                                                        </label>
                                                </td>
                                                <td>
                                                    {{-- <input type="checkbox" class="toggle-status"
                                                        data-id="{{ $record->id }}" data-field="is_show_in_home"
                                                        {{ $record->is_show_in_home ? 'checked' : '' }}> --}}
                                                    <!-- Rectangular switch -->
                                                    <label class="switch">
                                                        <input type="checkbox" class="toggle-status"
                                                        data-id="{{ $record->id }}" data-field="is_show_in_home"
                                                        {{ $record->is_show_in_home ? 'checked' : '' }}>
                                                        <span class="slider round"></span>
                                                    </label>

                                                </td>
                                                <td>


                                                        <label class="switch">
                                                            <input type="checkbox" class="toggle-status"
                                                            data-id="{{ $record->id }}" data-field="status"
                                                            {{ $record->status ? 'checked' : '' }}>
                                                            <span class="slider round"></span>
                                                        </label>
                                                </td>
                                                <td>
                                                    <!-- Add your action buttons or links here -->
                                                    {{-- Example: --}}
                                                    <a href="{{ route('event.edit', $record->id) }}"
                                                        class="btn btn-primary btn-sm">Edit</a>
                                                    <a href="#" class="btn btn-danger btn-sm"
                                                        onclick="confirmDelete('{{ route('event.delete', $record->id) }}')">Delete</a>

                                                    <script>
                                                        function confirmDelete(url) {
                                                            if (confirm('Are you sure you want to delete this?')) {
                                                                window.location.href = url;
                                                            }
                                                        }
                                                    </script>
                                                </td>
                                            </tr>
                                        @endforeach

                                    </tbody>
                                </table>

                            </div>
                            <!-- /.box-body -->

                        </div>

                    </div>

                </div>
                <!-- /.box -->

            </section>
            <!-- /.content -->
        </div>
        <!-- /.content-wrapper -->

        @include('includes.footer')

    </div>
    <!-- ./wrapper -->

    @include('includes.scripts')
    <script>
        document.addEventListener("DOMContentLoaded", function() {
            document.querySelectorAll(".toggle-status").forEach(function(toggle) {
                toggle.addEventListener("change", function() {
                    let eventId = this.getAttribute("data-id");
                    let field = this.getAttribute("data-field");
                    let value = this.checked ? 1 : 0;

                    fetch("{{ route('event.update_status') }}", {
                            method: "POST",
                            headers: {
                                "Content-Type": "application/json",
                                "X-CSRF-TOKEN": "{{ csrf_token() }}"
                            },
                            body: JSON.stringify({
                                id: eventId,
                                field: field,
                                value: value
                            })
                        })
                        .then(response => response.json())
                        .then(data => {
                            if (!data.success) {
                                alert(data.message);
                                this.checked = !this.checked; // Revert change on failure
                            }
                        })
                        .catch(error => {
                            alert("Error updating status!");
                            this.checked = !this.checked;
                        });
                });
            });
        });
    </script>
</body>

</html>
