@extends('admin.layouts.app')

@section('title', 'Units')
@section('page_title', 'Units')

@section('content')
@php
    $items = $units ?? $items ?? collect();
@endphp

<div class="page-shell">
    <div class="page-head">
        <div>
            <h1 class="page-title">Units</h1>
            <div class="page-subtitle">Manage product units like piece, box, kg, liter, and more.</div>
        </div>
    </div>

    <div class="grid-3">
        <div class="card">
            <h3 style="margin-top:0;">Add Unit</h3>

            @if(Route::has('admin.units.store'))
                <form method="POST" action="{{ route('admin.units.store') }}">
                    @csrf
                    <label for="name">Unit Name</label>
                    <input type="text" id="name" name="name" value="{{ old('name') }}" placeholder="Enter unit name" required>

                    <div class="form-actions">
                        <button type="submit" class="btn primary">Save Unit</button>
                        <a href="{{ route('admin.units.index') }}" class="btn btn-light">Reset</a>
                    </div>
                </form>
            @else
                <div class="alert alert-info">Unit store route is not available yet.</div>
            @endif
        </div>

        <div class="card" style="grid-column:span 2;">
            <div class="page-head" style="margin-bottom:10px;">
                <div>
                    <h3 style="margin:0;">Unit List</h3>
                    <div class="page-subtitle">All saved units are shown here.</div>
                </div>
            </div>

            <div class="table-wrap">
                <table class="table">
                    <thead>
                        <tr>
                            <th style="width:80px;">#</th>
                            <th>Name</th>
                            <th class="text-end">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($items as $index => $item)
                            @php
                                $name = $item->name ?? $item->unit_name ?? '—';
                            @endphp
                            <tr>
                                <td>{{ method_exists($items, 'firstItem') ? $items->firstItem() + $index : $index + 1 }}</td>
                                <td>{{ $name }}</td>
                                <td class="text-end" style="white-space:nowrap;">
                                    @if(Route::has('admin.units.edit'))
                                        <a href="{{ route('admin.units.edit', $item) }}" class="btn btn-light">Edit</a>
                                    @endif

                                    @if(Route::has('admin.units.destroy'))
                                        <form method="POST" action="{{ route('admin.units.destroy', $item) }}" style="display:inline-block;" onsubmit="return confirm('Delete this unit?')">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="btn btn-danger">Delete</button>
                                        </form>
                                    @endif
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="3">No units found.</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            @if(method_exists($items, 'links'))
                <div style="margin-top:14px;">
                    {{ $items->links() }}
                </div>
            @endif
        </div>
    </div>
</div>
@endsection