<?php

    $submit = getuserdata('submit');
    $actiune = getuserdata('actiune');
    $id = getuserdata('id', 0);
    $pachet = getuserdata('pachet', '');
    $prescurtare = getuserdata('prescurtare', '');
    $conditii = getuserdata('conditii', '');
    
    $sterge_produs = 0; // id-ul produsului care trebuie sters
    $adauga_produs = 0; // id-ul produsului care trebuie adaugat
    
    // Verific daca se doreste stergerea sau adaugarea unui produs
    foreach (array_keys($_REQUEST) as $cheie)
    {
        if (substr($cheie, 0, 7) == 'pd_del_')
        {
            $submit = 'Salvează';
            $sterge_produs = (int)substr($cheie, 7);
        }
        elseif ($cheie == 'pd_adaugaprodus')
        {
            $submit = 'Salvează';
            $adauga_produs = getuserdata('pd_0_idprodus', 0);
        }
    }
    
    switch ($actiune)
    {
        case 'do_editare':
            // Salvez pachetul
            if ($submit != 'Salvează')
            {
                header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
                exit;
            }
            if ($pachet == '')
            {
                $eroare = 'Denumirea pachetului nu poate fi vidă';
            }
            else
            {
                $q = mysql_query("SELECT * FROM pachete WHERE id = $id");
                if ($r = mysql_fetch_assoc($q))
                {
                    mysql_query("UPDATE pachete SET pachet = '$pachet', prescurtare = '$prescurtare', conditii = '$conditii' WHERE id = $id");
                    $eroare = mysql_error();
                }
                else
                {
                    mysql_query("INSERT INTO pachete (produs, prescurtare, conditii) VALUES ('$produs', '$prescurtare', '$conditii')");
                    $id = mysql_insert_id();
                    $eroare = mysql_error();
                }
            }
            
            // Salvez detaliile pachetului
            foreach (array_keys($_REQUEST) as $cheie)
            {
                if ( (substr($cheie, 0, 3) == 'pd_') && ($cheie != 'pd_0') && (!$eroare) )
                {
                    $i = (int)substr($cheie, 3);
                    $pd_id = getuserdata("pd_$i", 0);
                    $pd_id_produs = getuserdata("pd_{$i}_idprodus", 0);
                    $pd_pret = getuserdata("pd_{$i}_pret", 0);
                    $pd_initial = getuserdata("pd_{$i}_initial", 0);
                    mysql_query("UPDATE pachete_detalii SET id_pachet = $id, id_produs = $pd_id_produs, pret = $pd_pret, initial = $pd_initial ".
                                "WHERE id = $pd_id");
                    $eroare = mysql_error();
                }
            }
            
            if ( ($sterge_produs) && (!$eroare) )
            {
                mysql_query("DELETE FROM pachete_detalii WHERE id = $sterge_produs");
                $eroare = mysql_error();
            }
            
            if ( ($adauga_produs) && (!$eroare) )
            {
                $pd_id_produs = getuserdata("pd_0_idprodus", 0);
                $pd_pret = getuserdata("pd_0_pret", 0);
                $pd_initial = getuserdata("pd_0_initial", 0);
                mysql_query("INSERT INTO pachete_detalii (id_pachet, id_produs, pret, initial) VALUES ($id, $pd_id_produs, $pd_pret, $pd_initial)");
                $eroare = mysql_error();
            }
            
            if ( (!$eroare) && ($adauga_produs == 0) && ($sterge_produs == 0) )
            {
                header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
                exit;
            }
        case 'editare':
            writepagetitle();
            writepagetop();
            writeinfolayer();
            $q = mysql_query("SELECT * FROM pachete WHERE id = $id");
            if ($r = mysql_fetch_assoc($q))
            {
                $pachet = $r['pachet'];
                $prescurtare = $r['prescurtare'];
                $conditii = $r['conditii'];
                $initial = 0;
            }
            if ($eroare) echo "<div style='color: red; text-align: center'>$eroare</div>";
            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 pachet:</th><td><input type='text' name='pachet' value='$pachet'></td></tr>";
            echo "<tr><th>Prescurtare:</th><td><input type='text' name='prescurtare' value='$prescurtare'></td></tr>";
            echo "<tr><th>Condiții:</th><td><input type='text' name='conditii' value='$conditii'></td></tr>";
            echo "<tr><th>Produse incluse:</th><td><table>";
            $qd = mysql_query("SELECT pd.*, p.produs FROM pachete_detalii pd JOIN produse p ON p.id = pd.id_produs WHERE pd.id_pachet = $id");
            while ($rd = mysql_fetch_assoc($qd))
            {
            	echo "<tr><td>{$rd['produs']}</td></tr><tr><td>";
            	echo "<input type='hidden' name='pd_{$rd['id']}' value='{$rd['id']}'>";
            	echo "<input type='hidden' name='pd_{$rd['id']}_idprodus' value='{$rd['id_produs']}'>";
            	echo "<input type='text' name='pd_{$rd['id']}_pret' value='{$rd['pret']}' style='width: 70px; margin-left: 2em;'> lei</td></tr>";
            	echo "<tr><td><label for='pd_chk_{$rd['id']}_initial' style='margin-left: 2em;'>".
                     "<input type='checkbox' name='pd_{$rd['id']}_initial' value='1' style='width: auto' id='pd_chk_{$rd['id']}_initial'";
            	if ($rd['initial']) echo ' checked';
            	echo "> Intră în prețul inițial de vânzare</label>";
            	echo "</td></tr><tr><td><input type='submit' name='pd_del_{$rd['id']}' value='Șterge produsul' ".
                     "style='margin-left: 2em; width: 200px; margin-bottom: 2em'></td></tr>";
            }
            // preiau produsele neincluse in pachet
            $qpn = mysql_query("SELECT * FROM produse WHERE id NOT IN (SELECT id_produs FROM pachete_detalii WHERE id_pachet = {$r['id']})");
            if (mysql_num_rows($qpn))
            {
                echo "<tr><td><hr>Adaugă un produs nou în pachet:<br><input type='hidden' name='pd_0' value='0'>";
                echo "<select name='pd_0_idprodus'>";
                while ($rpn = mysql_fetch_assoc($qpn))
                {
                    echo "<option value='{$rpn['id']}'>{$rpn['produs']}</option>";
                }
                echo "</select></td></tr><tr><td>";
            	echo "<input type='text' name='pd_0_pret' style='width: 70px; margin-left: 2em;'> lei</td></tr>";
            	echo "<tr><td><label for='pd_chk_0_initial' style='margin-left: 2em;'>".
                     "<input type='checkbox' name='pd_0_initial' value='1' style='width: auto' id='pd_chk_0_initial' checked>";
            	echo "Intră în prețul inițial de vânzare</label>";
            	echo "</td></tr><tr><td><input type='submit' name='pd_adaugaprodus' value='Adaugă produsul' ".
                     "style='margin-left: 2em; width: 200px; margin-bottom: 2em'></td></tr>";
            }
            echo "</table></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>Pachete de produse oferite la vânzare</h1>";
    echo "<table><tr><th style='width: 500px'>Denumire pachet</th><th>&nbsp;</th></tr>";
    $q = mysql_query("SELECT * FROM pachete ORDER BY pachet");
    while ($r = mysql_fetch_assoc($q))
    {
        echo "<tr><td>{$r['pachet']}</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();
?>