<?php

    $submit = getuserdata('submit');
    $actiune = getuserdata('actiune');
    $id = getuserdata('id', 0);
    $admin_utilizator = getuserdata('admin_utilizator', '');
    $admin_parola = getuserdata('admin_parola', '');
    $admin_parola2 = getuserdata('admin_parola2', '');
    
    switch ($actiune)
    {
        case 'do_editare':
            if ($submit != 'Salvează')
            {
                header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
                exit;
            }
            if ($admin_utilizator == '')
            {
                $eroare = 'Numele de utilizator nu poate fi vid';
            }
            elseif ($admin_parola == '')
            {
                $eroare = 'Parola nu poate fi vidă';
            }
            elseif ($admin_parola != $admin_parola2)
            {
                $eroare = 'Parolele nu coincid';
            }
            else
            {
                $q = mysql_query("SELECT * FROM utilizatori WHERE id = $id");
                if ($r = mysql_fetch_assoc($q))
                {
                    mysql_query("UPDATE utilizatori SET utilizator = '$admin_utilizator', parola = SHA1('$admin_parola') WHERE id = $id");
                    $eroare = mysql_error();
                }
                else
                {
                    mysql_query("INSERT INTO articole (utilizator, parola) VALUES ('$admin_utilizator', SHA1('$admin_parola'))");
                    $eroare = mysql_error();
                }
            }
            if (!$eroare)
            {
                header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
                exit;
            }
        case 'editare':
            writepagetitle();
            writepagetop();
            writeinfolayer();
            $q = mysql_query("SELECT * FROM utilizatori WHERE id = $id");
            if ($r = mysql_fetch_assoc($q))
            {
                $admin_utilizator = $r['utilizator'];
            }
            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>Utilizator:</th><td><input type='text' name='admin_utilizator' value='$admin_utilizator'></td></tr>";
            echo "<tr><th>Parola:</th><td><input type='password' name='admin_parola'></td></tr>";
            echo "<tr><th>Confirmare parolă:</th><td><input type='password' name='admin_parola2'></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 utilizatori WHERE id = $id");
            if ($r = mysql_fetch_assoc($q))
            {
                mysql_query("DELETE FROM utilizatori WHERE id = $id");
                $eroare = mysql_error();
            }
            else
            {
                $eroare = 'Utilizatorul selectat nu poate fi accesat';
            }
            if (!$eroare)
            {
                header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
                exit;
            }
        case 'stergere':
            $q = mysql_query("SELECT * FROM utilizatori 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 utilizatorul '{$r['utilizator']}'?</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>Utilizatori</h1>";
    echo "<table><tr><th style='width: 500px'>Utilizatori</th><th>&nbsp;</th></tr>";
    $q = mysql_query("SELECT * FROM utilizatori ORDER BY utilizator");
    while ($r = mysql_fetch_assoc($q))
    {
        echo "<tr><td>{$r['utilizator']}</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();
?>