@extends('admin.layouts.app')

@section('title', $title)

@section('content')
    <div class="col-md-12">
        <!-- Page Heading & Search Area -->
        <div class="row mb-3">
            <div class="col-md-9">
                <h4 class="c-grey-900">{{ $title }}</h4>
            </div>
            <div class="col-md-3">
                <form method="GET" action="{{ route('ch-admin.order.index') }}">
                    <div class="input-group">
                        <input type="text" name="search" class="form-control" placeholder="Search…" aria-label="Search" value="{{ request('search') }}">
                        <button class="btn btn-info" type="submit"><i class="ti ti-search"></i></button>
                    </div>
                    <!-- Include current filter values as hidden inputs -->
                    <input type="hidden" name="user_id" value="{{ request('user_id') }}">
                    <input type="hidden" name="status" value="{{ request('status') }}">
                    <input type="hidden" name="discount_code" value="{{ request('discount_code') }}">
                    <input type="hidden" name="payment_method" value="{{ request('payment_method') }}">
                </form>
            </div>
        </div>

        <!-- Main Content -->
        <div class="row">
            <div class="bgc-white bd bdrs-3 p-20 mB-20">
                <!-- Status Filters Section -->
                <div class="row mb-3">
                    <div class="d-flex flex-row gap-3">
                        <a href="{{ route('ch-admin.order.index') }}">All ({{ $allOrders->count() }})</a>
                        @foreach($orderStatuses as $status => $count)
                            @php
                                $statusDetails = getStatusDetails($status);
                            @endphp
                            <a href="{{ route('ch-admin.order.index', ['status' => $status, 'search' => request('search'), 'user_id' => request('user_id'), 'discount_code' => request('discount_code'), 'payment_method' => request('payment_method')]) }}">
                                {{ $statusDetails['label'] }} ({{ $count }})
                            </a>
                        @endforeach
                    </div>
                </div>

                <!-- Other Filters Section -->
                <div class="row mb-3">
                    <form method="GET" action="{{ route('ch-admin.order.index') }}" class="d-flex align-items-center gap-3 w-100">
                        <!-- Filter by Users -->
                        <div class="flex-shrink-0">
                            <select name="user_id" class="form-control">
                                <option value="">All Users</option>
                                @foreach($allOrders->unique('user_id') as $order)
                                    <option value="{{ $order->user->id }}">{{ $order->user->name }}</option>
                                @endforeach
                            </select>
                        </div>

                        <!-- Filter by Coupons -->
                        <div class="flex-shrink-0">
                            <select name="discount_code" class="form-control">
                                <option value="">All Coupons</option>
                                @foreach($allOrders->unique('discount_code') as $order)
                                    <option value="{{ $order->discount_code }}">{{ $order->discount_code }}</option>
                                @endforeach
                            </select>
                        </div>

                        <!-- Filter by Payment Methods -->
                        <div class="flex-shrink-0">
                            <select name="payment_method" class="form-control">
                                <option value="">All Payment Methods</option>
                                @foreach($allOrders->unique('payment_method') as $order)
                                    <option value="{{ $order->payment_method }}" {{ request('payment_method') == $order->payment_method ? 'selected' : '' }}>
                                        {{ $order->payment_method }}
                                    </option>
                                @endforeach
                            </select>
                        </div>

                        <!-- Action Buttons -->
                        <div class="flex-shrink-0">
                            <button type="submit" class="btn btn-info">Apply Filters</button>
                            @if(request()->hasAny(['search', 'status', 'user_id', 'discount_code', 'payment_method']))
                                <a href="{{ route('ch-admin.order.index') }}" class="btn btn-secondary">Reset</a>
                            @endif
                            <button class="btn btn-danger" id="delete-bulk-btn" type="button"><i class="ti-trash"></i>Delete</button>
                        </div>

                        <!-- Hidden Inputs to Retain Filters -->
                        <input type="hidden" name="search" value="{{ request('search') }}">
                        <input type="hidden" name="status" value="{{ request('status') }}">
                        <input type="hidden" name="user_id" value="{{ request('user_id') }}">
                        <input type="hidden" name="discount_code" value="{{ request('discount_code') }}">
                        <input type="hidden" name="payment_method" value="{{ request('payment_method') }}">
                    </form>
                </div>

                <!-- Orders Table -->
                <div class="row">
                    <table class="table table-striped">
                        <thead>
                        <tr>
                            <th><input type="checkbox" id="select-all" class="form-check-input"></th>
                            <th>ID</th>
                            <th>Order</th>
                            <th>Status</th>
                            <th>User</th>
                            <th>Payment Method</th>
                            <th>Coupon</th>
                            <th>Date</th>
                            <th>Total</th>
                            <th>Actions</th>
                        </tr>
                        </thead>
                        <tbody>
                        @forelse($orders as $order)
                            <tr>
                                <td><input type="checkbox" class="form-check-input select-single" value="{{ $order->id }}"></td>
                                <th>{{ $order->id }}</th>
                                <td><a href="{{ route('ch-admin.order.show', [$order->id]) }}">{{ $order->order_reference }}</a></td>
                                <td>
                                    @php
                                        $statusDetails = getStatusDetails((int)$order->status);
                                    @endphp
                                    <span class="order-status badge {{ $statusDetails['class'] }}">{{ $statusDetails['label'] }}</span>
                                </td>
                                <td>{{ $order->user->name }}</td>
                                <td>{{ $order->payment_method }}</td>
                                <td>{{ $order->discount_code ?? 'N/A' }}</td>
                                <td>{{ $order->created_at->diffForHumans() ?? $order->created_at }}</td>
                                <td>{!! ch_format_price($order->total, $order->currency) !!}</td>
                                <td class="d-flex flex-row">
                                    <a href="{{ route('ch-admin.order.show', [$order->id]) }}" class="btn btn-sm btn-primary me-1"><i class="fa fa-pencil"></i></a>
                                    <form method="post" action="{{ route('ch-admin.order.destroy', $order->id) }}">
                                        @csrf
                                        @method('DELETE')
                                        <button class="btn btn-danger btn-sm" title="Delete" onclick="return confirm('Are you sure you want to delete this order?')">
                                            <i class="fa fa-trash-o"></i>
                                        </button>
                                    </form>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td class="text-center" colspan="10">No Orders found.</td>
                            </tr>
                        @endforelse
                        </tbody>
                    </table>

                    @if($orders->hasPages())
                        <div class="mt-4">
                            {{ $orders->links() }}
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
@endsection

@push('scripts')
    <script src="{{url('assets/custom/js/axios.min.js')}}"></script>
    <script src="{{url('assets/backend/js/bulkDelete.js')}}"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            initBulkActions('{{ route("ch-admin.order.bulk.destroy") }}');
        });
    </script>
@endpush
