<?php 

$controller = 'Baking';

$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 BakingForm();

$_waste = new BakingRepository();

$product = new ProductRepository();

$_stringDetails = '';

$dataProduct = $product->getProductsByBaking();


switch($action){

    case 'insert':        

        $_form->populate($_POST);   

        $_POST['attachments'] = $_FILES;


        if ($_form->isValid()) {

            $_waste->setOptions($_POST); 

            if ($_waste->save($_waste->getOptions())) {                

                $_wasteId = $_waste->getLastInsertId();                

                $flashmessenger->addMessage(array(

                    "success"=>$_translator->_getTranslation("Orden registrada exitosamente.")));

                

                header("Location: Baking.php?action=edit&id=$_wasteId");

            }else{                

                $vista = 'Baking.php';

                include $root . '/View/Template.php';                

            }

        } else {

            $vista = 'Baking.php';

            include $root . '/View/Template.php';

        }

        break;
        

    case 'ajax':

        $ajaxWaste = new BakingAjax();

        $json = $ajaxWaste->getResponse($_POST['request'],$_POST);

      
        echo json_encode($json);

        break;       

    

    default:              

        $vista = 'Baking.php';

        include $root.'/View/Template.php';

        break;

}