<!-- pages/Anjungan/AntreanMasuk/index.vue -->
<template>
  <div>
    <div class="selection-header">
      <div class="header-icon">
        <img
          src="/RSSA logo 1.png"
          alt="RSUD Logo"
          class="header-logo"
          width="40"
          height="40"
          style="width: 40px; height: 40px; object-fit: contain;"
        />
      </div>
      <div class="header-content">
        <h1 class="main-title">Pilih Layar Antrean Masuk</h1>
        <p class="subtitle">RSUD dr. Saiful Anwar Provinsi Jawa Timur</p>
      </div>
    </div>

    <div class="screen-selection-container">
      <div v-if="paginatedScreens.length > 0" class="screens-grid">
      <div
        v-for="screen in paginatedScreens"
        :key="screen.id"
        class="screen-card"
        @click="navigateToScreen(screen.id)"
      >
        <div class="screen-card-header">
          <v-icon size="28" color="primary">mdi-monitor</v-icon>
          <div class="screen-info">
            <h3 class="screen-name">{{ screen.namaScreen }}</h3>
            <p class="screen-details">{{ screen.nomorScreen }}</p>
          </div>
        </div>
        
        <div class="screen-preview">
          <div class="screen-loket-count">
            <v-icon size="16" color="primary">mdi-view-dashboard</v-icon>
            <span>{{ screen.loket?.length || 0 }} Loket</span>
          </div>
          <div class="screen-tags">
            <v-chip
              v-for="(loketId, idx) in (screen.loket || []).slice(0, 3)"
              :key="idx"
              size="small"
              class="ma-1 chip-preview"
            >
              {{ getLoketNameById(loketId) }}
            </v-chip>
            <v-chip
              v-if="(screen.loket || []).length > 3"
              size="small"
              class="ma-1 chip-more"
            >
              +{{ (screen.loket || []).length - 3 }}
            </v-chip>
          </div>
        </div>

        <div class="screen-card-footer">
          <v-btn
            color="primary-600"
            variant="flat"
            size="large"
            block
            class="btn-view text-white"
          >
            <v-icon left size="20">mdi-eye</v-icon>
            Tampilkan
          </v-btn>
        </div>
      </div>
    </div>

    <div v-else class="empty-state">
      <v-icon size="48" color="grey-lighten-1">mdi-monitor-off</v-icon>
      <h3>Tidak Ada Layar Tersedia</h3>
      <p>Silakan tambah layar terlebih dahulu di halaman master</p>
      <v-btn
        color="primary-600"
        variant="flat"
        class="mt-4"
        @click="navigateToSettings"
      >
        <v-icon left size="20">mdi-cog</v-icon>
        Ke Halaman Master
      </v-btn>
    </div>

      <div v-if="paginatedScreens.length > 0 && totalPages > 1" class="pagination">
        <v-btn variant="outlined" :disabled="page <= 1" @click="goPrev">Prev</v-btn>
        <span class="page-info">Page {{ page }} / {{ totalPages }}</span>
        <v-btn variant="outlined" :disabled="page >= totalPages" @click="goNext">Next</v-btn>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed, onMounted } from 'vue';
import { useAntreanMasukScreenStore } from '@/stores/antreanMasukScreenStore';
import { useLoketStore } from '@/stores/loketStore';
import { useRoute } from '#app';

definePageMeta({
  layout: false,
});

const antreanMasukScreenStore = useAntreanMasukScreenStore();
const loketStore = useLoketStore();
const route = useRoute();

onMounted(() => {
  antreanMasukScreenStore.fetchAntreanMasukScreens();
});

// Helper to get loket name by ID
const getLoketNameById = (loketId) => {
  const loket = loketStore.getLoketById(loketId);
  return loket ? loket.namaLoket : `Loket ${loketId}`;
};

// Get all screens from store
const allScreens = computed(() => {
  return antreanMasukScreenStore.antreanMasukScreenItems || [];
});

// Pagination
const itemsPerPage = 10;
const page = computed({
  get: () => Number(route.query.page || 1),
  set: (val) => navigateTo({ query: { ...route.query, page: val } }),
});

const totalPages = computed(() => Math.max(1, Math.ceil(allScreens.value.length / itemsPerPage)));

