<template>
  <q-dialog ref="dialogRef" @hide="onDialogHide">
    <q-card style="width: 800px; max-width: 95vw; border-radius: 12px">
      <q-bar class="bg-transparent q-px-md" style="height: 55px">
        <span class="text-h6 text-dark" style="font-weight: 600"
          >Frequência Média</span
        >
        <q-space />
        <q-btn dense flat icon="mdi-close" @click="onDialogCancel" />
      </q-bar>

      <q-card-section class="q-pt-none q-pb-md q-px-md">
        <q-card flat bordered style="border-radius: 8px">
          <q-card-section class="q-pb-sm">
            <q-input
              v-model="search"
              dense
              borderless
              placeholder="Busque por unidade, status"
            >
              <template #prepend>
                <q-icon name="mdi-magnify" color="grey-6" />
              </template>
            </q-input>
          </q-card-section>

          <q-separator />

          <div class="list-header q-px-md q-py-xs">
            <span class="text-caption text-grey-7">Unidade</span>
            <span class="text-caption text-grey-7">Frequência média</span>
            <span class="text-caption text-grey-7">Status</span>
          </div>

          <q-separator />

          <div>
            <template
              v-for="(item, index) in filteredUnidades"
              :key="item.id"
            >
              <div
                class="list-row q-px-md q-py-sm"
                :class="{ 'row-selected': index === selectedIndex }"
                @click="selectedIndex = index"
              >
                <span class="text-body2 text-dark">{{ item.unidade }}</span>
                <span class="text-body2 text-dark">{{ item.frequencia }}</span>
                <q-badge
                  :label="item.status"
                  :color="item.status === 'Alto' ? 'secondary' : 'orange'"
                  style="
                    border-radius: 8px;
                    font-size: 11px;
                    padding: 4px 8px;
                    width: max-content;
                  "
                />
              </div>
              <q-separator v-if="index < filteredUnidades.length - 1" />
            </template>
          </div>
        </q-card>
      </q-card-section>
    </q-card>
  </q-dialog>
</template>

<script setup>
import { ref, computed } from "vue";
import { useDialogPluginComponent } from "quasar";

defineEmits([...useDialogPluginComponent.emits]);

const { dialogRef, onDialogHide, onDialogCancel } = useDialogPluginComponent();

const search = ref("");
const selectedIndex = ref(0);

const unidades = [
  { id: 1, unidade: "São Paulo", frequencia: "47%", status: "Baixa" },
  { id: 2, unidade: "Maringá", frequencia: "20 %", status: "Baixa" },
  { id: 3, unidade: "Curitiba", frequencia: "79 %", status: "Alto" },
  { id: 4, unidade: "Foz Iguaçu", frequencia: "98%", status: "Alto" },
];

const filteredUnidades = computed(() => {
  if (!search.value) return unidades;
  const q = search.value.toLowerCase();
  return unidades.filter(
    (u) =>
      u.unidade.toLowerCase().includes(q) ||
      u.status.toLowerCase().includes(q),
  );
});
</script>

<style scoped>
.list-header {
  display: grid;
  grid-template-columns: 1fr 1fr 80px;
  align-items: center;
}

.list-row {
  display: grid;
  grid-template-columns: 1fr 1fr 80px;
  align-items: center;
  cursor: pointer;
  transition: background-color 0.15s;
}

.list-row:hover {
  background-color: #f5f5f5;
}

.row-selected {
  background-color: #b2dfdb !important;
}
</style>
