<?php 
$controller = 'CustomerPayment';
$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 CustomerPaymentForm();
$customerPayment = new CustomerPaymentRepository();

switch($action){   
    case 'insert':
        $form->setActionController('insert');
        $form->populate($_POST);
        $_POST['attachments'] = $_FILES;
        
        if(isset($_POST['pago'])){
            $_facturas = $_POST['pago'];
            $form->setFacturasAPagar($_facturas);
            $_listFacturas = true;
        }      

       if($form->isValid()){
            $customerPayment->setOptions($_POST);
            $options = $customerPayment->getOptions();    
                

            if($customerPayment->save($options)){
                $flashmessenger->addMessage(array('success'=>'Pago registrado exitosamente.'));
                $lastInsert= $customerPayment->getLastInsertId();
                header("Location: CustomerPayment.php?action=edit&id=$lastInsert");
            }
        }else{  
            $vista = 'CustomerPayment.php';
            include $root.'/View/Template.php';
        }  
        break;        

    case 'edit':              
        if($_POST){
            $_POST['attachments'] = $_FILES;
            $data = $_POST;           
        }         
        
        $login = new Login();
        if($_GET){          
            $data = $customerPayment->getDataToEdit($id);
            
            if($login->getRole() != '1' && $login->getStoreId() != $data['store_id']){
                header("Location: CustomerPayment.php?action=list");
            }   
            $_SESSION['facturasLiquidadas'] = $data['facturasLiquidadas'];
            $_SESSION['pagosOriginales'] = $data['pagosOriginales'];
            $data = $data['data'];             
            $data['suma_de_pagos'] = $data['amount'];
            $data['monto_original'] = $data['amount']; 
            
            $data = $form->formatDouble($data,$customerPayment->inputs_double);          
        }      

        $form->setActionController('edit');
        $form->populate($data);
        $form->setId($id);

       // $form->disabledElements(array('store_payment','customer'));
      // $form->addClassToElement('store_payment','deshabilitado');
      $form->setReadOnlydElements(array('customer','store_payment'));

        $_disabled = true;
        if($data['status']==2){$form->disabledAllElements();}

        if(isset($data['pago'])){          
            $_facturas = $data['pago'];
            $form->setFacturasAPagar($_facturas);
            $_listFacturas = true;
        }      

        if(isset($_POST['send'])){
            if($form->isValid()){
              //  var_dump($_POST);exit;
                $dataPago = $customerPayment->getOptions($customerPayment->setOptions($_POST));
              

                 if($customerPayment->updateMultiple($id,$dataPago)){
                     $flashmessenger->addMessage(array('success'=>'Pago actualizados exitosamente.'));
                     header("Location: CustomerPayment.php?action=edit&id=$id");
                 }
             }else{
                 $vista = 'CustomerPayment.php';
                 include $root.'/View/Template.php';
             }
        }else{
            $customerPayment->setOptions($data);
            $vista = 'CustomerPayment.php';
            include $root.'/View/Template.php';
        }
        break;               

    case 'list':
            $searchFilter = null;
            if(isset($_POST['search'])){$searchFilter = $_POST;}
            $_listPagos = $customerPayment->getListPagos($searchFilter);            

            $vista = 'CustomerPaymentList.php';
            include $root.'/View/Template.php';
        break;  
        
    case 'delete':
            $rs = $customerPayment->delete($id);          
            if($rs || !$rs){header("Location: CustomerPayment.php?action=list");}
           
        break;

    case 'ajax':
        $ajaxPago = new CustomerPaymentAjax();
        $json = $ajaxPago->getResponse($_POST['request'],$_POST);
       
        echo json_encode($json);
        break;    

    case 'import':
        switch($_GET['flag']){
            case 'pdf':
                $pdf = new PagoPDF($_GET['id']);
                break;
        }        
        break;   

     case 'descargar':        
            $customerPayment = new PagoEntity();
            $customerPayment->setOptions($customerPayment->getById($_GET['idPago']));
            $customerPayment->setId($_GET['idPago']);
            $customerPayment->crearPDF();
            $customerPayment->descargarZip();   
            header('Location: CustomerPayment.php?action=list');
        break;   

    case 'excel':
        switch($_GET['flag']){
            case 'search':
                $customerPayment->resultSearchToExcel($_GET);
                break;
        }       

        break;      

    default:

        unset($_SESSION['facturasLiquidadas']);
        unset($_SESSION['pagosOriginales']);

        $form->setActionController('insert');
        $vista = 'CustomerPayment.php';
        include $root.'/View/Template.php';
        break;
}