<?php namespace App\Controllers\Admin;

class Career extends AdminController
{
	private $mObj;
	
	public function __construct(){		
        $this->mObj = model('App\Models\Admin\CareerModel');
		$this->menu_id = 23;
    }
	
	public function index(){
		$this->allowed('is_list');
		$data['footer_includes'] = ['career_list_js'];		
		echo $this->view_inner_template('career_list',$data);
	}
	
	public function ajax_list(){
		$this->allowed('is_list');
		$result = $this->mObj->get_datatable();
		$output = array(
			"meta" => $result['meta'],
            "data" => $result['data']
        );
        
        // Output to JSON format
        echo json_encode($output);
	}
	
	public function add(){
		$this->allowed('is_add');
		$data['form_heading'] = "Career Add";
		
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules();
			if ($this->validate($rules)) { 
				$data_in = $this->get_column_values();
				$this->mObj->insert($data_in);
				$this->notify_success('Record Added Successfully');
				return redirect()->to(get_admin_path('career'));
			}
		}
		$data['action'] = "add";
		$data['footer_includes'] = ['career_add_edit_js'];	
		echo $this->view_inner_template('career_add_edit',$data);
	}
	
	public function edit($career_id){
		$this->allowed('is_edit');
		$data['form_heading'] = "Career Edit";
		if($this->request->getMethod() === 'post'){
			$rules = $this->get_validation_rules($career_id);
			if ($this->validate($rules)) { 
				$data_in = $this->get_column_values();
				$this->mObj->update($data_in,$career_id);
				$this->notify_success('Record Updated Successfully');
				return redirect()->to(get_admin_path('career'));
			}
		}else{
			$data['update_data'] = $this->mObj->get($career_id);
		}
		$data['action'] = "edit";
		$data['career_id'] = $career_id;
		$data['footer_includes'] = ['career_add_edit_js'];	
		echo $this->view_inner_template('career_add_edit',$data);
	}
	
	public function delete($career_id){
		$this->allowed('is_edit');
		$this->mObj->delete($career_id);
		$this->notify_success('Record Deleted Successfully');
		return redirect()->to(get_admin_path('career'));
	}
	
	
	private function get_column_values(){
		$data['title'] = $this->request->getPost('title');
		$data['type'] = $this->request->getPost('type');
		$data['location'] = $this->request->getPost('location');
		$data['description'] = $this->request->getPost('description');
		$data['career_url'] = $this->request->getPost('career_url');
		$data['status'] = $this->request->getPost('status');
		return $data;
	}
	
	private function get_validation_rules($career_id=""){
		$rules['title'] = ['label'=>'Title', 'rules' => 'trim|required'];
		$rules['career_url'] = ['label'=>'Url', 'rules' => 'trim|required|pr_callback['.$this->mObj->is_unique_career_url($this->request->getPost('career_url'),$career_id).']'];
		$rules['location'] = ['label'=>'location', 'rules' => 'trim|required'];
		$rules['description'] = ['label'=>'Description', 'rules' => 'trim|required'];
		$rules['status'] = ['label'=>'Status', 'rules' => 'trim|required'];
		return $rules;
	}
	
	
	
	
}