@extends('backend.layout.app')

@section('content')
<div class="container">
    <div class="container-header">
        <h1>{{ $title }}</h1>
    </div>

    <div class="card mb-25">
        <div class="card-body">
            <form action="{{ route('backend.custom-prices.store') }}" method="post">
                @csrf
                <div class="row">
                    <div class="form-group width-50">
                        <label class="form-label">User<span class="required">*</span></label>
                        <select class="form-control" name="user_id" required>
                            <option value="">Select user</option>
                            @foreach(\App\Models\User::orderBy('email')->get() as $u)
                            <option value="{{ $u->id }}">{{ $u->email }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="form-group width-50">
                        <label class="form-label">Service ID<span class="required">*</span></label>
                        <input type="text" class="form-control" name="service_id" placeholder="e.g. wa" required>
                    </div>
                </div>
                <div class="row">
                    <div class="form-group width-50">
                        <label class="form-label">Server ID<span class="required">*</span></label>
                        <input type="text" class="form-control" name="server_id" placeholder="e.g. 1" required>
                    </div>
                    <div class="form-group width-50">
                        <label class="form-label">Type<span class="required">*</span></label>
                        <select class="form-control" name="type">
                            <option value="flat">Flat price</option>
                            <option value="percent">Discount %</option>
                        </select>
                    </div>
                </div>
                <div class="row">
                    <div class="form-group width-50">
                        <label class="form-label">Value (flat amount or %)<span class="required">*</span></label>
                        <input type="number" step="0.01" class="form-control" name="discount" placeholder="0" required>
                    </div>
                </div>
                <button type="submit" class="btn btn-primary">Add custom price</button>
            </form>
        </div>
    </div>

    <div class="card table-card">
        <div class="table-header">
            <form method="GET" action="{{ route('backend.custom-prices.index') }}" class="table-search-form">
                <div class="table-search">
                    <input type="text" name="search" class="search-input" placeholder="Search by user, service ID, server..." value="{{ $search ?? '' }}">
                    <i class="las la-search"></i>
                </div>
                <button type="submit" class="btn btn-secondary search-btn">Search</button>
                @if(!empty($search))
                    <a href="{{ route('backend.custom-prices.index') }}" class="btn btn-outline">Clear</a>
                @endif
            </form>
        </div>
        <div class="table-responsive">
            <table class="table">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>User</th>
                        <th>Service ID</th>
                        <th>Server ID</th>
                        <th>Type</th>
                        <th>Value</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($customPrices as $cp)
                    <tr>
                        <td>{{ $customPrices->firstItem() + $loop->index }}</td>
                        <td>{{ $cp->user->email ?? $cp->user_id }}</td>
                        <td>{{ $cp->service_id }}</td>
                        <td>{{ $cp->server_id }}</td>
                        <td>{{ $cp->type }}</td>
                        <td>{{ $cp->discount }}</td>
                        <td>
                            <form action="{{ route('backend.custom-prices.destroy', $cp->id) }}" method="post" class="d-inline" onsubmit="return confirm('Remove?');">
                                @csrf
                                @method('DELETE')
                                <button type="submit" class="table-btn"><i class="las la-trash"></i></button>
                            </form>
                        </td>
                    </tr>
                    @empty
                    <tr><td colspan="7" class="text-center">No custom prices yet.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        {{ $customPrices->links('vendor.pagination.default') }}
    </div>
</div>
@endsection