@extends('layouts.app')

@section('title', 'Supplier Management')

@section('breadcrumb')
    @include('components.breadcrumb', ['items' => ['Parties' => null, 'Suppliers' => null]])
@endsection

@section('content')
<div>
    <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4 mb-6">
        <div>
            <h1 class="text-2xl font-bold text-slate-900 dark:text-white">Supplier Management</h1>
            <p class="text-sm text-slate-500 dark:text-slate-400 mt-1">{{ $suppliers->total() }} suppliers registered</p>
        </div>
        <x-button variant="primary" icon="user-plus" onclick="document.getElementById('addSupplierModal').classList.remove('hidden')">Add Supplier</x-button>
    </div>

    {{-- Add Supplier Modal --}}
    <div id="addSupplierModal" class="hidden fixed inset-0 z-50 flex items-center justify-center bg-slate-900/50 p-4">
        <div class="bg-white dark:bg-slate-900 rounded-2xl shadow-card border border-slate-200 dark:border-slate-800 w-full max-w-lg">
            <div class="px-6 py-4 border-b border-slate-200 dark:border-slate-800 flex items-center justify-between">
                <h3 class="font-semibold text-slate-900 dark:text-white">Add New Supplier</h3>
                <button onclick="document.getElementById('addSupplierModal').classList.add('hidden')" class="w-8 h-8 flex items-center justify-center rounded-lg hover:bg-slate-100 dark:hover:bg-slate-800">
                    <i data-lucide="x" class="w-4 h-4"></i>
                </button>
            </div>
            <form action="{{ route('party.store') }}" method="POST" class="p-6 space-y-4">
                @csrf
                <input type="hidden" name="type" value="supplier" />
                <x-form-input name="name" label="Supplier Name" icon="building" required />
                <div class="grid grid-cols-2 gap-4">
                    <x-form-input name="phone" label="Phone" icon="phone" required />
                    <x-form-input name="email" label="Email" type="email" icon="mail" />
                </div>
                <x-form-input name="address" label="Address" type="textarea" icon="map-pin" />
                <div class="flex justify-end gap-2 pt-2">
                    <x-button variant="secondary" type="button" onclick="document.getElementById('addSupplierModal').classList.add('hidden')">Cancel</x-button>
                    <x-button variant="primary" icon="save" type="submit">Save</x-button>
                </div>
            </form>
        </div>
    </div>

    <x-card :padding="false">
        <div class="overflow-x-auto">
            <table class="w-full text-sm">
                <thead>
                    <tr class="bg-slate-50 dark:bg-slate-900/50 border-b border-slate-200 dark:border-slate-800">
                        <th class="px-4 py-3 text-left text-[11px] font-bold uppercase tracking-wider text-slate-500">Supplier</th>
                        <th class="px-4 py-3 text-left text-[11px] font-bold uppercase tracking-wider text-slate-500">Contact</th>
                        <th class="px-4 py-3 text-right text-[11px] font-bold uppercase tracking-wider text-slate-500">Total Purchase</th>
                        <th class="px-4 py-3 text-right text-[11px] font-bold uppercase tracking-wider text-slate-500">Paid</th>
                        <th class="px-4 py-3 text-right text-[11px] font-bold uppercase tracking-wider text-slate-500">Due</th>
                        <th class="px-4 py-3 text-center text-[11px] font-bold uppercase tracking-wider text-slate-500">Status</th>
                        <th class="px-4 py-3 text-center text-[11px] font-bold uppercase tracking-wider text-slate-500">Action</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100 dark:divide-slate-800">
                    @forelse($suppliers as $s)
                        @php
                            $balance = $s->balance;
                            $due = max(0, $balance['purchase'] - $balance['paid'] - $balance['given']);
                        @endphp
                        <tr class="hover:bg-slate-50 dark:hover:bg-slate-800/50">
                            <td class="px-4 py-3.5">
                                <div class="flex items-center gap-3">
                                    <div class="w-9 h-9 rounded-lg bg-primary-100 dark:bg-primary-500/15 flex items-center justify-center font-bold text-primary-700 text-xs">
                                        {{ strtoupper(substr($s->name, 0, 2)) }}
                                    </div>
                                    <div>
                                        <p class="text-sm font-semibold text-slate-900 dark:text-white">{{ $s->name }}</p>
                                        <p class="text-xs text-slate-500">{{ $s->code }}</p>
                                    </div>
                                </div>
                            </td>
                            <td class="px-4 py-3.5 text-xs text-slate-500">{{ $s->phone }}</td>
                            <td class="px-4 py-3.5 text-right text-sm font-semibold counter-num">৳{{ number_format($balance['purchase']) }}</td>
                            <td class="px-4 py-3.5 text-right text-sm text-emerald-600 counter-num">৳{{ number_format($balance['paid'] + $balance['given']) }}</td>
                            <td class="px-4 py-3.5 text-right text-sm font-bold {{ $due > 0 ? 'text-rose-600' : 'text-slate-400' }} counter-num">৳{{ number_format($due) }}</td>
                            <td class="px-4 py-3.5 text-center">
                                <x-badge :variant="$s->is_active ? 'success' : 'gray'">{{ $s->is_active ? 'Active' : 'Inactive' }}</x-badge>
                            </td>
                            <td class="px-4 py-3.5 text-center">
                                <div class="flex items-center justify-center gap-1">
                                    <a href="{{ route('reports.supplier', ['party_id' => $s->id]) }}" class="w-8 h-8 flex items-center justify-center rounded-lg text-slate-500 hover:bg-slate-100 dark:hover:bg-slate-800" title="View Ledger">
                                        <i data-lucide="scroll-text" class="w-4 h-4"></i>
                                    </a>
                                    <a href="{{ route('purchase.create', ['party_id' => $s->id]) }}" class="w-8 h-8 flex items-center justify-center rounded-lg text-slate-500 hover:bg-slate-100 dark:hover:bg-slate-800" title="New Purchase">
                                        <i data-lucide="shopping-cart" class="w-4 h-4"></i>
                                    </a>
                                </div>
                            </td>
                        </tr>
                    @empty
                        <tr><td colspan="7" class="text-center py-8 text-sm text-slate-500">No suppliers found.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($suppliers->hasPages())
        <div class="px-4 py-3 border-t border-slate-200 dark:border-slate-800">
            {{ $suppliers->withQueryString()->links() }}
        </div>
        @endif
    </x-card>
</div>
@endsection
