<?php
class ControllerCommonAdsbanner extends Controller {

	public function index(){

		$this->load->helper('ftr_geral');

	  $this->language->load('common/home');
		
		$this->data['no_display_ads'] = $this->language->get('no_display_ads');
		$this->data['text_yes']       = $this->language->get('text_yes');
		$this->data['text_no']        = $this->language->get('text_no');
		$this->data['title_app']      = $this->language->get('title_app');
		
		$this->data['warning'] = false;
		
		$promotion_id = $this->db->query("SELECT promotion_id FROM `parametros_efutura` WHERE 1 order by id desc limit 1")->row;

		$TipoSistema = array(
			$this->ftrgeralregistry->isSistema(Type_Sistema::Tp_Aplicativo_IOS),
			$this->ftrgeralregistry->isSistema(Type_Sistema::Tp_Aplicativo_Android)
		);	

		if(count(array_filter($TipoSistema)) < 2 ){
			try{
				$results = ftr_geral::callJson('https://efutura.futurasistemas.com.br/promocoes_ecommerce/promotion_json.php');
	
				if($results){
					$this->data['adsbanners'] = $results;
					if($results[0]['promotion_id'] <= $promotion_id['promotion_id']){
						$this->data['warning'] = true ;
					}
				}else{
					$this->data['warning'] = true ;
				}			
			}catch (Exception $e){
				$this->data['warning'] = true ;
			}		
		}else{
			$this->data['warning'] = true ;
		}
		
		$this->response->setOutput($this->render('common/adsbanner'));
	}

	public function remove(){

		$json = array();

		if(isset($this->request->post['promotion_id'])){
			$promotion_id = $this->request->post['promotion_id']; 	
		}else{
			$json['error']['promotion_id'] = 'Id da promoção não encontrado';
		}

		if(!$json){
			$this->db->query("UPDATE parametros_efutura SET promotion_id = ".$promotion_id);
			$json['success'] = true;
		}		

		$this->response->setOutputJson($json);

	}


}
