@extends('layouts.app')

@section('title', 'Requests')

@section('content')
<div class="content-wrapper">
    <div class="content-header">
        <div class="container-fluid">
            <div class="row mb-2">
                <div class="col-sm-6">
                    <h1 class="m-0">Requests</h1>
                </div>
                <div class="col-sm-6">
                    <div class="float-sm-right">
                        @if(auth()->user()->isOwner())
                            <a href="{{ route('requests.create') }}" class="btn btn-primary">
                                <i class="fas fa-plus"></i> New Request
                            </a>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    </div>

    <section class="content">
        <div class="container-fluid">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">All Requests</h3>
                </div>
                <div class="card-body">
                    <form action="{{ route('requests.index') }}" method="GET" class="mb-4">
                        <div class="row">
                            <div class="col-md-4">
                                <div class="form-group">
                                    <div class="input-group">
                                        <input type="text" name="search" class="form-control" placeholder="Search by note, name, email or phone..." value="{{ request('search') }}">
                                        <div class="input-group-append">
                                            <button type="submit" class="btn btn-primary">
                                                <i class="fas fa-search"></i>
                                            </button>
                                            @if(request('search'))
                                                <a href="{{ route('requests.index', array_merge(request()->except('search'), ['search' => ''])) }}" class="btn btn-danger">
                                                    <i class="fas fa-times"></i>
                                                </a>
                                            @endif
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <select name="status" class="form-control" onchange="this.form.submit()">
                                        <option value="">All Statuses</option>
                                        <option value="pending" {{ request('status') == 'pending' ? 'selected' : '' }}>Pending</option>
                                        <option value="in_progress" {{ request('status') == 'in_progress' ? 'selected' : '' }}>In Progress</option>
                                        <option value="completed" {{ request('status') == 'completed' ? 'selected' : '' }}>Completed</option>
                                        <option value="cancelled" {{ request('status') == 'cancelled' ? 'selected' : '' }}>Cancelled</option>
                                    </select>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <select name="type" class="form-control" onchange="this.form.submit()">
                                        <option value="">All Types</option>
                                        @foreach($requestTypes as $type)
                                            <option value="{{ $type->id }}" {{ request('type') == $type->id ? 'selected' : '' }}>
                                                {{ $type->name }}
                                            </option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>
                        </div>
                    </form>

                    <div class="table-responsive">
                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Type</th>
                                    <th>Owner</th>
                                    <th>Phone</th>
                                    <th>Note</th>
                                    <th>Status</th>
                                    <th>Created</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse($requests as $request)
                                    <tr>
                                        <td>{{ $request->id }}</td>
                                        <td>{{ $request->requestType->name }}</td>
                                        <td>{{ $request->user->name }}</td>
                                        <td>{{ $request->user->phone }}</td>
                                        <td>{{ Str::limit($request->note, 50) }}</td>
                                        <td>
                                            @php
                                                $statusColors = [
                                                    'pending' => ['bg' => 'bg-warning', 'text' => 'text-dark'],
                                                    'in_progress' => ['bg' => 'bg-info', 'text' => 'text-white'],
                                                    'completed' => ['bg' => 'bg-success', 'text' => 'text-white'],
                                                    'cancelled' => ['bg' => 'bg-danger', 'text' => 'text-white']
                                                ];
                                                $status = $request->status;
                                                $colors = $statusColors[$status];
                                            @endphp
                                            <span class="badge {{ $colors['bg'] }} {{ $colors['text'] }} p-2">
                                                {{ ucfirst($status) }}
                                            </span>
                                        </td>
                                        <td>{{ $request->created_at->format('M d, Y H:i') }}</td>
                                        <td>
                                            <a href="{{ route('requests.show', $request) }}" class="btn btn-sm btn-info">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            @if(auth()->user()->isAdmin() || auth()->user()->isOperation())
                                                <a href="{{ route('requests.edit', $request) }}" class="btn btn-sm btn-primary">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                            @endif
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="7" class="text-center">No requests found</td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="card-footer">
                    {{ $requests->appends(request()->query())->links() }}
                </div>
            </div>
        </div>
    </section>
</div>

<style>
.badge {
    font-size: 0.9em;
    font-weight: 500;
    padding: 0.5em 1em;
}
.badge.bg-warning {
    background-color: #ffc107 !important;
}
.badge.bg-info {
    background-color: #17a2b8 !important;
}
.badge.bg-success {
    background-color: #28a745 !important;
}
.badge.bg-danger {
    background-color: #dc3545 !important;
}
</style>
@endsection 