<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Career_model extends CI_Model
{
    //Get All Category
    public function get_all_category()
        {
            return $this->db->get('career_category')->result();   
        }
    //Get Category By id
    public function get_category_by_id($id)
        {
            $this->db->where('id', $id);
            return $this->db->get('career_category')->row();   
        }
    public function get_category_by_slug($slug)
        {
            $this->db->where('slug', $slug);
            return $this->db->get('career_category')->row();   
        }
    //Add Category
    public function add_new_job_category()
        {
            $data = array(
                'category_name' => $this->input->post('category_name'),
                'slug' => $this->slugURL($this->input->post('category_name')),
                'date' => date('Y-m-d')
            );
            return $this->db->insert('career_category', $data);   
        }
    //Update Category
    public function update_job_category()
        {
            $id = $this->input->post('category_id');
            $data = array(
                'category_name' => $this->input->post('category_name'),
                'slug' => $this->slugURL($this->input->post('category_name')),
            );
            $this->db->where('id', $id);
            return $this->db->update('career_category', $data);   
        }
    //Delete Category
    public function delete_job_category($id)
        {
            $this->db->where('id', $id);
            return $this->db->delete('career_category');   
        }
    //Add New Job
    public function add_job()
        {
            $data = array(
                'title' => $this->input->post('title'),
                'slug' => $this->slugURL($this->input->post('title')),
                'category' => $this->input->post('category'),
                'employment_status' => $this->input->post('employment_status'),
                'vacancy' => $this->input->post('vacancy'),
                'application_deadline' => $this->input->post('application_deadline'),
                'experience_year' => $this->input->post('experience_year'),
                'job_responsibilities' => $this->input->post('job_responsibilities'),
                'educational_requirements' => $this->input->post('educational_requirements'),
                'experience_requirements' => $this->input->post('experience_requirements'),
                'additional_requirements' => $this->input->post('additional_requirements'),
                'salary' => $this->input->post('salary'),
                'benefits' => $this->input->post('benefits'),
                'job_location' => $this->input->post('job_location'),
                'read_before_apply' => $this->input->post('read_before_apply'),
                'email' => $this->input->post('email'),
                'code' => rand(1000, 9999)
            );
            return $this->db->insert('career_page', $data);   
        }
     //View All Job 
    public function view_all_job()
        {
            return $this->db->get('career_page')->result();   
        }
     //View Job By code 
    public function view_job_by_id($code)
        {
            $this->db->where('code', $code);
            return $this->db->get('career_page')->row();   
        }
    public function view_job_by_slug($slug)
        {
            $this->db->where('slug', $slug);
            return $this->db->get('career_page')->row();   
        }
    //Get No of Job By Category id 
    public function get_no_of_job_by_category_id($id)
        {
            $this->db->where('category', $id);
            return $this->db->get('career_page')->num_rows();   
        }
     //Get Job By Category id 
    public function get_job_by_category_id($id)
        {
            $this->db->where('category', $id);
            return $this->db->get('career_page')->result();   
        }
    //Update Job 
    public function update_job()
        {
            $id = $this->input->post('job_id');
            $data = array(
                'title' => $this->input->post('title'),
                'slug' => $this->slugURL($this->input->post('title')),
                'category' => $this->input->post('category'),
                'employment_status' => $this->input->post('employment_status'),
                'vacancy' => $this->input->post('vacancy'),
                'application_deadline' => $this->input->post('application_deadline'),
                'experience_year' => $this->input->post('experience_year'),
                'job_responsibilities' => $this->input->post('job_responsibilities'),
                'educational_requirements' => $this->input->post('educational_requirements'),
                'experience_requirements' => $this->input->post('experience_requirements'),
                'additional_requirements' => $this->input->post('additional_requirements'),
                'salary' => $this->input->post('salary'),
                'benefits' => $this->input->post('benefits'),
                'job_location' => $this->input->post('job_location'),
                'read_before_apply' => $this->input->post('read_before_apply'),
                'email' => $this->input->post('email'),

            );
            $this->db->where('id', $id);
            return $this->db->update('career_page', $data);   
        }
    //View Job By id 
    public function delete_job($id)
        {
            $this->db->where('id', $id);
            return $this->db->delete('career_page');   
        }
    //Apply Job 
    public function apply_job()
        {
           $data = array(
            'job_id' => $this->input->post('job_id'),
            'name' => $this->input->post('name'), 
            'father_name' => $this->input->post('father_name'),  
            'gender' => $this->input->post('gender'), 
            'phone_no' => $this->input->post('phone_no'), 
            'email_id' => $this->input->post('email_id'), 
            'address' =>  $this->input->post('address')
            );
            return $this->db->insert('job_apply', $data);   
        }
        //Get All Job Application 
    public function get_all_job_application()
        {
            return $this->db->get('job_apply')->result();   
        }
     //Delete Job Application 
    public function delete_job_application($id)
        {
            $this->db->where('id', $id);
            return $this->db->delete('job_apply');   
        }
    //get sitemap Career
    public function get_sitemap_career_page()
    {   
        $this->db->select('career_page');
        $career_page = $this->db->get('home_page_setting')->row();
        if($career_page->career_page == 1){
        $this->db->order_by('career_page.id', 'DESC');
        $query = $this->db->get('career_page');
        return $query->result();
        }
    }
    private function slugURL($text)
    {
        $clean =  preg_replace('/[^a-zA-Z0-9\s]/', '', strtolower($text)); 
        $clean =  preg_replace('!\s+!', '-', $clean); 
        return $clean;
    }
    

        

}