<!DOCTYPE html>
<html lang="id">

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
  <title>Data Balita - NutriPosy</title>
  <link rel="icon" type="image/png" href="images/nutriposy.png">
  <link rel="stylesheet" href="{{ asset('css/bootstrap.css') }}" />
  <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700;900&display=swap" rel="stylesheet">
  <link href="{{ asset('css/font-awesome.min.css') }}" rel="stylesheet" />
  <link href="{{ asset('css/style.css') }}" rel="stylesheet" />
  <link href="{{ asset('css/responsive.css') }}" rel="stylesheet" />
</head>

<body>

  <div class="hero_area">
    <header class="header_section">
      <div class="header_bottom">
        <div class="container-fluid">
          <nav class="navbar navbar-expand-lg custom_nav-container">
            <a class="navbar-brand" href="#">
              <img src="images/nutriposy.png" alt="NutriPosy Logo">
            </a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
              aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
              <span class="navbar-toggler-icon"></span>
            </button>

            <div class="collapse navbar-collapse" id="navbarSupportedContent">
              <div class="d-flex mr-auto flex-column flex-lg-row align-items-center">
                <ul class="navbar-nav">
                  <li class="nav-item active">
                    <a class="nav-link" href="{{ route('dashboard') }}">Beranda</a>
                  </li>
                </ul>
              </div>
            </div>
          </nav>
        </div>
      </div>
    </header>

    <section class="book_section layout_padding">
      <div class="container">
        <h4>Data <span>Balita</span></h4>

        @if(session('success'))
        <div class="alert alert-success alert-dismissible fade show" role="alert">
          {{ session('success') }}
          <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        @endif
<!-- Form Pencarian Otomatis -->
<form action="{{ route('balita.index') }}" method="GET" class="mb-3" id="searchForm">
    <div class="input-group">
        <input type="text" name="search" class="form-control" placeholder="Cari berdasarkan Nama Anak atau NIK" value="{{ request('search') }}" id="searchInput" autocomplete="off">
    </div>
</form>




        <div class="table-responsive">
          <table class="table table-bordered table-striped">
            <thead class="thead-dark">
              <tr>
                <th>NIK</th>
                <th>Nama Anak</th>
                <th>Jenis Kelamin</th>
                <th>Umur</th>
                <th>Nama Ibu</th>
                <th>Aksi</th>
              </tr>
            </thead>
            <tbody>
              @foreach($balitas as $balita)
              <tr>
                <td>{{ $balita->nik }}</td>
                <td>{{ $balita->nama_anak }}</td>
                <td>{{ $balita->jenis_kelamin }}</td>
                <td>{{ number_format($balita->umur_bulan, 1) }} bulan</td>


                <td>{{ $balita->ibu_kandung }}</td>
                <td>
                  <button type="button" class="btn btn-info btn-sm" data-toggle="modal" data-target="#showModal{{ $balita->id }}" title="Lihat">
                    <i class="fa fa-eye"></i>
                  </button>
                  <button type="button" class="btn btn-warning btn-sm" data-toggle="modal" data-target="#editModal{{ $balita->id }}" title="Edit">
                    <i class="fa fa-edit"></i>
                  </button>
                  <button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#deleteModal{{ $balita->id }}" title="Hapus">
                    <i class="fa fa-trash"></i>
                  </button>
                </td>
              </tr>

              <!-- Show Modal -->
              <div class="modal fade" id="showModal{{ $balita->id }}" tabindex="-1" role="dialog" aria-labelledby="showModalLabel{{ $balita->id }}" aria-hidden="true">
                <div class="modal-dialog modal-lg" role="document">
                  <div class="modal-content">
                    <div class="modal-header">
                      <h5 class="modal-title" id="showModalLabel{{ $balita->id }}">Detail Balita</h5>
                      <button type="button" class="close" data-dismiss="modal" aria-label="Tutup">
                        <span aria-hidden="true">&times;</span>
                      </button>
                    </div>
                    <div class="modal-body">
                      <p><strong>NIK:</strong> {{ $balita->nik }}</p>
                      <p><strong>Nama Anak:</strong> {{ $balita->nama_anak }}</p>
                      <p><strong>Jenis Kelamin:</strong> {{ $balita->jenis_kelamin }}</p>
                      <p><strong>Umur:</strong> {{ number_format($balita->umur_bulan, 1) }} bulan</p>


                      <p><strong>Tempat Lahir:</strong> {{ $balita->tempat_lahir }}</p>
                      <p><strong>Nama Ibu:</strong> {{ $balita->ibu_kandung }}</p>
                      <p><strong>Nama Ayah:</strong> {{ $balita->nama_ayah }}</p>
                      <p><strong>Alamat:</strong> {{ $balita->alamat }}</p>
                    </div>
                    <div class="modal-footer">
                      <button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>
                    </div>
                  </div>
                </div>
              </div>

              <!-- Edit Modal -->
              <div class="modal fade" id="editModal{{ $balita->id }}" tabindex="-1" role="dialog" aria-labelledby="editModalLabel{{ $balita->id }}" aria-hidden="true">
                <div class="modal-dialog modal-lg" role="document">
                  <div class="modal-content">
                    <div class="modal-header">
                      <h5 class="modal-title" id="editModalLabel{{ $balita->id }}">Edit Balita</h5>
                      <button type="button" class="close" data-dismiss="modal" aria-label="Tutup">
                        <span aria-hidden="true">&times;</span>
                      </button>
                    </div>
                    <form action="{{ route('balita.update', $balita->id) }}" method="POST">
                      @csrf
                      @method('PUT')
                      <div class="modal-body">
                        <div class="form-group">
                          <label for="nik">NIK</label>
                          <input type="text" class="form-control" name="nik" value="{{ $balita->nik }}" required>
                        </div>
                        <div class="form-group">
                          <label for="nama_anak">Nama Anak</label>
                          <input type="text" class="form-control" name="nama_anak" value="{{ $balita->nama_anak }}" required>
                        </div>
                        <div class="form-group">
                          <label for="jenis_kelamin">Jenis Kelamin</label>
                          <select name="jenis_kelamin" class="form-control" required>
                            <option value="Laki-laki" {{ $balita->jenis_kelamin == 'Laki-laki' ? 'selected' : '' }}>Laki-laki</option>
                            <option value="Perempuan" {{ $balita->jenis_kelamin == 'Perempuan' ? 'selected' : '' }}>Perempuan</option>
                          </select>
                        </div>
                        <div class="form-group">
                          <label for="tanggal_lahir">Tanggal Lahir</label>
                          <input type="date" name="tanggal_lahir" class="form-control" value="{{ $balita->tanggal_lahir }}" required>
                        </div>
                       <!-- Tempat Lahir -->
