@extends('layouts.app', ['title' => 'Form Inventaris', 'heading' => $item->exists ? 'Edit Barang' : 'Tambah Barang'])

@section('content')
<div class="row">
    <div class="col-12">
        <div class="card mb-4">
            <h5 class="card-header">{{ $item->exists ? 'Edit Barang' : 'Tambah Barang' }}</h5>
            <div class="card-body">
                <form method="POST" action="{{ $action }}" class="row g-3">
                    @csrf
                    @if ($method !== 'POST')
                        @method($method)
                    @endif

                    <div class="col-md-6">
                        <label class="form-label">Lokasi</label>
                        <select name="location_id" class="form-select" required>
                            <option value="">Pilih lokasi</option>
                            @foreach ($locations as $location)
                                <option value="{{ $location->id }}" @selected(old('location_id', $item->location_id) == $location->id)>{{ $location->name }}</option>
                            @endforeach
                        </select>
                    </div>

                    <div class="col-md-6">
                        <label class="form-label">Nama barang</label>
                        <input type="text" name="name" class="form-control" value="{{ old('name', $item->name) }}" required>
                    </div>

                    <div class="col-md-6">
                        <label class="form-label">Kategori</label>
                        <input list="category-list" name="category_name" class="form-control" value="{{ old('category_name', $item->category?->name) }}" placeholder="Contoh: Elektronik">
                        <datalist id="category-list">
                            @foreach ($categories as $category)
                                <option value="{{ $category->name }}"></option>
                            @endforeach
                        </datalist>
                    </div>

                    <div class="col-md-6">
                        <label class="form-label">Tracking mode</label>
                        <select name="tracking_mode" class="form-select" required>
                            <option value="stock" @selected(old('tracking_mode', $item->tracking_mode) === 'stock')>Stock</option>
                            <option value="unit" @selected(old('tracking_mode', $item->tracking_mode) === 'unit')>Unit</option>
                        </select>
                    </div>

                    <div class="col-md-6">
                        <label class="form-label">Satuan</label>
                        <input type="text" name="unit_of_measure" class="form-control" value="{{ old('unit_of_measure', $item->unit_of_measure ?: 'unit') }}" required>
                    </div>

                    <div class="col-md-6">
                        <label class="form-label">Total jumlah</label>
                        <input type="number" name="total_quantity" class="form-control" min="0" value="{{ old('total_quantity', $item->total_quantity) }}" required>
                    </div>

                    <div class="col-md-3">
                        <label class="form-label text-success">Kondisi Baik</label>
                        <input type="number" name="good_count" class="form-control" min="0" value="{{ old('good_count', $item->good_count) }}" required>
                    </div>

                    <div class="col-md-3">
                        <label class="form-label text-warning">Rusak Ringan</label>
                        <input type="number" name="minor_damage_count" class="form-control" min="0" value="{{ old('minor_damage_count', $item->minor_damage_count) }}" required>
                    </div>

                    <div class="col-md-3">
                        <label class="form-label text-danger">Rusak Berat</label>
                        <input type="number" name="major_damage_count" class="form-control" min="0" value="{{ old('major_damage_count', $item->major_damage_count) }}" required>
                    </div>

                    <div class="col-md-3">
                        <label class="form-label text-secondary">Hilang</label>
                        <input type="number" name="lost_count" class="form-control" min="0" value="{{ old('lost_count', $item->lost_count) }}" required>
                    </div>

                    <div class="col-md-6">
                        <label class="form-label">Tanggal pengecekan</label>
                        <input type="datetime-local" name="last_checked_at" class="form-control" value="{{ old('last_checked_at', $item->last_checked_at && $item->last_checked_at->format('H:i') !== '00:00' ? $item->last_checked_at->format('Y-m-d\TH:i') : now()->format('Y-m-d\TH:i')) }}">
                    </div>

                    <div class="col-12">
                        <label class="form-label">Catatan</label>
                        <textarea name="notes" class="form-control" rows="4">{{ old('notes', $item->notes) }}</textarea>
                    </div>

                    <div class="col-12">
                        <div class="form-check">
                            <input type="checkbox" class="form-check-input" name="is_active" id="is_active" value="1" @checked(old('is_active', $item->is_active ?? true))>
                            <label class="form-check-label" for="is_active">Barang aktif</label>
                        </div>
                    </div>

                    <div class="col-12 text-end mt-4">
                        <a href="{{ route('items.index') }}" class="btn btn-label-secondary me-2">Kembali</a>
                        <button type="submit" class="btn btn-primary">Simpan Barang</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection
