<?php

/****** INIT PAGE ***********/
$GLOBALS['prefixe'] = '../';
$GLOBALS['admin'] = true;
include_once('../conf/conf.php');
if(!$GLOBALS['db_admin']['man']) {
    // CREATION DU DB MANAGER APP
    $GLOBALS['db_admin']['man'] = new dbSqlManager;
    $GLOBALS['db_admin']['man']->setDbConf(
        $GLOBALS['db_admin']['type'],
        $GLOBALS['db_admin']['db'],
        $GLOBALS['db_admin']['user'],
        $GLOBALS['db_admin']['pwd'],
        $GLOBALS['db_admin']['host']
    );
    $r = $GLOBALS['db_admin']['man']->connect();
    if($r!==true) die("ERREUR : impossible de se connecter à la base de donnée '".$GLOBALS['db_admin']['db']."' ! (".$r.")");
}
$GLOBALS['racine'] = 'legumes.php';
$GLOBALS['smarty']->assign('page','legumes');
$GLOBALS['smarty']->assign('secondbar','legumes/legumes_secondbar.tpl');

include_once(FUNCTIONS_DIR_PATH.'functions_legumes.php');

$action = false;
if(isset($_REQUEST['action'])) $action = strval( $_REQUEST['action'] );

if(isset($_REQUEST["ref"]) && (int)$_REQUEST["ref"]>0 && $action) {
    $id = intval($_REQUEST["ref"]);
    $infos = getLegumeDatas($id);
    
    switch($action) {
        // AJAX GET DATAS
        case "getDatas": die(json_encode($infos)); break;
        // EDIT
        case "edit": {
            $datas = getLegumeDatasFromRequest();
        
            // TARIF
            if(isset($datas["new_tarif"])) {
                $datas["new_tarif"]['legume'] = $id;
                $r = addLegumeTarif($datas["new_tarif"]);
                if((int)$r>0) $datas["tarif"] = $r;
                else die("ERREUR : une errreur est survenue durant l'enregistrement du nouveau tarif du légume. ($r)");
            }

            die( strval( updateLegume($id,$datas) ) );
        } break;
        // DELETE
        case "delete" : die( strval( deleteLegume($id) ) ); break;
        // DEFAULT
        default: die("NO ACTION");
    }
}
switch($action) {
    // ADD
    case "add": {
        $datas = getLegumeDatasFromRequest();
        $r = addLegume($datas);

        if((int)$r>0) {
            $id = $r;

            // TARIF
            if(isset($datas["new_tarif"])) {
                $datas["new_tarif"]['legume'] = $id;
                $r = addLegumeTarif($datas["new_tarif"]);
                if((int)$r>0) {
                    $tarif_id = $r;

                    // UPDATE CURRENT TARIF
                    $r = updateLegume($id, array("tarif" => $tarif_id));
                    if(!(int)$r>0) die("ERREUR : une errreur est survenue durant l'enregistrement du tarif dans la fiche légume. ($r)");
                }
                else die("ERREUR : une errreur est survenue durant l'enregistrement du tarif du légume. ($r)");
            }

            die( (string)$id );
        }
        else die($r);
    } break;
    // DELETE TARIF
    case "deleteTarif": {
        if(!isset($_REQUEST['tarif']) || !(int)$_REQUEST['tarif']>0) die("NO ID !");
        die( strval( deleteLegumeTarif(intval($_REQUEST['tarif'])) ) ); break;
    } break;
    // AUTOCOMPLETE LIST
    case "autocomplete_list": die( json_encode( getLegumesAutocompleteList() ) ); break;
    // DEFAULT - LIST
    default: {
        // SEARCH
        $search = getSearch("legumes_search");
        
        // LIMITS
        $limits = array("start" => 0, "end" => LIST_NB_LIGNES_PAR_PAGE, "nbItemsByPage" => LIST_NB_LIGNES_PAR_PAGE, "max" => getNbLegumesInList($search));
        if(isset($_REQUEST['startListAt']) && (int)$_REQUEST['startListAt']>0) {
            $limits['start'] = intval($_REQUEST['startListAt']);
            $limits['end'] = $limits['start'] + $limits['nbItemsByPage'];
            if($limits['end'] > $limits['max']) $limits['end'] = $limits['max'];
        }
        $limits['rest'] = $limits['max'] - $limits['end'];
        $GLOBALS['smarty'] -> assign('list_limits',$limits);
        
        // LIST
        $order = getListOrder('legumes_list_order', 'legumes_list_sens', $allowLegumesListOrder, 'prenom');
        $list = getLegumesList($order["order"], $order["sens"], $search, $limits);
        $GLOBALS['smarty'] -> assign('list',$list);
        
        // LIST PART
        if(isset($_REQUEST['startListAt']) && (int)$_REQUEST['startListAt']>0) {
            $limits["list"] = $GLOBALS['smarty']->fetch("legumes/legumes_list_builder.tpl");
            die(json_encode($limits));
        }

        // TEMPLATE
        $GLOBALS['template'] = 'legumes/legumes_list.tpl';
        $jsFiles[] = PUBLIC_HTML_ADMIN.'js/legumes.js';
        
        /****** DISPLAY PAGE ***********/
        $GLOBALS['smarty']->assign('racine',$GLOBALS['racine']);
        display();
    }
}