@php
  $item = $item ?? null;
  $photoInputId = 'item-photo-upload-' . ($item?->id ?? 'new');
@endphp

<form action="{{ $action }}" method="POST" enctype="multipart/form-data">
  @csrf
  @isset($method)
    @method($method)
  @endisset

  <div class="card-body">
    <div class="row g-3">
      <div class="col-md-6">
        <label class="form-label">Nama Barang</label>
        <input class="form-control" name="name" value="{{ old('name', $item?->nama_barang) }}" placeholder="Printer Laser A4" required>
      </div>
      <div class="col-md-6">
        <label class="form-label">Kode Aset</label>
        <input class="form-control" name="asset_code" value="{{ old('asset_code', $item?->kode_aset) }}" placeholder="INV-ELK-00001" required>
      </div>
      <div class="col-md-6">
        <label class="form-label">Kategori</label>
        <select class="form-select" name="category_id" required>
          @foreach ($categories as $category)
            <option value="{{ $category->id }}" @selected(old('category_id', $item?->category_id) == $category->id)>{{ $category->nama_kategori }}</option>
          @endforeach
        </select>
      </div>
      <div class="col-md-6">
        <label class="form-label">Ruangan</label>
        <select class="form-select" name="room_id" required>
          @foreach ($rooms as $room)
            <option value="{{ $room->id }}" @selected(old('room_id', $item?->room_id) == $room->id)>{{ $room->nama_ruangan }}</option>
          @endforeach
        </select>
      </div>
      <div class="col-md-3">
        <label class="form-label">Tahun Perolehan</label>
        <input class="form-control" type="number" name="acquisition_year" value="{{ old('acquisition_year', $item?->tahun_perolehan ?? date('Y')) }}" min="1980" required>
      </div>
      <div class="col-md-3">
        <label class="form-label">Umur Pakai</label>
        <input class="form-control" type="number" name="useful_life_years" value="{{ old('useful_life_years', $item?->umur_manfaat_tahun ?? 5) }}" min="1" required>
      </div>
      <div class="col-md-3">
        <label class="form-label">Status</label>
        <select class="form-select" name="status" required>
          @foreach ($statuses as $value => $label)
            <option value="{{ $value }}" @selected(old('status', $item?->status_barang) === $value)>{{ $label }}</option>
          @endforeach
        </select>
      </div>
      <div class="col-md-3">
        <label class="form-label">Jadwal Pemeliharaan</label>
        <input class="form-control" type="date" name="maintenance_due_date" value="{{ old('maintenance_due_date', $item?->tanggal_jadwal_pemeliharaan?->format('Y-m-d')) }}">
      </div>
      <div class="col-md-6 item-upload-column">
        <label class="form-label" for="{{ $photoInputId }}">Foto Barang</label>
        <label class="item-upload-dropzone" data-item-upload-zone for="{{ $photoInputId }}">
          <input
            id="{{ $photoInputId }}"
            class="item-upload-input"
            type="file"
            name="photo"
            accept="image/*"
            data-item-upload-input
          >
          <span class="item-upload-icon" aria-hidden="true">
            <i class="ti ti-file-upload"></i>
          </span>
          <span class="item-upload-title">Drag and drop foto barang</span>
          <span class="item-upload-format">File format: JPG / PNG / WebP</span>
          <span class="item-upload-button" data-item-upload-button>Browse</span>
          <span class="item-upload-size">Maximum size: 4MB</span>
          <span class="item-upload-name" data-item-upload-name>Belum ada file dipilih</span>
        </label>
      </div>
      @if ($item?->photo_url)
        <div class="col-md-6">
          <label class="form-label">Foto Saat Ini</label>
          <div class="item-photo-preview">
            <img src="{{ $item->photo_url }}" alt="Foto {{ $item->nama_barang }}">
          </div>
        </div>
      @endif
      <div class="col-md-4">
        <label class="form-check mt-2">
          <input class="form-check-input" type="checkbox" name="replacement_requested" value="1" @checked(old('replacement_requested', $item?->pengajuan_penggantian))>
          <span class="form-check-label">Ajukan penggantian</span>
        </label>
      </div>
      <div class="col-12">
        <label class="form-label">Catatan</label>
        <textarea class="form-control" name="notes" rows="4" placeholder="Catatan kondisi atau lokasi barang">{{ old('notes', $item?->catatan) }}</textarea>
      </div>
    </div>
  </div>

  <div class="card-footer text-end">
    <button class="btn btn-primary" type="submit">
      <i class="ti ti-device-floppy me-1"></i>
      {{ $submitLabel }}
    </button>
  </div>
</form>
