@extends('layout.admin')

@section('content')
<div class="container py-3">

    {{-- Header --}}
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="fw-bold">
            <i class="fas fa-clock text-warning me-2"></i> পেন্ডিং ড্রাইভ অর্ডার
        </h2>
    </div>

    {{-- Success Message --}}
    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            <i class="fas fa-check-circle me-1"></i> {{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    @endif

    {{-- No Orders --}}
    @if($orders->isEmpty())
        <div class="alert alert-warning text-center">
            <i class="fas fa-exclamation-circle me-1"></i> কোনো পেন্ডিং অর্ডার পাওয়া যায়নি!
        </div>
    @else
        {{-- Order Cards --}}
        <div class="row row-cols-1 row-cols-md-2 g-4">
            @foreach($orders as $order)
                <div class="col">
                    <div class="card shadow-sm border-0 h-100 p-3" style="border-radius: 14px;">
                        <div class="d-flex align-items-start mb-2">
                            {{-- Logo --}}
                            <img src="{{ asset($order->offer->logo) }}" alt="Logo"
                                 style="height: 50px; width: 50px; object-fit: contain; border-radius: 8px; background-color: #f2f2f2; margin-right: 10px;">

                            {{-- Title & Validity --}}
                            <div>
                                <h5 class="fw-bold mb-1" style="font-size: 16px;">{{ $order->offer->title }}</h5>
                                <small class="text-muted">
                                    <i class="fas fa-calendar-alt me-1"></i> মেয়াদ: {{ $order->offer->validity }}
                                </small>
                            </div>
                        </div>

                        {{-- Description --}}
                        <p class="mb-2 small text-muted" style="font-size: 13px; line-height: 1.5;">
                            {{ $order->offer->description }}
                        </p>

                        {{-- User Info --}}
                        <div class="d-flex justify-content-between mb-2 small text-muted align-items-center" style="font-size: 13px;">
                            <span><i class="fas fa-user me-1 text-secondary"></i> {{ $order->user->name }}</span>
                            <span class="fw-bold d-flex align-items-center">
                                <i class="fas fa-phone me-1 text-secondary"></i> 
                                <span id="mobile_{{ $order->id }}">{{ $order->mobile_number }}</span>
                                <button onclick="copyToClipboard('mobile_{{ $order->id }}')" class="btn btn-sm btn-light ms-2 py-0 px-1" title="কপি করুন" style="font-size: 12px;">
                                    <i class="fas fa-copy"></i>
                                </button>
                            </span>
                        </div>

                        {{-- Price & Cashback --}}
                        <div class="d-flex justify-content-between mb-3">
                            <span class="text-success fw-bold">
                                <i class="fas fa-money-bill-wave me-1"></i> ৳{{ $order->offer->price }}
                            </span>
                            <span class="text-primary fw-bold">
                                <i class="fas fa-sync-alt me-1"></i> ৳{{ $order->offer->cashback }}
                            </span>
                        </div>

                        {{-- Action Buttons --}}
                        <div class="d-flex justify-content-between gap-2 pt-2 border-top mt-2">
                            <form action="{{ route('admin.orders.complete', $order->id) }}" method="POST" class="w-50">
                                @csrf
                                <button type="submit" class="btn btn-sm btn-outline-success w-100 d-flex justify-content-center align-items-center">
                                    <i class="fas fa-check me-1"></i> Complete
                                </button>
                            </form>
                            <form action="{{ route('admin.orders.cancel', $order->id) }}" method="POST" class="w-50">
                                @csrf
                                <button type="submit" class="btn btn-sm btn-outline-danger w-100 d-flex justify-content-center align-items-center">
                                    <i class="fas fa-times me-1"></i> Cancel
                                </button>
                            </form>
                        </div>
                    </div>
                </div>
            @endforeach
        </div>
    @endif
</div>

{{-- Copy to Clipboard Script --}}
<script>
    function copyToClipboard(elementId) {
        var text = document.getElementById(elementId).innerText;
        navigator.clipboard.writeText(text).then(function() {
            alert('মোবাইল নাম্বার কপি হয়েছে: ' + text);
        }, function(err) {
            alert('কপি করা যায়নি');
        });
    }
</script>
@endsection