<?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'] = 'contrats.php';
$GLOBALS['smarty']->assign('page','contrats');
$GLOBALS['smarty']->assign('secondbar','contrats/contrats_secondbar.tpl');

include_once(FUNCTIONS_DIR_PATH.'functions_contrats.php');
include_once(FUNCTIONS_DIR_PATH.'functions_contrats_types.php');
include_once(FUNCTIONS_DIR_PATH.'functions_paniers_groupes.php');
include_once(FUNCTIONS_DIR_PATH.'functions_paniers_types.php');
include_once(FUNCTIONS_DIR_PATH.'functions_lieux.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 = getContratDatas($id);
    
    switch($action) {
        // AJAX GET DATAS
        case "getDatas": die(json_encode($infos)); break;
        // MODAL VIEW - GET TAB
        case "modalView_getTab": {
            $GLOBALS['smarty'] -> assign('infos', $infos);
            $tabs = array(
                "paniers"   => "contrats/contrat_modal_view_tab_paniers.tpl"
            );
            if(isset($_REQUEST['tab']) && array_key_exists($_REQUEST['tab'], $tabs)) die( $GLOBALS['smarty']->fetch($tabs[$_REQUEST['tab']]) );
            die("NO TAB");
        } break;
        // EDIT
        case "edit": {
            $datas = getContratDatasFromRequest();
            die( strval( updateContrat($id,$datas) ) );
        } break;
        // ARCHIVE
        case "archive": die( strval( archiveContrat($id, true) ) ); break;
        // UNARCHIVE
        case "unarchive": die( strval( archiveContrat($id, false) ) ); break;
        // DELETE
        case "delete" : die( strval( deleteContrat($id) ) ); break;
        // DEFAULT
        default: die("NO ACTION");
    }
}

switch($action) {
    /****** CONTRATS TYPES ***********/
    // ADD CONTRATS TYPE
    case "add_contrats_type": {
        $datas = getContratsTypeDatasFromRequest();
        die( strval( addContratsType($datas) ) );
    } break;
    // EDIT CONTRATS TYPE
    case "edit_contrats_type": {
        $id = isset($_REQUEST['type_ref']) ? intval($_REQUEST['type_ref']) : 0;
        if($id>0) {
            $datas = getContratsTypeDatasFromRequest();
            die( strval( updateContratsType($id, $datas) ) );
        }
        else die("ERREUR : aucune référence de type de contrat transmis...");
    } break;
    // DELETE CONTRATS TYPE
    case "delete_contrats_type": {
        $id = isset($_REQUEST['type_ref']) ? intval($_REQUEST['type_ref']) : 0;
        if($id>0) die( strval( deleteContratsType($id) ) );
        else die("ERREUR : aucune référence de type de contrat transmis...");
    } break;
    // CONTRATS TYPES SELECT LIST
    case "contrats_types_select_list": {
        $GLOBALS['smarty'] -> assign('contrats_types_list', getContratsTypesList());
        die( $GLOBALS['smarty']->fetch("contrats/contrats_types_select_list.tpl") );
    } break;
    
    /****** LIEUX DE DEPOT ***********/
    // ADD LIEU
    case "add_lieu": {
        $datas = getLieuDatasFromRequest();
        die( strval( addLieu($datas) ) );
    } break;
    // EDIT LIEU
    case "edit_lieu": {
        $id = isset($_REQUEST['lieu_ref']) ? intval($_REQUEST['lieu_ref']) : 0;
        if($id>0) {
            $datas = getLieuDatasFromRequest();
            die( strval( updateLieu($id, $datas) ) );
        }
        else die("ERREUR : aucune référence de lieu transmis...");
    } break;
    // DELETE LIEU
    case "delete_lieu": {
        $id = isset($_REQUEST['lieu_ref']) ? intval($_REQUEST['lieu_ref']) : 0;
        if($id>0) die( strval( deleteLieu($id) ) );
        else die("ERREUR : aucune référence de lieu transmis...");
    } break;
    // LIEUX SELECT LIST
    case "lieux_select_list": {
        $GLOBALS['smarty'] -> assign('lieux_list', getLieuxList());
        die( $GLOBALS['smarty']->fetch("contrats/lieux_select_list.tpl") );
    } break;
    
    /****** CONTRAT ***********/
    // ADD
    case "add": {
        $datas = getContratDatasFromRequest();
        die( strval( addContrat($datas) ) );
    } break;
    // AUTOCOMPLETE LIST
    case "autocomplete_list": die( json_encode( getClientsAutocompleteList() ) ); break;
    // DEFAULT - LIST
    default: {
        // REQUIRED LISTS & FILTRE
        $groupesList = getPaniersGroupesList();
        $groupe = getRequestAndSessionSave("groupe", "livraisons_filtre_groupe", "groupe", "int", 0, $groupesList, true);
        $paniersTypesList = getPaniersTypesList('nom','ASC', false, $groupe);
        $lieuxList = getLieuxList();
        $filtres = array(
            "groupe" => $groupe,
            "frequence" => getRequestAndSessionSave("frequence", "livraisons_filtre_frequence", "frequence", "txt", 0, array("hebdo", "quinz", "quinz_A", "quinz_B")),
            "panier" => getRequestAndSessionSave("panier", "livraisons_filtre_panier", "panier", "int", 0, $paniersTypesList, true),
            "lieu" => getRequestAndSessionSave("lieu", "livraisons_filtre_lieu", "lieu", "int", 0, $lieuxList, true),
            "archive" => getRequestAndSessionSave("archive", "livraisons_filtre_archive", "archive", "int", 0, array(-1,0,1))
        );
        
        // SEARCH
        $search = getSearch("contrats_search");
        
        // LIMITS
        $limits = array("start" => 0, "end" => LIST_NB_LIGNES_PAR_PAGE, "nbItemsByPage" => LIST_NB_LIGNES_PAR_PAGE, "max" => getNbContratsInList($filtres, $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('contrats_list_order', 'contrats_list_sens', $allowContratsListOrder, 'prenom');
        $list = getContratsList($order["order"], $order["sens"], $filtres, $search, $limits);
        $GLOBALS['smarty'] -> assign('list',$list);
        
        // LIST PART
        if(isset($_REQUEST['startListAt']) && (int)$_REQUEST['startListAt']>0) {
            $limits["list"] = $GLOBALS['smarty']->fetch("contrats/contrats_list_builder.tpl");
            die(json_encode($limits));
        }
        
        // TEMPLATE
        $GLOBALS['template'] = 'contrats/contrats_list.tpl';
        $jsFiles[] = PUBLIC_HTML_ADMIN.'js/contrats.js';
        $cssFiles[] = PUBLIC_HTML_ADMIN.'css/contrats.css';
        
        // REQUIRED LISTS
        $GLOBALS['smarty'] -> assign('paniers_groupes_list', $groupesList);
        $GLOBALS['smarty'] -> assign('contrats_types_list', getContratsTypesList());
        $GLOBALS['smarty'] -> assign('paniers_list', $paniersTypesList);
        $GLOBALS['smarty'] -> assign('lieux_list', $lieuxList);
        
        /****** DISPLAY PAGE ***********/
        $GLOBALS['smarty']->assign('racine',$GLOBALS['racine']);
        display();
    }
}