<script setup lang="ts">
import { ref, watch } from "vue"
import { type DialogRootEmits, type DialogRootProps, useForwardPropsEmits } from 'reka-ui'
import { Button } from "@/components/ui/button"
import { Dialog, DialogContent, DialogClose, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"

const props = defineProps<DialogRootProps & {
    title: string,
    description?: string,
    to?: string,
    cc?: string
}>()
const to = ref<string | undefined>(props.to)
const cc = ref<string | undefined>(props.cc)
const emits = defineEmits<DialogRootEmits & {
    send: [to: string | undefined, cc: string | undefined]
}>()
const forwarded = useForwardPropsEmits(props, emits)

watch(() => props.open,
    (newValue, oldValue) => {
        to.value = props.to
    }
)
</script>

<template>
    <Dialog v-bind="forwarded" :open="open" data-slot="send-mail-dialog">
        <DialogContent class="sm:max-w-[425px]">
            <DialogHeader>
                <DialogTitle>{{ props.title }}</DialogTitle>
                <DialogDescription v-if="props.description" v-html="props.description" />
            </DialogHeader>
            <div class="grid grid-cols-[min-content_1fr] gap-4 py-4">
                <Label for="to" class="text-right text-muted-foreground">
                    An
                </Label>
                <Input id="to" required v-model="to" type="email" placeholder="E-Mail" />

                <Label for="cc" class="text-right text-muted-foreground">
                    Kopie
                </Label>
                <Input id="cc" v-model="cc" type="email" placeholder="E-Mail" />
            </div>
            <DialogFooter>
                <DialogClose as-child>
                    <Button>
                        Abbrechen
                    </Button>
                </DialogClose>
                <Button variant="destructive" @click="$emit('send', to, cc)">
                    E-Mail senden
                </Button>
            </DialogFooter>
        </DialogContent>
    </Dialog>
</template>

<style scoped>
/* Remove close X */
[data-slot=send-mail-dialog] [data-slot=dialog-content] button.ring-offset-background {
    display: none;
}
</style>