<?php 
$controller = 'Customer';
$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 ClienteForm();
$_cliente = new ClienteEntity();
$_listaClientes = $_cliente->getListaClientes();

switch($action){
    case 'insert':
        $_form->populate($_POST);
        if ($_form->isValid()) {
            $_cliente->setOptions($_POST);
            $result = $_cliente->save($_cliente->getOptions());
            if ($result) {
                $flashmessenger->addMessage(array('success'=>'Cliente registrado exitosamente.'));
                header('Location: Customer.php');
            }else{
                $flashmessenger->addMessage(array('danger'=>'Error. Intenta nuevamente o contacta a tu proveedor de sistemas.'));
                $vista = 'Cliente.php';
                include $root.'/View/Template.php';                
            }
        } else {
            $vista = 'Cliente.php';
            include $root . '/View/Template.php';
        }
        break;
    
    case 'edit':
        if($_GET){$_clienteData = $_cliente->getById($id);}
        if($_POST){$_clienteData = $_POST;}
        $_form->setActionController('edit');
        $_form->setId($id);
        $_form->populate($_clienteData);
        
        if(isset($_POST['id'])){
            if($_form->isValid()){
                $_cliente->setOptions($_clienteData);
                $result = $_cliente->update($id,$_cliente->getOptions()); //no tengo id porque viene de post
                if($result){
                    $flashmessenger->addMessage(array('success'=>'Cliente actualizado exitosamente.'));
                    header("Location: Customer.php?action=edit&id=$id");
                }else{
                    $flashmessenger->addMessage(array('danger'=>'Error. Intenta nuevamente o contacta a tu proveedor de sistemas.'));
                }       
            }else{
                $vista = 'Cliente.php';
                include $root . '/View/Template.php';
            }
        }else{
            $vista = 'Cliente.php';
            include $root . '/View/Template.php';
        }        
        break;
    
    case 'delete':
        if(!$_cliente->isUsedInRecord($id)){
            if($_cliente->delete($id)){
                $flashmessenger->addMessage(array('success'=>'Cliente eliminado satisfactoriamente.'));
            }
        }else{
           $message = 'Este Cliente no puede ser eliminado, esta siendo utilizado en almenos un registro.';
           $flashmessenger->addMessage(array('info'=>$message));
        }
        header("Location: Customer.php");
        break;
        
    case 'import':
        switch($_GET['flag']){
            case 'statement':
                $pdf = new SaldoPendienteVentasDetalles($_GET);
                break;
        }        
        break;
    
    default:
        $vista = 'Cliente.php';
        include $root.'/View/Template.php';
}