<div class="form-group">
  <label for="tempat_lahir">Tempat Lahir</label>
  <input type="text" class="form-control" name="tempat_lahir" value="{{ $balita->tempat_lahir }}" required>
</div>
                        <!-- Nama Ibu Kandung -->
<div class="form-group">
  <label for="ibu_kandung">Nama Ibu</label>
  <input type="text" class="form-control" name="ibu_kandung" value="{{ $balita->ibu_kandung }}" required>
</div>

<!-- Nama Ayah -->
<div class="form-group">
  <label for="nama_ayah">Nama Ayah</label>
  <input type="text" class="form-control" name="nama_ayah" value="{{ $balita->nama_ayah }}" required>
</div>
                        <div class="form-group">
                          <label for="alamat">Alamat</label>
                          <textarea name="alamat" class="form-control" rows="3" required>{{ $balita->alamat }}</textarea>
                        </div>
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>
                        <button type="submit" class="btn btn-primary">Simpan Perubahan</button>
                      </div>
                    </form>
                  </div>
                </div>
              </div>

              <!-- Delete Modal -->
              <div class="modal fade" id="deleteModal{{ $balita->id }}" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel{{ $balita->id }}" aria-hidden="true">
                <div class="modal-dialog" role="document">
                  <div class="modal-content">
                    <form action="{{ route('balita.destroy', $balita->id) }}" method="POST">
                      @csrf
                      @method('DELETE')
                      <div class="modal-header">
                        <h5 class="modal-title" id="deleteModalLabel{{ $balita->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">
                        <p>Apakah Anda yakin ingin menghapus data balita dengan nama <strong>{{ $balita->nama_anak }}</strong>?</p>
                      </div>
                      <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Batal</button>
                        <button type="submit" class="btn btn-danger">Hapus</button>
                      </div>
                    </form>
                  </div>
                </div>
              </div>

              @endforeach
            </tbody>
          </table>
        </div>
      </div>
    </section>
     <!-- footer section -->
    <footer class="footer_section">
      <div class="container">
        <p>
          &copy; <span id="displayYear"></span> All Rights Reserved By
          <a href="#">NutriPosy</a>
        </p>
      </div>
    </footer>
  </div>

  <script>
    document.getElementById('searchInput').addEventListener('input', function () {
        clearTimeout(this.delayTimer);
        this.delayTimer = setTimeout(function () {
            document.getElementById('searchForm').submit();
        }, 500); // delay 500ms untuk menghindari submit terlalu sering
    });
</script>


  <script src="{{ asset('js/jquery-3.4.1.min.js') }}"></script>
  <script src="{{ asset('js/bootstrap.js') }}"></script>
</body>

</html>
