<?php


if (!defined("ROOT_PATH"))

{

	header("HTTP/1.1 403 Forbidden");

	exit;

}

class pjAdminSuperServices extends pjAdmin

{


	public function pjActionCreate()
	{
		$this->checkLogin();
		
		if (!pjAuth::factory()->hasAccess())
		{
			$this->sendForbidden();
			return;
		}
		
		
		if (self::isPost() && $this->_post->toInt('service_create'))
		{
			$err = 'AS03';

			$_POST = $this->_post->raw();
			$data['calendar_id'] = $this->getForeignId();
			$id = pjSuperServiceModel::factory(array_merge($_POST, $data))->insert()->getInsertId();
			
			if ($id !== false && (int) $id > 0)
			{
				
				$i18n_arr = $this->_post->toI18n('i18n');
				if (!empty($i18n_arr))
				{
					pjMultiLangModel::factory()->saveMultiLang($i18n_arr, $id, 'pjSuperService');
				}
			} else {
				$err = 'AS04';
			}
			
			pjUtil::redirect($_SERVER['PHP_SELF'] . "?controller=pjAdminSuperServices&action=pjActionIndex&err=$err");
		} 
		
		if (self::isGet())
		{
			$locale_arr = pjLocaleModel::factory()->select('t1.*, t2.file')
				->join('pjLocaleLanguage', 't2.iso=t1.language_iso', 'left')
				->where('t2.file IS NOT NULL')
				->orderBy('t1.sort ASC')->findAll()->getData();
					
			$lp_arr = array();
			foreach ($locale_arr as $item)
			{
				$lp_arr[$item['id']."_"] = $item['file'];
			}
			$this->set('lp_arr', $locale_arr);
			$this->set('locale_str', self::jsonEncode($lp_arr));
			
			$this->set('is_flag_ready', $this->requestAction(array('controller' => 'pjLocale', 'action' => 'pjActionIsFlagReady'), array('return')));
			
			
			$this->appendCss('awesome-bootstrap-checkbox.css', PJ_THIRD_PARTY_PATH . 'awesome_bootstrap_checkbox/');
			
			$this->appendCss('jasny-bootstrap.min.css', PJ_THIRD_PARTY_PATH . 'jasny/');
			$this->appendJs('jasny-bootstrap.min.js', PJ_THIRD_PARTY_PATH . 'jasny/');
			
			$this->appendCss('css/select2.min.css', PJ_THIRD_PARTY_PATH . 'select2/');
			$this->appendJs('js/select2.full.min.js', PJ_THIRD_PARTY_PATH . 'select2/');
			
			$this->appendJs('jquery.multilang.js', PJ_FRAMEWORK_LIBS_PATH . 'pj/js/');
			$this->appendJs('jquery.validate.min.js', PJ_THIRD_PARTY_PATH . 'validate/');
			$this->appendJs('additional-methods.js', PJ_THIRD_PARTY_PATH . 'validate/');
			$this->appendJs('pjAdminSuperServices.js');
		}
	}


	public function pjActionDeleteService()
	{
		$this->setAjax(true);
	
		if (!$this->isXHR())
		{
			self::jsonResponse(array('status' => 'ERR', 'code' => 100, 'text' => 'Missing headers.'));
		}
		
		if (!self::isPost())
		{
			self::jsonResponse(array('status' => 'ERR', 'code' => 100, 'text' => 'HTTP method not allowed.'));
		}

		if ($this->_get->check('id') && $this->_get->toInt('id') > 0)
		{
			$pjSuperServiceModel = pjSuperServiceModel::factory();
			$arr = $pjSuperServiceModel->find($this->_get->toInt('id'))->getData();
			if (!empty($arr) && $pjSuperServiceModel->setAttributes(array('id' => $arr['id']))->erase()->getAffectedRows() == 1)
			{
				pjMultiLangModel::factory()->where('model', 'pjSuperService')->where('foreign_id', $this->_get->toInt('id'))->eraseAll();
				
				self::jsonResponse(array('status' => 'OK', 'code' => 200, 'text' => 'Service has been deleted.'));
			} else {
				self::jsonResponse(array('status' => 'ERR', 'code' => 100, 'text' => 'Service has not been deleted.'));
			}
		}
		
		self::jsonResponse(array('status' => 'ERR', 'code' => 100, 'text' => 'Missing, empty or invalid parameters.'));
	}
	

