<!-- pages/Anjungan/AntrianKlinikRuang/index.vue -->
<template>
  <div class="klinik-selection-container">
    <div class="selection-header">
      <div class="header-icon">
        <v-icon size="64" color="white">mdi-door-open</v-icon>
      </div>
      <div class="header-content">
        <h1 class="main-title">Pilih Klinik Ruang</h1>
        <p class="subtitle">RSUD dr. Saiful Anwar Provinsi Jawa Timur</p>
      </div>
    </div>

    <div class="kliniks-grid" v-if="paginatedKliniks.length > 0">
      <div
        v-for="klinik in paginatedKliniks"
        :key="klinik.kodeKlinik"
        class="klinik-card"
        @click="navigateToKlinik(klinik.kodeKlinik)"
      >
        <div class="klinik-card-header">
          <v-icon size="32" color="success">mdi-hospital-building</v-icon>
          <div class="klinik-info">
            <h3 class="klinik-name">{{ klinik.namaKlinik }}</h3>
            <p class="klinik-code">{{ klinik.kodeKlinik }}</p>
          </div>
        </div>
        
        <div class="klinik-ruang-preview">
          <div class="ruang-count">
            <v-icon size="18" color="success">mdi-door</v-icon>
            <span>{{ klinik.ruangList.length }} Ruang</span>
          </div>
          <div class="ruang-tags">
            <v-chip
              v-for="(ruang, idx) in klinik.ruangList.slice(0, 3)"
              :key="idx"
              size="small"
              class="ma-1 chip-preview"
            >
              {{ ruang.namaRuang }}
            </v-chip>
            <v-chip
              v-if="klinik.ruangList.length > 3"
              size="small"
              class="ma-1 chip-more"
            >
              +{{ klinik.ruangList.length - 3 }}
            </v-chip>
          </div>
        </div>

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

    <div v-else class="empty-state">
      <v-icon size="64" color="grey-lighten-1">mdi-hospital-building-off</v-icon>
      <h3>Tidak Ada Klinik dengan Ruang Tersedia</h3>
      <p>Silakan tambah ruang klinik terlebih dahulu di halaman master</p>
      <v-btn
        color="success"
        variant="flat"
        @click="navigateToSettings"
        class="mt-4"
      >
        <v-icon left>mdi-cog</v-icon>
        Ke Halaman Master
      </v-btn>
    </div>

    <div v-if="paginatedKliniks.length > 0 && 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 { useMasterStore } from '@/stores/masterStore';
import { useRoute } from '#app';

definePageMeta({
  layout: false,
});

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

// Group klinik berdasarkan data flatten getAllRuangList agar nama ruang baru langsung muncul
const kliniksWithRuang = computed(() => {
  const fromGetter = masterStore.getAllRuangList?.value;
  const fromState = masterStore.ruangData?.value || masterStore.ruangData || [];

  let allRuang = Array.isArray(fromGetter) ? fromGetter : [];

  // Jika getter kosong, flatten dari ruangData (grouped)
  if (allRuang.length === 0 && Array.isArray(fromState)) {
    fromState.forEach((r) => {
      (r?.ruangList || []).forEach((ru) => {
        allRuang.push({
          kodeKlinik: r.kodeKlinik,
          namaKlinik: r.namaKlinik,
          nomorRuang: ru.nomorRuang,
          namaRuang: ru.namaRuang,
          nomorScreen: ru.nomorScreen,
        });
      });
    });
  }

  const klinikMap = {};
  allRuang.forEach((ruang) => {
    if (!klinikMap[ruang.kodeKlinik]) {
      klinikMap[ruang.kodeKlinik] = {
        kodeKlinik: ruang.kodeKlinik,
        namaKlinik: ruang.namaKlinik,
        ruangList: [],
      };
    }
    klinikMap[ruang.kodeKlinik].ruangList.push({
      namaRuang: ruang.namaRuang,
      nomorRuang: ruang.nomorRuang,
      nomorScreen: ruang.nomorScreen,
    });
  });

  return Object.values(klinikMap);
});

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

const paginatedKliniks = computed(() => {
  const start = (page.value - 1) * itemsPerPage;
  return kliniksWithRuang.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 navigateToKlinik = (kodeKlinik) => {
  navigateTo(`/anjungan/antrianklinikruang/${kodeKlinik}`);
};

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

<style scoped lang="scss">
.klinik-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-success-600) 0%, var(--color-success-700) 100%);
  border-radius: 20px;
  padding: 40px;
  margin-bottom: 40px;
  display: flex;
  align-items: center;
  gap: 32px;
  box-shadow: 0 8px 24px rgba(0, 146, 98, 0.3);
}

.header-icon {
  background: rgba(255, 255, 255, 0.15);
  border-radius: 20px;
  padding: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(10px);
}

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

.main-title {
  font-size: 48px;
  font-weight: 800;
  margin: 0;
  letter-spacing: 2px;
  color: var(--color-neutral-100);
  line-height: 1.2;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

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

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

.klinik-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(0, 146, 98, 0.2);
    border-color: var(--color-success-600);
  }
}

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

.klinik-info {
  flex: 1;
}

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

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

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

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

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

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

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

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

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

  .subtitle {
    font-size: 16px;
  }

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

