<?php
if (Auth('admin')->User()->dashboard_style == 'light') {
    $text = 'dark';
} else {
    $text = 'light';
}
?>
@extends('layouts.app')
@section('content')
    @include('admin.topmenu')
    @include('admin.sidebar')
    <div class="main-panel">
        <div class="content">
            <div class="page-inner">
                <div class="page-header">
                    <h4 class="page-title text-{{ $text }}">Order #{{ $vipCardOrder->order_number }}</h4>
                    <ul class="breadcrumbs">
                        <li class="nav-home"><a href="{{ route('admin.dashboard') }}"><i class="flaticon-home"></i></a></li>
                        <li class="separator"><i class="flaticon-right-arrow"></i></li>
                        <li class="nav-item"><a href="{{ route('admin.vip-card-orders.index') }}">VIP Card Orders</a></li>
                        <li class="separator"><i class="flaticon-right-arrow"></i></li>
                        <li class="nav-item"><span class="text-{{ $text }}">{{ $vipCardOrder->order_number }}</span></li>
                    </ul>
                </div>

                @if(session('success'))
                    <div class="alert alert-success alert-dismissible fade show">
                        {{ session('success') }}
                        <button type="button" class="close" data-dismiss="alert"><span>&times;</span></button>
                    </div>
                @endif
                @if(session('error'))
                    <div class="alert alert-danger alert-dismissible fade show">
                        {{ session('error') }}
                        <button type="button" class="close" data-dismiss="alert"><span>&times;</span></button>
                    </div>
                @endif

                <div class="row">
                    {{-- Left Column: Order & Customer Info --}}
                    <div class="col-md-6">
                        <div class="card">
                            <div class="card-header"><h4 class="card-title text-{{ $text }}">Order Details</h4></div>
                            <div class="card-body">
                                <table class="table table-borderless table-sm">
                                    <tr><td><strong>Order #:</strong></td><td>{{ $vipCardOrder->order_number }}</td></tr>
                                    <tr>
                                        <td><strong>Status:</strong></td>
                                        <td>
                                            @if($vipCardOrder->is_active)
                                                <span class="badge badge-success">Active</span>
                                            @elseif($vipCardOrder->is_expired)
                                                <span class="badge badge-secondary">Expired</span>
                                            @elseif($vipCardOrder->status == 'pending')
                                                <span class="badge badge-warning">Pending</span>
                                            @elseif($vipCardOrder->status == 'cancelled')
                                                <span class="badge badge-danger">Cancelled</span>
                                            @elseif($vipCardOrder->status == 'refunded')
                                                <span class="badge badge-info">Refunded</span>
                                            @else
                                                <span class="badge badge-secondary">{{ ucfirst($vipCardOrder->status) }}</span>
                                            @endif
                                        </td>
                                    </tr>
                                    <tr><td><strong>Amount:</strong></td><td><strong>${{ number_format($vipCardOrder->amount, 2) }}</strong></td></tr>
                                    <tr><td><strong>Payment Method:</strong></td><td>{{ $vipCardOrder->payment_method ?? 'N/A' }}</td></tr>
                                    <tr>
                                        <td><strong>Payment Status:</strong></td>
                                        <td>
                                            @if($vipCardOrder->payment_status == 'verified')
                                                <span class="badge badge-success">Verified</span>
                                            @elseif($vipCardOrder->payment_status == 'failed')
                                                <span class="badge badge-danger">Failed</span>
                                            @else
                                                <span class="badge badge-warning">Pending</span>
                                            @endif
                                        </td>
                                    </tr>
                                    <tr><td><strong>Created:</strong></td><td>{{ $vipCardOrder->created_at->format('M d, Y H:i') }}</td></tr>
                                    @if($vipCardOrder->activated_at)
                                        <tr><td><strong>Activated:</strong></td><td>{{ $vipCardOrder->activated_at->format('M d, Y H:i') }}</td></tr>
                                    @endif
                                    @if($vipCardOrder->expires_at)
                                        <tr>
                                            <td><strong>Expires:</strong></td>
                                            <td>
                                                {{ $vipCardOrder->expires_at->format('M d, Y H:i') }}
                                                @if($vipCardOrder->is_active)
                                                    <br><small class="text-success">{{ $vipCardOrder->days_remaining }} days remaining</small>
                                                @elseif($vipCardOrder->is_expired)
                                                    <br><small class="text-danger">Expired {{ $vipCardOrder->expires_at->diffForHumans() }}</small>
                                                @endif
                                            </td>
                                        </tr>
                                    @endif
                                </table>

                                @if($vipCardOrder->payment_proof)
                                    <hr>
                                    <p><strong>Payment Proof:</strong></p>
                                    <a href="{{ asset('dash/' . $vipCardOrder->payment_proof) }}" target="_blank">
                                        <img src="{{ asset('dash/' . $vipCardOrder->payment_proof) }}" class="img-fluid rounded" style="max-height:300px;">
                                    </a>
                                @endif

                                @if($vipCardOrder->admin_note)
                                    <hr>
                                    <p><strong>Admin Note:</strong></p>
                                    <p>{{ $vipCardOrder->admin_note }}</p>
                                @endif
                            </div>
                        </div>

                        <div class="card">
                            <div class="card-header"><h4 class="card-title text-{{ $text }}">Customer</h4></div>
                            <div class="card-body">
                                <table class="table table-borderless table-sm">
                                    <tr><td><strong>Name:</strong></td><td>{{ $vipCardOrder->user->name ?? 'N/A' }}</td></tr>
                                    <tr><td><strong>Email:</strong></td><td>{{ $vipCardOrder->user->email ?? 'N/A' }}</td></tr>
                                </table>
                            </div>
                        </div>
                    </div>

                    {{-- Right Column: VIP Card Info + Actions --}}
                    <div class="col-md-6">
                        <div class="card">
                            <div class="card-header"><h4 class="card-title text-{{ $text }}">VIP Card</h4></div>
                            <div class="card-body text-center">
                                @if($vipCardOrder->vipCard && $vipCardOrder->vipCard->card_image)
                                    <img src="{{ asset('dash/' . $vipCardOrder->vipCard->card_image) }}" class="img-fluid rounded mb-3" style="max-height:200px;">
                                @endif
                                <h5>{{ $vipCardOrder->vipCard->name ?? 'Deleted Card' }}</h5>
                                @if($vipCardOrder->vipCard)
                                    <p class="text-muted">${{ number_format($vipCardOrder->vipCard->price, 2) }} &bull; {{ $vipCardOrder->vipCard->duration_text }}</p>
                                    @if($vipCardOrder->vipCard->benefits)
                                        <ul class="list-unstyled text-left mt-3">
                                            @foreach($vipCardOrder->vipCard->benefits as $benefit)
                                                <li class="mb-1"><i class="fas fa-check text-success mr-2"></i>{{ $benefit }}</li>
                                            @endforeach
                                        </ul>
                                    @endif
                                @endif
                            </div>
                        </div>

                        <div class="card">
                            <div class="card-header"><h4 class="card-title text-{{ $text }}">Actions</h4></div>
                            <div class="card-body">
                                @if($vipCardOrder->status == 'pending')
                                    <form action="{{ route('admin.vip-card-orders.approve', $vipCardOrder) }}" method="POST" class="mb-2" onsubmit="return confirm('Approve this order and activate membership?')">
                                        @csrf
                                        <button class="btn btn-success btn-block"><i class="fas fa-check"></i> Approve & Activate</button>
                                    </form>
                                    <form action="{{ route('admin.vip-card-orders.decline', $vipCardOrder) }}" method="POST" class="mb-2" onsubmit="return confirm('Decline this order?')">
                                        @csrf
                                        <button class="btn btn-danger btn-block"><i class="fas fa-times"></i> Decline</button>
                                    </form>
                                @endif

                                @if(in_array($vipCardOrder->status, ['active']))
                                    <form action="{{ route('admin.vip-card-orders.cancel', $vipCardOrder) }}" method="POST" class="mb-2" onsubmit="return confirm('Cancel this active membership?')">
                                        @csrf
                                        <button class="btn btn-warning btn-block"><i class="fas fa-ban"></i> Cancel Membership</button>
                                    </form>
                                @endif

                                @if(!in_array($vipCardOrder->status, ['refunded']))
                                    <form action="{{ route('admin.vip-card-orders.refund', $vipCardOrder) }}" method="POST" class="mb-2" onsubmit="return confirm('Refund this order?')">
                                        @csrf
                                        <button class="btn btn-info btn-block"><i class="fas fa-undo"></i> Refund</button>
                                    </form>
                                @endif

                                @if(in_array($vipCardOrder->status, ['cancelled', 'refunded', 'expired']))
                                    <form action="{{ route('admin.vip-card-orders.destroy', $vipCardOrder) }}" method="POST" onsubmit="return confirm('Permanently delete this order?')">
                                        @csrf @method('DELETE')
                                        <button class="btn btn-danger btn-block"><i class="fas fa-trash"></i> Delete</button>
                                    </form>
                                @endif

                                @if($vipCardOrder->status == 'pending' || in_array($vipCardOrder->status, ['cancelled', 'refunded']))
                                    <p class="text-muted text-center mt-2">
                                        <small>Order is {{ $vipCardOrder->status }}.</small>
                                    </p>
                                @endif
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
