@extends('layouts.admin')

@section('title', 'Transaction Details')

@section('admin-content')
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h4 class="mb-0">Transaction #{{ $transaction->id }}</h4>
        <div>
            @if($transaction->status === 'pending_approval')
                <button type="button" class="btn btn-danger me-2" data-bs-toggle="modal" data-bs-target="#rejectModal">
                    <i class="bi bi-x-circle me-2"></i>Reject
                </button>
                <button type="button" class="btn btn-success" data-bs-toggle="modal" data-bs-target="#approveModal">
                    <i class="bi bi-check-circle me-2"></i>Approve
                </button>
            @endif
            <a href="{{ route('admin.transactions.index') }}" class="btn btn-outline-secondary ms-2">
                <i class="bi bi-arrow-left me-2"></i>Back
            </a>
        </div>
    </div>

    </div>

    <div class="row">
        <div class="col-lg-8">
            {{-- Transaction Info --}}
            <div class="card shadow-sm mb-4">
                <div class="card-header bg-light">
                    <h5 class="mb-0">Payment Details</h5>
                </div>
                <div class="card-body">
                    <div class="row g-3">
                        <div class="col-md-6">
                            <label class="text-muted small">Status</label>
                            <div>
                                <span class="badge {{ $transaction->getStatusBadgeClass() }} fs-6">
                                    {{ ucfirst(str_replace('_', ' ', $transaction->status)) }}
                                </span>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <label class="text-muted small">Date</label>
                            <p class="fw-bold">{{ $transaction->created_at->format('F d, Y h:i A') }}</p>
                        </div>
                        <div class="col-md-6">
                            <label class="text-muted small">Amount</label>
                            <p class="fw-bold fs-5">₹{{ number_format($transaction->total_amount, 2) }}</p>
                        </div>
                        <div class="col-md-6">
                            <label class="text-muted small">Payment Method</label>
                            <p class="fw-bold text-capitalize">
                                {{ str_replace('_', ' ', $transaction->payment_method ?? 'N/A') }} 
                                <span class="text-muted small">({{ ucfirst($transaction->payment_gateway) }})</span>
                            </p>
                        </div>
                        <div class="col-md-6">
                            <label class="text-muted small">
                                {{ $transaction->payment_gateway === 'manual' ? 'Reference ID' : 'Transaction ID (Gateway)' }}
                            </label>
                            <p class="font-monospace">{{ $transaction->payment_id ?? 'N/A' }}</p>
                        </div>
                        @if($transaction->order_id)
                        <div class="col-md-6">
                            <label class="text-muted small">Order ID</label>
                            <p class="font-monospace">{{ $transaction->order_id }}</p>
                        </div>
                        @endif
                        @if($transaction->failure_reason)
                        <div class="col-12">
                            <div class="alert alert-danger mb-0">
                                <strong>Failure Reason:</strong> {{ $transaction->failure_reason }}
                            </div>
                        </div>
                        @endif
                    </div>
                </div>
            </div>

            {{-- Breakdown --}}
            <div class="card shadow-sm mb-4">
                <div class="card-header bg-light">
                    <h5 class="mb-0">Payment Breakdown</h5>
                </div>
                <div class="card-body">
                    <table class="table table-borderless">
                        <tr>
                            <td>Base Amount</td>
                            <td class="text-end">₹{{ number_format($transaction->amount, 2) }}</td>
                        </tr>
                        <tr>
                            <td>Platform Commission</td>
                            <td class="text-end">+ ₹{{ number_format($transaction->platform_commission, 2) }}</td>
                        </tr>
                        <tr>
                            <td>GST ({{ $transaction->gst_percentage ?? 18 }}%)</td>
                            <td class="text-end">+ ₹{{ number_format($transaction->gst_amount, 2) }}</td>
                        </tr>
                        <tr class="border-top">
                            <td class="fw-bold">Total Paid</td>
                            <td class="text-end fw-bold fs-5">₹{{ number_format($transaction->total_amount, 2) }}</td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>

        <div class="col-lg-4">
            {{-- User Info --}}
            <div class="card shadow-sm mb-4">
                <div class="card-header bg-light">
                    <h5 class="mb-0">Payer Information</h5>
                </div>
                <div class="card-body">
                    <div class="d-flex align-items-center mb-3">
                        <div class="avatar me-3 bg-primary text-white rounded-circle d-flex align-items-center justify-content-center" style="width: 48px; height: 48px;">
                            {{ substr($transaction->user->name ?? 'U', 0, 1) }}
                        </div>
                        <div>
                            <h6 class="mb-0">{{ $transaction->user->name ?? 'Unknown User' }}</h6>
                            <small class="text-muted">{{ $transaction->user->email ?? '' }}</small>
                        </div>
                    </div>
                    @if($transaction->seller)
                        <hr>
                        <p class="mb-1"><small class="text-muted">Seller Profile:</small></p>
                        <a href="{{ route('admin.sellers.edit', $transaction->seller->id) }}" class="text-decoration-none">
                            <i class="bi bi-shop me-1"></i> {{ $transaction->seller->company_name }}
                        </a>
                    @endif
                </div>
            </div>

            {{-- Item Details --}}
            <div class="card shadow-sm">
                <div class="card-header bg-light">
                    <h5 class="mb-0">Item Details</h5>
                </div>
                <div class="card-body">
                    @if($transaction->transaction_type === 'promotion')
                        <span class="badge bg-primary mb-2">Promotion</span>
                        <h5>{{ $transaction->productPromotion->package->name ?? $transaction->metadata['package_name'] ?? 'Unknown Package' }}</h5>
                        <p class="text-muted small mb-0">
                            <strong>Products:</strong> {{ $transaction->metadata['product_count'] ?? 0 }}<br>
                            <strong>Duration:</strong> {{ $transaction->metadata['duration_days'] ?? 0 }} Days
                        </p>
                    @elseif($transaction->transaction_type === 'subscription')
                        <span class="badge bg-success mb-2">Subscription</span>
                        <h5>{{ $transaction->subscription?->plan->name ?? $transaction->metadata['plan_name'] ?? 'Unknown Plan' }}</h5>
                        <p class="text-muted small mb-0">
                            <strong>Duration:</strong> {{ $transaction->metadata['duration_days'] ?? 30 }} Days<br>
                            <strong>Valid Until:</strong> 
                            @if($transaction->subscription && $transaction->subscription->end_date)
                                {{ $transaction->subscription->end_date->format('M d, Y') }}
                            @else
                                <span class="text-warning">Pending Activation</span>
                            @endif
                        </p>
                    @else
                        <span class="badge bg-secondary">{{ ucfirst($transaction->transaction_type) }}</span>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
    <!-- Reject Modal -->
    <div class="modal fade" id="rejectModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <form action="{{ route('admin.transactions.reject', $transaction) }}" method="POST">
                    @csrf
                    <div class="modal-header">
                        <h5 class="modal-title">Reject Transaction</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <p>Are you sure you want to reject this transaction? This action cannot be undone.</p>
                        <div class="mb-3">
                            <label for="failure_reason" class="form-label">Rejection Reason</label>
                            <textarea class="form-control" id="failure_reason" name="failure_reason" rows="3" required placeholder="e.g., Invalid Reference ID, Payment not received"></textarea>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-danger">Reject Transaction</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- Approve Modal -->
    <div class="modal fade" id="approveModal" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <form action="{{ route('admin.transactions.approve', $transaction) }}" method="POST">
                    @csrf
                    <div class="modal-header">
                        <h5 class="modal-title">Approve Transaction</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <p>Are you sure you want to approve this transaction?</p>
                        <p class="text-muted small">This will activate the subscription/promotion for the seller.</p>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                        <button type="submit" class="btn btn-success">Approve Transaction</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
@endsection
