@extends('admin.layouts.app')
@section('title', $title)
@section('content')
<div class="d-flex justify-content-between align-items-center mb-3 flex-wrap gap-2"><h1>{{ $title }}</h1></div>
<form class="card card-body mb-3"><div class="row g-2"><div class="col-md-4"><input class="form-control" name="q" value="{{ request('q') }}" placeholder="Search {{ strtolower($title) }}"></div><div class="col-auto"><button class="btn btn-primary">Search</button></div></div></form>
<div class="row g-3">
    <div class="col-lg-4">
        <div class="card card-body">
            <h5>{{ $editItem ? 'Edit' : 'Add New' }} {{ rtrim($title, 's') }}</h5>
            <form method="POST" enctype="multipart/form-data" action="{{ $editItem ? route(str_replace('.index', '.update', $routeName), $editItem->id) : route(str_replace('.index', '.store', $routeName)) }}">
                @csrf
                @if($editItem) @method('PUT') @endif
                <div class="mb-2"><label>Name</label><input class="form-control" name="name" value="{{ old('name', $editItem->name ?? '') }}" required></div>
                <div class="mb-2"><label>Code</label><input class="form-control" name="code" value="{{ old('code', $editItem->code ?? '') }}"></div>
                @if($title === 'Suppliers')
                    <div class="mb-2"><label>Phone</label><input class="form-control" name="phone" value="{{ old('phone', $editItem->phone ?? '') }}"></div>
                    <div class="mb-2"><label>Email</label><input class="form-control" name="email" value="{{ old('email', $editItem->email ?? '') }}"></div>
                    <div class="mb-2"><label>Contact Person</label><input class="form-control" name="contact_person" value="{{ old('contact_person', $editItem->contact_person ?? '') }}"></div>
                    <div class="mb-2"><label>Address</label><textarea class="form-control" name="address">{{ old('address', $editItem->address ?? '') }}</textarea></div>
                    <div class="mb-2"><label>Opening Balance</label><input class="form-control" type="number" step="0.01" name="opening_balance" value="{{ old('opening_balance', $editItem->opening_balance ?? 0) }}"></div>
                    <div class="mb-2"><label>Rating</label><input class="form-control" type="number" min="1" max="5" name="rating" value="{{ old('rating', $editItem->rating ?? 5) }}"></div>
                    <div class="mb-2"><label>Notes</label><textarea class="form-control" name="notes">{{ old('notes', $editItem->notes ?? '') }}</textarea></div>
                    <div class="mb-2"><label>Document Upload</label><input class="form-control" type="file" name="document"></div>
                @endif
                <div class="mb-2"><label>Status</label><select class="form-select" name="status"><option value="Active" @selected(old('status', $editItem->status ?? 'Active') === 'Active')>Active</option><option value="Inactive" @selected(old('status', $editItem->status ?? 'Active') === 'Inactive')>Inactive</option></select></div>
                <div class="d-flex gap-2"><button class="btn btn-primary">{{ $editItem ? 'Update' : 'Save' }}</button>@if($editItem)<a class="btn btn-light" href="{{ route($routeName) }}">Cancel</a>@endif</div>
            </form>
        </div>
    </div>
    <div class="col-lg-8">
        <div class="card"><div class="table-responsive"><table class="table table-striped mb-0"><thead><tr><th>#</th><th>Name</th><th>Code</th>@if($title==='Suppliers')<th>Phone</th><th>Contact</th>@endif<th>Status</th><th>Action</th></tr></thead><tbody>
        @forelse($items as $item)
            <tr><td>{{ $loop->iteration + ($items->firstItem() - 1) }}</td><td>{{ $item->name }}</td><td>{{ $item->code ?: '—' }}</td>@if($title==='Suppliers')<td>{{ $item->phone ?: '—' }}</td><td>{{ $item->contact_person ?: '—' }}</td>@endif<td>{{ $item->status }}</td><td class="d-flex gap-1 flex-wrap"><a class="btn btn-sm btn-outline-primary" href="{{ route($routeName, ['edit' => $item->id]) }}">Edit</a>@if($title==='Suppliers')<a class="btn btn-sm btn-outline-secondary" href="{{ route('admin.suppliers.ledger',$item) }}">Ledger</a><a class="btn btn-sm btn-outline-info" href="{{ route('admin.suppliers.statement',$item) }}">Statement</a>@endif<form method="POST" action="{{ route(str_replace('.index', '.destroy', $routeName), $item->id) }}" onsubmit="return confirm('Delete this item?')">@csrf @method('DELETE')<button class="btn btn-sm btn-outline-danger">Delete</button></form></td></tr>
        @empty
            <tr><td colspan="{{ $title==='Suppliers' ? 7 : 5 }}">No data found.</td></tr>
        @endforelse
        </tbody></table></div></div>
        <div class="mt-3">{{ $items->links() }}</div>
    </div>
</div>
@endsection
