<!-- front-end/app/pages/sistema/configuracoes.vue -->
<script setup lang="ts">
const { apiFetch } = useApi()

interface ApiTenant {
  ID: string
  Slug: string
  Name: string
  LegalName: string
  DocumentType: string
  Document: string
  Plan: string
  MaxUsers: number
  IsActive: boolean
}

const { data: tenant, refresh } = await useAsyncData('tenant-me', () =>
  apiFetch<ApiTenant>('/tenant/me')
)

const form = reactive({
  name: tenant.value?.Name ?? '',
  legalName: tenant.value?.LegalName ?? '',
  documentType: tenant.value?.DocumentType ?? 'pj',
  document: tenant.value?.Document ?? '',
})

const saving = ref(false)
const saved = ref(false)

async function salvar() {
  saving.value = true
  saved.value = false
  await apiFetch('/tenant/me', {
    method: 'PUT',
    body: {
      name: form.name,
      legal_name: form.legalName,
      document_type: form.documentType,
      document: form.document,
    },
  })
  await refresh()
  saving.value = false
  saved.value = true
  setTimeout(() => { saved.value = false }, 3000)
}

const documentLabel = computed(() => form.documentType === 'pj' ? 'CNPJ' : 'CPF')
</script>

<template>
  <div class="page">
    <AppTopbar title="Configurações" breadcrumb="Sistema · Configurações" />
    <div class="content">
      <div class="card pad">
        <h2 class="section-title">Dados da Empresa</h2>

        <div class="type-toggle">
          <button type="button" :class="['type-btn', form.documentType === 'pj' && 'active']" @click="form.documentType = 'pj'">
            Pessoa Jurídica
          </button>
          <button type="button" :class="['type-btn', form.documentType === 'pf' && 'active']" @click="form.documentType = 'pf'">
            Pessoa Física
          </button>
        </div>

        <UFormField label="Nome Fantasia / Apelido" class="field">
          <UInput v-model="form.name" />
        </UFormField>
        <UFormField :label="form.documentType === 'pj' ? 'Razão Social' : 'Nome Completo'" class="field">
          <UInput v-model="form.legalName" />
        </UFormField>
        <UFormField :label="documentLabel" class="field">
          <UInput v-model="form.document" />
        </UFormField>
        <UFormField label="Slug" class="field">
          <UInput :model-value="tenant?.Slug ?? ''" disabled />
        </UFormField>
        <UFormField label="Plano Atual" class="field">
          <UInput :model-value="tenant?.Plan ?? ''" disabled />
        </UFormField>

        <div class="actions">
          <UButton class="btn-primary" size="sm" :loading="saving" @click="salvar">Salvar Alterações</UButton>
          <span v-if="saved" class="saved-msg">Salvo!</span>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.page { display: flex; flex-direction: column; height: 100vh; }
.content { padding: 20px 22px; flex: 1; overflow-y: auto; }
.card { background: white; border-radius: 11px; border: 1px solid #e2e8f0; }
.pad { padding: 24px; }
.section-title { font-size: 15px; font-weight: 700; color: #0f172a; margin-bottom: 16px; }
.type-toggle { display: flex; gap: 8px; margin-bottom: 18px; }
.type-btn {
  flex: 1; padding: 7px; border-radius: 8px; border: 1.5px solid #e2e8f0;
  font-size: 13px; font-weight: 500; color: #64748b; cursor: pointer; background: white;
  transition: all 0.15s;
}
.type-btn.active { border-color: #667eea; color: #667eea; background: #f0f3ff; }
.field { margin-bottom: 14px; }
.actions { display: flex; align-items: center; gap: 12px; margin-top: 4px; }
.btn-primary { background: linear-gradient(135deg, #667eea, #764ba2) !important; }
.saved-msg { font-size: 13px; color: #10b981; font-weight: 500; }
</style>
