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

$_formCustomer = new CustomerRewardsForm();
$_customer = new CustomerRewardsRepository();

$showTabRewardsRecord = 'hide';
$filters = null;
if(isset($_POST['search'])){$filters = $_POST;}
$_customerList = $_customer->getListCustomers($filters);

switch($action){                          
     case 'insert':
        $_formCustomer->setActionController('reedem-insert');
        $_formCustomer->populate($_POST);
        
        if ($_formCustomer->isValid()) {
            $_customer->setOptions($_POST);
            $result = $_customer->save($_customer->getOptions());
            if ($result) {
                $flashmessenger->addMessage(array('success'=>'Cliente registrado exitosamente.'));
                header("Location: CustomerRewards.php");
            }else{
                $flashmessenger->addMessage(array('danger'=>'Error. Intenta nuevamente o contacta a tu proveedor de sistemas.'));
                $vista = 'CustomerRewards.php';
                include $root . '/Rewards/View/Template.php';           
            }
        } else {
            $vista = 'CustomerRewards.php';
            include $root . '/Rewards/View/Template.php';
        }
        break;
        
    case 'edit':         
        $showTabRewardsRecord = '';
            
        if($_GET){$_customerData = $_customer->getById($id);}
        if($_POST){$_customerData = $_POST;}
        $_formCustomer->setActionController('edit');
        $_formCustomer->setId($id);
        $_formCustomer->populate($_customerData);
        $_rewardRecordList = $_customer->getRewardsRecodByCustomerId($_customerData['id']);
       
        if(isset($_POST['id'])){
            if($_formCustomer->isValid()){
                $_customer->setOptions($_customerData);
                $result = $_customer->update($id,$_customer->getOptions()); //no tengo id porque viene de post
                if($result){
                    $flashmessenger->addMessage(array('success'=>'Cliente actualizado exitosamente.'));
                    header("Location: CustomerRewards.php");
                }else{
                    $flashmessenger->addMessage(array('danger'=>'Error. Intenta nuevamente o contacta a tu proveedor de sistemas.'));
                }       
            }else{
                $vista = 'CustomerRewards.php';
                include $root . '/Rewards/View/Template.php';
            }
        }else{
           
            $vista = 'CustomerRewards.php';
            include $root . '/Rewards/View/Template.php';
        }        
        break;    
    
    default:               
        $vista = 'CustomerRewards.php';
        include $root.'/Rewards/View/Template.php';
}