<?php

    $submit = getuserdata('submit');
    $actiune = getuserdata('actiune');
    $id = getuserdata('id', 0);
    $id_parinte = getuserdata('id_parinte', 0);
    $denumire = getuserdata('denumire', '');
    $descriere = getuserdata('descriere', '');
    $meniu = getuserdata('meniu', 0);
    $id_meniu = getuserdata('id_meniu', 0);
    $id_articol = getuserdata('id_articol', 0);
    $id_modul = getuserdata('id_modul', 0);
    $link = getuserdata('link', '');
    $fundal = getuserdata('fundal', '');
    $imagine_antet_meniu = getuserdata('imagine_antet', '');
    $imagine = getuserdata('imagine', '');
    $butoane_cumparare = getuserdata('butoane_cumparare', 0);
    $medici_colaboratori = getuserdata('medici_colaboratori', 0);
    $margine = getuserdata('margine', '0px');
    
    switch ($actiune)
    {
        case 'do_editare':
            if ($submit != 'Salvează')
            {
                header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
                exit;
            }
            $q = mysql_query("SELECT * FROM meniu WHERE id = $id");
            if ($r = mysql_fetch_assoc($q))
            {
                if ($id_parinte != (int)$r['id_parinte'])
                {
                    mysql_query("UPDATE meniu SET nrcrt = nrcrt - 1 WHERE ".
                                (($r['id_parinte'] == 0)?"(id_parinte = 0 OR id_parinte IS NULL)":"id_parinte = {$r['id_parinte']}").
                                " AND meniu = 1 AND nrcrt > {$r['nrcrt']}");
                    $q1 = mysql_query("SELECT MAX(IFNULL(nrcrt, 0))+1 AS nrcrt FROM meniu WHERE ".
                          (($r['id_parinte'] == 0)?"(id_parinte = 0 OR id_parinte IS NULL)":"id_parinte = $id_parinte")." AND meniu = $meniu");
                    $r1 = mysql_fetch_assoc($q1);
                    mysql_query("UPDATE meniu SET nrcrt = {$r1['nrcrt']}, id_parinte = $id_parinte WHERE id = $id");
                }
                mysql_query("UPDATE meniu SET denumire = '$denumire', descriere = '$descriere', id_meniu = $id_meniu, id_articol = $id_articol, ".
                            "id_modul = $id_modul, link = '$link', fundal = '$fundal', imagine_antet = '$imagine_antet_meniu', ".
                            "imagine = '$imagine', butoane_cumparare = $butoane_cumparare, medici_colaboratori = $medici_colaboratori, ".
							"margine = '$margine' WHERE id = $id");
                $eroare = mysql_error();
            }
            else
            {
                $q1 = mysql_query("SELECT MAX(IFNULL(nrcrt, 0))+1 AS nrcrt FROM meniu WHERE ".
                      (($r['id_parinte'] == 0)?"(id_parinte = 0 OR id_parinte IS NULL)":"id_parinte = $id_parinte")." AND meniu = 1");
                $r1 = mysql_fetch_assoc($q1);
                mysql_query("INSERT INTO meniu (id_parinte, denumire, descriere, id_meniu, id_articol, id_modul, link, fundal, imagine_antet, ".
                            "imagine, nrcrt, meniu, butoane_cumparare, medici_colaboratori, margine) VALUES ($id_parinte, '$denumire', '$descriere', ".
							"$id_meniu, $id_articol, $id_modul, '$link', '$fundal', '$imagine_antet_meniu', '$imagine', {$r1['nrcrt']}, $meniu, ".
							"$butoane_cumparare, $medici_colaboratori, '$margine')");
                $eroare = mysql_error();
            }
            if (!$eroare)
            {
                header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
                exit;
            }
        case 'editare':
            writepagetitle();
            writepagetop();
            writeinfolayer();
            echo "1. $descriere";
            $q = mysql_query("SELECT * FROM meniu WHERE id = $id");
            if ($r = mysql_fetch_assoc($q))
            {
                $id_parinte = $r['id_parinte'];
                $meniu = $r['meniu'];
                $denumire = $r['denumire'];
                $descriere = $r['descriere'];
                $id_meniu = $r['id_meniu'];
                $id_articol = $r['id_articol'];
                $id_modul = $r['id_modul'];
                $link = $r['link'];
                $fundal = $r['fundal'];
                $imagine_antet_meniu = $r['imagine_antet'];
                $imagine = $r['imagine'];
                $butoane_cumparare = $r['butoane_cumparare'];
                $medici_colaboratori = $r['medici_colaboratori'];
                $margine = $r['margine'];
            }
            echo "2. $descriere";
            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 "<center><table class='formular'>";
            if ($meniu == 1)
            {
            	echo "<tr><th>Meniu:</th><td><select name='id_parinte'><option value='0'";
            	if ($id_parinte == 0) echo ' selected';
            	echo ">&laquo; principal &raquo;</option>";
            	$q = mysql_query("SELECT * FROM meniu WHERE (id_parinte = 0 OR id_parinte IS NULL) AND meniu = $meniu");
            	while ($r = mysql_fetch_assoc($q))
            	{
                	echo "<option value='{$r['id']}'";
                	if ($id_parinte == $r['id']) echo ' selected';
                	echo ">{$r['denumire']}</option>";
            	}
            	echo "</select></td></tr>";
            }
            else echo "<input type='hidden' name='id_parinte' value='0'>";
            echo "<input type='hidden' name='meniu' value='$meniu'>";
            echo "<tr><th>Denumire:</th><td><input type='text' name='denumire' value='$denumire'></td></tr>";
            if ($meniu == 3)
            {
            	echo "<tr><th>Descriere:</th><td><span class='editor_meniu'><textarea name='descriere' style='height: 50px'>$descriere</textarea></span></td></tr>";
            }
            else
            	echo "<input type='hidden' name='descriere' value=''>";
            echo "<tr><th>Acțiune:</th><td>";
            echo "<input type='radio' name='tip_actiune' id='radio_meniu' class='radio' style='width: auto; border: none;'";
            if ($id_meniu) echo ' checked';
            echo "><label for='radio_meniu'>accesare meniu:</label>";
            echo "<select name='id_meniu' style='width: 200px; margin-left: 20px'><option value='0'";
            if ($id_meniu == 0) echo ' selected';
            echo ">&nbsp;</option>";
            $q = mysql_query("SELECT meniu.*, m.denumire AS p FROM meniu LEFT JOIN meniu AS m ON m.id = meniu.id_parinte ".
                             "WHERE meniu.meniu = 1 ORDER BY m.nrcrt, meniu.nrcrt");
            while ($r = mysql_fetch_assoc($q))
            {
                echo "<option value='{$r['id']}'";
                if ($id_meniu == $r['id']) echo ' selected';
                echo ">".(($r['p']!='')?($r['p'].' / '):'')."{$r['denumire']}</option>";
            }
            echo "</select>";
            echo "<br><input type='radio' name='tip_actiune' id='radio_articol' class='radio' style='width: auto; border: none;'";
            if ($id_articol) echo ' checked';
            echo "><label for='radio_articol'>afișare articol:</label>";
            echo "<select name='id_articol' style='width: 200px; margin-left: 20px'><option value='0'";
            if ($id_meniu == 0) echo ' selected';
            echo ">&nbsp;</option>";
            $q = mysql_query("SELECT * FROM articole ORDER BY titlu");
            while ($r = mysql_fetch_assoc($q))
            {
                echo "<option value='{$r['id']}'";
                if ($id_articol == $r['id']) echo ' selected';
                echo ">{$r['titlu']}</option>";
            }
            echo "</select>";
            echo "<br><input type='radio' name='tip_actiune' id='radio_modul' class='radio' style='width: auto; border: none;'";
            if ($id_modul) echo ' checked';
            echo "><label for='radio_modul'>afișare modul:</label>";
            echo "<select name='id_modul' style='width: 200px; margin-left: 20px'><option value='0'";
            if ($id_meniu == 0) echo ' selected';
            echo ">&nbsp;</option>";
            $q = mysql_query("SELECT * FROM module ORDER BY titlu");
            while ($r = mysql_fetch_assoc($q))
            {
                echo "<option value='{$r['id']}'";
                if ($id_modul == $r['id']) echo ' selected';
                echo ">{$r['titlu']}</option>";
            }
            echo "</select>";
            echo "<br><input type='radio' name='tip_actiune' id='radio_link' class='radio' style='width: auto; border: none;'";
            if ($link) echo ' checked';
            echo "><label for='radio_link'>link extern:</label>";
            echo "<input type='text' name='link' value='$link' style='margin-left: 20px'>";
            echo "</td></tr>";
            echo "<tr><th>Imagine antet:</th><td><input type='text' name='imagine_antet' value='$imagine_antet_meniu'></td></tr>";
            echo "<tr><th>Imagine:</th><td><input type='text' name='imagine' value='$imagine'></td></tr>";
            echo "<tr><th>&nbsp;</th><td><label for='butoane_cumparare'>".
			     "<input type='checkbox' style='width: auto; border: none' name='butoane_cumparare' id='butoane_cumparare' value='1'".
				 (($butoane_cumparare == 1)?' checked':'').
				 "> Afișează butoanele de cumpărare</label></td></tr>";
            echo "<tr><th>&nbsp;</th><td><label for='medici_colaboratori'>".
			     "<input type='checkbox' style='width: auto; border: none' name='medici_colaboratori' id='medici_colaboratori' value='1'".
				 (($medici_colaboratori == 1)?' checked':'').
				 "> Afișează medicii colaboratori</label></td></tr>";
            if (($meniu == 1) && ($id_parinte == 0))
            {
            	echo "<tr><th>Margine dreapta:</th><td><input name='margine' value='$margine'></td></tr>";
            }
            else
            	echo "<input type='hidden' name='margine' value=''>";
            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']}?pagina=$pagina");
                exit;
            }
            $q = mysql_query("SELECT * FROM meniu WHERE id = $id");
            if ($r = mysql_fetch_assoc($q))
            {
                mysql_query("UPDATE meniu SET nrcrt = nrcrt - 1 WHERE ".
                            (($r['id_parinte'] == 0)?"(id_parinte = 0 OR id_parinte IS NULL)":"id_parinte = {$r['id_parinte']}").
                            " AND meniu = {$r['meniu']} AND nrcrt > {$r['nrcrt']}");
                mysql_query("DELETE FROM meniu WHERE id = $id OR id_parinte = $id");
                $eroare = mysql_error();
            }
            else
            {
                $eroare = 'Meniul selectat nu poate fi accesat';
            }
            if (!$eroare)
            {
                header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
                exit;
            }
        case 'stergere':
            $q = mysql_query("SELECT * FROM meniu WHERE id = $id");
            if ($r = mysql_fetch_assoc($q))
            {
                writepagetitle();
                writepagetop();
                writeinfolayer();
                $denumire = $r['denumire'];
                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 opțiunea '$denumire'?</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>Meniu principal</h1>";
    $q = mysql_query("SELECT * FROM meniu WHERE (id_parinte = 0 OR id_parinte IS NULL) AND meniu = 1 ORDER BY nrcrt");
    while ($r = mysql_fetch_assoc($q))
    {
        echo "<table><tr><th style='width: 500px'>{$r['denumire']}</th>";
        echo "<th 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='hidden' name='id_parinte' value='0'>";
        echo "<input type='hidden' name='meniu' value='1'>";
        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='hidden' name='id_parinte' value='0'>";
        echo "<input type='hidden' name='meniu' value='1'>";
        echo "<input type='submit' name='submit' id='delete' value='Șterge'>";
        echo "</form></th></tr>";
        $qs = mysql_query("SELECT * FROM meniu WHERE id_parinte = {$r['id']} ORDER BY nrcrt");
        while ($rs = mysql_fetch_assoc($qs))
        {
            echo "<tr><td>{$rs['denumire']}</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='{$rs['id']}'>";
            echo "<input type='hidden' name='id_parinte' value='{$r['id']}'>";
            echo "<input type='hidden' name='meniu' value='1'>";
            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='{$rs['id']}'>";
            echo "<input type='hidden' name='id_parinte' value='{$r['id']}'>";
            echo "<input type='hidden' name='meniu' value='1'>";
            echo "<input type='submit' name='submit' id='delete' value='Șterge'>";
            echo "</form></td></tr>";
        }
        echo "<tr><td class='butoane' colspan='2' style='border-top: solid 1px'>";
        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='hidden' name='id_parinte' value='{$r['id']}'>";
        echo "<input type='hidden' name='meniu' value='1'>";
        echo "<input type='submit' name='submit' id='add' style='width: 155px' value='Adaugă opțiune'>";
        echo "</form></td></tr></table><br>";
    }    
    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='hidden' name='id_parinte' value='0'>";
    echo "<input type='hidden' name='meniu' value='1'>";
    echo "<input type='submit' name='submit' id='add' style='width: 130px' value='Adaugă meniu'>";
    echo "</form></span>";

    echo "<h1>Butoane comandă</h1><table>";
    $q = mysql_query("SELECT * FROM meniu WHERE meniu = 2 ORDER BY nrcrt");
    while ($r = mysql_fetch_assoc($q))
    {
        echo "<tr><td style='width: 500px'>{$r['denumire']}</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='hidden' name='id_parinte' value='0'>";
        echo "<input type='hidden' name='meniu' value='2'>";
        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='hidden' name='id_parinte' value='0'>";
        echo "<input type='hidden' name='meniu' value='2'>";
        echo "<input type='submit' name='submit' id='delete' value='Șterge'>";
        echo "</form></td></tr>";
    }    
    echo "</table><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='hidden' name='id_parinte' value='0'>";
    echo "<input type='hidden' name='meniu' value='2'>";
    echo "<input type='submit' name='submit' id='add' style='width: 130px' value='Adaugă buton'>";
    echo "</form></span>";

    echo "<h1>Meniu dreapta</h1><table>";
    $q = mysql_query("SELECT * FROM meniu WHERE meniu = 3 ORDER BY nrcrt");
    while ($r = mysql_fetch_assoc($q))
    {
        echo "<tr><td style='width: 500px'>{$r['denumire']}</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='hidden' name='id_parinte' value='0'>";
        echo "<input type='hidden' name='meniu' value='3'>";
        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='hidden' name='id_parinte' value='0'>";
        echo "<input type='hidden' name='meniu' value='3'>";
        echo "<input type='submit' name='submit' id='delete' value='Șterge'>";
        echo "</form></td></tr>";
    }    
    echo "</table><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='hidden' name='id_parinte' value='0'>";
    echo "<input type='hidden' name='meniu' value='3'>";
    echo "<input type='submit' name='submit' id='add' style='width: 130px' value='Adaugă opțiune'>";
    echo "</form></span>";

    echo "<h1>Meniu jos</h1><table>";
    $q = mysql_query("SELECT * FROM meniu WHERE meniu = 4 ORDER BY nrcrt");
    while ($r = mysql_fetch_assoc($q))
    {
        echo "<tr><td style='width: 500px'>{$r['denumire']}</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='hidden' name='id_parinte' value='0'>";
        echo "<input type='hidden' name='meniu' value='4'>";
        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='hidden' name='id_parinte' value='0'>";
        echo "<input type='hidden' name='meniu' value='4'>";
        echo "<input type='submit' name='submit' id='delete' value='Șterge'>";
        echo "</form></td></tr>";
    }    
    echo "</table><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='hidden' name='id_parinte' value='0'>";
    echo "<input type='hidden' name='meniu' value='4'>";
    echo "<input type='submit' name='submit' id='add' style='width: 130px' value='Adaugă opțiune'>";
    echo "</form></span>";

    writepagebottom();
?>