<!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>
                    User
                    <small>Manage User</small>
                </h1>
                <ol class="breadcrumb">
                    <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
                    <li><a href="#">User</a></li>
                    <li class="active">User</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">User 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 User </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('user.list') }}">
    <div class="row">
        <div class="col-md-3 mb-2">
            <input type="text" name="name" value="{{ request('name') }}" class="form-control" placeholder="Search Name">
        </div>
        <div class="col-md-3 mb-2">
            <input type="text" name="email" value="{{ request('email') }}" class="form-control" placeholder="Search Email">
        </div>
        <div class="col-md-2 mb-2">
            <input type="date" name="from_date" value="{{ request('from_date') }}" class="form-control">
        </div>
        <div class="col-md-2 mb-2">
            <input type="date" name="to_date" value="{{ request('to_date') }}" class="form-control">
        </div>
        <div class="col-md-1 mb-2">
            <button type="submit" class="btn btn-primary">Search</button>
        </div>
        <div class="col-md-1 mb-2">
            <a href="{{ route('user.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>Name & Details</th>
                                            <th>Ref User Name</th>
                                            <th>Joining Date</th>

                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach ($records as $record)
                                            <tr>
                                                <td>{{ $loop->iteration }}</td>
                                                <td>
                                                    {{ $record->first_name ?? '' }}  {{ $record->last_name ?? '' }}<br>
                                                     {{ $record->email ?? '' }} <br>
                                                     {{ $record->phone_number ?? '' }}<br>
                                                      <b>REF No :</b> {{ $record->ref_code ?? '' }}
                                                </td>
                                                <td>{{$record->ref_user->first_name ??''}} {{$record->ref_user->last_name ??""}}</td>
                                                 <td>{{$record->joining_date ??""}}</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>
