<?php
class ControllerToolTestcareTable extends Controller {
	private $error = array();
	
	public function index() {		
		$this->load->language('tool/testcare_table');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('tool/testcare_table');
		
		$this->model_tool_testcare_table->checkDB();

		$this->data['heading_title'] = $this->language->get('heading_title');
		$this->data['text_call_type'] = $this->language->get('text_call_type');
		$this->data['text_priority_code'] = $this->language->get('text_priority_code');
		$this->data['text_problem_type'] = $this->language->get('text_problem_type');
		$this->data['entry_existing_types'] = $this->language->get('entry_existing_types');
		$this->data['entry_existing_codes'] = $this->language->get('entry_existing_codes');
		$this->data['entry_existing_problems'] = $this->language->get('entry_existing_problems');
		$this->data['entry_new_type'] = $this->language->get('entry_new_type');
		$this->data['entry_new_code'] = $this->language->get('entry_new_code');
		$this->data['entry_new_problem'] = $this->language->get('entry_new_problem');
		$this->data['entry_edit'] = $this->language->get('entry_edit');
		$this->data['entry_delete'] = $this->language->get('entry_delete');
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_update'] = $this->language->get('button_update');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} elseif (isset($this->session->data['error_warning'])) {
			$this->data['error_warning'] = $this->session->data['error_warning'];
			unset($this->session->data['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'] = '';
		}
		
  		$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('tool/testcare_table', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);

		$this->data['call_types'] = array();
		$results = $this->model_tool_testcare_table->getValues("1");
		foreach ($results as $result) {
			$this->data['call_types'][] = array(
				'table_id'	=> $result['table_id'],
				'value'		=> $result['value'] ,
                	'dvalue'		=> $result['dvalue']
			);
		}
		
		$this->data['priority_codes'] = array();
		$results = $this->model_tool_testcare_table->getValues("2");
		foreach ($results as $result) {
			$this->data['priority_codes'][] = array(
				'table_id'	=> $result['table_id'],
				'value'		=> $result['value'],
                	'dvalue'		=> $result['dvalue']
			);
		}
		
		$this->data['problem_types'] = array();
		$results = $this->model_tool_testcare_table->getValues("3");
		foreach ($results as $result) {
			$this->data['problem_types'][] = array(
				'table_id'	=> $result['table_id'],
				'value'		=> $result['value'],
                	'dvalue'		=> $result['dvalue']
			);
		}
		
		$this->data['token'] = $this->session->data['token'];
		$this->data['cancel'] = $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL');

		$this->template = 'tool/testcare_table.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}

	public function save_call_type() {
		$this->load->language('tool/testcare_table');
		$this->load->model('tool/testcare_table');
		$json = "";
		$results = $this->model_tool_testcare_table->addValue($this->request->post);
		if ($results) {
			$json = "<option value=''></option>";
			foreach ($results as $result) {
				$json .= "<option value='" . $result['table_id'] . "'>" . $result['value'] . " " . $result['dvalue'] . "</option>";
			}
		}
		echo json_encode($json);
	}
	
	public function edit_call_type() {
		$this->load->language('tool/testcare_table');
		$this->load->model('tool/testcare_table');
		$json = "";
		$results = $this->model_tool_testcare_table->editValue($this->request->post);
		if ($results) {
			$json = "<option value=''></option>";
			foreach ($results as $result) {
				$json .= "<option value='" . $result['table_id'] . "'>" . $result['value'] . " " . $result['dvalue'] . "</option>";
			}
		}
		echo json_encode($json);
	}
	
	public function delete_call_type() {
		$this->load->language('tool/testcare_table');
		$this->load->model('tool/testcare_table');
		$json = "";
		$results = $this->model_tool_testcare_table->deleteValue($this->request->post);
		if ($results) {
			$json = "<option value=''></option>";
			foreach ($results as $result) {
				$json .= "<option value='" . $result['table_id'] . "'>" . $result['value'] . " " . $result['dvalue'] . "</option>";
			}
		}
		echo json_encode($json);
	}
	
	public function save_priority_code() {
		$this->load->language('tool/testcare_table');
		$this->load->model('tool/testcare_table');
		$json = "";
		$results = $this->model_tool_testcare_table->addValue($this->request->post);
		if ($results) {
			$json = "<option value=''></option>";
			foreach ($results as $result) {
				$json .= "<option value='" . $result['table_id'] . "'>" . $result['value'] . " " . $result['dvalue'] . "</option>";
			}
		}
		echo json_encode($json);
	}
	
	public function edit_priority_code() {
		$this->load->language('tool/testcare_table');
		$this->load->model('tool/testcare_table');
		$json = "";
		$results = $this->model_tool_testcare_table->editValue($this->request->post);
		if ($results) {
			$json = "<option value=''></option>";
			foreach ($results as $result) {
				$json .= "<option value='" . $result['table_id'] . "'>" . $result['value'] . " " . $result['dvalue'] . "</option>";
			}
		}
		echo json_encode($json);
	}
	
	public function delete_priority_code() {
		$this->load->language('tool/testcare_table');
		$this->load->model('tool/testcare_table');
		$json = "";
		$results = $this->model_tool_testcare_table->deleteValue($this->request->post);
		if ($results) {
			$json = "<option value=''></option>";
			foreach ($results as $result) {
				$json .= "<option value='" . $result['table_id'] . "'>" . $result['value'] . " " . $result['dvalue'] . "</option>";
			}
		}
		echo json_encode($json);
	}
	
	public function save_problem_type() {
		$this->load->language('tool/testcare_table');
		$this->load->model('tool/testcare_table');
		$json = "";
		$results = $this->model_tool_testcare_table->addValue($this->request->post);
		if ($results) {
			$json = "<option value=''></option>";
			foreach ($results as $result) {
				$json .= "<option value='" . $result['table_id'] . "'>" . $result['value'] . " " . $result['dvalue'] . "</option>";
			}
		}
		echo json_encode($json);
	}
	
	public function edit_problem_type() {
		$this->load->language('tool/testcare_table');
		$this->load->model('tool/testcare_table');
		$json = "";
		$results = $this->model_tool_testcare_table->editValue($this->request->post);
		if ($results) {
			$json = "<option value=''></option>";
			foreach ($results as $result) {
				$json .= "<option value='" . $result['table_id'] . "'>" . $result['value'] . " " . $result['dvalue'] . "</option>";
			}
		}
		echo json_encode($json);
	}
	
	public function delete_problem_type() {
		$this->load->language('tool/testcare_table');
		$this->load->model('tool/testcare_table');
		$json = "";
		$results = $this->model_tool_testcare_table->deleteValue($this->request->post);
		if ($results) {
			$json = "<option value=''></option>";
			foreach ($results as $result) {
				$json .= "<option value='" . $result['table_id'] . "'>" . $result['value'] . " " . $result['dvalue'] . "</option>";
			}
		}
		echo json_encode($json);
	}
	
}

?>
