<?php

    $submit = getuserdata('submit');
    $actiune = getuserdata('actiune');
    $id = getuserdata('id', 0);
    $titlu = getuserdata('titlu', '');
    $articol = getuserdata('articol', '');
    $aspect_titlu = getuserdata('aspect_titlu', 1);
    
    switch ($actiune)
    {
        case 'do_editare':
            if ($submit != 'Salvează')
            {
                header("Location: {$_SERVER['PHP_SELF']}");
                exit;
            }
            $q = mysql_query("SELECT * FROM articole WHERE id = $id");
            if ($r = mysql_fetch_assoc($q))
            {
                mysql_query("UPDATE articole SET titlu = '$titlu', articol = '$articol', aspect_titlu = $aspect_titlu WHERE id = $id");
                $eroare = mysql_error();
            }
            else
            {
                mysql_query("INSERT INTO articole (titlu, articol, aspect_titlu) VALUES ('$titlu', '$articol', $aspect_titlu)");
                $eroare = mysql_error();
            }
            if (!$eroare)
            {
                header("Location: {$_SERVER['PHP_SELF']}");
                exit;
            }
        case 'editare':
            writepagetitle();
            writepagetop();
            writeinfolayer();
            $q = mysql_query("SELECT * FROM articole WHERE id = $id");
            if ($r = mysql_fetch_assoc($q))
            {
                $titlu = $r['titlu'];
                $articol = $r['articol'];
                $aspect_titlu = $r['aspect_titlu'];
            }
            echo "<form action='{$_SERVER['PHP_SELF']}' method='POST'>";
            echo "<input type='hidden' name='actiune' value='do_editare'>";
            echo "<input type='hidden' name='id' value='{$r['id']}'>";
            echo "<center><table class='formular'>";
            echo "<tr><th>Titlu:</th><td><input type='text' name='titlu' value='$titlu'></td></tr>";
            echo "<tr><th>Aspect titlu:</th><td>";
			echo "<input type='radio' style='width: auto; border: none' name='aspect_titlu' value='1'".(($aspect_titlu == 1)?' checked':'').">mic</input>";
			echo "<input type='radio' style='width: auto; border: none' name='aspect_titlu' value='2'".(($aspect_titlu == 2)?' checked':'').">mare</input>";
			echo "</td></tr>";
            echo "<tr><th>Articol:</th><td><span class='editor'>";
            echo "<textarea name='articol' style='height: 400px'>$articol</textarea></span>  </td></tr>";
            echo "<tr><td>&nbsp;</td><td class='butoane'>";
            echo "<input type='submit' name='submit' id='save' value='Salvează' class='buton_salveaza'>";
            echo "<input type='submit' name='submit' id='cancel' value='Anulează' class='buton_anuleaza'>";
            echo "</td></tr></table></center></form>";
            writepagebottom();
            exit;
        case 'do_stergere':
            if ($submit != 'Șterge')
            {
                header("Location: {$_SERVER['PHP_SELF']}");
                exit;
            }
            $q = mysql_query("SELECT * FROM articole WHERE id = $id");
            if ($r = mysql_fetch_assoc($q))
            {
                mysql_query("DELETE FROM articole WHERE id = $id");
                $eroare = mysql_error();
            }
            else
            {
                $eroare = 'Articolul selectat nu poate fi accesat';
            }
            if (!$eroare)
            {
                header("Location: {$_SERVER['PHP_SELF']}");
                exit;
            }
        case 'stergere':
            $q = mysql_query("SELECT * FROM articole WHERE id = $id");
            if ($r = mysql_fetch_assoc($q))
            {
                writepagetitle();
                writepagetop();
                writeinfolayer();
                $titlu = $r['titlu'];
                $articol = $r['articol'];
                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 "<center><table class='formular'>";
                echo "<tr><th>Sigur doriți să ștergeți articolul '$titlu'?</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>Editare articole</h1>";
    echo "<table><tr><th style='width: 500px'>Articol</th><th>Acțiuni</th></tr>";
    $q = mysql_query("SELECT * FROM articole");
    while ($r = mysql_fetch_assoc($q))
    {
        echo "<tr><td>{$r['titlu']}</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='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='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' class='butoane'>";
    echo "<input type='hidden' name='actiune' value='editare'>";
    echo "<input type='hidden' name='id' value='0'>";
    echo "<input type='submit' name='submit' id='add' value='Adaugă'>";
    echo "</form></span>";    
    writepagebottom();
?>