	public function pjActionDeleteServiceBulk()
	{
		$this->setAjax(true);
	
		if (!$this->isXHR())
		{
			self::jsonResponse(array('status' => 'ERR', 'code' => 100, 'text' => 'Missing headers.'));
		}
		
		if (!self::isPost())
		{
			self::jsonResponse(array('status' => 'ERR', 'code' => 100, 'text' => 'HTTP method not allowed.'));
		}
		
		$record = $this->_post->toArray('record');
		if (count($record))
		{
			$pjSuperServiceModel = pjSuperServiceModel::factory();
			$arr = $pjSuperServiceModel->whereIn('id', $record)->findAll()->getData();
			
			$pjSuperServiceModel->reset()->whereIn('id', $record)->eraseAll();
			pjMultiLangModel::factory()->where('model', 'pjSuperService')->whereIn('foreign_id', $record)->eraseAll();
			
			self::jsonResponse(array('status' => 'OK', 'code' => 200, 'text' => 'Service(s) has been deleted.'));
		}
		exit;
	}
	

	public function pjActionGetService()

	{

		$this->setAjax(true);

	

		if ($this->isXHR() && $this->isLoged())

		{

			$pjSuperServiceModel = pjSuperServiceModel::factory()

				->join('pjMultiLang', "t2.model='pjSuperService' AND t2.foreign_id=t1.id AND t2.field='name' AND t2.locale='".$this->getLocaleId()."'", 'left outer')

				->where('t1.calendar_id', $this->getForeignId());

			// $get = $this->_get->raw();

			// if (isset($_GET['q']) && !empty($_GET['q']))

			$get = $this->_get->raw();
			if ($q = $this->_get->toString('q'))

			{

				//$q = str_replace(array('_', '%'), array('\_', '\%'), trim($_GET['q']));

				$pjSuperServiceModel->where('t2.content LIKE', "%$q%");

			}



			// if (isset($_GET['is_active']) && strlen($_GET['is_active']) > 0 && in_array($_GET['is_active'], array(1, 0)))

			if ($this->_get->check('is_active') && $get['is_active'] != '' && in_array($this->_get->toString('is_active'), array(0,1))) 

			{

				$pjSuperServiceModel->where('t1.is_active', $this->_get->toString('is_active'));

			}



			$column = 'name';

			$direction = 'ASC';

			// if (isset($_GET['direction']) && isset($_GET['column']) && in_array(strtoupper($_GET['direction']), array('ASC', 'DESC')))

			// {
			if ($this->_get->check('column') && in_array(strtoupper($this->_get->toString('direction')), array('ASC', 'DESC')))
			{

				// $column = $_GET['column'];

				// $direction = strtoupper($_GET['direction']);
				$column = $this->_get->toString('column');
				$direction = strtoupper($this->_get->toString('direction'));

			}



			$total = $pjSuperServiceModel->findCount()->getData();

			$rowCount = $this->_get->toInt('rowCount') ? $this->_get->toInt('rowCount') : 10;

			$pages = ceil($total / $rowCount);

			$page = $this->_get->toInt('page') ? $this->_get->toInt('page') : 1;

			$offset = ((int) $page - 1) * $rowCount;

			if ($page > $pages)

			{

				$page = $pages;

			}



			$data = $pjSuperServiceModel

			->select("t1.*, t2.content AS `name`")

			->orderBy("$column $direction")->limit($rowCount, $offset)->findAll()->getData();

			foreach ($data as $k => $v)

			{

				$data[$k]['price_from'] = pjCurrency::formatPrice(number_format($v['price_from'], 2));

				$data[$k]['price_to'] = pjCurrency::formatPrice(number_format($v['price_to'], 2));

			}

			pjAppController::jsonResponse(compact('data', 'total', 'pages', 'page', 'rowCount', 'column', 'direction'));

		}

		exit;

	}

	

