<script lang="ts" setup>
import { computed } from 'vue'
import ServiceTabLayout from '@/Components/Panel/ServiceTabLayout.vue'
import type { Service } from '@/types'

interface Database { id: number; name: string; size: number; user: string; host: string }
interface Props { service: Service; databases: Database[] }

const props = defineProps<Props>()
const tabs = computed(() => [
  { label: 'Dashboard', icon: 'tabler-dashboard', to: `/services/${props.service.id}/hosting-panel` },
  { label: 'Files', icon: 'tabler-folder', to: `/services/${props.service.id}/hosting-panel/files` },
  { label: 'Databases', icon: 'tabler-database', to: `/services/${props.service.id}/hosting-panel/databases` },
  { label: 'Email', icon: 'tabler-mail', to: `/services/${props.service.id}/hosting-panel/email` },
  { label: 'Domains', icon: 'tabler-world', to: `/services/${props.service.id}/hosting-panel/domains` },
  { label: 'DNS', icon: 'tabler-dns', to: `/services/${props.service.id}/hosting-panel/dns` },
  { label: 'SSL', icon: 'tabler-lock', to: `/services/${props.service.id}/hosting-panel/ssl` },
  { label: 'PHP', icon: 'tabler-brand-php', to: `/services/${props.service.id}/hosting-panel/php` },
  { label: 'Cron', icon: 'tabler-clock', to: `/services/${props.service.id}/hosting-panel/cron` },
  { label: 'Settings', icon: 'tabler-settings', to: `/services/${props.service.id}/hosting-panel/settings` },
])
</script>

<template>
  <ServiceTabLayout :service="service" :tabs="tabs" :current-tab="`/services/${service.id}/hosting-panel/databases`">
    <VCard elevation="0">
      <VCardTitle class="d-flex align-center gap-2">
        <VIcon icon="tabler-database" color="primary" />
        MySQL Databases
      </VCardTitle>
      <VCardText>
        <VTable v-if="databases.length > 0" hover>
          <thead>
            <tr>
              <th class="font-weight-bold">Database</th>
              <th class="font-weight-bold">User</th>
              <th class="font-weight-bold">Host</th>
              <th class="font-weight-bold">Size</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="db in databases" :key="db.id">
              <td class="font-weight-medium">{{ db.name }}</td>
              <td><code>{{ db.user }}</code></td>
              <td><code>{{ db.host }}</code></td>
              <td>{{ (db.size / 1024 / 1024).toFixed(2) }} MB</td>
            </tr>
          </tbody>
        </VTable>
        <div v-else class="text-center pa-8 text-medium-emphasis">
          <VIcon icon="tabler-database-off" size="48" class="mb-2" />
          <div class="text-body-1 mb-2">No databases</div>
          <div class="text-body-2">Database management requires Enhance API configuration.</div>
        </div>
      </VCardText>
    </VCard>
  </ServiceTabLayout>
</template>
