<?php 
$controller = 'ScheduleEmployee';
$action = '';
if(isset($_POST['action'])){
    $action = $_POST['action'];
    if (isset($_POST['id'])) {$id = $_POST['id'];}
}elseif(isset($_GET['action'])){
    $action = $_GET['action'];
    if (isset($_GET['id'])) {$id = $_GET['id'];}
}
include $_SERVER["DOCUMENT_ROOT"].'/app/include/bootstrap.php';

$_formSchedule = new ScheduleEmployeeForm();
$_schedule = new ScheduleEmployeeRepository();


switch($action){
    case 'insert': 
        $_formSchedule->populate($_POST);
        if($_formSchedule->isValid()){            
            $_schedule->setOptions($_POST);
            $result = $_schedule->save($_schedule->getOptions());
            if($result){
                $flashmessenger->addMessage(array('success'=>'Horarios registrados exitosamente.'));
                header('Location: ScheduleEmployee.php');
            }else{
                $flashmessenger->addMessage(array('danger'=>'Error. Intenta nuevamente o contacta a tu proveedor de sistemas.'));
            }
        }else{  
            $_noValid = true;
            $vista = 'ScheduleEmployee.php';
            include $root.'/View/Template.php';
        }
        break;
   
    case 'edit':
        if($_GET){
            $_scheduleData = $_schedule->getById($id);
            $_detailsData = $_schedule->getScheduleDetailsByScheduleId($id);    
            
            $_days_of_period = $_detailsData['days_of_period'];
            $_user_list = $_detailsData['user_list'];
            $_details = $_detailsData['details'];            
        }
        
        if($_POST){           
            $_scheduleData = $_POST;            
            $_detailsData = $_schedule->getScheduleDetailsByScheduleId($id);           
            $_days_of_period = $_detailsData['days_of_period'];
            $_user_list = $_detailsData['user_list'];
            
            $_details = $_detailsData['details'];     
        }
        
        $_formSchedule->setActionController('edit');
        $_formSchedule->setId($id);
        $_formSchedule->populate($_scheduleData);
      
        if(isset($_POST['id'])){
            if($_formSchedule->isValid()){
                $_schedule->setOptions($_scheduleData);
                $result = $_schedule->update($id,$_schedule->getOptions()); //no tengo id porque viene de post
                if($result){       
                    
                    $flashmessenger->addMessage(array('success'=>'Programacion de horarios actualizada exitosamente.'));
                    //echo "$id";exit;
                    header("Location: ScheduleEmployee.php?action=edit&id=$id");
                    exit;
                }else{
                    $flashmessenger->addMessage(array('danger'=>'Error. Intenta nuevamente o contacta a tu proveedor de sistemas.'));
                }       
            }else{
                $_noValid = true;
                $vista = 'ScheduleEmployee.php';
                include $root . '/View/Template.php';
            }
        }else{
            $vista = 'ScheduleEmployee.php';
            include $root . '/View/Template.php';
        }        
        break;
        
    case 'list': 
        $searchFilter = null;
        if (isset($_POST['search'])) {
            $searchFilter = $_POST;
        }
        
        $_listScheduleEmployee = $_schedule->getScheduleList($searchFilter);        
        
        $vista = 'ScheduleEmployeeList.php';
        include $root . '/View/Template.php';
        
        break;
    
    case 'delete':      
       
        if($_schedule->delete($id)){
            $flashmessenger->addMessage(array('success'=>'Horarios eliminados satisfactoriamente.'));
        }       
        
        header("Location: ScheduleEmployee.php?action=list");
        exit;
        break;
        
     case 'ajax':
        $ajaxScheduleEmployee = new ScheduleEmployeeAjax();
        $json = $ajaxScheduleEmployee->getResponse($_POST['request'], $_POST);
        echo json_encode($json);
        break;
    
    default:       
        
        $vista = 'ScheduleEmployee.php';
        include $root.'/View/Template.php';
}