<?php 
class ControllerEredeRisco extends Controller {
	private $error = array(); 
     
  	public function index() {
		$this->language->load('payment/erede');
    	
		$this->document->setTitle($this->language->get('heading_title')); 
		
		$this->load->model('erede/risco');
		
		$this->getList();
  	}

  	public function update() {
    	$this->language->load('payment/erede');

    	$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('erede/risco');
		
		if (isset($this->request->post['risco_produto'])) {
			foreach ($this->request->post['risco_produto'] as $k => $v) 
			{
				$this->model_erede_risco->updateRisk($k, $v);
	  		}
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('erede/risco', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

    	$this->getList();
  	}
	
  	protected function getList() {				
		
						
		$url = '';
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
						
  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('payment/erede', 'token=' . $this->session->data['token'] . $url, 'SSL'),       		
      		'separator' => ' :: '
   		);
		
		$this->data['update'] = $this->url->link('erede/risco/update', 'token=' . $this->session->data['token'] . $url, 'SSL');
    	
		$this->data['products'] = array();

		$data = array(
			'start'           => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit'           => $this->config->get('config_admin_limit')
		);
		
		$this->load->model('tool/image');
		
		$product_total = $this->model_erede_risco->getTotalProducts($data);
		$results = $this->model_erede_risco->getProducts($data);
				    	
		foreach ($results as $result) {
			$action = array();
			
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('erede/risco/update', 'token=' . $this->session->data['token'] . '&product_id=' . $result['product_id'] . $url, 'SSL')
			);
			
			if ($result['image'] && file_exists(DIR_IMAGE . $result['image'])) {
				$image = $this->model_tool_image->resize($result['image'], 40, 40);
			} else {
				$image = $this->model_tool_image->resize('no_image.jpg', 40, 40);
			}			

      		$this->data['products'][] = array(
				'product_id' => $result['product_id'],
				'name'       => $result['name'],
				'risco_produto' => $result['risco_produto'],
				'model'      => $result['model'],
				'price'      => $result['price'],
				'image'      => $image,
				'quantity'   => $result['quantity'],
				'status'     => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
				'selected'   => isset($this->request->post['selected']) && in_array($result['product_id'], $this->request->post['selected']),
				'action'     => $action
			);
    	}
		
		$this->data['heading_title_risco'] = $this->language->get('heading_title_risco');		
				
		$this->data['text_enabled'] = $this->language->get('text_enabled');		
		$this->data['text_disabled'] = $this->language->get('text_disabled');		
		$this->data['text_no_results'] = $this->language->get('text_no_results');		
		$this->data['text_image_manager'] = $this->language->get('text_image_manager');		
			
		$this->data['column_image'] = $this->language->get('column_image');		
		$this->data['column_product_name'] = $this->language->get('column_product_name');		
		$this->data['column_risk'] = $this->language->get('column_risk');		

		$this->data['risco_low'] = $this->language->get('risco_low');
		$this->data['risco_medium'] = $this->language->get('risco_medium');
		$this->data['risco_high'] = $this->language->get('risco_high');
		
		$this->data['button_save'] = $this->language->get('button_save');	
		$this->data['button_back'] = $this->language->get('button_back');
		$this->data['back'] = $this->url->link('payment/erede', 'token=' . $this->session->data['token'], 'SSL');		
		 
 		$this->data['token'] = $this->session->data['token'];
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
				
		$pagination = new Pagination();
		$pagination->total = $product_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('erede/risco', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
			
		$this->data['pagination'] = $pagination->render();

		$this->template = 'erede/risco.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
  	}
}
?>