<?php 
$controller = 'Flour';
$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 FlourForm();
$_flour = new FlourRepository();
$_listFlours = $_flour->getListFlours();
$_noValid = null;

switch($action){
    case 'insert': 
        $form->populate($_POST);
        if($form->isValid()){            
            $_flour->setOptions($_POST);
            $result = $_flour->save($_flour->getOptions());
            if($result){
                $flashmessenger->addMessage(array('success'=>'Harina registrada exitosamente.'));
                header('Location: Flour.php');
            }else{
                $flashmessenger->addMessage(array('danger'=>'Error. Intenta nuevamente o contacta a tu proveedor de sistemas.'));
            }
        }else{
            $_noValid = true;
            $vista = 'Flour.php';
            include $root.'/View/Template.php';
        }
        break;
   
    case 'edit':
        if($_GET){
            $_flourData = $_flour->getById($id);
        }
        if($_POST){$_flourData = $_POST;}
        
        $form->setActionController('edit');
        $form->setId($id);
        $form->populate($_flourData);
        
        if(isset($_POST['id'])){
            if($form->isValid()){
                $_flour->setOptions($_flourData);
                $result = $_flour->update($id,$_flour->getOptions()); //no tengo id porque viene de post
                if($result){
                    $flashmessenger->addMessage(array('success'=>'Harina actualizado exitosamente.'));
                    header("Location: Flour.php");
                }else{
                    $flashmessenger->addMessage(array('danger'=>'Error. Intenta nuevamente o contacta a tu proveedor de sistemas.'));
                }       
            }else{
                $_noValid = true;
                $vista = 'Flour.php';
                include $root . '/View/Template.php';
            }
        }else{
            $vista = 'Flour.php';
            include $root . '/View/Template.php';
        }        
        break;
    
    case 'delete': 
        if(!$_flour->isUsedInRecord($id)){
            if($_flour->delete($id)){
                $flashmessenger->addMessage(array('success'=>'Harina eliminada satisfactoriamente.'));
            }
        }else{
             $flashmessenger->addMessage(array('success'=>'Harina no puede ser eliminada esta siendo utilizada en almenos un registro.'));
        }
        
        header("Location: Flour.php");
        break;
        
     case 'ajax':
        $ajaxFlour = new FlourAjax();
        $json = $ajaxFlour->getResponse($_POST['request'], $_POST);
        echo json_encode($json);
        break;
    
    default:       
        $vista = 'Flour.php';
        include $root.'/View/Template.php';
}