<template>
  <div class="layout-px-spacing">
    <teleport to="#breadcrumb">
      <ul class="navbar-nav flex-row">
        <li>
          <div class="page-header">
            <nav class="breadcrumb-one" aria-label="breadcrumb">
              <ol class="breadcrumb">
                <li class="breadcrumb-item">
                  <a href="javascript:;">{{ $t("lavanderia.operador.breadcrumb_parent") }}</a>
                </li>
                <li class="breadcrumb-item active" aria-current="page">
                  <span>{{
                    $t("lavanderia.operador.breadcrumb_current", {
                      area: etiquetaEtapa(areaActual),
                    })
                  }}</span>
                </li>
              </ol>
            </nav>
          </div>
        </li>
      </ul>
    </teleport>

    <div class="row layout-top-spacing">
      <div class="col-xl-12 col-lg-12 col-sm-12 layout-spacing">
        <div class="panel p-3 text-center">
          <h4 class="mb-3">
            {{ $t("lavanderia.operador.heading", { area: etiquetaEtapa(areaActual) }) }}
          </h4>
          <p class="text-muted mb-4">
            {{ $t("lavanderia.operador.intro") }}
          </p>

          <div class="btn-group mb-3" role="group">
            <button
              type="button"
              class="btn"
              :class="areaActual === 'lavado' ? 'btn-primary' : 'btn-outline-primary'"
              @click="cambiarArea('lavado')"
            >
              {{ $t("lavanderia.operador.btn_lavado") }}
            </button>
            <button
              type="button"
              class="btn"
              :class="areaActual === 'secado' ? 'btn-primary' : 'btn-outline-primary'"
              @click="cambiarArea('secado')"
            >
              {{ $t("lavanderia.operador.btn_secado") }}
            </button>
            <button
              type="button"
              class="btn"
              :class="areaActual === 'planchado' ? 'btn-primary' : 'btn-outline-primary'"
              @click="cambiarArea('planchado')"
            >
              {{ $t("lavanderia.operador.btn_planchado") }}
            </button>
          </div>

          <div class="row g-3">
            <div
              v-for="orden in ordenes"
              :key="orden.id"
              class="col-xl-3 col-lg-4 col-md-6 col-sm-12"
            >
              <div
                class="btn btn-lg w-100 operador-btn text-start"
                :class="{
                  'btn-outline-success': orden.minutos_en_etapa <= 30,
                  'btn-outline-danger': orden.minutos_en_etapa > 30,
                }"
              >
                <div class="fw-bold">{{ orden.folio_unico }}</div>
                <div class="small">{{ orden.cliente_nombre }}</div>
                <div class="small text-muted">
                  {{ orden.minutos_en_etapa }} min en etapa
                </div>
                <div
                  v-if="orden.instrucciones"
                  class="small text-warning mt-1 text-truncate"
                >
                  {{ orden.instrucciones }}
                </div>
              </div>
            </div>

            <div v-if="!ordenes.length && !isLoading" class="col-12">
              <div class="alert alert-info mb-0">
                {{ $t("lavanderia.operador.empty_area") }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <loading
      v-model:active="isLoading"
      :can-cancel="false"
      :is-full-page="true"
    />
  </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import { useI18n } from "vue-i18n";
import { useRoute, useRouter } from "vue-router";
import Loading from "vue-loading-overlay";
import "vue-loading-overlay/dist/css/index.css";
import { useMeta } from "@/composables/use-meta";
import { useOrderStepLabel } from "@/composables/use-order-step-label";
import * as OrdenLavanderiaRepository from "@/repositories/OrdenLavanderiaRepository";

const { t } = useI18n();
const { etiquetaEtapa } = useOrderStepLabel();
useMeta({ title: t("lavanderia.operador.page_title") });

const route = useRoute();
const router = useRouter();

const areaActual = ref(route.params.area || "lavado");
const ordenes = ref([]);
const isLoading = ref(false);

const showMessage = (msg = "", type = "success") => {
  const toast = window.Swal.mixin({
    toast: true,
    position: "top-end",
    showConfirmButton: false,
    timer: 2500,
    timerProgressBar: true,
  });

  toast.fire({
    icon: type,
    title: msg,
  });
};

const cargarDatos = async () => {
  isLoading.value = true;
  try {
    // Usamos el dashboard resumen y filtramos por etapa actual
    const response = await OrdenLavanderiaRepository.getDashboardResumen();
    const data = response.data || [];
    ordenes.value = data.filter(
      (o) => o.current_step === areaActual.value
    );
  } catch (error) {
    console.error(error);
    showMessage("Error al cargar órdenes", "error");
  } finally {
    isLoading.value = false;
  }
};

const cambiarArea = async (area) => {
  areaActual.value = area;
  await router.replace({ name: "lavanderia-operador-router", params: { area } });
  await cargarDatos();
};

onMounted(async () => {
  await cargarDatos();
});
</script>

<style scoped>
.operador-btn {
  padding: 1.5rem 1rem;
  font-size: 1.1rem;
  border-width: 2px;
}
</style>

