<?php 
$controller = 'Rewards';
$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';

$_formRewards = new RewardsForm();
$_formRules = new RewardsRulesForm();
$_formReedem = new RewardsReedemForm();

$_reward = new RewardsRepository();
$_rewardData = $_reward->getById(1);

$_rules = new RewardsRulesRepository();
$_rulesList = $_rules->getListRules();

$_reedem = new RewardsReedemRepository();
$_reedemList = $_reedem->getListReedems();

switch($action){   
    case 'edit':        
        $_formRules->setActionController('rules-insert');
        $_formReedem->setActionController('reedem-insert');
        
        $_tabActive = 'settings';
        if($_POST){$_rewardData = $_POST;}
        $_formRewards->setActionController('edit');
        $_formRewards->setId($id);
        $_formRewards->populate($_rewardData);
        if(isset($_POST['id'])){
            if($_formRewards->isValid()){
                $_reward->setOptions($_rewardData);
                $result = $_reward->update($id,$_reward->getOptions()); //no tengo id porque viene de post
                if($result){
                    $flashmessenger->addMessage(array('success'=>'Rewards actualizado exitosamente.'));
                    header("Location: Rewards.php");
                }else{
                    $flashmessenger->addMessage(array('danger'=>'Error. Intenta nuevamente o contacta a tu proveedor de sistemas.'));
                }       
            }else{
                $vista = 'Rewards.php';
                include $root . '/Rewards/View/Template.php';
            }
        }else{
            $vista = 'Rewards.php';
            include $root . '/Rewards/View/Template.php';
        }        
        break;
        
    case 'rules-insert':
        $_formRewards->setActionController('edit');
        $_formReedem->setActionController('reedem-insert');
        
        $_tabActive = 'rules';
        $_formRules->setActionController('rules-insert');
        $_formRules->populate($_POST);
        
        if ($_formRules->isValid()) {
            $_rules->setOptions($_POST);
            $result = $_rules->save($_rules->getOptions());
            if ($result) {
                $flashmessenger->addMessage(array('success'=>'Regla registrada exitosamente.'));
                header("Location: Rewards.php?rules");
            }else{
                $flashmessenger->addMessage(array('danger'=>'Error. Intenta nuevamente o contacta a tu proveedor de sistemas.'));
                $vista = 'Rewards.php';
                include $root . '/Rewards/View/Template.php';           
            }
        } else {
            $vista = 'Rewards.php';
            include $root . '/Rewards/View/Template.php';
        }
        break;
        
    case 'rules-edit': 
        $_formRewards->setActionController('edit');
        $_formReedem->setActionController('reedem-insert');
        
        $_tabActive = 'rules';
        if($_GET){$_rulesData = $_rules->getById($id);}
        if($_POST){$_rulesData = $_POST;}
        $_formRules->setActionController('rules-edit');
        $_formRules->setId($id);
        $_formRules->populate($_rulesData);
        
        if(isset($_POST['id'])){
            if($_formRules->isValid()){
                $_rules->setOptions($_rulesData);
                $result = $_rules->update($id,$_rules->getOptions()); //no tengo id porque viene de post
                if($result){
                    $flashmessenger->addMessage(array('success'=>'Regla de puntos actualizada exitosamente.'));
                    header("Location: Rewards.php?rules");
                }else{
                    $flashmessenger->addMessage(array('danger'=>'Error. Intenta nuevamente o contacta a tu proveedor de sistemas.'));
                }       
            }else{
                $vista = 'Rewards.php';
                include $root . '/Rewards/View/Template.php';
            }
        }else{
            $vista = 'Rewards.php';
            include $root . '/Rewards/View/Template.php';
        }        
        break;      
        
     case 'reedem-insert':
        $_formRewards->setActionController('edit');
        $_formRules->setActionController('reedem-insert');
        
        $_tabActive = 'reedem';
        $_formReedem->setActionController('reedem-insert');
        $_formReedem->populate($_POST);
        
        if ($_formReedem->isValid()) {
            $_reedem->setOptions($_POST);
            $result = $_reedem->save($_reedem->getOptions());
            if ($result) {
                $flashmessenger->addMessage(array('success'=>'Canje registrado exitosamente.'));
                header("Location: Rewards.php?reedem");
            }else{
                $flashmessenger->addMessage(array('danger'=>'Error. Intenta nuevamente o contacta a tu proveedor de sistemas.'));
                $vista = 'Rewards.php';
                include $root . '/Rewards/View/Template.php';           
            }
        } else {
            $vista = 'Rewards.php';
            include $root . '/Rewards/View/Template.php';
        }
        break;
        
    case 'reedem-edit':   
        $_formRewards->setActionController('edit');
        $_formRules->setActionController('reedem-insert');
        
        $_tabActive = 'reedem';
        if($_GET){$_reedemData = $_reedem->getById($id);}
        if($_POST){$_reedemData = $_POST;}
        $_formReedem->setActionController('reedem-edit');
        $_formReedem->setId($id);
        $_formReedem->populate($_reedemData);
       
        if(isset($_POST['id'])){
            if($_formReedem->isValid()){
                $_reedem->setOptions($_reedemData);
                $result = $_reedem->update($id,$_reedem->getOptions()); //no tengo id porque viene de post
                if($result){
                    $flashmessenger->addMessage(array('success'=>'Canje de puntos actualizado exitosamente.'));
                    header("Location: Rewards.php?reedem");
                }else{
                    $flashmessenger->addMessage(array('danger'=>'Error. Intenta nuevamente o contacta a tu proveedor de sistemas.'));
                }       
            }else{
                $vista = 'Rewards.php';
                include $root . '/Rewards/View/Template.php';
            }
        }else{
           
            $vista = 'Rewards.php';
            include $root . '/Rewards/View/Template.php';
        }        
        break;    
        
    case 'ajax':
        $ajax = new RewardsAjax();
        $json = $ajax->getResponse($_POST['request'],$_POST);
        echo json_encode($json);
        break;
    
     case 'emailing':
        $ajax = new EmailingAjax();   
        $json = $ajax->getResponse($_POST['request'],$_POST);
        echo json_encode($json);
        break;
    
    default:
        $_tabActive = 'settings';
        if(isset($_GET['rules'])){$_tabActive = 'rules';}
        if(isset($_GET['reedem'])){$_tabActive = 'reedem';}
        
        $_formRewards->setActionController('edit');
        $_formRewards->setId(1);
        $_formRewards->populate($_rewardData);
        
        $_formRules->setActionController('rules-insert');
        $_formReedem->setActionController('reedem-insert');
        
        $vista = 'Rewards.php';
        include $root.'/Rewards/View/Template.php';
}