@extends('app')

@section('content')
<section class="book_section layout_padding">
<div class="container">
    <h4>Riwayat <span>Pemeriksaan</span></h4>

    <!-- Alert Success & Error -->
    @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
            {{ session('success') }}
            <button type="button" class="close" data-dismiss="alert">&times;</button>
        </div>
    @endif
    @if(session('error'))
        <div class="alert alert-danger">
            {{ session('error') }}
        </div>
    @endif
        <!-- Form Pencarian Otomatis -->
<form action="{{ route('riwayat.index') }}" method="GET" class="mb-3" id="searchForm">
    <div class="input-group">
        <input type="text" name="search" class="form-control" placeholder="Cari berdasarkan Nama" value="{{ request('search') }}" id="searchInput" autocomplete="off">
    </div>
</form>

    <!-- Table Section -->
    <div class="table-responsive">
        <table class="table table-bordered table-striped">
            <thead class="thead-dark">
                <tr>
                    <th>Nama</th>
                    <th>Umur</th>
                    <th>Berat</th>
                    <th>Tinggi</th>
                    <th>Hasil Analisis</th>
                    <th>Tanggal Pemeriksaan</th>
                    <th>Aksi</th>
                </tr>
            </thead>
            <tbody>
                @foreach($riwayatPemeriksaan as $pemeriksaan)
                    <tr>
                        <td>{{ $pemeriksaan->nama }}</td>
                        <td>{{ $pemeriksaan->umur }}</td>
                        <td>{{ $pemeriksaan->berat }} kg</td>
                        <td>{{ $pemeriksaan->tinggi }} cm</td>
                        <td>{{ $pemeriksaan->hasil_analisis }}</td>
                        <td>{{ $pemeriksaan->tanggal_pemeriksaan->format('d-m-Y') }}</td>
                        <td>
                            <!-- Edit Icon Bigger -->
                            <button type="button" class="btn btn-link p-0" style="font-size: 22px;" data-toggle="modal" data-target="#editModal{{ $pemeriksaan->id }}" title="Edit">
                                <i class="fa fa-edit text-primary"></i>
                            </button>

                            <!-- Delete Icon Bigger -->
                            <button type="button" class="btn btn-link p-0 ml-2" style="font-size: 22px;" data-toggle="modal" data-target="#deleteModal{{ $pemeriksaan->id }}" title="Hapus">
                                <i class="fa fa-trash text-danger"></i>
                            </button>
                        </td>
                    </tr>

                    <!-- Modal Edit -->
                    <div class="modal fade" id="editModal{{ $pemeriksaan->id }}" tabindex="-1" role="dialog" aria-labelledby="editModalLabel{{ $pemeriksaan->id }}" aria-hidden="true">
                        <div class="modal-dialog" role="document">
                            <form action="{{ route('riwayat.update', $pemeriksaan->id) }}" method="POST">
                                @csrf
                                @method('PUT')
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <h5 class="modal-title" id="editModalLabel{{ $pemeriksaan->id }}">Edit Pemeriksaan</h5>
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Tutup">
                                            <span aria-hidden="true">&times;</span>
                                        </button>
                                    </div>
                                    <div class="modal-body">
                                        <div class="form-group">
                                            <label>Nama</label>
                                            <input type="text" name="nama" value="{{ $pemeriksaan->nama }}" class="form-control" required>
                                        </div>
                                        <div class="form-group">
                                            <label>Umur</label>
                                            <input type="number" name="umur" value="{{ $pemeriksaan->umur }}" class="form-control" required>
                                        </div>
                                        <div class="form-group">
                                            <label>Berat (kg)</label>
                                            <input type="number" step="0.01" name="berat" value="{{ $pemeriksaan->berat }}" class="form-control" required>
                                        </div>
                                        <div class="form-group">
                                            <label>Tinggi (cm)</label>
                                            <input type="number" step="0.01" name="tinggi" value="{{ $pemeriksaan->tinggi }}" class="form-control" required>
                                        </div>
                                        <div class="form-group">
                                            <label>Hasil Analisis</label>
                                            <textarea name="hasil_analisis" class="form-control" required>{{ $pemeriksaan->hasil_analisis }}</textarea>
                                        </div>
                                        <div class="form-group">
                                            <label>Tanggal Pemeriksaan</label>
                                            <input type="date" name="tanggal_pemeriksaan" value="{{ $pemeriksaan->tanggal_pemeriksaan->format('Y-m-d') }}" class="form-control" required>
                                        </div>
                                    </div>
                                    <div class="modal-footer">
                                        <button type="submit" class="btn btn-success">Simpan</button>
                                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                    <!-- End Modal Edit -->

                    <!-- Modal Delete -->
                    <div class="modal fade" id="deleteModal{{ $pemeriksaan->id }}" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel{{ $pemeriksaan->id }}" aria-hidden="true">
                        <div class="modal-dialog" role="document">
                            <form action="{{ route('riwayat.destroy', $pemeriksaan->id) }}" method="POST">
                                @csrf
                                @method('DELETE')
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <h5 class="modal-title" id="deleteModalLabel{{ $pemeriksaan->id }}">Konfirmasi Hapus</h5>
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Tutup">
                                            <span aria-hidden="true">&times;</span>
                                        </button>
                                    </div>
                                    <div class="modal-body">
                                        Apakah Anda yakin ingin menghapus data pemeriksaan <strong>{{ $pemeriksaan->nama }}</strong>?
                                    </div>
                                    <div class="modal-footer">
                                        <button type="submit" class="btn btn-danger">Hapus</button>
                                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                    <!-- End Modal Delete -->
                @endforeach
            </tbody>
        </table>
    </div>
</div>
</section>
<script>
    document.getElementById('searchInput').addEventListener('input', function () {
        clearTimeout(this.delayTimer);
        this.delayTimer = setTimeout(function () {
            document.getElementById('searchForm').submit();
        }, 500); // delay 500ms untuk mencegah submit terlalu cepat
    });
</script>

@endsection
