<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"
          >Tickets em Aberto</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 ticket, nome, data , setor"
            >
              <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">Ticket</span>
            <span class="text-caption text-grey-7">Prioridade</span>
            <span class="text-caption text-grey-7">Data</span>
            <span class="text-caption text-grey-7">Setor</span>
            <span class="text-caption text-grey-7">Status</span>
          </div>

          <q-separator />

          <div style="max-height: 320px; overflow-y: auto">
            <div v-if="filteredTickets.length === 0" class="text-grey-6 text-center q-pa-lg">
              Nenhum ticket aberto encontrado.
            </div>
            <template v-for="(ticket, index) in filteredTickets" :key="ticket.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">{{ ticket.number }}</span>
                <q-badge
                  :label="priorityLabel(ticket.priority)"
                  :color="priorityColor(ticket.priority)"
                  style="border-radius: 8px; font-size: 11px; padding: 4px 8px; width: max-content"
                />
                <span class="text-caption text-dark">{{ ticket.date }}</span>
                <span class="text-caption text-dark">{{ ticket.sector }}</span>
                <q-badge
                  :label="statusLabel(ticket.status)"
                  :color="statusColor(ticket.status)"
                  style="border-radius: 8px; font-size: 11px; padding: 4px 8px; width: max-content"
                />
              </div>
              <q-separator v-if="index < filteredTickets.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({
  tickets: {
    type: Array,
    default: () => [],
  },
});

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

const priorityLabel = (priority) =>
  ({ high: "Alta", medium: "Média", low: "Baixa" })[priority] ?? priority;

const priorityColor = (priority) => {
  if (priority === "high") return "negative";
  if (priority === "medium") return "warning";
  return "orange";
};

const statusColor = (s) => {
  if (s === "resolved") return "positive";
  if (s === "in_progress") return "warning";
  return "negative";
};

const statusLabel = (status) =>
  ({
    in_progress: "Em andamento",
    resolved: "Resolvido",
    unresolved: "Não resolvido",
  })[status] ?? status;

const filteredTickets = computed(() => {
  if (!search.value) return props.tickets;
  const q = search.value.toLowerCase();
  return props.tickets.filter(
    (t) =>
      t.number.toLowerCase().includes(q) ||
      t.title.toLowerCase().includes(q) ||
      t.date.includes(q) ||
      t.sector.toLowerCase().includes(q) ||
      t.status.toLowerCase().includes(q),
  );
});
</script>

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

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

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

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