<?php

    $submit = getuserdata('submit');
    $actiune = getuserdata('actiune');
    $id = getuserdata('id', 0);
    $categorie = getuserdata('categorie', 0);
    $titlu = getuserdata('titlu', '');
    $descriere = getuserdata('descriere', '');
    $zi = getuserdata('zi', 0);
    $luna = getuserdata('luna', 0);
    $an = getuserdata('an', 0);
    $ora = getuserdata('ora', 0);
    $min = getuserdata('min', 0);
    $sec = getuserdata('sec', 0);
    $autoarh = getuserdata('autoarh', 0);
    
    switch ($actiune)
    {
        case 'do_editare':
            if ($submit != 'Salvează')
            {
                header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
                exit;
            }
            $q = mysql_query("SELECT * FROM stiri WHERE id = $id");
            if ($r = mysql_fetch_assoc($q))
            {
                mysql_query("UPDATE stiri SET titlu = '$titlu', descriere = '$descriere', data = '".
                            date('Y-m-d H:i:s', mktime($ora, $min, $sec, $luna, $zi, $an))."' WHERE id = $id");
                $eroare = mysql_error();
            }
            else
            {
                mysql_query("INSERT INTO stiri (categorie, titlu, descriere, data, activ, arhivat) ".
                            "VALUES ($categorie, '$titlu', '$descriere', '".
                            date('Y-m-d H:i:s', mktime($ora, $min, $sec, $luna, $zi, $an))."', 1, 0)");
                $eroare = mysql_error();
            }
            if ( (!$eroare) && ($autoarh) )
            {
                $q = mysql_query("SELECT MAX(DATE(data)) AS data FROM stiri WHERE categorie = $categorie");
                if ($r = mysql_fetch_assoc($q))
                {
                    $data_max = $r['data'];
                    if ($data_max)
                    {
                        mysql_query("UPDATE stiri SET arhivat = 1 WHERE DATE(data) < '$data_max' AND categorie = $categorie");
                        $eroare = mysql_error();                        
                    }
                }
            }
            if (!$eroare)
            {
                header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
                exit;
            }
        case 'editare':
            writepagetitle();
            writepagetop();
            writeinfolayer();
            $q = mysql_query("SELECT * FROM stiri WHERE id = $id");
            if ($r = mysql_fetch_assoc($q))
            {
                $categorie = $r['categorie'];
                $titlu = $r['titlu'];
                $descriere = $r['descriere'];
                $data = $r['data'];
                $zi = (int)substr($data, 8, 2);
                $luna = (int)substr($data, 5, 2);
                $an = (int)substr($data, 0, 4);
                $ora = (int)substr($data, 11, 2);
                $min = (int)substr($data, 14, 2);
                $sec = (int)substr($data, 17, 2);
            }
            else
            {
            	$data = getdate();
            	$zi = (int)$data['mday'];
            	$luna = (int)$data['month'];
            	$an = (int)$data['year'];
            	$ora = (int)$data['hours'];
            	$min = (int)$data['minutes'];
            	$sec = (int)$data['seconds'];
            }
            if ($eroare) echo "<div class='error'>$eroare</div>";
            echo "<form action='{$_SERVER['PHP_SELF']}' method='POST'>";
            echo "<input type='hidden' name='pagina' value='$pagina'>";
            echo "<input type='hidden' name='actiune' value='do_editare'>";
            echo "<input type='hidden' name='id' value='{$r['id']}'>";
            echo "<input type='hidden' name='categorie' value='$categorie'>";
            echo "<table class='formular'>";
            echo "<tr><th>Titlu:</th><td><input type='text' name='titlu' value='$titlu'></td></tr>";
            echo "<tr><th>Data:</th><td>";
            echo "<select name='zi'>";
            for ($i = 1; $i <= 31; $i++) echo "<option value='$i'".(($zi==$i)?' selected':'').">$i</option>";
            echo "</select>-";
            echo "<select name='luna'>";
            for ($i = 1; $i <= 12; $i++) echo "<option value='$i'".(($luna==$i)?' selected':'').">$i</option>";
            echo "</select>-";
            echo "<input type='text' name='an' value='$an' style='width: 50px'> ";
            echo "<select name='ora'>";
            for ($i = 0; $i <= 24; $i++) echo "<option value='$i'".(($ora==$i)?' selected':'').">$i</option>";
            echo "</select>:";
            echo "<select name='min'>";
            for ($i = 0; $i <= 59; $i++) echo "<option value='$i'".(($min==$i)?' selected':'').">$i</option>";
            echo "</select>:";
            echo "<select name='sec'>";
            for ($i = 0; $i <= 59; $i++) echo "<option value='$i'".(($sec==$i)?' selected':'').">$i</option>";
            echo "</select>";
            echo "</td></tr>";
            echo "<tr><th>Descriere:</th><td><textarea name='descriere'>$descriere</textarea></td></tr>";
            echo "<tr><td>&nbsp;</td><td>";
            echo "<input type='checkbox' name='autoarh' id='autoarh' value='1' checked style='width: auto'>";
            echo "<label for='autoarh'>Arhivează automat stirile din zilele precedente</label></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>";
            echo "</form>";
            writepagebottom();
            exit;
        case 'do_stergere':
            if ($submit != 'Șterge')
            {
                header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
                exit;
            }
            $q = mysql_query("SELECT * FROM stiri WHERE id = $id");
            if ($r = mysql_fetch_assoc($q))
            {
                mysql_query("DELETE FROM stiri WHERE id = $id");
                $eroare = mysql_error();
            }
            else
            {
                $eroare = 'Știrea selectată nu poate fi accesată';
            }
            if (!$eroare)
            {
                header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
                exit;
            }
        case 'stergere':
            $q = mysql_query("SELECT * FROM stiri WHERE id = $id");
            if ($r = mysql_fetch_assoc($q))
            {
                writepagetitle();
                writepagetop();
                writeinfolayer();
                $denumire = $r['titlu'];
                echo "<form action='{$_SERVER['PHP_SELF']}' method='POST'>";
                echo "<input type='hidden' name='pagina' value='$pagina'>";
                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 știrea '$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;
            }
        case 'activare':
            mysql_query("UPDATE stiri SET activ = 1 WHERE id = $id");
            $eroare = mysql_error();
            header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
            exit;
        case 'arhivare':
            mysql_query("UPDATE stiri SET arhivat = 1 WHERE id = $id");
            $eroare = mysql_error();
            header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
            exit;
        case 'dezactivare':
            mysql_query("UPDATE stiri SET activ = 0 WHERE id = $id");
            $eroare = mysql_error();
            header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
            exit;
        case 'dezarhivare':
            mysql_query("UPDATE stiri SET arhivat = 0 WHERE id = $id");
            $eroare = mysql_error();
            header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
            exit;
    }

    writepagetitle();
    writepagetop();
    writeinfolayer();
    echo "<span class='articol'>";
    echo "<h1>Știri medicale</h1>";
    echo "<table><tr><th>Titlu</th><th colspan='2'>Data</th></tr>";
    $q = mysql_query("SELECT * FROM stiri WHERE activ = 1 AND (arhivat = 0 OR arhivat IS NULL) AND categorie = 1 ORDER BY data DESC");
    while ($r = mysql_fetch_assoc($q))
    {
        echo "<tr><td>{$r['titlu']}</td><td>{$r['data']}</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='categorie' value='1'>";
        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='categorie' value='1'>";
        echo "<input type='hidden' name='id' value='{$r['id']}'>";
        echo "<input type='submit' name='submit' id='delete' value='Șterge'>";
        echo "</form>";
        echo "<form action='{$_SERVER['PHP_SELF']}' method='POST'>";
        echo "<input type='hidden' name='actiune' value='arhivare'>";
        echo "<input type='hidden' name='pagina' value='$pagina'>";
        echo "<input type='hidden' name='categorie' value='1'>";
        echo "<input type='hidden' name='id' value='{$r['id']}'>";
        echo "<input type='submit' name='submit' id='pack' value='Arhivează'>";
        echo "</form>";
        echo "<form action='{$_SERVER['PHP_SELF']}' method='POST'>";
        echo "<input type='hidden' name='actiune' value='dezactivare'>";
        echo "<input type='hidden' name='categorie' value='1'>";
        echo "<input type='hidden' name='pagina' value='$pagina'>";
        echo "<input type='hidden' name='id' value='{$r['id']}'>";
        echo "<input type='submit' name='submit' id='disable' value='Dezactivează'>";
        echo "</form></td></tr>";
    }
    echo "</table>";
    echo "<form action='{$_SERVER['PHP_SELF']}' method='POST'>";
    echo "<input type='hidden' name='categorie' value='1'>";
    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' style='width: 130px' value='Adaugă'>";
    echo "</form><br><br>";
    echo "<table><tr><th colspan='4'>Elemente arhivate (ultimele 10)</th></tr>";
    $q = mysql_query("SELECT * FROM stiri WHERE (arhivat = 1 AND activ = 1) AND categorie = 1 ORDER BY data DESC LIMIT 0,10");
    while ($r = mysql_fetch_assoc($q))
    {
        echo "<tr><td>{$r['titlu']}</td><td>{$r['data']}</td>";
        echo "<td class='butoane'>";
        echo "<form action='{$_SERVER['PHP_SELF']}' method='POST'>";
        echo "<input type='hidden' name='actiune' value='dezarhivare'>";
        echo "<input type='hidden' name='pagina' value='$pagina'>";
        echo "<input type='hidden' name='categorie' value='1'>";
        echo "<input type='hidden' name='id' value='{$r['id']}'>";
        echo "<input type='submit' name='submit' id='unpack' value='Dezarhivează'>";
        echo "</form></td></tr>";
    }
    echo "</table>";
    echo "<table><tr><th colspan='4'>Elemente dezactivate (ultimele 10)</th></tr>";
    $q = mysql_query("SELECT * FROM stiri WHERE (activ = 0 OR activ IS NULL) AND categorie = 1 ORDER BY data DESC LIMIT 0,10");
    while ($r = mysql_fetch_assoc($q))
    {
        echo "<tr><td>{$r['titlu']}</td><td>{$r['data']}</td>";
        echo "<td class='butoane'>";
        echo "<form action='{$_SERVER['PHP_SELF']}' method='POST'>";
        echo "<input type='hidden' name='actiune' value='activare'>";
        echo "<input type='hidden' name='pagina' value='$pagina'>";
        echo "<input type='hidden' name='categorie' value='1'>";
        echo "<input type='hidden' name='id' value='{$r['id']}'>";
        echo "<input type='submit' name='submit' id='enable' value='Activează'>";
        echo "</form></td></tr>";
    }
    echo "</table></span>";
    writepagebottom();
?>