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

include_once(FUNCTIONS_DIR_PATH.'functions_clients.php');
include_once(FUNCTIONS_DIR_PATH.'functions_contrats.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 = getClientDatas($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(
                "contrats"   => "clients/client_modal_view_contrats_tab.tpl",
                "absences"   => "clients/client_modal_view_absences_tab.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 = getClientDatasFromRequest();
            die( strval( updateClient($id,$datas) ) );
        } break;
        // DELETE
        case "delete" : die( strval( deleteClient($id) ) ); break;
        // DEFAULT
        default: die("NO ACTION");
    }
}

switch($action) {
    /****** ABSENCES ***********/
    // AJAX GET CLIENT ABSENCE DATAS
    case "get_client_absence_datas": {
        $id = isset($_REQUEST['absence_ref']) ? intval($_REQUEST['absence_ref']) : 0;
        if($id>0) die( json_encode( getClientAbsenceDatas($id) ) );
        else die("ERREUR : aucune référence d'absence client transmis...");
    } break;
    // ADD CLIENT ABSENCE
    case "add_client_absence": {
        $datas = getClientAbsenceDatasFromRequest();
        die( strval( addClientAbsence($datas) ) );
    } break;
    // EDIT CLIENT ABSENCE
    case "edit_client_absence": {
        $id = isset($_REQUEST['absence_ref']) ? intval($_REQUEST['absence_ref']) : 0;
        if($id>0) {
            $datas = getClientAbsenceDatasFromRequest();
            die( strval( updateClientAbsence($id, $datas) ) );
        }
        else die("ERREUR : aucune référence d'absence client transmis...");
    } break;
    // DELETE CLIENT ABSENCE
    case "delete_client_absence": {
        $id = isset($_REQUEST['absence_ref']) ? intval($_REQUEST['absence_ref']) : 0;
        if($id>0) die( strval( deleteClientAbsence($id) ) );
        else die("ERREUR : aucune référence d'absence client transmis...");
    } break;   
    
    /****** CLIENTS ***********/
    // ADD
    case "add": {
        $datas = getClientDatasFromRequest();
        die( strval( addClient($datas) ) );
    } break;
    // AUTOCOMPLETE LIST
    case "autocomplete_list": die( json_encode( getClientsAutocompleteList() ) ); break;
    // DEFAULT - LIST
    default: {
        // SEARCH
        $search = getSearch("clients_search");
        
        // LIMITS
        $limits = array("start" => 0, "end" => LIST_NB_LIGNES_PAR_PAGE, "nbItemsByPage" => LIST_NB_LIGNES_PAR_PAGE, "max" => getNbClientsInList($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('clients_list_order', 'clients_list_sens', $allowClientsListOrder, 'prenom');
        $list = getClientsList($order["order"], $order["sens"], false, $search, $limits);
        $GLOBALS['smarty'] -> assign('list',$list);
        
        // LIST PART
        if(isset($_REQUEST['startListAt']) && (int)$_REQUEST['startListAt']>0) {
            $limits["list"] = $GLOBALS['smarty']->fetch("clients/clients_list_builder.tpl");
            die(json_encode($limits));
        }
        
        // TEMPLATE
        $GLOBALS['template'] = 'clients/clients_list.tpl';
        $jsFiles[] = PUBLIC_HTML_ADMIN.'js/clients.js';
        $cssFiles[] = PUBLIC_HTML_ADMIN.'css/clients.css';
        
        /****** DISPLAY PAGE ***********/
        $GLOBALS['smarty']->assign('racine',$GLOBALS['racine']);
        display();
    }
}