<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class service_model extends CI_Model {
    public function __construct() {
        parent::__construct();
        $this->service = TBL_SERVICE;
        $this->service_menus = TBL_SERVICE_MENU;
    }
    
    public function list_all() {
        $this->db->select('*, CASE status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str',FALSE);
        $this->db->where('status !=', 2);
        $this->db->order_by('id', 'desc');
        return $this->db->get($this->service)->result();
    }
    
    public function list_service_menus() {
        $this->db->where('status', 1);
        return $this->db->get($this->service_menus)->result();
    }
    
    public  function insert($data){  
        $this->db->insert($this->service, $data);  
        return $this->db->insert_id();
    }   
    
    public function update($data, $id){
        $this->db->where('id', $id);
        $this->db->update($this->service, $data);
        return $this->db->affected_rows();
    }
    
    public function get_service_by_id($id) {
        $this->db->select('s.*, CASE s.status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str, sm.service_name',FALSE);
        $this->db->join($this->service_menus . ' as sm','sm.id = s.menu_name');
        $this->db->where('s.id', $id);
        return $this->db->get($this->service . ' as s')->row();
    }
   
    public function service_url_key_exists($url_key, $id) {
        if (!empty($id)) {
            $this->db->where('id !=', $id);
        }
        $this->db->where('url_key', $url_key);
        return $this->db->get($this->service)->num_rows();
    }
     
    public function service_name_exists($menu_id,$service,$id) {
        $this->db->where('menu_name',$menu_id);
        $this->db->where('name',$service);
        if(!empty($id)) 
            $this->db->where('id !=', $id);
        
        $this->db->where('status !=',2);
        if($this->db->get($this->service)->num_rows())
            return 1;
        else 
            return 0;
    }
}
?>