import { a7 as head, a5 as ensure_array_like, a6 as attr, a8 as stringify, a3 as attr_class, a4 as escape_html, Z as derived } from '../../../chunks/index.js-BVnekaYQ.js';
import { p as page } from '../../../chunks/index2.js-CfhO9oMK.js';
import { a as formatDateTime, f as formatDate, i as isOverdue } from '../../../chunks/format.js-B0aMHEZW.js';
import { r as roleAtLeast } from '../../../chunks/roles.js-aWc1U_OX.js';
import '../../../chunks/utils.js-mIi_c4Oz.js';
import '../../../chunks/utils2.js-BQzn9ikS.js';
import '../../../chunks/state.svelte.js-DC2zSH9Y.js';
import '../../../chunks/exports.js-8HOoaa4e.js';
import '../../../chunks/root.js-l4cSj5gU.js';
import '../../../chunks/false.js-B2gHlHjM.js';
import '../../../chunks/client.js-CmSsxc12.js';
import '../../../chunks/index3.js-C9fZseUW.js';

function _page($$renderer, $$props) {
  $$renderer.component(($$renderer2) => {
    let { data } = $$props;
    const canManage = derived(() => roleAtLeast(page.data.user?.role, "gestor"));
    const tabs = derived(() => [
      {
        key: "active",
        label: "Activos",
        count: data.counts.active ?? 0
      },
      {
        key: "overdue",
        label: "Vencidos",
        count: data.counts.overdue ?? 0
      },
      {
        key: "all",
        label: "Historial",
        count: data.counts.all_count ?? 0
      }
    ]);
    head("1jj77xg", $$renderer2, ($$renderer3) => {
      $$renderer3.title(($$renderer4) => {
        $$renderer4.push(`<title>Préstamos · Inventario EDUmind</title>`);
      });
    });
    $$renderer2.push(`<div class="mb-6"><h1 class="text-2xl font-bold text-slate-900">Préstamos</h1> <p class="text-slate-600">Trazabilidad de entradas y salidas de equipos.</p></div> <div class="mb-4 flex flex-wrap gap-2" role="tablist"><!--[-->`);
    const each_array = ensure_array_like(tabs());
    for (let $$index = 0, $$length = each_array.length; $$index < $$length; $$index++) {
      let t = each_array[$$index];
      $$renderer2.push(`<a${attr("href", `/loans?filter=${stringify(t.key)}`)} role="tab"${attr("aria-selected", data.filter === t.key)}${attr_class(`rounded-lg px-4 py-2 text-sm font-semibold transition-colors ${data.filter === t.key ? "bg-brand-700 text-white" : "bg-white text-slate-700 ring-1 ring-slate-200 hover:bg-slate-50"}`)}>${escape_html(t.label)} <span${attr_class(`ml-1 rounded-full px-1.5 text-xs ${data.filter === t.key ? "bg-white/20" : t.key === "overdue" && t.count > 0 ? "bg-red-100 text-red-700" : "bg-slate-100 text-slate-600"}`)}>${escape_html(t.count)}</span></a>`);
    }
    $$renderer2.push(`<!--]--></div> <div class="card overflow-hidden">`);
    if (data.loans.length === 0) {
      $$renderer2.push("<!--[0-->");
      $$renderer2.push(`<p class="px-5 py-16 text-center text-slate-500">${escape_html(data.filter === "overdue" ? "No hay préstamos vencidos. 👍" : data.filter === "active" ? "No hay préstamos activos." : "Todavía no hay movimientos.")}</p>`);
    } else {
      $$renderer2.push("<!--[-1-->");
      $$renderer2.push(`<div class="overflow-x-auto"><table class="w-full text-left text-sm"><thead class="border-b border-slate-200 bg-slate-50 text-xs uppercase text-slate-500"><tr><th class="px-4 py-3 font-semibold">Equipo</th><th class="px-4 py-3 font-semibold">Usuario</th><th class="px-4 py-3 font-semibold">Salida</th><th class="px-4 py-3 font-semibold">Prevista</th><th class="px-4 py-3 font-semibold">Estado</th>`);
      if (canManage()) {
        $$renderer2.push("<!--[0-->");
        $$renderer2.push(`<th class="px-4 py-3 font-semibold">Actas</th>`);
      } else {
        $$renderer2.push("<!--[-1-->");
      }
      $$renderer2.push(`<!--]--></tr></thead><tbody class="divide-y divide-slate-100"><!--[-->`);
      const each_array_1 = ensure_array_like(data.loans);
      for (let $$index_1 = 0, $$length = each_array_1.length; $$index_1 < $$length; $$index_1++) {
        let l = each_array_1[$$index_1];
        $$renderer2.push(`<tr class="hover:bg-slate-50"><td class="px-4 py-3"><a${attr("href", `/devices/${stringify(l.device_id)}`)} class="font-mono font-semibold text-brand-700 hover:underline">${escape_html(l.device_code)}</a> <span class="block text-xs text-slate-400">${escape_html(l.device_name || l.category_name || "")}</span></td><td class="px-4 py-3 font-medium text-slate-900">${escape_html(l.borrower_name || "—")}</td><td class="px-4 py-3 text-slate-600">${escape_html(formatDateTime(l.checkout_at))}</td><td class="px-4 py-3 text-slate-600">${escape_html(l.due_at ? formatDate(l.due_at) : "—")}</td><td class="px-4 py-3">`);
        if (l.returned_at) {
          $$renderer2.push("<!--[0-->");
          $$renderer2.push(`<span class="badge bg-emerald-100 text-emerald-800">Devuelto ${escape_html(formatDate(l.returned_at))}</span>`);
        } else if (isOverdue(l.due_at, l.returned_at)) {
          $$renderer2.push("<!--[1-->");
          $$renderer2.push(`<span class="badge bg-red-100 text-red-700">Vencido</span>`);
        } else {
          $$renderer2.push("<!--[-1-->");
          $$renderer2.push(`<span class="badge bg-amber-100 text-amber-800">En préstamo</span>`);
        }
        $$renderer2.push(`<!--]--></td>`);
        if (canManage()) {
          $$renderer2.push("<!--[0-->");
          $$renderer2.push(`<td class="px-4 py-3"><div class="flex gap-2 text-xs"><a class="font-medium text-brand-700 hover:underline"${attr("href", `/loans/${stringify(l.id)}/document?type=entrega`)} target="_blank">Entrega</a> <a class="font-medium text-brand-700 hover:underline"${attr("href", `/loans/${stringify(l.id)}/document?type=recogida`)} target="_blank">Recogida</a></div></td>`);
        } else {
          $$renderer2.push("<!--[-1-->");
        }
        $$renderer2.push(`<!--]--></tr>`);
      }
      $$renderer2.push(`<!--]--></tbody></table></div>`);
    }
    $$renderer2.push(`<!--]--></div>`);
  });
}

export { _page as default };
//# sourceMappingURL=_page.svelte.js-B8HO-XWy.js.map
