tablemail = nil
function mailbox_menu(id, whitelist)
    local playerJob = getPlayerJob()

    if playerJob == whitelist or whitelist == "no" or whitelist == identifier then
        elements = {
            { 
                title = Locale["deposit_mail"],
                icon = 'fa-solid fa-plus', 
                description = Locale["deposit_mailbox_description"],  
                arrow = true,
                iconColor = "rgba(0, 255, 72, 1)",
                onSelect = function(args)
                    if Config.anonymOption == true then
                        local message = lib.inputDialog(Locale["deposit_mail"], {
                            { type = 'input', label = Locale["content"], required = true, options = {}},
                            {type = 'select', label = Locale["anonyme"], required = true, options = {
                                { value = 'no', label = Locale["no"]},
                                { value = 'yes', label = Locale["yes"]},
                            }} 
                        })
        
                        if message ~= nil then
                            TriggerServerEvent('mailbox:sendlettre', id, message[1], message[2])
                            mailbox_menu(id, whitelist)
                        else
                            mailbox_menu(id, whitelist)
                        end
                    else
                        local message = lib.inputDialog(Locale["deposit_mail"], {
                            { type = 'input', label = Locale["content"], required = true, options = {}},
                        })
        
                        if message ~= nil then
                            TriggerServerEvent('mailbox:sendlettre', id, message[1], "no")
                            mailbox_menu(id, whitelist)
                        else
                            mailbox_menu(id, whitelist)
                        end
                    end
                end 
            },
    
            { 
                title = Locale["manage_letter"],
                icon = 'fa-solid fa-list', 
                description = Locale["manage_letter_description"], 
                arrow = true,
                iconColor = "rgba(0, 255, 72, 1)",
                onSelect = function(args)
                    tablemail = nil
                    ESX.TriggerServerCallback('mailbox:getlettre', function(result)
                        tablemail = result 
                    end, id)
    
                    while tablemail == nil do
                        Wait(1)
                    end
    
                    mailbox_manage(id, whitelist)
                end
            },
        }
    else
        elements = {
            { 
                title = Locale["deposit_mail"],
                icon = 'fa-solid fa-plus', 
                description = Locale["deposit_mailbox_description"],  
                arrow = true,
                iconColor = "rgba(0, 255, 72, 1)",
                onSelect = function(args)
                    local message = lib.inputDialog(Locale["deposit_mail"], {
                        { type = 'input', label = Locale["content"], required = true, options = {}},
                        {type = 'select', label = Locale["anonyme"], required = true, options = {
                            { value = 'no', label = Locale["no"]},
                            { value = 'yes', label = Locale["yes"]},
                        }} 
                    })
    
                    if message ~= nil then
                        TriggerServerEvent('mailbox:sendlettre', id, message[1], message[2])
                        mailbox_menu(id)
                    else
                        mailbox_menu(id)
                    end
                end 
            },
        }
    end

    lib.registerContext({
        id = 'letter_menu',
        title = Locale["letter_menu_header"],
        options = elements,
    })
    lib.showContext('letter_menu')
end

function mailbox_manage(id, whitelist)
    elements = {}
    for k,v in pairs(tablemail) do
        table.insert(elements, {
            title = v.name.." - "..v.date,
            icon = 'fa-solid fa-file',  
            arrow = true,
            iconColor = "rgba(255, 218, 72, 1)",
            onSelect = function(args)
                local input = lib.inputDialog(v.name.." - "..v.date, {
                    {type = 'select', label = Locale["action"], required = true, options = {
                        { value = 'look', label = Locale["read_letter"]},
                        { value = 'delete', label = Locale["delete_letter"]},
                    }},
                })

                if input ~= nil then
                    if input[1] == "look" then
                        local alert = lib.alertDialog({
                            header = Locale["author"]..v.name.." "..Locale["when"]..""..v.date,
                            content = v.contenu,
                            centered = true,
                            labels = {
                                confirm = "Fermer la lettre"
                            },
                        })

                        mailbox_manage(id, whitelist)
                    else
                        TriggerServerEvent('mailbox:suppressionlettre', v.id)
                        Wait(100)
                        mailbox_menu(id, whitelist)
                    end
                else
                    mailbox_menu(id, whitelist)
                end
            end
        })
    end 

    if #tablemail == 0 then
        table.insert(elements, {
            title = Locale["no_letter"],
            disabled = true,
        })
    end

    lib.registerContext({
        id = 'letter_magage_menu',
        title = Locale["letter_magage_menu_header"],
        options = elements,
        menu = 'letter_menu',
    })
    lib.showContext('letter_magage_menu')
end