<?php 

$controller = 'Waste';

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

$_waste = new WasteRepository();

$_stringDetails = '';



switch($action){

    case 'insert':        

        $_form->populate($_POST);   

        $_POST['attachments'] = $_FILES;

        $_stringDetails = $_form->getDetails($_POST);

        if ($_form->isValid()) {

            $_waste->setOptions($_POST); 

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

                $_wasteId = $_waste->getLastInsertId();                

                $flashmessenger->addMessage(array(

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

                

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

            }else{                

                $vista = 'Waste.php';

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

            }

        } else {

            $vista = 'Waste.php';

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

        }

        break;

    

    case 'list':

        $searchFilter = null;    

         if(isset($_POST['search'])){$searchFilter = $_POST;}

         

        $_listWastes = $_waste->getListWaste($searchFilter);

        

        $vista = 'WasteList.php';

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

        break;

        

    case 'export':

        switch($_GET['flag']){

            case 'waste':

                $pdf = new WastePDF($_GET['id']);

                break;

        }        

        break;

    

    case 'edit':   

        $_productionData = $_waste->getById($id);

        #Para usar status y statusName

        $_waste->setOptions($_productionData);

        

        if($_GET){

            $_wasteData = $_productionData;

            $_stringDetails = $_waste->getStringDetailsToEdit($id);  

        }

        if($_POST){

            $_POST['attachments'] = $_FILES;

            $_wasteData = $_POST;

            $_stringDetails = $_form->getDetails($_POST);            

        }

        $_form->setActionController('edit');

        $_form->setId($id);

        $_form->populate($_wasteData);

        

        if($_wasteData['status'] == '2'){

            $_form->disabledAllElements();

            $_form->hideElement(array('agregar_producto','terminar'));

        }

        

        $_disabled = null;        

        if(isset($_POST['id'])){

            if($_form->isValid()){

                $_waste->setOptions($_wasteData);

                $result = $_waste->update($id,$_waste->getOptions()); //no tengo id porque viene de post

                if($result){

                    $flashmessenger->addMessage(array('success'=>'Merma actualizada exitosamente.'));

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

                }else{

                     $vista = 'Waste.php';

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

                }       

            }else{

                $vista = 'Waste.php';

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

            }

        }else{

            $vista = 'Waste.php';

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

        }        

        break;

    

    case 'delete':

        if($_waste->delete($id)){

            $flashmessenger->addMessage(array('success'=>'Merma cancelada satisfactoriamente.'));

        }        

        header("Location: Waste.php?action=list");

        break;

        

    case 'ajax':

        $ajaxWaste = new WasteAjax();

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

        

        echo json_encode($json);

        break;       

    

    default:              

        $vista = 'Waste.php';

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

        break;

}