@extends('layouts.franchise', ['heading' => 'Payments & Settlement'])

@section('content')
    <form method="GET" class="user-card mb-4 grid grid-cols-1 gap-3 p-4 md:grid-cols-5">
        <div>
            <label class="text-xs text-[var(--user-muted)]">From</label>
            <input type="date" name="from" value="{{ $from }}" class="mt-1 w-full rounded-lg border border-[var(--user-border)] bg-transparent px-3 py-2 text-sm">
        </div>
        <div>
            <label class="text-xs text-[var(--user-muted)]">To</label>
            <input type="date" name="to" value="{{ $to }}" class="mt-1 w-full rounded-lg border border-[var(--user-border)] bg-transparent px-3 py-2 text-sm">
        </div>
        <div>
            <label class="text-xs text-[var(--user-muted)]">Status</label>
            <select name="status" class="mt-1 w-full rounded-lg border border-[var(--user-border)] bg-transparent px-3 py-2 text-sm">
                <option value="">All</option>
                @foreach(['pending', 'success', 'failed', 'refunded'] as $option)
                    <option value="{{ $option }}" @selected($status === $option)>{{ ucfirst($option) }}</option>
                @endforeach
            </select>
        </div>
        <div>
            <label class="text-xs text-[var(--user-muted)]">Mode</label>
            <select name="gateway" class="mt-1 w-full rounded-lg border border-[var(--user-border)] bg-transparent px-3 py-2 text-sm">
                <option value="">All</option>
                @foreach(['cash', 'upi', 'wallet', 'gateway'] as $option)
                    <option value="{{ $option }}" @selected($gateway === $option)>{{ strtoupper($option) }}</option>
                @endforeach
            </select>
        </div>
        <div class="self-end">
            <button class="w-full rounded-lg bg-[var(--user-primary)] px-4 py-2 text-sm font-semibold text-white">Apply</button>
        </div>
    </form>

    <div class="grid grid-cols-1 gap-3 md:grid-cols-4">
        <div class="user-card p-4">
            <div class="text-xs text-[var(--user-muted)]">Gross Sales</div>
            <div class="mt-1 text-xl font-semibold">₹ {{ number_format((float) $summary['gross_sales'], 2) }}</div>
        </div>
        <div class="user-card p-4">
            <div class="text-xs text-[var(--user-muted)]">Total Orders</div>
            <div class="mt-1 text-xl font-semibold">{{ number_format((int) $summary['orders']) }}</div>
        </div>
        <div class="user-card p-4">
            <div class="text-xs text-[var(--user-muted)]">BV Generated</div>
            <div class="mt-1 text-xl font-semibold">{{ number_format((float) $summary['bv_generated'], 2) }}</div>
        </div>
        <div class="user-card p-4">
            <div class="text-xs text-[var(--user-muted)]">UPI + Gateway</div>
            <div class="mt-1 text-xl font-semibold">₹ {{ number_format(((float) $summary['upi_sales'] + (float) $summary['gateway_sales']), 2) }}</div>
        </div>
    </div>

    <div class="mt-4 user-card p-4">
        <div class="mb-3 text-sm font-semibold">Daily Settlement</div>
        <div class="overflow-x-auto">
            <table class="min-w-full text-left text-sm">
                <thead class="text-xs text-[var(--user-muted)]">
                    <tr>
                        <th class="px-2 py-2">Date</th>
                        <th class="px-2 py-2">Orders</th>
                        <th class="px-2 py-2">Cash</th>
                        <th class="px-2 py-2">UPI</th>
                        <th class="px-2 py-2">Wallet</th>
                        <th class="px-2 py-2">Gateway</th>
                        <th class="px-2 py-2">Total</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($dailySettlement as $row)
                        <tr class="border-t border-[var(--user-border)]">
                            <td class="px-2 py-2">{{ \Illuminate\Support\Carbon::parse($row->run_date)->format('d M Y') }}</td>
                            <td class="px-2 py-2">{{ $row->total_orders }}</td>
                            <td class="px-2 py-2">₹ {{ number_format((float) $row->cash_sales, 2) }}</td>
                            <td class="px-2 py-2">₹ {{ number_format((float) $row->upi_sales, 2) }}</td>
                            <td class="px-2 py-2">₹ {{ number_format((float) $row->wallet_sales, 2) }}</td>
                            <td class="px-2 py-2">₹ {{ number_format((float) $row->gateway_sales, 2) }}</td>
                            <td class="px-2 py-2 font-semibold">₹ {{ number_format((float) $row->total_sales, 2) }}</td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="px-2 py-4 text-center text-[var(--user-muted)]">No settlement rows in selected range.</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>

    <div class="mt-4 user-card p-4">
        <div class="mb-3 text-sm font-semibold">Payment Transactions</div>
        <div class="grid grid-cols-1 gap-3">
            @forelse($payments as $payment)
                <div class="rounded-xl border border-[var(--user-border)] p-3">
                    <div class="flex items-center justify-between">
                        <div class="text-sm font-semibold">{{ $payment->order_number ?? $payment->merchant_order_ref }}</div>
                        <span class="user-badge {{ $payment->status === 'success' ? 'success' : ($payment->status === 'failed' ? 'danger' : 'warning') }}">
                            {{ strtoupper($payment->status) }}
                        </span>
                    </div>
                    <div class="mt-1 text-xs text-[var(--user-muted)]">
                        {{ $payment->member_code ?: '-' }} {{ $payment->name ? '• '.$payment->name : '' }}
                    </div>
                    <div class="mt-1 text-sm">₹ {{ number_format((float) $payment->amount, 2) }} • {{ strtoupper($payment->gateway) }}</div>
                    <div class="text-xs text-[var(--user-muted)]">
                        {{ optional(\Illuminate\Support\Carbon::parse($payment->created_at))->format('d M Y h:i A') }}
                        @if($payment->gateway_transaction_id)
                            • Ref: {{ $payment->gateway_transaction_id }}
                        @endif
                    </div>
                </div>
            @empty
                <div class="rounded-xl border border-[var(--user-border)] p-4 text-center text-sm text-[var(--user-muted)]">No payments found.</div>
            @endforelse
        </div>
        <div class="mt-4">{{ $payments->links() }}</div>
    </div>
@endsection

