<?php
class ControllerReportBrechSpa extends Controller {
	
	const CATEGORY_SPA = 62;
	
	public function index() {
		$this->load->language('report/brech_spa');

		$this->document->setTitle($this->language->get('heading_title'));

		$data['breadcrumbs'] = $this->breadcrumbs();

		$labels = [
			'heading_title',
			'entry_category',
			'entry_start_date',
			'entry_end_date',
			'button_filter',
			'column_name',
			'column_group',
			'column_date',
			'column_service',
			'column_price',
			'column_time',
			'column_quantity',
			'column_pay_free',
			'column_pay_cod',
			'column_pay_card',
			'column_admin',
		];

		$this->loadLanguageData($data, $labels);
		
		$data['start_date'] = isset($this->request->get['start_date']) ? $this->request->get['start_date'] : date('Y-m-d');
		$data['end_date'] = isset($this->request->get['end_date']) ? $this->request->get['end_date'] : date('Y-m-d');
		
		$data['categories'] = $this->categories();

		$data['token'] = $this->session->data['token'];
		$data['route'] = 'report/brech_spa';
		
		$this->document->addScript('https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js');
		$this->document->addScript('https://cdn.datatables.net/buttons/1.2.2/js/dataTables.buttons.min.js');
		$this->document->addScript('https://cdn.datatables.net/buttons/1.2.2/js/buttons.html5.min.js');
		$this->document->addScript('https://cdn.datatables.net/buttons/1.2.2/js/buttons.flash.min.js');
		$this->document->addScript('https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js');
		$this->document->addScript('https://cdn.datatables.net/buttons/1.2.2/js/buttons.print.min.js');
		$this->document->addScript('https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/pdfmake.min.js');
		$this->document->addScript('https://cdn.rawgit.com/bpampuch/pdfmake/0.1.18/build/vfs_fonts.js');
		$this->document->addStyle('https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css');
		$this->document->addStyle('https://cdn.datatables.net/buttons/1.2.2/css/buttons.dataTables.min.css');

		$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('report/brech_spa', $data));
	}

	public function orders_ajax() {
		/*
		$json = [
			"draw" => (int)$this->request->get['draw'],
			"recordsTotal" => 57,
			"recordsFiltered" => 57,
			"data" => []
		];
		 * 
		 */
		
		$this->load->model('report/brech_spa');

		$data = [
			'request'    => $this->request->get,
			'db'         => $this->db,
			'primaryKey' => 'order_product_id',
			'columns'    => [
				['db' => 'c_name',      'dt' => 'client_name'],
				['db' => 'date',        'dt' => 'date'],
				['db' => 'service',     'dt' => 'service'],
				['db' => 'price',       'dt' => 'price'],
				['db' => 'option_time', 'dt' => 'option_time'],
				['db' => 'quantity',    'dt' => 'quantity'],
				['db' => 'total_free',  'dt' => 'payment_free'],
				['db' => 'total_cod',   'dt' => 'payment_cod'],
			]
		];

		$json = $this->model_report_brech_spa->order($data);

		$this->output($json);
	}

	protected function loadLanguageData(&$data, array $labels = []) {
		foreach ($labels as $label) {
			$data[$label] = $this->language->get($label);
		}
	}

	protected function breadcrumbs() {
		$breadcrumbs = [];

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

		$breadcrumbs[] = array(
			'text' => $this->language->get('heading_title'),
			'href' => $this->url->link('report/brech_spa', ['token' => $this->session->data['token']], true)
		);

		return $breadcrumbs;
	}

	protected function output($json) {
 		if(isset($this->request->get['cb'])){
			$this->response->addHeader('Content-Type: application/javascript');
			$this->response->setOutput($this->request->get['cb'] . "(" . json_encode($json). ");");
		}else{
			$this->response->addHeader('Content-Type: application/json');
			$this->response->setOutput(json_encode($json));
		}
	}
	
	public function orders() {
		$this->load->model('report/brech_spa');
		
		$filter_data = [
			'start_date' => isset($this->request->get['start_date'])? $this->request->get['start_date'] : date('Y-m-d'),
			'end_date'   => isset($this->request->get['end_date'])  ? $this->request->get['end_date']   : date('Y-m-d'),
			'category'   => isset($this->request->get['category'])  ? $this->request->get['category']   : self::CATEGORY_SPA,
		];
		
		$result = $this->model_report_brech_spa->service($filter_data);
		
		$json['data'] = [];
		
		foreach ($result['data'] as $row) {
			$json['data'][] = [
				'client_name'  => isset($row['customer_name']) ? $row['customer_name'] : '',
				'client_group' => isset($row['customer_group_name']) ? $row['customer_group_name'] : '',
				'date'         => isset($row['booking_date']) ? $row['booking_date'] : '',
				'service'      => isset($row['product_name']) ? $row['product_name'] : '',
				'price'        => isset($row['total']) ? sprintf('%6.2f', $row['total']) : '',
				'option_time'  => '',
				'quantity'     => isset($row['quantity']) ? $row['quantity'] : '',
				'payment_free' => isset($row['pay'][0]) ? $row['pay'][0] : '',
				'payment_cod'  => isset($row['pay'][55]) ? $row['pay'][55] : '',
				'payment_card' => isset($row['pay'][56]) ? $row['pay'][56] : '',
				'admin'        => isset($row['user_name']) ? $row['user_name'] : '',
			];
		}
		
		$this->output($json);
	}
	
	protected function categories() {
		$this->load->model('report/brech_spa');

		$result = array();

		$categories = $this->model_report_brech_spa->categories(0);
		
		foreach ($categories as $category) {
			if((int)$category['category_id'] === self::CATEGORY_SPA){
				$result[$category['category_id']] = $category['name'];

				$children = $this->model_report_brech_spa->categories($category['category_id']);

				foreach($children as $child) {
					$result[$child['category_id']] = $child['name'];
				}
			}
		}
		
		return $result;
	}
}