'use client'
import * as React from 'react';
import { Button, Dialog, DialogActions, DialogContent, DialogContentText, DialogTitle } from '@mui/material';
import { redirect } from 'next/navigation';

export default function LogoutDialog({ open, handleClose }) {

    const handleLogout = () => {
        fetch('/api/logout', { method: 'POST' })
            .then(response => {
                if (response.status === 200) {
                    redirect('/');
                } else {
                    console.error('Failed to logout');
                }
            });
    }

    return (
        <Dialog open={open} onClose={handleClose}>
            <DialogTitle>Se déconnecter</DialogTitle>
            <DialogContent>
                <DialogContentText>
                    Êtes-vous sûr de vouloir vous déconnecter ?
                </DialogContentText>
            </DialogContent>
            <DialogActions>
                <Button variant='text' onClick={handleClose}>Annuler</Button>
                <Button variant='text' onClick={handleLogout}>Se déconnecter</Button>
            </DialogActions>
        </Dialog>
    );
}