Agenda, Agendador, Configurações

This commit is contained in:
Leonardo
2026-03-12 08:58:36 -03:00
parent f733db8436
commit f4b185ae17
197 changed files with 33405 additions and 6507 deletions

View File

@@ -0,0 +1,332 @@
<!-- src/layout/configuracoes/ConfiguracoesPrecificacaoPage.vue -->
<script setup>
import { ref, onMounted } from 'vue'
import { supabase } from '@/lib/supabase/client'
import { useTenantStore } from '@/stores/tenantStore'
import { useToast } from 'primevue/usetoast'
const toast = useToast()
const tenantStore = useTenantStore()
const loading = ref(true)
const saving = ref(false)
const ownerId = ref(null)
const tenantId = ref(null)
// ── Tipos de compromisso do tenant ─────────────────────────────────
const commitments = ref([]) // [{ id, label, native_key }]
// ── Preços: Map<commitmentId | '__default__', { price, notes }> ────
// '__default__' = linha com determined_commitment_id IS NULL
const prices = ref({}) // { [key]: { price: number|null, notes: '' } }
// ── Carregar commitments do tenant ─────────────────────────────────
async function loadCommitments () {
if (!tenantId.value) return
const { data, error } = await supabase
.from('determined_commitments')
.select('id, name, native_key, active')
.eq('tenant_id', tenantId.value)
.eq('active', true)
.order('name')
if (error) throw error
commitments.value = data || []
}
// ── Carregar preços existentes ──────────────────────────────────────
async function loadPrices (uid) {
const { data, error } = await supabase
.from('professional_pricing')
.select('id, determined_commitment_id, price, notes')
.eq('owner_id', uid)
if (error) throw error
const map = {}
for (const row of (data || [])) {
const key = row.determined_commitment_id ?? '__default__'
map[key] = { price: row.price != null ? Number(row.price) : null, notes: row.notes ?? '' }
}
prices.value = map
}
// ── Garantir que todos os commitments + default têm entrada no mapa
function ensureDefaults () {
if (!prices.value['__default__']) {
prices.value['__default__'] = { price: null, notes: '' }
}
for (const c of commitments.value) {
if (!prices.value[c.id]) {
prices.value[c.id] = { price: null, notes: '' }
}
}
}
// ── Mount ───────────────────────────────────────────────────────────
onMounted(async () => {
try {
const uid = tenantStore.user?.id || (await supabase.auth.getUser()).data?.user?.id
if (!uid) return
ownerId.value = uid
tenantId.value = tenantStore.activeTenantId || null
await Promise.all([
loadCommitments(),
loadPrices(uid),
])
ensureDefaults()
} catch (e) {
toast.add({ severity: 'error', summary: 'Erro', detail: e?.message || 'Falha ao carregar.', life: 4000 })
} finally {
loading.value = false
}
})
// ── Salvar todos os preços configurados ────────────────────────────
async function save () {
if (!ownerId.value) return
saving.value = true
try {
const uid = ownerId.value
const tid = tenantId.value
const rows = []
// Linha padrão (NULL commitment)
const def = prices.value['__default__']
if (def?.price != null && def.price !== '') {
rows.push({
owner_id: uid,
tenant_id: tid,
determined_commitment_id: null,
price: Number(def.price),
notes: def.notes?.trim() || null,
})
}
// Linhas por tipo de compromisso
for (const c of commitments.value) {
const entry = prices.value[c.id]
if (entry?.price != null && entry.price !== '') {
rows.push({
owner_id: uid,
tenant_id: tid,
determined_commitment_id: c.id,
price: Number(entry.price),
notes: entry.notes?.trim() || null,
})
}
}
if (!rows.length) {
toast.add({ severity: 'warn', summary: 'Aviso', detail: 'Nenhum preço configurado para salvar.', life: 3000 })
return
}
const { error } = await supabase
.from('professional_pricing')
.upsert(rows, { onConflict: 'owner_id,determined_commitment_id' })
if (error) throw error
toast.add({ severity: 'success', summary: 'Salvo', detail: 'Precificação atualizada!', life: 3000 })
} catch (e) {
toast.add({ severity: 'error', summary: 'Erro', detail: e?.message || 'Falha ao salvar.', life: 4000 })
} finally {
saving.value = false
}
}
function fmtBRL (v) {
if (v == null || v === '') return '—'
return Number(v).toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' })
}
</script>
<template>
<Toast />
<div class="flex flex-col gap-4">
<!-- Header card -->
<Card>
<template #content>
<div class="flex items-center justify-between gap-3 flex-wrap">
<div class="flex items-center gap-3">
<div class="cfg-icon-box">
<i class="pi pi-tag text-lg" />
</div>
<div>
<div class="text-900 font-semibold text-lg">Precificação</div>
<div class="text-600 text-sm">Defina o valor padrão da sessão e valores específicos por tipo de compromisso.</div>
</div>
</div>
<Button
label="Salvar preços"
icon="pi pi-check"
:loading="saving"
:disabled="loading"
@click="save"
/>
</div>
</template>
</Card>
<!-- Loading -->
<div v-if="loading" class="flex justify-center py-10">
<ProgressSpinner style="width:40px;height:40px" />
</div>
<template v-else>
<!-- Preço padrão -->
<Card>
<template #content>
<div class="flex flex-col gap-3">
<div class="flex items-center gap-2">
<i class="pi pi-star text-primary-500" />
<span class="font-semibold text-900">Preço padrão (fallback)</span>
</div>
<div class="text-600 text-sm">
Aplicado quando o tipo de compromisso da sessão não tem um preço específico cadastrado.
</div>
<div class="grid grid-cols-12 gap-3 mt-1">
<div class="col-span-12 sm:col-span-5">
<FloatLabel variant="on">
<InputNumber
v-model="prices['__default__'].price"
inputId="price-default"
mode="currency"
currency="BRL"
locale="pt-BR"
:min="0"
:max="99999"
:minFractionDigits="2"
fluid
placeholder="R$ 0,00"
/>
<label for="price-default">Valor da sessão (R$)</label>
</FloatLabel>
</div>
<div class="col-span-12 sm:col-span-7">
<FloatLabel variant="on">
<InputText
v-model="prices['__default__'].notes"
inputId="notes-default"
class="w-full"
placeholder="Ex: Particular, valor padrão"
/>
<label for="notes-default">Observação (opcional)</label>
</FloatLabel>
</div>
</div>
</div>
</template>
</Card>
<!-- Preços por tipo de compromisso -->
<Card v-if="commitments.length">
<template #title>
<div class="flex items-center gap-2">
<i class="pi pi-list" />
<span>Por tipo de compromisso</span>
</div>
</template>
<template #content>
<div class="text-600 text-sm mb-4">
Valores específicos sobrepõem o preço padrão quando o tipo de compromisso coincide.
</div>
<div class="flex flex-col gap-4">
<div
v-for="c in commitments"
:key="c.id"
class="commitment-row"
>
<div class="commitment-label">
<div class="font-medium text-900">{{ c.name }}</div>
<div v-if="prices[c.id]?.price != null" class="text-xs text-color-secondary mt-0.5">
{{ fmtBRL(prices[c.id]?.price) }}
</div>
</div>
<div class="grid grid-cols-12 gap-3 flex-1">
<div class="col-span-12 sm:col-span-5">
<FloatLabel variant="on">
<InputNumber
v-model="prices[c.id].price"
:inputId="`price-${c.id}`"
mode="currency"
currency="BRL"
locale="pt-BR"
:min="0"
:max="99999"
:minFractionDigits="2"
fluid
placeholder="R$ 0,00"
/>
<label :for="`price-${c.id}`">Valor (R$)</label>
</FloatLabel>
</div>
<div class="col-span-12 sm:col-span-7">
<FloatLabel variant="on">
<InputText
v-model="prices[c.id].notes"
:id="`notes-${c.id}`"
class="w-full"
placeholder="Ex: Convênio, valor reduzido..."
/>
<label :for="`notes-${c.id}`">Observação (opcional)</label>
</FloatLabel>
</div>
</div>
</div>
</div>
</template>
</Card>
<!-- Dica -->
<Message severity="info" :closable="false">
<span class="text-sm">
O preço configurado aqui é preenchido automaticamente ao criar uma sessão na agenda.
Você ainda pode ajustá-lo manualmente no diálogo de cada evento.
</span>
</Message>
</template>
</div>
</template>
<style scoped>
.cfg-icon-box {
display: grid;
place-items: center;
width: 2.5rem;
height: 2.5rem;
border-radius: 0.875rem;
background: color-mix(in srgb, var(--p-primary-500, #6366f1) 12%, transparent);
color: var(--p-primary-500, #6366f1);
flex-shrink: 0;
}
.commitment-row {
display: flex;
align-items: flex-start;
gap: 1rem;
padding: 0.75rem;
border-radius: 0.75rem;
border: 1px solid var(--surface-border);
background: var(--surface-ground);
}
.commitment-label {
min-width: 9rem;
flex-shrink: 0;
padding-top: 0.5rem;
}
</style>