@extends('layouts.layout-admin')

@section('title', 'POS')

@section('header_title', 'Point of Sales')
@section('header_subtitle', 'Quick checkout without shipping cost')

@section('content')
<div class="container-fluid">
    <div class="row">
        <!-- Products Panel -->
        <div class="col-lg-8 mb-4">
            <div class="card shadow">
                <div class="card-body">
                    <form action="{{ route('admin.pos.index') }}" method="GET" class="row g-2 mb-3">
                        <div class="col">
                            <input type="text" name="search" class="form-control" placeholder="Search products..."
                                value="{{ request('search') }}">
                        </div>
                        <div class="col-auto">
                            <button type="submit" class="btn btn-primary"><i class="bi bi-search"></i></button>
                        </div>
                    </form>

                    <div class="row g-3">
                        @forelse($products as $product)
                            <div class="col-md-4 col-sm-6">
                                <div class="card h-100 border-0 shadow-sm hover-shadow">
                                    @if ($product->getPrimaryImage())
                                        <img src="{{ $product->getPrimaryImage() }}" alt="{{ $product->name }}"
                                            class="card-img-top" style="height: 140px; object-fit: cover;">
                                    @endif
                                    <div class="card-body p-3">
                                        <h6 class="card-title mb-1">{{ $product->name }}</h6>
                                        <p class="text-success fw-bold mb-2">Rp {{ number_format($product->price, 0, ',', '.') }}</p>
                                        <small class="text-muted">Stock: {{ $product->stock }}</small>
                                        <form action="{{ route('admin.pos.add-to-cart') }}" method="POST" class="mt-2">
                                            @csrf
                                            <input type="hidden" name="product_id" value="{{ $product->id }}">
                                            <button type="submit" class="btn btn-sm btn-success w-100"
                                                {{ $product->stock < 1 ? 'disabled' : '' }}>
                                                <i class="bi bi-cart-plus"></i> Add
                                            </button>
                                        </form>
                                    </div>
                                </div>
                            </div>
                        @empty
                            <div class="col-12 text-center py-5">
                                <p class="text-muted">No products found</p>
                            </div>
                        @endforelse
                    </div>

                    <div class="mt-4">
                        {{ $products->links() }}
                    </div>
                </div>
            </div>
        </div>

        <!-- Cart Panel -->
        <div class="col-lg-4 mb-4">
            <div class="card shadow">
                <div class="card-header bg-white d-flex justify-content-between align-items-center">
                    <h5 class="mb-0"><i class="bi bi-cart-fill"></i> Cart</h5>
                    @if (count($cart) > 0)
                        <form action="{{ route('admin.pos.clear-cart') }}" method="POST" class="d-inline">
                            @csrf
                            <button type="submit" class="btn btn-sm btn-outline-danger"
                                onclick="return confirm('Clear all items?')">
                                <i class="bi bi-trash"></i>
                            </button>
                        </form>
                    @endif
                </div>
                <div class="card-body p-0">
                    @if (count($cart) > 0)
                        <div class="list-group list-group-flush" style="max-height: 400px; overflow-y: auto;">
                            @foreach ($cart as $item)
                                <div class="list-group-item p-3">
                                    <div class="d-flex justify-content-between align-items-start">
                                        <div class="flex-grow-1">
                                            <h6 class="mb-1">{{ $item['name'] }}</h6>
                                            <small class="text-muted">Rp {{ number_format($item['price'], 0, ',', '.') }}</small>
                                        </div>
                                        <form action="{{ route('admin.pos.remove-from-cart') }}" method="POST">
                                            @csrf
                                            <input type="hidden" name="product_id" value="{{ $item['id'] }}">
                                            <button type="submit" class="btn btn-sm text-danger p-0">
                                                <i class="bi bi-x-circle"></i>
                                            </button>
                                        </form>
                                    </div>
                                    <div class="d-flex align-items-center mt-2">
                                        <form action="{{ route('admin.pos.update-cart') }}" method="POST" class="d-flex align-items-center gap-2">
                                            @csrf
                                            <input type="hidden" name="product_id" value="{{ $item['id'] }}">
                                            <button type="submit" name="quantity" value="{{ $item['quantity'] - 1 }}"
                                                class="btn btn-sm btn-outline-secondary" {{ $item['quantity'] <= 1 ? 'disabled' : '' }}>
                                                <i class="bi bi-dash"></i>
                                            </button>
                                            <span class="fw-bold mx-2">{{ $item['quantity'] }}</span>
                                            <button type="submit" name="quantity" value="{{ $item['quantity'] + 1 }}"
                                                class="btn btn-sm btn-outline-secondary"
                                                {{ $item['quantity'] >= $item['stock'] ? 'disabled' : '' }}>
                                                <i class="bi bi-plus"></i>
                                            </button>
                                        </form>
                                        <span class="ms-auto fw-bold text-success">
                                            Rp {{ number_format($item['price'] * $item['quantity'], 0, ',', '.') }}
                                        </span>
                                    </div>
                                </div>
                            @endforeach
                        </div>

                        <div class="p-3 border-top">
                            <div class="d-flex justify-content-between mb-3">
                                <h5 class="mb-0">Total</h5>
                                <h5 class="mb-0 text-success fw-bold">Rp {{ number_format($total, 0, ',', '.') }}</h5>
                            </div>

                            <form action="{{ route('admin.pos.checkout') }}" method="POST">
                                @csrf
                                <div class="mb-2">
                                    <select name="customer_id" class="form-select" required>
                                        <option value="">Select customer</option>
                                        @foreach ($customers as $customer)
                                            <option value="{{ $customer->id }}">{{ $customer->name }} - {{ $customer->phone }}</option>
                                        @endforeach
                                    </select>
                                </div>
                                <div class="mb-3">
                                    <select name="payment_method" class="form-select" required>
                                        <option value="">Select payment</option>
                                        <option value="tunai">Tunai (Cash)</option>
                                        <option value="transfer">Transfer</option>
                                        <option value="wallet">E-Wallet</option>
                                    </select>
                                </div>
                                <button type="submit" class="btn btn-success w-100 btn-lg"
                                    onclick="return confirm('Process this POS order?')">
                                    <i class="bi bi-check-circle"></i> Process Payment
                                </button>
                            </form>
                        </div>
                    @else
                        <div class="text-center py-5">
                            <i class="bi bi-cart-x" style="font-size: 3rem; color: #ddd;"></i>
                            <p class="text-muted mt-2">Cart is empty</p>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@push('styles')
    <style>
        .hover-shadow:hover { box-shadow: 0 .5rem 1rem rgba(0,0,0,.15) !important; }
        .list-group-item:last-child { border-bottom: 0; }
    </style>
@endpush

@push('scripts')
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <script>
        @if (session('success'))
            Swal.fire({ icon: 'success', title: 'Success!', text: "{{ session('success') }}", showConfirmButton: false, timer: 3000, toast: true, position: 'top-end' });
        @endif
        @if (session('error'))
            Swal.fire({ icon: 'error', title: 'Error!', text: "{{ session('error') }}", showConfirmButton: true });
        @endif
    </script>
@endpush
