<?php
$loader = $this->registry->get('load');
$loader->controller('payment/cielo_api_credito');

class ControllerPaymentCieloApiCreditoPos extends ControllerPaymentCieloApiCredito {

  public function index() {
    $this->load->dmodel('checkout/order');
    $this->load->model('account/order');
    $this->load->dmodel('account/order');
    $this->load->dmodel('payment/payment_info');
    $this->load->dmodel('catalog/product');
    $this->load->type('tipocieloapi');
    $this->load->type('tipocomum');
    $this->session->data['order_id'] = $this->request->get['pedido_id'];



    $language = $this->language->load('payment/cielo_api');
		$this->data = array_merge($this->data, $language);

    $this->document->addScriptPrioridade('catalog/view/theme/vista/public/js/mainheader.js');

    $this->document->addScript([
      'catalog/view/javascript/jquery/jquery-1.7.1.min.js',
      'catalog/view/javascript/jquery/ui/jquery-ui-1.8.16.custom.min.js',
      'catalog/view/theme/vista/js/jquery.serialize-object.js',
      'catalog/view/theme/vista/js/jquery/additional-methods.js',
      'catalog/view/theme/vista/js/jquery/jquery.validate.js',
      'catalog/view/theme/vista/js/jquery/jquery.validate.futura-methods.js',
      'catalog/view/theme/vista/js/jquery.mask.js',
      'catalog/view/javascript/jquery.payment/jquery.payment.js',
      'catalog/view/theme/vista/js/page.payment_address.js',
      $this->ftrgeralregistry->getScriptManifest('public/js/postcode.js', false)
    ]);

    $this->document->addStylePrioridade('catalog/view/theme/vista/public/css/mainheader.css');



    $this->data['Type_StatusAutorizacao'] = $this->load->typeForTwig('Type_StatusAutorizacao');
    $this->data['cielo_api_payment_address'] = in_array($this->config->get('cielo_api_anti_fraude'), array( Type_AntiFraudeCheckout::Tp_CleaSaleTotal));

    $this->data['scripts'] = $this->document->getScripts();
    $this->data['styles'] = $this->document->getStyles();

    $this->data['base'] = $this->url->link('', '', 'SSL', false);



    $setting = [
      'payment_name' => 'cieloapi',
    ];

    $order_info  = $this->dmodel_checkout_order->getOrder($this->request->get['pedido_id']);
    $this->data['credit_cards'] = ftr_geral::getPaymentMethods($this->config->get('cielo_api_credit_card'), $order_info['total']);
    $this->data['payment_address'] = $this->getChild('payment/cielo_api_credito_payment_address', $setting);

    $this->data['totals'] = $this->model_account_order->getOrderTotals($this->request->get['pedido_id']);

    if ($order_info) {
      $order_products = $this->dmodel_account_order->getOrderProducts($this->request->get['pedido_id']);

      $controle_kit = array(); //Como id do kit está no item, controla para não gerar duplicidades;
      foreach ($order_products as $product) {
        if (in_array($product['product_kit_id'],$controle_kit)) {continue;}

        if(isset($product['lista_escolar_id']) && $product['lista_escolar_id']>0){
          $tipoLista = Type_ProductCart::Tp_ListaEscolar;
          $id_lista  = $product['lista_escolar_id'];
          $this->data['productlist'][$tipoLista][$id_lista]['isListaEscolar'] = true;
          $this->data['productlist'][$tipoLista][$id_lista]['descricao'] = $product['lista_escolar_descricao'];
        }else{
          $tipoLista = Type_ProductCart::Tp_Default;
          $id_lista  = 0;
          $this->data['productlist'][$tipoLista][$id_lista]['isListaEscolar'] = false;
        }

        //Brinde
        $produto_brinde = $this->dmodel_account_order->getProductOrderBrinde($product['product_id'],$this->request->get['pedido_id']);
        if ($produto_brinde) {
          $produto_brinde['product_name'] = $produto_brinde['name'];
          $produto_brinde['href'] = $this->url->link('product/product', 'product_id=' . $produto_brinde['product_id']);
          $product['price'] += $produto_brinde['price_order'];
          $product['total'] += $produto_brinde['total_order'];
        }
        //Brinde

        //KIT
        $produto_kit = false;
        if ($product['product_kit_id']) {
          $produto_kit = $this->dmodel_account_order->getOrdertKit($this->request->get['order_id'],$product['product_kit_id']);
          $controle_kit[] = $product['product_kit_id'];
        }
        //KIT
        if ($produto_kit){
          $this->data['productlist'][$tipoLista][$id_lista]['products'][] = $produto_kit;
        }else{
          $this->data['productlist'][$tipoLista][$id_lista]['products'][] = array(
            'id'                        => $product['product_id'],
            'descricao'                 => $product['name'],
            'modelo'                    => $product['model'],
            'quantidade'                => $product['quantity'],
            'preco_unitario'            => $this->currency->format($product['price'], $order_info['currency_code'], $order_info['currency_value']),
            'total'                     => $this->currency->format($product['total'], $order_info['currency_code'], $order_info['currency_value']),
            'brinde'                    => $produto_brinde,
            'itens_kit'                 => false
          );
        }
      }
    }

    $tolken_id = ftr_geral::cripty($this->request->get['pedido_id'], $this->config->get('config_encryption'));
    $this->data['tolken_id'] = $tolken_id;
    if ($tolken_id == $this->request->get['tolken_id']){
      $this->data['tolken_valido'] = true;
    }else{
      $this->data['tolken_valido'] = false;
    }

    $this->data['background_color'] = $this->config->get('primary_color');
    $this->data['btn_background'] = $this->config->get('wg24_btn_1bg_col');
    $this->data['btn_textcolor'] = $this->config->get('wg24_btn_text_col');
    $this->data['title_textcolor'] = $this->config->get('wg24_l_linkc');

    $this->data['empresa_razao'] =     $this->config->get('config_owner');
    $this->data['empresa_endereco'] =  $this->config->get('config_address');
    $this->data['empresa_cnpj'] = $this->config->get('config_cnpj');
    $this->data['empresa_tel'] = false;
    $config_telephone = $this->config->get('config_telephone');
    if($config_telephone){
        $config = array();
        foreach (array_reverse($config_telephone) as $telefone){
            if($telefone['display']){
                $config[$telefone['type']][] = $telefone['numero'];
            }
        }
        $config_telephone = $config;
    }

    $this->data['config_telephone'] = $config_telephone;

    $this->data['empresa_email'] = $this->config->get('config_email');

    $cieloorderID = $this->dmodel_payment_payment_info->getPaymentInfo($this->request->get['pedido_id'], Type_PagamentoTabela::Tp_Cartao, true);

    if (!$cieloorderID){
      $this->data['pagamento_aberto'] = true;
    }else{
      $this->data['pagamento_aberto'] = false;
    }

    $this->data['logo'] = $this->config->get('config_linkproducao_ssl') . 'image/' . $this->config->get('config_logo');

    $this->data['pedido_id'] = $order_info['order_id'];
    $this->data['pedido_data'] = date('d/m/Y', strtotime($order_info['date_added'])) ;
    $this->data['pedido_cliente'] = $order_info['firstname'].' '.$order_info['lastname'];

    $this->data['pedido_total'] = $this->currency->format($order_info['total']);

    //$this->data['idorder'] = $this->request->get('idPedido');


    if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/cieloapi/cielo_api_credito_pos.twig')) {
      $this->template = $this->config->get('config_template') . '/template/payment/cieloapi/cielo_api_credito_pos.twig';
    } else {
      $this->template = 'default/template/payment/cielo_api_credito_pos.twig';
    }

    $this->response->setOutput($this->render());


  }

  public function parcelamentoPos() {
    $this->session->data['order_id'] = $this->request->get['order_id'];
    parent::parcelamento();
  }




}
