<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">Contratos Cancelados</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-xs">
            <div class="text-subtitle2 text-dark">Lista de contratos cancelados</div>
            <div class="text-caption text-grey-6">
              {{ contracts.length }} Contratos Cancelados
              <q-spinner v-if="loading" size="16px" color="primary" class="q-ml-sm" />
            </div>
          </q-card-section>

          <q-card-section class="q-pt-xs q-pb-sm">
            <q-input
              v-model="search"
              dense
              borderless
              placeholder="Busque por nome, tel/whats ou unidade"
            >
              <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">Nome</span>
            <span class="text-caption text-grey-7">Contato/Unidade</span>
            <span class="text-caption text-grey-7">Contrato</span>
          </div>

          <q-separator />

          <div style="max-height: 320px; overflow-y: auto">
            <template v-for="(contract, index) in filteredContracts" :key="contract.id">
              <div class="list-row q-px-md q-py-sm">
                <span class="text-body2 text-dark">{{ contract.studentName }}</span>
                <div class="column" style="gap: 2px">
                  <span class="text-caption text-dark">{{ contract.studentPhone }}</span>
                  <span class="text-caption text-grey-6">{{ contract.unitName }}</span>
                </div>
                <q-badge
                  label="Cancelado"
                  color="negative"
                  style="
                    border-radius: 8px;
                    font-size: 11px;
                    padding: 4px;
                    width: max-content;
                    margin-left: 10px;
                  "
                />
              </div>
              <q-separator v-if="index < filteredContracts.length - 1" />
            </template>
          </div>
        </q-card>
      </q-card-section>

      <q-card-actions align="right" class="q-px-md q-pb-md q-pt-none">
        <q-btn
          label="EXPORTAR"
          color="primary-2"
          unelevated
          style="border-radius: 8px; font-weight: 600; letter-spacing: 0.5px"
        />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

<script setup>
import { ref, computed, onMounted } from "vue";
import { useDialogPluginComponent } from "quasar";
import { getFranchisorCancelledContracts } from "src/api/student_contract";

const props = defineProps({
  unitIds: { type: Array, default: () => [] },
});

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

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

const search = ref("");
const loading = ref(false);
const contracts = ref([]);

onMounted(async () => {
  loading.value = true;
  try {
    const data = await getFranchisorCancelledContracts(props.unitIds);
    contracts.value = data.map((c) => ({
      id: c.id,
      studentName: c.student_name ?? "—",
      studentPhone: c.student_phone ?? "—",
      unitName: c.unit_name ?? "—",
    }));
  } catch {
    // silent
  } finally {
    loading.value = false;
  }
});

const filteredContracts = computed(() => {
  if (!search.value) return contracts.value;
  const q = search.value.toLowerCase();
  return contracts.value.filter(
    (c) =>
      c.studentName.toLowerCase().includes(q) ||
      c.studentPhone.includes(q) ||
      c.unitName.toLowerCase().includes(q),
  );
});
</script>

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

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

.list-row:hover {
  background-color: #f5f5f5;
}
</style>
