@extends('admin.layout')

@section('title', 'Users')

@section('content')
    <div class="admin-heading">
        <div>
            <span>Access</span>
            <h1>Users</h1>
        </div>
    </div>

    <form class="admin-card admin-form-grid" method="post" action="{{ route('admin.users.store') }}">
        @csrf
        <input name="name" placeholder="Name" required>
        <input type="email" name="email" placeholder="Email" required>
        <select name="role" required>
            <option value="user">Website user</option>
            <option value="admin">Admin</option>
        </select>
        <input type="password" name="password" placeholder="Password" required>
        <button class="btn btn-primary" type="submit">Create user</button>
    </form>

    <section class="admin-card">
        <div class="admin-table">
            <table>
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Email</th>
                        <th>Role</th>
                        <th>Status</th>
                        <th>Update</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($users as $user)
                        <tr>
                            <td colspan="5">
                                <form class="admin-user-row" method="post" action="{{ route('admin.users.update', $user) }}">
                                    @csrf
                                    @method('put')
                                    <input name="name" value="{{ $user->name }}" required>
                                    <input type="email" name="email" value="{{ $user->email }}" required>
                                    <select name="role">
                                        <option value="user" @selected($user->role === 'user')>Website user</option>
                                        <option value="admin" @selected($user->role === 'admin')>Admin</option>
                                    </select>
                                    <label class="check-row">
                                        <input type="checkbox" name="is_active" value="1" @checked($user->is_active)>
                                        <span>Active</span>
                                    </label>
                                    <input type="password" name="password" placeholder="New password optional">
                                    <button class="btn btn-ghost" type="submit">Save</button>
                                </form>
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
        {{ $users->links() }}
    </section>
@endsection
