@extends('layout.layout')

@php
    $title = 'Stok Meteran';
    $subTitle = 'Kelola Stok Meteran Air';
@endphp

@section('content')

<div class="grid grid-cols-12 gap-6">
    <div class="col-span-12 lg:col-span-4">
        <div class="card border-0 shadow-sm">
            <div class="card-header px-6 py-4 border-b">
                <h6 class="text-lg font-semibold">Tambah Stok Meteran</h6>
            </div>
            <div class="card-body p-6">
                <form action="{{ route('DataPetugas.stok.store') }}" method="POST">
                    @csrf
                    <div class="mb-4">
                        <label class="block font-semibold text-sm mb-2">Nomor Seri Meteran <span class="text-danger-600">*</span></label>
                        <input type="text" name="nomor_seri" class="form-control rounded-lg w-full" placeholder="Contoh: SN-2024-006" required>
                        @error('nomor_seri') <small class="text-danger-600">{{ $message }}</small> @enderror
                    </div>
                    <button type="submit" class="px-4 py-2 bg-primary-600 text-white rounded-lg hover:bg-primary-700 transition w-full">
                        <iconify-icon icon="mdi:plus"></iconify-icon> Tambah Meteran
                    </button>
                </form>
            </div>
        </div>
    </div>

    <div class="col-span-12 lg:col-span-8">
        <div class="card border-0 overflow-hidden">
            <div class="card-header flex justify-between items-center px-6 py-4 border-b">
                <h6 class="card-title mb-0 text-lg font-semibold">Daftar Stok Meteran</h6>
                <span class="bg-primary-100 text-primary-600 px-3 py-1 rounded-full text-sm font-medium">
                    {{ $meterans->total() }} Meteran
                </span>
            </div>

            <div class="card-body p-6">
                @if(session('success'))
                    <div class="alert alert-success mb-4">{{ session('success') }}</div>
                @endif
                @if(session('error'))
                    <div class="alert alert-error mb-4">{{ session('error') }}</div>
                @endif

                <div class="overflow-x-auto">
                    <table class="w-full border border-neutral-200 dark:border-neutral-600 rounded-lg">
                        <thead>
                            <tr class="bg-neutral-50 dark:bg-neutral-700">
                                <th class="px-4 py-3 text-left font-bold">No.</th>
                                <th class="px-4 py-3 text-left font-semibold">Nomor Seri</th>
                                <th class="px-4 py-3 text-left font-semibold">Status</th>
                                <th class="px-4 py-3 text-left font-semibold">Pelanggan</th>
                                <th class="px-4 py-3 text-left font-semibold">Tgl Pasang</th>
                                <th class="px-4 py-3 text-center font-semibold">Aksi</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($meterans as $index => $m)
                            <tr class="border-b border-neutral-200 hover:bg-neutral-50 transition">
                                <td class="px-4 py-3 text-sm">{{ $meterans->firstItem() + $index }}</td>
                                <td class="px-4 py-3 font-medium">{{ $m->nomor_seri }}</td>
                                <td class="px-4 py-3">
                                    @if($m->user_id)
                                        <span class="bg-success-100 text-success-600 px-2 py-1 rounded-full text-xs font-medium">Terpasang</span>
                                    @else
                                        <span class="bg-warning-100 text-warning-600 px-2 py-1 rounded-full text-xs font-medium">Tersedia</span>
                                    @endif
                                </td>
                                <td class="px-4 py-3 text-sm">{{ $m->user->name ?? '-' }}</td>
                                <td class="px-4 py-3 text-sm">{{ $m->tanggal_pasang?->format('d M Y') ?? '-' }}</td>
                                <td class="px-4 py-3 text-center">
                                    @if(!$m->user_id)
                                        <form method="POST" action="{{ route('DataPetugas.stok.destroy', $m->id) }}" onsubmit="return confirm('Hapus meteran ini?')">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="w-8 h-8 bg-danger-50 text-danger-600 rounded-lg inline-flex items-center justify-center hover:bg-danger-600 hover:text-white transition">
                                                <iconify-icon icon="mdi:trash"></iconify-icon>
                                            </button>
                                        </form>
                                    @else
                                        <span class="text-xs text-neutral-400">-</span>
                                    @endif
                                </td>
                            </tr>
                            @empty
                            <tr>
                                <td colspan="6" class="text-center py-16 text-neutral-500">
                                    <p class="font-semibold">Belum ada stok meteran</p>
                                </td>
                            </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
                <div class="mt-6">{{ $meterans->links() }}</div>
            </div>
        </div>
    </div>
</div>

@endsection