<?php

    $submit = getuserdata('submit');
    $actiune = getuserdata('actiune');
    $id = getuserdata('id', 0);
    $produs = getuserdata('produs', '');
    
    switch ($actiune)
    {
        case 'do_editare':
            if ($submit != 'Salvează')
            {
                header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
                exit;
            }
            if ($produs == '')
            {
                $eroare = 'Denumirea produsului nu poate fi vidă';
            }
            else
            {
                $q = mysql_query("SELECT * FROM produse WHERE id = $id");
                if ($r = mysql_fetch_assoc($q))
                {
                    mysql_query("UPDATE produse SET produs = '$produs' WHERE id = $id");
                    $eroare = mysql_error();
                }
                else
                {
                    mysql_query("INSERT INTO produse (produs) VALUES ('$produs')");
                    $eroare = mysql_error();
                }
            }
            if (!$eroare)
            {
                header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
                exit;
            }
        case 'editare':
            writepagetitle();
            writepagetop();
            writeinfolayer();
            $q = mysql_query("SELECT * FROM produse WHERE id = $id");
            if ($r = mysql_fetch_assoc($q))
            {
                $produs = $r['produs'];
            }
            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>Denumire produs:</th><td><input type='text' name='produs' value='$produs'></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 produse WHERE id = $id");
            if ($r = mysql_fetch_assoc($q))
            {
                mysql_query("DELETE FROM produse WHERE id = $id");
                $eroare = mysql_error();
            }
            else
            {
                $eroare = 'Produsul selectat nu poate fi accesat';
            }
            if (!$eroare)
            {
                header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
                exit;
            }
        case 'stergere':
            $q = mysql_query("SELECT * FROM produse 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 produsul '{$r['produs']}'?</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>Produse</h1>";
    echo "<table><tr><th style='width: 500px'>Denumire produs</th><th>&nbsp;</th></tr>";
    $q = mysql_query("SELECT * FROM produse ORDER BY produs");
    while ($r = mysql_fetch_assoc($q))
    {
        echo "<tr><td>{$r['produs']}</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();
?>