<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>
            <div v-if="filteredUnits.length === 0" class="text-grey-6 text-center q-pa-lg">
              Nenhuma unidade encontrada.
            </div>
            <template v-for="(item, index) in filteredUnits" :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.unit }}</span>
                <span class="text-body2 text-dark">{{ item.frequency }}%</span>
                <q-badge
                  :label="item.status"
                  :color="statusColor(item.status)"
                  style="
                    border-radius: 8px;
                    font-size: 11px;
                    padding: 4px 8px;
                    width: max-content;
                  "
                />
              </div>
              <q-separator v-if="index < filteredUnits.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 props = defineProps({
  units: {
    type: Array,
    default: () => [],
  },
});

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

const statusColor = (status) => {
  if (status === "Alto") return "secondary";
  if (status === "Sem dados") return "grey";
  return "orange";
};

const filteredUnits = computed(() => {
  if (!search.value) return props.units;
  const q = search.value.toLowerCase();
  return props.units.filter(
    (u) =>
      u.unit.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>
