<!-- pages/Anjungan/Anjungan/index.vue -->
<template>
  <div class="anjungan-selection-container">
    <div class="selection-header">
      <div class="header-icon">
        <img 
          src="/Rumah_Sakit_Umum_Daerah_Dr._Saiful_Anwar.webp" 
          alt="RSUD Logo" 
          class="header-logo"
        />
      </div>
      <div class="header-content">
        <h1 class="main-title">Pilih Anjungan</h1>
        <p class="subtitle">RSUD dr. Saiful Anwar Provinsi Jawa Timur</p>
      </div>
    </div>

    <div class="anjungan-grid">
      <div
        v-for="anj in paginatedAnjungan"
        :key="anj.id"
        class="anjungan-card"
        @click="navigateToAnjungan(anj.id)"
      >
        <div class="anjungan-card-header">
          <v-icon size="32" color="primary">mdi-monitor</v-icon>
          <div class="anjungan-info">
            <h3 class="anjungan-name">{{ anj.namaAnjungan }}</h3>
            <p class="anjungan-type">{{ anj.jenisPasien }}</p>
          </div>
        </div>

        <div class="anjungan-klinik-preview">
          <div class="klinik-count">
            <v-icon size="18" color="secondary">mdi-hospital-box</v-icon>
            <span>{{ anj.klinik.length }} Klinik</span>
          </div>
          <div class="klinik-tags">
            <v-chip
              v-for="(kode, idx) in anj.klinik.slice(0, 4)"
              :key="idx"
              size="small"
              class="ma-1 chip-preview"
            >
              {{ masterStore.getKlinikNameByKode(kode) }}
            </v-chip>
            <v-chip
              v-if="anj.klinik.length > 4"
              size="small"
              class="ma-1 chip-more"
            >
              +{{ anj.klinik.length - 4 }}
            </v-chip>
          </div>
        </div>

        <div class="anjungan-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="anjunganList.length === 0" class="empty-state">
      <v-icon size="64" color="grey-lighten-1">mdi-monitor-off</v-icon>
      <h3>Tidak Ada Anjungan Tersedia</h3>
      <p>Silakan tambah anjungan terlebih dahulu di halaman master</p>
      <v-btn
        color="primary"
        variant="flat"
        @click="navigateToSettings"
        class="mt-4"
      >
        <v-icon left>mdi-cog</v-icon>
        Ke Halaman Master Anjungan
      </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>
</template>

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

definePageMeta({
  layout: false,
});

const anjunganStore = useAnjunganStore();
const masterStore = useMasterStore();
const route = useRoute();

// Safeguard supaya tidak undefined saat store belum terisi
const anjunganList = computed(() => {
  const fromGetter = anjunganStore.getAllAnjungan?.value;
  const fromState = anjunganStore.anjunganItems?.value || anjunganStore.anjunganItems || [];
  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(anjunganList.value.length / itemsPerPage)));

const paginatedAnjungan = computed(() => {
  const start = (page.value - 1) * itemsPerPage;
  return anjunganList.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 navigateToAnjungan = (anjunganId) => {
  const id = typeof anjunganId === 'number' ? anjunganId : parseInt(anjunganId, 10);
  if (isNaN(id)) {
    console.error('Invalid anjungan ID:', anjunganId);
    return;
  }
  navigateTo(`/anjungan/anjungan/${id}`);
};

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

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

.selection-header {
  background: linear-gradient(135deg, var(--color-primary-600) 0%, var(--color-primary-700) 100%);
  border-radius: 20px;
  padding: 40px;
  margin-bottom: 40px;
  display: flex;
  align-items: center;
  gap: 32px;
  box-shadow: 0 8px 24px rgba(255, 155, 27, 0.3);
}

.header-icon {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 20px;
  padding: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(10px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.header-logo {
  width: 80px;
  height: 80px;
  object-fit: contain;
  padding: 4px;
}

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

.main-title {
  font-size: 40px;
  font-weight: 800;
  margin: 0;
  color: var(--color-neutral-100);
  line-height: 1.2;
}

.subtitle {
  font-size: 18px;
  font-weight: 600;
  margin: 8px 0 0 0;
  color: var(--color-neutral-100);
  opacity: 0.95;
}

.anjungan-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 24px;
  max-width: 1400px;
  margin: 0 auto;
}

.anjungan-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);
  }
}

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

.anjungan-info {
  flex: 1;
}

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

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

.anjungan-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;
}

.anjungan-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) {
  .anjungan-selection-container {
    padding: 20px;
  }

  .selection-header {
    flex-direction: column;
    text-align: center;
    padding: 32px 24px;
  }

  .main-title {
    font-size: 32px;
  }

  .subtitle {
    font-size: 16px;
  }

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

