<?php

class ControllerMadeshopMinprice extends Controller {
	private $error = array();

	public function index() {

	    $this->load->model('setting/setting');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			$this->model_setting_setting->editSetting('minprice', $this->request->post, 0);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->response->redirect($this->url->link('madeshop/minprice', 'user_token=' . $this->session->data['user_token'] . '&store_id=0', true));
		}
		
        $this->load->model('madeshop/minprice');
		
		$this->model_madeshop_minprice->install();
		
		$this->load->language('madeshop/minprice');
		
		$this->document->setTitle($this->language->get('heading_title'));

		$data['heading_title'] = $this->language->get('heading_title');
		$data['text_list'] = $this->language->get('text_list');
		$data['text_no_results'] = $this->language->get('text_no_results');
		
		$data['text_enabled'] = $this->language->get('text_enabled');
		$data['text_disabled'] = $this->language->get('text_disabled');

		$data['entry_status'] = $this->language->get('entry_status');
		$data['button_save'] = $this->language->get('button_save');
		$data['button_cancel'] = $this->language->get('button_cancel');
		
		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
		);

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('heading_title'),
			'href' => $this->url->link('report/report', 'user_token=' . $this->session->data['user_token'], true)
		);
		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else { 
			$page = 1;
		}
		
		$url = '';
		
		$filter_data = array(
			'page' => $page,
			'limit' => $this->config->get('config_limit_admin'),
			'start' => $this->config->get('config_limit_admin') * ($page - 1),
		);
        $total = $this->model_madeshop_minprice->getTotalMinprice();

		$pagination = new Pagination();
		$pagination->total = $total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_limit_admin');
		$pagination->url = $this->url->link('madeshop/minprice', 'user_token=' . $this->session->data['user_token'] . $url . '&page={page}', 'SSL');

		$data['pagination'] = $pagination->render();

		$data['results'] = sprintf($this->language->get('text_pagination'), ($total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($total - $this->config->get('config_limit_admin'))) ? $total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $total, ceil($total / $this->config->get('config_limit_admin')));
		$data['user_token'] = $this->session->data['user_token'];

		$data['minprices'] = array();
		
		$minprices = $this->model_madeshop_minprice->getMinprice($filter_data);
		$text = $this->language->get('time');
		
		foreach ($minprices as $result) {
			$data['minprices'][] = array(
				'id'  => $result['id'],
				'mail'       => $result['mail'],				
				'name'       => $result['name'],
				'url'       => $result['url'],	
                'shop_url'       => HTTP_CATALOG.'index.php?route=product/product&product_id='.$result['shop_url'],			
				'manager_comment'     => $result['manager_comment'],
				'manager_status'     => $result['manager_status'],
				'date_created'     => $result['date_created'],

			);
		}
		
		$data['action'] = $this->url->link('madeshop/minprice', 'user_token=' . $this->session->data['user_token'] . '&store_id=0', true);

		$data['cancel'] = $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'] . '&type=extension', true);

		if (isset($this->request->get['store_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
			$setting_info = $this->model_setting_setting->getSetting('minprice', 0);
		}

		
		if (isset($this->request->post['minprice_status'])) {
			$data['minprice_status'] = $this->request->post['minprice_status'];
		} else {
			$data['minprice_status'] = $this->config->get('minprice_status');
		}
		
		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');

		$this->response->setOutput($this->load->view('madeshop/minprice', $data));
	}
	
	public function posting() {
		$this->load->model('madeshop/minprice');
		$data = array (
			'manager_comment'     => $this->request->post['manager_comment'],
			'id'           => $this->request->post['id']			
		);	
	    $this->model_madeshop_minprice->posting($data);
	}
	
	public function status() {
		    $this->load->model('madeshop/minprice');
			$data = array (
			'manager_status'     => $this->request->post['manager_status'],
			'id'           => $this->request->post['id']			
		    );
			$this->model_madeshop_minprice->status($data);
	}
	
	public function del() {
		    $this->load->model('madeshop/minprice');
		    $id = $this->request->post['id'];
			$this->model_madeshop_minprice->del($id);
	}
	
	protected function validate() {
		if (!$this->user->hasPermission('modify', 'madeshop/minprice')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		return !$this->error;
	}

}