	public function pjActionIndex()

	{

		$this->checkLogin();

		

		if ($this->isAdmin())

		{

			$this->appendJs('jquery.datagrid.js', PJ_FRAMEWORK_LIBS_PATH . 'pj/js/');

			$this->appendJs('pjAdminSuperServices.js');

			$this->appendJs('index.php?controller=pjAdmin&action=pjActionMessages', PJ_INSTALL_URL, true);

		} else {

			$this->set('status', 2);

		}

	}


	public function pjActionUpdate()
	{
		if (!pjAuth::factory()->hasAccess())
		{
			$this->sendForbidden();
			return;
		}
		
		
		if (self::isPost() && $this->_post->toInt('service_update'))
		{
		    
			$err = 'AS01';

			$data = array();
			
			$_POST = $this->_post->raw();
			
			pjSuperServiceModel::factory()->set('id', $_POST['id'])->modify(array_merge($_POST, $data));

			
			$i18n_arr = $this->_post->toI18n('i18n');
			if (!empty($i18n_arr))
			{
				pjMultiLangModel::factory()->updateMultiLang($i18n_arr, $this->_post->toInt('id'), 'pjSuperService');
			}
			
			if($err == 'AS01')
			{
				pjUtil::redirect(PJ_INSTALL_URL . "index.php?controller=pjAdminSuperServices&action=pjActionIndex&err=AS01");
			}else{
				pjUtil::redirect($_SERVER['PHP_SELF'] . "?controller=pjAdminSuperServices&action=pjActionUpdate&id=".$this->_post->toString('id')."&err=$err");
			}
				
		} 
		
		if (self::isGet())
		{
			$arr = pjSuperServiceModel::factory()->find($this->_get->toInt('id'))->getData();
			if (empty($arr))
			{
				pjUtil::redirect(PJ_INSTALL_URL. "index.php?controller=pjAdminSuperServices&action=pjActionIndex&err=AS08");
			}
			$arr['i18n'] = pjMultiLangModel::factory()->getMultiLang($arr['id'], 'pjSuperService');
			$this->set('arr', $arr);
			
			$locale_arr = pjLocaleModel::factory()->select('t1.*, t2.file')
							->join('pjLocaleLanguage', 't2.iso=t1.language_iso', 'left')
							->where('t2.file IS NOT NULL')
							->orderBy('t1.sort ASC')->findAll()->getData();
				
			$lp_arr = array();
			foreach ($locale_arr as $item)
			{
				$lp_arr[$item['id']."_"] = $item['file'];
			}
			$this->set('lp_arr', $locale_arr);
			$this->set('locale_str', self::jsonEncode($lp_arr));
		
			
			$this->set('is_flag_ready', $this->requestAction(array('controller' => 'pjLocale', 'action' => 'pjActionIsFlagReady'), array('return')));
				
			$this->appendCss('awesome-bootstrap-checkbox.css', PJ_THIRD_PARTY_PATH . 'awesome_bootstrap_checkbox/');
			
			$this->appendCss('jasny-bootstrap.min.css', PJ_THIRD_PARTY_PATH . 'jasny/');
			$this->appendJs('jasny-bootstrap.min.js', PJ_THIRD_PARTY_PATH . 'jasny/');
			
			$this->appendCss('css/select2.min.css', PJ_THIRD_PARTY_PATH . 'select2/');
			$this->appendJs('js/select2.full.min.js', PJ_THIRD_PARTY_PATH . 'select2/');
				
			$this->appendJs('jquery.multilang.js', PJ_FRAMEWORK_LIBS_PATH . 'pj/js/');
			$this->appendJs('jquery.validate.min.js', PJ_THIRD_PARTY_PATH . 'validate/');
			$this->appendJs('additional-methods.js', PJ_THIRD_PARTY_PATH . 'validate/');
			$this->appendJs('pjAdminSuperServices.js');
		}
	}


}

?>