<!-- pages/Anjungan/AntrianKlinik/index.vue -->
<template>
  <div>
    <div class="selection-header">
      <div class="header-icon">
        <v-icon size="64" color="white">mdi-monitor-multiple</v-icon>
      </div>
      <div class="header-content">
        <h1 class="main-title">Pilih Layar Antrian Klinik</h1>
        <p class="subtitle">RSUD dr. Saiful Anwar Provinsi Jawa Timur</p>
      </div>
    </div>

    <div class="screen-selection-container">
      <div 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="32" color="primary">mdi-monitor</v-icon>
          <div class="screen-info">
            <h3 class="screen-name">{{ screen.namaScreen }}</h3>
            <p class="screen-number">{{ screen.nomorScreen }}</p>
          </div>
        </div>
        
        <div class="screen-klinik-preview">
          <div class="klinik-count">
            <v-icon size="18" color="secondary">mdi-hospital-box</v-icon>
            <span>{{ screen.klinik.length }} Klinik</span>
          </div>
          <div class="klinik-tags">
            <v-chip
              v-for="(kode, idx) in screen.klinik.slice(0, 4)"
              :key="idx"
              size="small"
              class="ma-1 chip-preview"
            >
              {{ masterStore.getKlinikNameByKode(kode) }}
            </v-chip>
            <v-chip
              v-if="screen.klinik.length > 4"
              size="small"
              class="ma-1 chip-more"
            >
              +{{ screen.klinik.length - 4 }}
            </v-chip>
          </div>
        </div>

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

    <div v-if="screens.length === 0" class="empty-state">
      <v-icon size="64" color="grey-lighten-1">mdi-monitor-off</v-icon>
      <h3>Tidak Ada Screen Tersedia</h3>
      <p>Silakan tambah screen terlebih dahulu di halaman konfigurasi</p>
      <v-btn
        color="primary"
        variant="flat"
        @click="navigateToSettings"
        class="mt-4"
      >
        <v-icon left>mdi-cog</v-icon>
        Ke Halaman Konfigurasi
      </v-btn>
    </div>

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

<script setup>
import { computed } from 'vue';
import { useScreenStore } from '@/stores/screenStore';
import { useMasterStore } from '@/stores/masterStore';
import { useRoute } from '#app';

definePageMeta({
  layout: false,
});

const screenStore = useScreenStore();
const masterStore = useMasterStore();
const route = useRoute();

// Safeguard supaya tidak undefined saat store belum terisi
const screens = computed(() => {
  const fromGetter = screenStore.getAllScreens?.value;
  const fromState = screenStore.screenItems?.value || screenStore.screenItems || [];
  return Array.isArray(fromGetter) ? fromGetter : Array.isArray(fromState) ? fromState : [];
});

// 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(screens.value.length / itemsPerPage)));

const paginatedScreens = computed(() => {
  const start = (page.value - 1) * itemsPerPage;
  return screens.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) => {
  const id = typeof screenId === 'number' ? screenId : parseInt(screenId, 10);
  if (isNaN(id)) {
    console.error('Invalid screen ID:', screenId);
    return;
  }

  // gunakan path langsung agar tidak tergantung nama route auto-gen
  navigateTo(`/anjungan/antrianklinik/${id}`);
};

const navigateToSettings = () => {
  navigateTo('/setting/screen');
};
</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: 40px;
  height: 40px;
  backdrop-filter: blur(10px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.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(320px, 1fr));
  gap: 24px;
  max-width: 1400px;
  margin: 0 auto;
}

.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(6, 113, 224, 0.2);
    border-color: var(--color-secondary-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-number {
  font-size: 14px;
  color: var(--color-neutral-600);
  margin: 4px 0 0 0;
  font-weight: 500;
}

.screen-klinik-preview {
  flex: 1;
}

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

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

.chip-preview {
  background-color: var(--color-secondary-300) !important;
  color: var(--color-secondary-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: 4px;
    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>

