<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <title>Registros | Weight Tracker</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link th:href="@{/vendor/fontawesome-free/css/all.min.css}" rel="stylesheet">
  <link th:href="@{/css/sb-admin-2.min.css}" rel="stylesheet">
  <link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/jquery.dataTables.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />
  <link rel="icon" href="data:,">
</head>
<body id="page-top">
<div id="wrapper">
  <div th:replace="~{fragments/fragment-menu :: menu}"></div>
  <div id="content-wrapper" class="d-flex flex-column">
    <div id="content" class="p-4">
      <nav class="navbar navbar-expand navbar-light bg-white topbar mb-4 static-top shadow">
        <button id="sidebarToggleTop" class="btn btn-link d-md-none rounded-circle mr-3">
          <i class="fa fa-bars"></i>
        </button>
        <h1 class="h4 mb-0 text-gray-800">Registros</h1>
      </nav>

      <div class="container-fluid">
        <a class="btn btn-primary mb-3" th:href="@{/registros/nuevo}">Nuevo</a>

        <div class="mb-2">
          <label>Filtrar por fecha:</label>
          <input type="text" id="dateRange" class="form-control form-control-sm w-auto d-inline-block">
        </div>

        <div class="table-responsive">
          <table id="tablaRegistros" class="table table-bordered table-striped align-middle">
            <thead class="table-light">
              <tr>
                <th>Fecha</th>
                <th>Peso (kg)</th>
                <th>% Grasa</th>
                <th>Masa muscular (kg)</th>
                <th>% Agua</th>
                <th>Acciones</th>
              </tr>
            </thead>
            <tbody>
              <tr th:each="r : ${registros}">
                <td th:text="${#temporals.format(r.date, 'yyyy-MM-dd HH:mm')}">2025-07-10 18:00</td>
                <td th:text="${r.weight}">70.5</td>
                <td th:text="${r.bodyFat}">15.0</td>
                <td th:text="${r.muscleMass}">55.0</td>
                <td th:text="${r.waterPercent}">60.0</td>
                <td>
                  <a class="btn btn-sm btn-outline-primary" th:href="@{'/registros/' + ${r.id} + '/editar'}">Editar</a>
                  <form th:action="@{'/registros/' + ${r.id} + '/eliminar'}" method="post" class="d-inline">
                    <input type="hidden" name="_method" value="delete" />
                    <button type="submit" class="btn btn-sm btn-outline-danger" onclick="return confirm('¿Eliminar registro?')">Eliminar</button>
                  </form>
                </td>
              </tr>
            </tbody>
          </table>
        </div>
      </div>
    </div>
  </div>
</div>

<!-- Scripts -->
<script th:src="@{/vendor/jquery/jquery.min.js}"></script>
<script th:src="@{/vendor/bootstrap/js/bootstrap.bundle.min.js}"></script>
<script th:src="@{/js/sb-admin-2.min.js}"></script>

<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>

<script th:inline="none">
  $(document).ready(function () {
    const table = $('#tablaRegistros').DataTable({
      order: [[0, 'desc']],
      pageLength: 10,
      searching: false,
      lengthMenu: [5, 10, 25, 50, 100],
      language: {
        url: 'https://cdn.datatables.net/plug-ins/2.3.2/i18n/es-ES.json',
      }
    });

    // Rango de fechas
    let startDate, endDate;
    $('#dateRange').daterangepicker({
      autoUpdateInput: false,
      locale: {
        cancelLabel: 'Limpiar',
        applyLabel: 'Aplicar',
        format: 'YYYY-MM-DD'
      }
    });

    $('#dateRange').on('apply.daterangepicker', function (ev, picker) {
      startDate = picker.startDate;
      endDate = picker.endDate;
      $(this).val(startDate.format('YYYY-MM-DD') + ' - ' + endDate.format('YYYY-MM-DD'));
      table.draw();
    });

    $('#dateRange').on('cancel.daterangepicker', function (ev, picker) {
      $(this).val('');
      startDate = endDate = null;
      table.draw();
    });

    $.fn.dataTable.ext.search.push(function (settings, data, dataIndex) {
      const fecha = moment(data[0], 'YYYY-MM-DD HH:mm');
      if (!startDate || !endDate) return true;
      return fecha.isBetween(startDate, endDate, 'day', '[]');
    });
  });
</script>

</body>
</html>