const paginatedScreens = computed(() => {
  const start = (page.value - 1) * itemsPerPage;
  return allScreens.value.slice(start, start + itemsPerPage);
});

const goPrev = () => {
  if (page.value > 1) page.value = page.value - 1;
};

const goNext = () => {
  if (page.value < totalPages.value) page.value = page.value + 1;
};

const navigateToScreen = (screenId) => {
  navigateTo(`/Anjungan/AntreanMasuk/${screenId}`);
};

const navigateToSettings = () => {
  navigateTo('/setting/screenantreanmasuk');
};
</script>

<style scoped lang="scss">
.screen-selection-container {
  background: var(--color-neutral-300);
  min-height: calc(100vh - 80px);
  padding: 16px;
  font-family: 'Inter', 'Roboto', sans-serif;
}

.selection-header {
  background: linear-gradient(135deg, var(--color-primary-600) 0%, var(--color-primary-700) 100%);
  border-radius: 0 !important;
  padding: 16px 28px;
  margin-bottom: 0;
  display: flex;
  align-items: center;
  gap: 16px;
  height: 80px;
  box-shadow: 0 4px 16px rgba(33, 150, 243, 0.2);
}

.header-icon {
  background: rgba(255, 255, 255, 0.9);
  border-radius: 50%;
  padding: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 54px;
  height: 54px;
  backdrop-filter: blur(10px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.header-logo {
  width: 40px;
  height: 40px;
  object-fit: contain;
}

.header-content {
  flex: 1;
  color: var(--color-neutral-100);
}

.main-title {
  font-size: 32px;
  font-weight: 600;
  margin: 0;
  color: var(--color-neutral-100);
  line-height: 40px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.subtitle {
  font-size: 15px;
  font-weight: 400;
  margin: 2px 0 0 0;
  color: var(--color-neutral-100);
  opacity: 0.9;
  line-height: 22px;
}

.screens-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
  gap: 20px;
  width: 100%;
  margin: 0;
}

.screen-card {
  background: var(--color-neutral-100);
  border-radius: 16px;
  padding: 24px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  border: 2px solid var(--color-neutral-400);
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  flex-direction: column;
  gap: 16px;

  &:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 24px rgba(25, 118, 210, 0.2);
    border-color: var(--color-primary-600);
  }
}

.screen-card-header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding-bottom: 16px;
  border-bottom: 2px solid var(--color-neutral-400);
}

.screen-info {
  flex: 1;
}

.screen-name {
  font-size: 20px;
  font-weight: 700;
  margin: 0;
  color: var(--color-neutral-900);
  line-height: 1.2;
}

.screen-details {
  font-size: 14px;
  color: var(--color-neutral-600);
  margin: 4px 0 0 0;
  font-weight: 500;
}

.screen-preview {
  flex: 1;
}

.screen-loket-count {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  font-weight: 600;
  color: var(--color-primary-600);
  margin-bottom: 12px;
}

.screen-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.chip-preview {
  background-color: var(--color-primary-300) !important;
  color: var(--color-primary-700) !important;
  font-weight: 500;
  font-size: 12px;
}

.chip-more {
  background-color: var(--color-neutral-600) !important;
  color: var(--color-neutral-100) !important;
  font-weight: 600;
  font-size: 12px;
}

.screen-card-footer {
  margin-top: auto;
}

.btn-view {
  font-weight: 600;
  text-transform: none;
  letter-spacing: 0.5px;
}

.empty-state {
  text-align: center;
  padding: 80px 20px;
  color: var(--color-neutral-700);

  h3 {
    font-size: 24px;
    font-weight: 700;
    margin: 24px 0 8px 0;
    color: var(--color-neutral-800);
  }

  p {
    font-size: 16px;
    color: var(--color-neutral-600);
  }
}

.pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-top: 24px;
}

.page-info {
  font-weight: 600;
  color: var(--color-neutral-800);
}

@media (max-width: 768px) {
  .screen-selection-container {
    padding: 12px;
  }

  .selection-header {
    flex-direction: row;
    padding: 12px 16px;
    height: auto;
    min-height: 64px;
  }

  .header-icon {
    padding: 6px;
    width: 36px;
    height: 36px;
  }

  .main-title {
    font-size: 20px;
    line-height: 28px;
  }

  .subtitle {
    font-size: 13px;
    line-height: 18px;
  }

  .screens-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }
}
</style>
