<?php

require_once('../init.php');
$GLOBALS['smarty']->assign('page','contrats_types');
$GLOBALS['template'] = 'contrats_types/contrats_types_list.tpl';
$GLOBALS['smarty']->assign('secondbar','contrats_types/contrats_types_secondbar.tpl');
$jsFiles[] = PUBLIC_HTML_ADMIN.'js/contrats_types.js';
$cssFiles[] = PUBLIC_HTML_ADMIN.'css/contrats.css';

if($id>0 && $action) {
    $infos = getContratsTypeDatas($id);
    switch($action) {
        // AJAX GET DATAS
        case "getDatas": die(json_encode($infos)); break;
        // EDIT
        case "edit": {
            $datas = getContratsTypeDatasFromRequest();
            $r = updateContratsType($id,$datas);
            if($r == 1) $r = updateContratsTypeLinks($id);
            die( strval( $r ) );
        } break;
        // DELETE
        case "delete" : die( strval( deleteContratsType($id) ) ); break;
        // DEFAULT
        default: die("NO ACTION");
    }
}
switch($action) {
    // ADD
    case "add": {
        $datas = getContratsTypeDatasFromRequest();
        die( strval( addContratsType($datas) ) );
    } break;
    // SELECT LIST
    case "select_list": {
        $GLOBALS['smarty'] -> assign('contrats_types_list', getContratsTypesList());
        die( $GLOBALS['smarty']->fetch("contrats_types/contrats_types_select_list.tpl") );
    } break;
    // DEFAULT - LIST
    default: {
        // REQUIRED LISTS & FILTRE
        $groupesList = getPaniersGroupesList();
        $groupe = getRequestAndSessionSave("groupe", "contrats_types_filtre_groupe", "groupe", "int", 0, $groupesList, true);
        $paniersTypesList = getPaniersTypesList('nom','ASC', false, $groupe);
        $filtres = array(
            "groupe" => $groupe,
            "frequence" => getRequestAndSessionSave("frequence", "contrats_types_filtre_frequence", "frequence", "txt", 0, array("hebdo", "quinz", "quinz_A", "quinz_B")),
            "panier" => getRequestAndSessionSave("panier", "contrats_types_filtre_panier", "panier", "int", 0, $paniersTypesList, true)
        );
        
        // LIMITS
        $limits = array("start" => 0, "end" => LIST_NB_LIGNES_PAR_PAGE, "nbItemsByPage" => LIST_NB_LIGNES_PAR_PAGE, "max" => getNbContratsTypesInList($filtres));
        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('contrats_types_list_order', 'contrats_types_list_sens', $allowContratsListOrder, 'nom');
        $list = getContratsTypesList($order["order"], $order["sens"], $filtres, $limits);
        $GLOBALS['smarty'] -> assign('list',$list);
        
        // LIST PART
        if(isset($_REQUEST['startListAt']) && (int)$_REQUEST['startListAt']>0) {
            $limits["list"] = $GLOBALS['smarty']->fetch("contrats_types/contrats_types_list_builder.tpl");
            die(json_encode($limits));
        }
        
        // REQUIRED LISTS
        $GLOBALS['smarty'] -> assign('paniers_groupes_list', $groupesList);
        $GLOBALS['smarty'] -> assign('paniers_list', $paniersTypesList);
        
        // DISPLAY
        display();
    }
}