<?php 
$controller = 'Store';
$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';

$_form = new SucursalForm();
$_sucursal = new SucursalRepository();

switch($action){
    case 'insert':
        $_form->populate($_POST);
        $_form->setTokenForm($_POST['token_form']);
        if($_form->isValid()){
            $_sucursal->setOptions($_POST);
            $_sucursal->setImage($_FILES['logo_file']);
            $result = $_sucursal->save($_sucursal->getOptions());
            if($result){
                $flashmessenger->addMessage(array('success'=>'Genial !! La Sucursal se registro exitosamente.'));
                header('Location: Store.php');
            }else{
                $flashmessenger->addMessage(array('danger'=>'Error. Intenta nuevamente o contacta a tu proveedor de sistemas.'));
                $vista = 'Store.php';
                include $root.'/View/Template.php';
            }
        }else{
            $vista = 'Store.php';
            include $root.'/View/Template.php';
        }
        break;
   
    case 'edit':
        if($_GET){
            $_sucursalData = $_sucursal->getById($id);
            $_sucursal->crearTablaDetallesForUser();
            $_sucursal->setStoreDetailsById($id, $_form->getTokenForm());
        }
     
        if($_POST){
            $_sucursalData = $_POST;
            $_form->setTokenForm($_POST['token_form']);
        }
        
        $_form->setActionController('edit');
        $_form->setId($id);
        $_form->populate($_sucursalData);   
        
        if($id == '1'){
            //$_form->disabledAllElements();
        }
        
        if(isset($_POST['id'])){
            if($_form->isValid()){
                $_sucursalData['archivos'] = $_FILES;
                $_sucursal->setOptions($_sucursalData);
                $_sucursal->setImage($_FILES['logo_file']);
                $result = $_sucursal->update($id,$_sucursal->getOptions()); //no tengo id porque viene de post
                if($result){
                    $flashmessenger->addMessage(array('success'=>'Estupendo !! La Sucursal se actualizo exitosamente.'));
                    header("Location: Store.php?action=edit&id=$id");
                }else{
                    $flashmessenger->addMessage(array('danger'=>'Error. Intenta nuevamente o contacta a tu proveedor de sistemas.'));
                     $vista = 'Store.php';
                    include $root.'/View/Template.php';
                }       
            }else{
                $vista = 'Store.php';
                include $root . '/View/Template.php';
            }
        }else{
            $vista = 'Store.php';
            include $root . '/View/Template.php';
        }        
        break;
        
    case 'list':
        $_listSucursals = $_sucursal->getListSucursales();
        
        $vista = 'StoreList.php';
        include $root . '/View/Template.php';
        break;
    
    case 'delete': 
        if(!$_sucursal->isUsedInRecord($id)){
            if($_sucursal->delete($id)){
                $flashmessenger->addMessage(array('success'=>'Hecho !! La Sucursal fue eliminada satisfactoriamente.'));
            }
        }else{
            $message = 'Hola !! este Sucursal no puede ser eliminado, esta siendo utilizado en almenos un registro.';
            $flashmessenger->addMessage(array('info'=>$message));
        }
        header('Location: Store.php');
        break;
    
    case 'ajax':
        $ajaxStore = new StoreAjax();
        $json = $ajaxStore->getResponse($_POST['request'],$_POST);
        
        echo json_encode($json);
        break;  
    
    default:
        $_sucursal->crearTablaDetallesForUser();
        $vista = 'Store.php';
        include $root.'/View/Template.php';
        break;
}