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

class ControllerApiCart extends ControllerApi{

  public function products(){
    parent::validaAutenticacao();

    if(parent::haserror()){
       $retorno = parent::geterror();
    }else{
      $this->load->dmodel('tool/image');
      $this->load->type('tipocomum');

      $retorno  = array();
      $products = $this->cart->getProducts();

      foreach ($products as $prod) {
        $retorno[] = array(
           'product_id'    =>  $prod['product_id'],
           'ipi_valor'     =>  (strtoupper($prod['ipi_valor']) == 'N')  ? false : true,
           'ipi_valor'     =>  (strtoupper($prod['st_valor'])  == 'N')  ? false : true,
           'fcpst_valor'   =>  (strtoupper($prod['fcpst_valor'])  == 'N')  ? false : true,
           'id_futura'     =>  $prod['id_futura'],
           'descricao'     =>  $prod['name'],
           'referencia'    =>  $prod['model'],
           'image'         =>  $this->dmodel_tool_image->resize($prod['image'], Type_MiniatureMedium::Tp_width, Type_MiniatureMedium::Tp_height),
           'cor'           =>  $prod['color_description'],
           'tamanho'       =>  $prod['size_description'],
           'especificacao' =>  $prod['specification_description'],
           'quantidade'    =>  $prod['quantity'],
           'valor'         =>  $prod['price'],
           'total'         =>  $prod['total']
         );
      }
    }
    $this->response->setOutputJson($retorno);
  }


  public function count(){
    parent::validaAutenticacao();

    if(parent::haserror()){
       $retorno = parent::geterror();
    }else{
        $retorno['quantidade'] = $this->cart->countProducts();
    }

    $this->response->setOutputJson($retorno);
  }

}
