<?php

    $submit = getuserdata('submit');
    $actiune = getuserdata('actiune');
    $id = getuserdata('id', 0);
    $email = getuserdata('email', '');
    
    switch ($actiune)
    {
        case 'do_editare':
            if ($submit != 'Salvează')
            {
                header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
                exit;
            }
            if ($email == '')
            {
                $eroare = 'Adresa de e-mail nu poate fi vidă';
            }
            else
            {
                $q = mysql_query("SELECT * FROM newsletter WHERE id = $id");
                if ($r = mysql_fetch_assoc($q))
                {
                    mysql_query("UPDATE newsletter SET email = '$email' WHERE id = $id");
                    $eroare = mysql_error();
                }
                else
                {
                    mysql_query("INSERT INTO newsletter (email) VALUES ('$email')");
                    $eroare = mysql_error();
                }
            }
            if (!$eroare)
            {
                header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
                exit;
            }
        case 'editare':
            writepagetitle();
            writepagetop();
            writeinfolayer();
            $q = mysql_query("SELECT * FROM newsletter WHERE id = $id");
            if ($r = mysql_fetch_assoc($q))
            {
                $email = $r['email'];
            }
            echo "<form action='{$_SERVER['PHP_SELF']}' method='POST'>";
            echo "<input type='hidden' name='actiune' value='do_editare'>";
            echo "<input type='hidden' name='pagina' value='$pagina'>";
            echo "<input type='hidden' name='id' value='{$r['id']}'>";
            echo "<center><table class='formular'>";
            echo "<tr><th>Adresă e-mail:</th><td><input type='text' name='email' value='$email'></td></tr>";
            echo "<tr><td>&nbsp;</td><td class='butoane'>";
            echo "<input type='submit' name='submit' id='save' value='Salvează'>";
            echo "<input type='submit' name='submit' id='cancel' value='Anulează'>";
            echo "</td></tr></table></center></form>";
            writepagebottom();
            exit;
        case 'do_stergere':
            if ($submit != 'Șterge')
            {
                header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
                exit;
            }
            $q = mysql_query("SELECT * FROM newsletter WHERE id = $id");
            if ($r = mysql_fetch_assoc($q))
            {
                mysql_query("DELETE FROM newsletter WHERE id = $id");
                $eroare = mysql_error();
            }
            else
            {
                $eroare = 'Adresa selectată nu poate fi accesată';
            }
            if (!$eroare)
            {
                header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
                exit;
            }
        case 'stergere':
            $q = mysql_query("SELECT * FROM newsletter WHERE id = $id");
            if ($r = mysql_fetch_assoc($q))
            {
                writepagetitle();
                writepagetop();
                writeinfolayer();
                echo "<form action='{$_SERVER['PHP_SELF']}' method='POST'>";
                echo "<input type='hidden' name='actiune' value='do_stergere'>";
                echo "<input type='hidden' name='id' value='{$r['id']}'>";
                echo "<input type='hidden' name='pagina' value='$pagina'>";
                echo "<center><table class='formular'>";
                echo "<tr><th>Sigur doriți să ștergeți adresa de e-mail '{$r['email']}'?</th></tr>";
                echo "<tr><td class='butoane'>";
                echo "<input type='submit' name='submit' id='save' value='Șterge'>";
                echo "<input type='submit' name='submit' id='cancel' value='Anulează'>";
                echo "</td></tr></table></center></form>";
                writepagebottom();
                exit;
            }
    }

    writepagetitle();
    writepagetop();
    writeinfolayer();
    echo "<span class='articol'>";
    echo "<h1>Abonați newsletter</h1>";
    echo "<table><tr><th style='width: 500px'>E-mail</th><th>&nbsp;</th></tr>";
    $q = mysql_query("SELECT * FROM newsletter ORDER BY email");
    while ($r = mysql_fetch_assoc($q))
    {
        echo "<tr><td>{$r['email']}</td>";
        echo "<td class='butoane'>";
        echo "<form action='{$_SERVER['PHP_SELF']}' method='POST'>";
        echo "<input type='hidden' name='actiune' value='editare'>";
        echo "<input type='hidden' name='pagina' value='$pagina'>";
        echo "<input type='hidden' name='id' value='{$r['id']}'>";
        echo "<input type='submit' name='submit' id='edit' value='Editează'>";
        echo "</form>";
        echo "<form action='{$_SERVER['PHP_SELF']}' method='POST'>";
        echo "<input type='hidden' name='actiune' value='stergere'>";
        echo "<input type='hidden' name='pagina' value='$pagina'>";
        echo "<input type='hidden' name='id' value='{$r['id']}'>";
        echo "<input type='submit' name='submit' id='delete' value='Șterge'>";
        echo "</form></td></tr>";
    }
    echo "</table>";
    echo "<form action='{$_SERVER['PHP_SELF']}' method='POST'>";
    echo "<input type='hidden' name='actiune' value='editare'>";
    echo "<input type='hidden' name='pagina' value='$pagina'>";
    echo "<input type='hidden' name='id' value='0'>";
    echo "<input type='submit' name='submit' id='add' value='Adaugă'>";
    echo "</form></span>";    
    writepagebottom();
?>