@extends('layouts.app')

@section('content')
@php
    $url = Request::getRequestUri();
@endphp
    <h3 class="mb-4">Search Students</h3>
    <div class="card">
        <div class="card-header">
            <form action="" method="get">
                <div class="row mb-3">
                    <div class="col-8">
                        <input type="text" name="search" class="form-control form-control-sm" placeholder="Search by ID or phone number"
                            value="{{ request('search') }}">
                    </div>
                    <div class="col-3">
                        <button type="button" class="btn btn-primary">Search</button>
                    </div>
                </div>
            </form>
            <form action="" method="get">
                <div class="row">
                    <div class="col-md-3 col-6">
                        <label class="form-label">Status</label>
                        <select name="status" class="form-select" required>
                            <option value="pending" {{ request('status') == 'pending' ? 'selected' : '' }}>Pending</option>
                            <option value="active" {{ request('status') == 'active' ? 'selected' : '' }}>Active</option>
                        </select>
                    </div>
                    <div class="col-md-3 col-6">
                        <label class="form-label">From</label>
                        <input type="date" name="from" value="{{ request('from') }}" class="form-control form-control-sm" required>
                    </div>
                    <div class="col-md-3 col-6">
                        <label class="form-label">To</label>
                        <input type="date" name="to" value="{{ request('to') }}" class="form-control form-control-sm" required>
                    </div>
                    <div class="col-md-3 col-6 mt-4">
                        <button type="submit" class="btn btn-dark">Filter</button>
                    </div>
                </div>
            </form>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-stripped">
                    <thead class="table-light">
                        <tr>
                            <th>Image</th>
                            <th>Details</th>
                            <th>Status</th>
                            <th>Balance</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($users as $user)
                            <td>
                                <img src="{{ img($user->avatar) }}" width="60" class="rounded-circle">
                            </td>
                            <td>
                                <span class="d-block mb-1">ID: {{ $user->id }}</span>
                                <span class="d-block mb-1">Name: {{ $user->name }}</span>
                                <span class="d-block mb-1">Email: {{ $user->email }}</span>
                                <span class="d-block mb-1">Phone: {{ $user->phone }} </span>
                                <span class="d-block mb-1">Refer By: {{ $user->referer?->name }}({{ $user->referer?->id }}) </span>
                                <a href="https://wa.me/{{ $user->phone }}?text={{ wp_msg($user) }}" target="_blank"
                                    class="btn btn-sm btn-dark">WhatsApp</a>
                            </td>
                            <td>
                                @if ($user->verified_at)
                                    @if ($user->status)
                                        <span class="badge bg-success">Active</span>
                                    @else
                                        <span class="badge bg-danger">Blocked</span>
                                    @endif
                                @else
                                    <span class="badge bg-warning">Pending</span>
                                @endif
                            </td>
                            <td>
                                <span>৳{{ $user->balance }}</span> <br>
                                <button class="btn btn-sm btn-info"
                                    onclick="loadModal({{ $user->id }})">Wallet</button>
                            </td>
                            <td>
                                <div class="btn-group">
                                    @if ($user->verified_at)
                                        <a href="{{ route('users.edit', ['user' => $user->id, 'source' => $url]) }}"
                                            class="btn btn-sm btn-primary">
                                            <i class="ti-pencil"></i>
                                        </a>
                                        <a href="{{ route('users.show', ['user' => $user->id, 'source' => $url]) }}"
                                            class="btn btn-sm btn-warning">
                                            <i class="ti-eye"></i>
                                        </a>
                                    @else
                                        <a href="{{ route('users.edit', ['user' => $user->id, 'source' => $url]) }}"
                                            class="btn btn-sm btn-primary">
                                            Approved
                                        </a>
                                    @endif
                                    <button class="btn btn-sm btn-danger text-white" onclick="deleteData({{ $user->id }})">
                                        <i class="ti-trash"></i>
                                    </button>
                                </div>
                            </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>

                <div class="mt-3">
                    {{ $users->links('pagination::bootstrap-5') }}
                </div>
            </div>
        </div>
    </div>

    <!-- Modal -->
    <div class="modal fade" id="walletModal" tabindex="-1" aria-labelledby="walletModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <form action="{{ route('updateBalance') }}" method="post">
                @csrf
                <div class="modal-content">
                    <div class="modal-header">
                        <h1 class="modal-title fs-5" id="walletModalLabel">Update Balance</h1>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <input type="hidden" name="id" id="user_id">
                        <input name="amount" type="number" step="any" min="1"
                            class="mb-3 form-control form-control-sm" required placeholder="amount">
                        <select name="type" class="mb-3 form-select" required>
                            <option value="credit">Cash in</option>
                            <option value="debit">Cash out</option>
                        </select>
                        <input name="details" type="text" class="mb-3 form-control form-control-sm" placeholder="details"
                            required>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-primary">Save</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    {{-- individual delete --}}
    <form action="#" method="post" id="delete-form">
        @csrf
        @method('DELETE')
    </form>
@endsection

@push('js')
    <script>
        function deleteData(id) {
            let ask = confirm("Deleted file can't be restored!")
            if (ask) {
                $("#delete-form").attr('action', `{{ route('users.index') }}/${id}`);
                $("#delete-form").submit();
            }
        }
        const myModal = new bootstrap.Modal("#walletModal")

        function loadModal(id) {
            $("#user_id").val(id)
            myModal.show()
        }
    </script>
@endpush