<?php

require_once('../init.php');
$GLOBALS['smarty']->assign('page','paniers_groupes');
$GLOBALS['template'] = 'paniers_groupes/paniers_groupes_list.tpl';
$GLOBALS['smarty']->assign('secondbar','paniers_groupes/paniers_groupes_secondbar.tpl');
$jsFiles[] = PUBLIC_HTML_ADMIN.'js/paniers_groupes.js';

if($id>0 && $action) {
    $infos = getPaniersGroupeDatas($id);
    switch($action) {
        // AJAX GET DATAS
        case "getDatas": die(json_encode($infos)); break;
        // EDIT
        case "edit": {
            $datas = getPaniersGroupeDatasFromRequest();
            die( strval( updatePaniersGroupe($id,$datas) ) );
        } break;
        // DELETE
        case "delete" : die( strval( deletePaniersGroupe($id) ) ); break;
        // DEFAULT
        default: die("NO ACTION");
    }
}
switch($action) {
    // ADD
    case "add": {
        $datas = getPaniersGroupeDatasFromRequest();
        die( strval( addPaniersGroupe($datas) ) );
    } break;
    // SELECT LIST
    case "select_list": {
        $GLOBALS['smarty'] -> assign('paniers_groupes_list', getPaniersGroupesList());
        die( $GLOBALS['smarty']->fetch("paniers_groupes/paniers_groupes_select_list.tpl") );
    } break;
    // DEFAULT - LIST
    default: {
        // SEARCH
        $search = getSearch("paniers_groupes_search");
        
        // LIMITS
        $limits = array("start" => 0, "end" => LIST_NB_LIGNES_PAR_PAGE, "nbItemsByPage" => LIST_NB_LIGNES_PAR_PAGE, "max" => getNbPaniersGroupesInList($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('paniers_groupes_list_order', 'paniers_groupes_list_sens', $allowPaniersGroupesListOrder, "nom");
        $list = getPaniersGroupesList($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("paniers_groupes/paniers_groupes_list_builder.tpl");
            die(json_encode($limits));
        }

        // DISPLAY
        display();
    }
}