<?php

    $submit = getuserdata('submit');
    $actiune = getuserdata('actiune');
    $id = getuserdata('id', 0);
    $numar = getuserdata('titlu', '');
    $zi = getuserdata('zi', 0);
    $luna = getuserdata('luna', 0);
    $an = getuserdata('an', 0);
    $id_locatie = getuserdata('id_locatie', 0);
    $zi_recoltare = getuserdata('zi_recoltare', 0);
    $luna_recoltare = getuserdata('luna_recoltare', 0);
    $an_recoltare = getuserdata('an_recoltare', 0);
    $zi_analiza = getuserdata('zi_analiza', 0);
    $luna_analiza = getuserdata('luna_analiza', 0);
    $an_analiza = getuserdata('an_analiza', 0);
    $id_prelevator = getuserdata('id_prelevator', 0);
    $concluzie = getuserdata('concluzie');
    
    switch ($actiune)
    {
        case 'do_editare':
            if ($submit != 'Salvează')
            {
                header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
                exit;
            }
            $q = mysql_query("SELECT * FROM analiza WHERE id = $id");
            if ($r = mysql_fetch_assoc($q))
            {
                mysql_query("UPDATE analiza SET numar = '$numar', data = '".
                            date('Y-m-d', mktime(0, 0, 0, $luna, $zi, $an))."', id_locatie = $id_locatie, data_recoltare = '".
                            date('Y-m-d', mktime(0, 0, 0, $luna_recoltare, $zi_recoltare, $an_recoltare))."', data_analiza = '".
                            date('Y-m-d', mktime(0, 0, 0, $luna_analiza, $zi_analiza, $an_analiza))."', ".
                            "id_prelevator = $id_prelevator, concluzie = '$concluzie' WHERE id = $id");
                $eroare = mysql_error();
            }
            else
            {
                mysql_query("INSERT INTO analiza (numar, data, id_locatie, data_recoltare, data_analiza, id_prelevator, concluzie) VALUES ($numar, '".
                            date('Y-m-d', mktime(0, 0, 0, $luna, $zi, $an))."', $id_locatie, '".
                            date('Y-m-d', mktime(0, 0, 0, $luna_recoltare, $zi_recoltare, $an_recoltare))."', '".
                            date('Y-m-d', mktime(0, 0, 0, $luna_analiza, $zi_analiza, $an_analiza))."', $id_prelevator, '$concluzie')");
                $id = mysql_insert_id();
                $eroare = mysql_error();
            }
            if (!$eroare)
            {
                $q1 = mysql_query("SELECT * FROM analiza_parametri");
                while ($r1 = mysql_fetch_assoc($q1))
                {
                    $q2 = mysql_query("SELECT * FROM analiza_detalii WHERE id_parametru = {$r1['id']} AND id_analiza = {$r['id']}");
                    if ( ($r2 = mysql_fetch_assoc($q2)) && (isset($_POST["val_{$r2['id']}"])) )
                    {
                        $valoare = getuserdata("val_{$r2['id']}");
                        mysql_query("UPDATE analiza_detalii SET valoare = '$valoare' WHERE id_parametru = {$r1['id']} AND id_analiza = {$r['id']}");
                    }
                    else
                    {
                        $valoare = getuserdata("val_{$r2['id']}");
                        mysql_query("INSERT INTO analiza_detalii(id_analiza, id_parametru, valoare) ".
                                    "VALUES ({$r['id']}, {$r1['id']}, '$valoare')");
                    }
                }
            }
            if (!$eroare)
            {
                header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
                exit;
            }
        case 'editare':
            writepagetitle();
            writepagetop();
            writeinfolayer();
            $q = mysql_query("SELECT * FROM analiza WHERE id = $id");
            if ($r = mysql_fetch_assoc($q))
            {
                $numar = $r['numar'];
                $data = $r['data'];
                $zi = (int)substr($data, 8, 2);
                $luna = (int)substr($data, 5, 2);
                $an = (int)substr($data, 0, 4);
                $id_locatie = $r['id_locatie'];
                $data_recoltare = $r['data_recoltare'];
                $zi_recoltare = (int)substr($data_recoltare, 8, 2);
                $luna_recoltare = (int)substr($data_recoltare, 5, 2);
                $an_recoltare = (int)substr($data_recoltare, 0, 4);
                $data_analiza = $r['data_analiza'];
                $zi_analiza = (int)substr($data_analiza, 8, 2);
                $luna_analiza = (int)substr($data_analiza, 5, 2);
                $an_analiza = (int)substr($data_analiza, 0, 4);
                $id_prelevator = $r['id_prelevator'];
                $concluzie = $r['concluzie'];
            }
            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'>";
            echo "<tr><th>Număr:</th><td><input type='text' name='numar' value='$numar'></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>-<input type='text' name='an' value='$an' style='width: 50px'></td></tr>";
            echo "<tr><th>Locație:</th><td><select name='id_locatie' style='width: 200px'>";
            $q1 = mysql_query("SELECT * FROM analiza_locatii ORDER BY denumire");
            while ($r1 = mysql_fetch_assoc($q1)) echo "<option value='{$r1['id']}'".(($r1['id']==$id_locatie)?' selected':'').">{$r1['denumire']}</option>";
            echo "</select></td></tr>";
            echo "<tr><th>Data recoltare:</th><td>";
            echo "<select name='zi_recoltare'>";
            for ($i = 1; $i <= 31; $i++) echo "<option value='$i'".(($zi_recoltare==$i)?' selected':'').">$i</option>";
            echo "</select>-";
            echo "<select name='luna_recoltare'>";
            for ($i = 1; $i <= 12; $i++) echo "<option value='$i'".(($luna_recoltare==$i)?' selected':'').">$i</option>";
            echo "</select>-<input type='text' name='an_recoltare' value='$an_recoltare' style='width: 50px'></td></tr>";
            echo "<tr><th>Data analiză:</th><td>";
            echo "<select name='zi_analiza'>";
            for ($i = 1; $i <= 31; $i++) echo "<option value='$i'".(($zi_analiza==$i)?' selected':'').">$i</option>";
            echo "</select>-";
            echo "<select name='luna_analiza'>";
            for ($i = 1; $i <= 12; $i++) echo "<option value='$i'".(($luna_analiza==$i)?' selected':'').">$i</option>";
            echo "</select>-<input type='text' name='an_analiza' value='$an_analiza' style='width: 50px'></td></tr>";
            echo "<tr><th>Prelevator:</th><td><select name='id_prelevator' style='width: 200px'>";
            $q1 = mysql_query("SELECT * FROM analiza_prelevatori ORDER BY denumire");
            while ($r1 = mysql_fetch_assoc($q1)) echo "<option value='{$r1['id']}'".(($r1['id']==$id_prelevator)?' selected':'').">{$r1['denumire']}</option>";
            echo "</select></td></tr>";
            echo "<tr><td colspan='2'><table border='1'>";
            echo "<tr><th>Nr. crt.</th><th>Parametru determinat</th><th>U.M.</th><th>Metoda de analiză</th><th>Valoarea obținută</th>";
            echo "<th>Limite maxime admise de Legea 458/2002 și 311/2004</th></tr>";
            $qv = mysql_query("SELECT * FROM analiza_parametri P LEFT JOIN analiza_detalii D ON P.id = D.id_parametru AND D.id_analiza = {$r['id']} ".
                              "ORDER BY P.nrcrt");
            while ($rv = mysql_fetch_assoc($qv))
                echo "<tr><td>{$rv['nrcrt']}</td><td>{$rv['denumire']}</td><td>{$rv['um']}</td><td>{$rv['metoda']}</td>".
                     "<td><input type='text' name='val_{$rv['id']}' value='{$rv['valoare']}'></td><td>{$rv['limita']}</td></tr>";            
            echo "</table></td></tr>";            
            echo "<tr><th>Concluzie:</th><td><textarea name='concluzie'><span class='articol'>$concluzie</span></textarea></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 comenzi WHERE id = $id");
            if ($r = mysql_fetch_assoc($q))
            {
                mysql_query("DELETE FROM comenzi WHERE id = $id");
                mysql_query("DELETE FROM comeezi_detalii WHERE id_comanda = $id");
                $eroare = mysql_error();
            }
            else
            {
                $eroare = 'Comanda selectată nu poate fi accesată';
            }
            if (!$eroare)
            {
                header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
                exit;
            }
        case 'stergere':
            $q = mysql_query("SELECT * FROM comenzi 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='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 comanda selectată?</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 'do_procesare':
            if ($submit != 'Procesează')
            {
                header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
                exit;
            }
            $q = mysql_query("SELECT * FROM comenzi WHERE id = $id");
            if ($r = mysql_fetch_assoc($q))
            {
                mysql_query("UPDATE comenzi SET procesata = 1 WHERE id = $id");
                $eroare = mysql_error();
            }
            else
            {
                $eroare = 'Comanda selectată nu poate fi accesată';
            }
            if (!$eroare)
            {
                header("Location: {$_SERVER['PHP_SELF']}?pagina=$pagina");
                exit;
            }
        case 'procesare':
            $q = mysql_query("SELECT * FROM comenzi 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='pagina' value='$pagina'>";
                echo "<input type='hidden' name='actiune' value='do_procesare'>";
                echo "<input type='hidden' name='id' value='{$r['id']}'>";
                echo "<center><table class='formular'>";
                echo "<tr><th>Sigur doriți să setați comanda ca fiind procesată?</th></tr>";
                echo "<tr><td class='butoane'>";
                echo "<input type='submit' name='submit' id='save' value='Procesează'>";
                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>Comenzi clienți</h1>";
    echo "<table><tr><th>Număr</th><th>Data</th><th>Pachet</th><th>Cantitate</th><th>Acțiuni</th></tr>";
    $q = mysql_query("SELECT C.*, P.pachet, S.specializare, TP.tip_plata ". 
                     "FROM comenzi C ".
                     "JOIN pachete P ON P.id = C.id_pachet ".
                     "JOIN specializari S ON S.id = C.id_specializare ".
                     "JOIN tipuri_plata TP ON TP.id = C.id_tipplata ".
                     "WHERE procesata = 0 ".
                     "ORDER BY data DESC");
    while ($r = mysql_fetch_assoc($q))
    {
        echo "<tr><td>{$r['id']}</td><td>{$r['data']}</td><td>{$r['pachet']}</td><td>{$r['cantitate']}</td>";
        echo "<td class='butoane'>";
        echo "<form action='{$_SERVER['PHP_SELF']}' method='POST'>";
        echo "<input type='hidden' name='actiune' value='procesare'>";
        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='Procesează'>";
        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 "<tr><td colspan='2' style='background-color: #EEEEEE; text-align: right; padding-right: 1em;'>Nume:</td>".
             "<td colspan='3' style='background-color: #EEEEEE; padding-left: 1em;'>{$r['nume']}</td></tr>";
        echo "<tr><td colspan='2' style='background-color: #EEEEEE; text-align: right; padding-right: 1em;'>Prenume:</td>".
             "<td colspan='3' style='background-color: #EEEEEE; padding-left: 1em;'>{$r['prenume']}</td></tr>";
        echo "<tr><td colspan='2' style='background-color: #EEEEEE; text-align: right; padding-right: 1em;'>Adresa:</td>".
             "<td colspan='3' style='background-color: #EEEEEE; padding-left: 1em;'>{$r['adresa']}</td></tr>";
        echo "<tr><td colspan='2' style='background-color: #EEEEEE; text-align: right; padding-right: 1em;'>Oraș:</td>".
             "<td colspan='3' style='background-color: #EEEEEE; padding-left: 1em;'>{$r['oras']}</td></tr>";
        echo "<tr><td colspan='2' style='background-color: #EEEEEE; text-align: right; padding-right: 1em;'>Cod poștal:</td>".
             "<td colspan='3' style='background-color: #EEEEEE; padding-left: 1em;'>{$r['cod_postal']}</td></tr>";
        echo "<tr><td colspan='2' style='background-color: #EEEEEE; text-align: right; padding-right: 1em;'>Telefon:</td>".
             "<td colspan='3' style='background-color: #EEEEEE; padding-left: 1em;'>{$r['telefon']}</td></tr>";
        echo "<tr><td colspan='2' style='background-color: #EEEEEE; text-align: right; padding-right: 1em;'>E-mail:</td>".
             "<td colspan='3' style='background-color: #EEEEEE; padding-left: 1em;'>{$r['email']}</td></tr>";
        echo "<tr><td colspan='2' style='background-color: #EEEEEE; text-align: right; padding-right: 1em;'>Tip plata:</td>".
             "<td colspan='3' style='background-color: #EEEEEE; padding-left: 1em;'>{$r['tip_plata']}";
        $qd = mysql_query("SELECT CD.*, TPD.caracteristica FROM comenzi_detalii CD ".
                          "JOIN tipuri_plata_detalii TPD ON TPD.id = CD.id_detaliu_tip_plata ".
                          "WHERE id_comanda = {$r['id']}");
        while ($rd = mysql_fetch_assoc($qd))
        {
            echo "<br>{$rd['caracteristica']}: {$rd['valoare']}";
        }
        echo "</td></tr>";
    }
    echo "</table></span>";
    writepagebottom();
?>