@extends('layouts.admin.app')

@section('title', translate('Purchase Module'))

@section('content')
    <div class="content container-fluid">
        <div class="page-header">
            <h1 class="page-header-title">
                <span class="page-header-icon">
                    <i class="tio-shopping-basket"></i>
                </span>
                <span>{{ translate('Purchase Module') }}</span>
            </h1>
            <div class="page-header-right">
                <a href="{{ route('admin.purchase.create') }}" class="btn btn--primary">
                    <i class="tio-add-circle"></i> {{ translate('Add Purchase Entry') }}
                </a>
            </div>
        </div>

        <div class="card mb-3">
            <div class="card-body">
                <form action="{{ route('admin.purchase.index') }}" method="get">
                    <div class="row g-3">
                        <div class="col-sm-12 col-md-6 col-lg-4">
                            <input type="text" name="search" value="{{ request('search') }}" class="form-control" placeholder="{{ translate('Search by purchase no, product, supplier or invoice') }}">
                        </div>
                        <div class="col-sm-6 col-md-3 col-lg-2">
                            <input type="date" name="from" value="{{ request('from') }}" class="form-control">
                        </div>
                        <div class="col-sm-6 col-md-3 col-lg-2">
                            <input type="date" name="to" value="{{ request('to') }}" class="form-control">
                        </div>
                        <div class="col-sm-12 col-lg-2">
                            <button class="btn btn--primary btn-block" type="submit">{{ translate('Filter') }}</button>
                        </div>
                        <div class="col-sm-12 col-lg-2">
                            <a class="btn btn--reset btn-block" href="{{ route('admin.purchase.index') }}">{{ translate('Reset') }}</a>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <div class="card">
            <div class="card-header border-0">
                <h4 class="card-title">{{ translate('Purchase History') }} <span class="badge badge-soft-secondary">{{ $purchases->total() }}</span></h4>
            </div>
            <div class="table-responsive">
                <table class="table table-borderless table-thead-bordered table-nowrap card-table">
                    <thead class="thead-light">
                    <tr>
                        <th>{{ translate('sl') }}</th>
                        <th>{{ translate('Purchase No') }}</th>
                        <th>{{ translate('Date') }}</th>
                        <th>{{ translate('Product') }}</th>
                        <th>{{ translate('Supplier / Source') }}</th>
                        <th>{{ translate('Invoice Number') }}</th>
                        <th class="text-center">{{ translate('Quantity') }}</th>
                        <th class="text-right">{{ translate('Purchase Price') }}</th>
                        <th class="text-right">{{ translate('Total Amount') }}</th>
                        <th class="text-center">{{ translate('Stock After') }}</th>
                    </tr>
                    </thead>
                    <tbody>
                    @forelse($purchases as $key => $purchase)
                        @php($purchaseItems = $purchase->items)
                        @php($purchaseItem = $purchaseItems->first())
                        <tr>
                            <td>{{ $key + $purchases->firstItem() }}</td>
                            <td>{{ $purchase->purchase_no }}</td>
                            <td>{{ $purchase->purchase_date?->format('d M Y') }}</td>
                            <td>
                                @forelse($purchaseItems->take(3) as $lineItem)
                                    <div class="font-weight-semibold">{{ $lineItem->item_name ?? translate('N/A') }}</div>
                                    @if($lineItem->variation_type)
                                        <div class="text-muted fz-12 mb-1">{{ translate('Variation') }}: {{ $lineItem->variation_type }}</div>
                                    @endif
                                @empty
                                    <div class="font-weight-semibold">{{ translate('N/A') }}</div>
                                @endforelse
                                @if($purchaseItems->count() > 3)
                                    <div class="text-muted fz-12">+ {{ $purchaseItems->count() - 3 }} {{ translate('more items') }}</div>
                                @endif
                            </td>
                            <td>{{ $purchase->supplier_name ?? translate('N/A') }}</td>
                            <td>{{ $purchase->invoice_number ?? translate('N/A') }}</td>
                            <td class="text-center">{{ $purchase->total_quantity }}</td>
                            <td class="text-right">
                                @if($purchaseItems->count() === 1)
                                    {{ \App\CentralLogics\Helpers::format_currency($purchaseItem?->purchase_price ?? 0) }}
                                @else
                                    {{ translate('Multiple') }}
                                @endif
                            </td>
                            <td class="text-right">{{ \App\CentralLogics\Helpers::format_currency($purchase->total_amount) }}</td>
                            <td class="text-center">
                                @if($purchaseItems->count() === 1)
                                    {{ $purchaseItem?->stock_after ?? 0 }}
                                @else
                                    --
                                @endif
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="10" class="text-center py-4">{{ translate('No purchase history found') }}</td>
                        </tr>
                    @endforelse
                    </tbody>
                </table>
            </div>
            @if($purchases->hasPages())
                <div class="card-footer">
                    {!! $purchases->links() !!}
                </div>
            @endif
        </div>
    </div>
@endsection
