<?php 
$controller = 'Compra';
$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 PurchaseForm();
$_purchase = new PurchaseRepository();

$searchFilter = null;
if(isset($_POST['search'])){$searchFilter = $_POST;}
$_listPurchases = $_purchase->getListPurchases($searchFilter);

switch($action){
    case 'insert': 
        $form->populate($_POST);
        if($form->isValid()){            
            $_purchase->setOptions($_POST);
            $result = $_purchase->save($_purchase->getOptions());
            if($result){
                $addNew = null;
                if($_POST['add_another_one']== '1'){$addNew = '?addnew';}
                $flashmessenger->addMessage(array('success'=>'Compra registrada exitosamente.'));
                header('Location: Compra.php'.$addNew);
            }else{
                $flashmessenger->addMessage(array('danger'=>'Error. Intenta nuevamente o contacta a tu proveedor de sistemas.'));
            }
        }else{
            $_noValid = true;
            $vista = 'Purchase.php';
            include $root.'/View/Template.php';
        }
        break;
   
    case 'edit':
        if($_GET){
            $_purchaseData = $_purchase->getById($id);
        }
        if($_POST){$_purchaseData = $_POST;}
        
        $_login = new Login();
        if($_login->getRole() != '1'){
            $flashmessenger->addMessage(array('info'=>'No tienes permiso para acceder a este modulo.'));
            header('Location: Home.php');
        }
        
        $form->setActionController('edit');
        $form->setId($id);
        $form->populate($_purchaseData);
        
        if(isset($_POST['id'])){
            if($form->isValid()){
                $_purchase->setOptions($_purchaseData);
                $result = $_purchase->update($id,$_purchase->getOptions()); //no tengo id porque viene de post
                if($result){
                   if($_POST['add_another_one'] == '1'){$addNew = '?addnew';}
                    $flashmessenger->addMessage(array('success'=>'Compra actualizada exitosamente.'));
                    header("Location: Compra.php".$addNew);
                }else{
                    $flashmessenger->addMessage(array('danger'=>'Error. Intenta nuevamente o contacta a tu proveedor de sistemas.'));
                }       
            }else{
                $_noValid = true;
                $vista = 'Purchase.php';
                include $root . '/View/Template.php';
            }
        }else{
            $vista = 'Purchase.php';
            include $root . '/View/Template.php';
        }        
        break;
    
    case 'delete': 
        if(!$_purchase->isUsedInRecord($id)){
            if($_purchase->delete($id)){
                $flashmessenger->addMessage(array('success'=>'Compra cancelada satisfactoriamente.'));
            }
        }else{
             $flashmessenger->addMessage(array('success'=>'Compra no puede ser cancelada esta siendo utilizado en almenos un registro.'));
        }
        
        header("Location: Compra.php");
        break;
        
     case 'ajax':
        $ajaxPurchase = new PurchaseAjax();
        $json = $ajaxPurchase->getResponse($_POST['request'], $_POST);
        echo json_encode($json);
        break;
    
    default:       
        $vista = 'Purchase.php';
        include $root.'/View/Template.php';
}