netgescon-day0/resources/views/condomino/letture-acqua/index.blade.php

184 lines
10 KiB
PHP

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>Letture Acqua - NetGescon</title>
@vite(['resources/css/app.css', 'resources/js/app.js'])
</head>
<body class="min-h-screen bg-slate-100 text-slate-900">
<div class="max-w-5xl mx-auto p-4 md:p-8 space-y-4">
<div class="flex items-center justify-between gap-3">
<div>
<h1 class="text-xl md:text-2xl font-semibold">Autoletture acqua</h1>
<p class="text-sm text-slate-600">Inserisci il totale letto e allega una foto del contatore. Il sistema calcola il consumo rispetto alla lettura precedente.</p>
</div>
<a href="{{ route('condomino.tickets.index') }}" class="text-sm text-blue-700 hover:underline">Vai ai ticket</a>
</div>
@if (session('success'))
<div class="rounded-lg border border-emerald-300 bg-emerald-50 p-3 text-emerald-700 text-sm">{{ session('success') }}</div>
@endif
@if ($errors->any())
<div class="rounded-lg border border-red-300 bg-red-50 p-3 text-red-700 text-sm">
<ul class="list-disc pl-5">
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form method="POST" action="{{ route('condomino.letture-acqua.store') }}" enctype="multipart/form-data" class="rounded-lg bg-white p-4 md:p-6 shadow space-y-4">
@csrf
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">
<div>
<label class="block text-sm font-medium" for="unita_immobiliare_id">Unità immobiliare</label>
<select id="unita_immobiliare_id" name="unita_immobiliare_id" class="mt-1 block w-full rounded-md border-slate-300" required>
<option value="">Seleziona unità</option>
@foreach($unitaImmobiliari as $unita)
@php $uid = (int) ($unita->id ?? 0); @endphp
<option value="{{ $uid }}" {{ (int) old('unita_immobiliare_id') === $uid ? 'selected' : '' }}>
{{ $unita->stabile->denominazione ?? 'Stabile' }} - {{ $unita->scala ?? 'S?' }}/{{ $unita->interno ?? 'I?' }}
</option>
@endforeach
</select>
</div>
<div>
<label class="block text-sm font-medium" for="stabile_servizio_id">Utenza acqua</label>
<select id="stabile_servizio_id" name="stabile_servizio_id" class="mt-1 block w-full rounded-md border-slate-300" required>
<option value="">Seleziona utenza</option>
@foreach($serviziByUnita as $unitaId => $servizi)
@foreach($servizi as $servizio)
<option value="{{ (int) $servizio->id }}" data-unita-id="{{ (int) $unitaId }}" {{ (int) old('stabile_servizio_id') === (int) $servizio->id ? 'selected' : '' }}>
{{ $servizio->nome ?: ('Utenza #' . (int) $servizio->id) }}
@if($servizio->contatore_matricola)
- Matricola {{ $servizio->contatore_matricola }}
@endif
</option>
@endforeach
@endforeach
</select>
<p class="mt-1 text-xs text-slate-500">Mostreremo solo le utenze dello stabile selezionato.</p>
</div>
<div>
<label class="block text-sm font-medium" for="totale_letto">Totale letto contatore</label>
<input id="totale_letto" name="totale_letto" type="number" step="0.001" min="0" value="{{ old('totale_letto') }}" class="mt-1 block w-full rounded-md border-slate-300" required>
</div>
<div>
<label class="block text-sm font-medium" for="data_lettura">Data lettura</label>
<input id="data_lettura" name="data_lettura" type="date" value="{{ old('data_lettura', now()->toDateString()) }}" class="mt-1 block w-full rounded-md border-slate-300">
</div>
</div>
<div>
<label class="block text-sm font-medium" for="foto">Foto contatore (opzionale)</label>
<input id="foto" name="foto" type="file" accept="image/*" capture="environment" class="mt-1 block w-full rounded-md border-slate-300">
</div>
<div>
<label class="block text-sm font-medium" for="note">Note</label>
<textarea id="note" name="note" rows="2" class="mt-1 block w-full rounded-md border-slate-300">{{ old('note') }}</textarea>
</div>
<button type="submit" class="inline-flex items-center rounded-lg bg-blue-600 px-4 py-2 text-white text-sm font-medium">Salva autolettura</button>
</form>
<div class="rounded-lg bg-white p-4 md:p-6 shadow">
<h2 class="font-medium mb-3">Storico letture</h2>
<form method="GET" class="mb-3 flex gap-2">
<select name="unita_immobiliare_id" class="rounded-md border-slate-300 text-sm">
<option value="">Tutte le unità</option>
@foreach($unitaImmobiliari as $unita)
@php $uid = (int) ($unita->id ?? 0); @endphp
<option value="{{ $uid }}" {{ (int) request('unita_immobiliare_id') === $uid ? 'selected' : '' }}>
{{ $unita->scala ?? 'S?' }}/{{ $unita->interno ?? 'I?' }}
</option>
@endforeach
</select>
<button class="rounded-md border border-slate-300 px-3 py-1.5 text-sm" type="submit">Filtra</button>
</form>
@if($letture->isEmpty())
<p class="text-sm text-slate-500">Nessuna lettura disponibile.</p>
@else
<div class="overflow-x-auto">
<table class="min-w-full text-sm">
<thead>
<tr class="text-left border-b">
<th class="py-2 pr-3">Data</th>
<th class="py-2 pr-3">Unità</th>
<th class="py-2 pr-3">Utenza</th>
<th class="py-2 pr-3">Precedente</th>
<th class="py-2 pr-3">Totale letto</th>
<th class="py-2 pr-3">Consumo</th>
<th class="py-2 pr-3">Foto</th>
</tr>
</thead>
<tbody>
@foreach($letture as $lettura)
@php
$meta = is_array($lettura->raw) ? $lettura->raw : [];
$foto = $meta['foto_path'] ?? null;
@endphp
<tr class="border-b last:border-0">
<td class="py-2 pr-3">{{ optional($lettura->periodo_al)->format('d/m/Y') }}</td>
<td class="py-2 pr-3">{{ $lettura->unitaImmobiliare->scala ?? 'S?' }}/{{ $lettura->unitaImmobiliare->interno ?? 'I?' }}</td>
<td class="py-2 pr-3">{{ $lettura->servizio->nome ?? ('#' . (int) $lettura->stabile_servizio_id) }}</td>
<td class="py-2 pr-3">{{ $lettura->lettura_inizio !== null ? number_format((float) $lettura->lettura_inizio, 3, ',', '.') : '—' }}</td>
<td class="py-2 pr-3 font-medium">{{ $lettura->lettura_fine !== null ? number_format((float) $lettura->lettura_fine, 3, ',', '.') : '—' }}</td>
<td class="py-2 pr-3">{{ $lettura->consumo_valore !== null ? number_format((float) $lettura->consumo_valore, 3, ',', '.') . ' mc' : '—' }}</td>
<td class="py-2 pr-3">
@if($foto)
<a class="text-blue-700 hover:underline" href="{{ asset('storage/' . ltrim($foto, '/')) }}" target="_blank">Apri foto</a>
@else
@endif
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div class="mt-3">{{ $letture->links() }}</div>
@endif
</div>
</div>
<script>
(function () {
const unita = document.getElementById('unita_immobiliare_id');
const servizio = document.getElementById('stabile_servizio_id');
if (!unita || !servizio) return;
const applyFilter = () => {
const selectedUnita = String(unita.value || '');
Array.from(servizio.options).forEach((opt, index) => {
if (index === 0) {
opt.hidden = false;
return;
}
const match = (opt.dataset.unitaId || '') === selectedUnita;
opt.hidden = selectedUnita !== '' && !match;
});
const current = servizio.options[servizio.selectedIndex];
if (current && current.hidden) {
servizio.value = '';
}
};
unita.addEventListener('change', applyFilter);
applyFilter();
})();
</script>
</body>
</html>