<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Doctor_Profile_Model extends CI_Model {
    var $table = TBL_DOCTOR_PROFILE;
    
    var $column_order = array(null, 'specialty_names', 'doc_name', 'sequence_no', 'status', null); 
    var $column_search = array('doc_name', 'doc_cat_sub', 'qualification');  
    var $order = array('sequence_no' => 'asc');  
    
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    
    private function _get_datatables_query()
    {
        // Add subquery to get specialty names for sorting
        // Using TBL_SPECIALIY (without ES) - matching your controller
        $this->db->select($this->table . '.*, 
            (SELECT GROUP_CONCAT(s.cat_name ORDER BY s.cat_name SEPARATOR ", ") 
             FROM ' . TBL_SPECIALIY . ' s 
             WHERE FIND_IN_SET(s.id, ' . $this->table . '.doc_cat_id) > 0
            ) as specialty_names', FALSE);
        
        $this->db->from($this->table);
        $i = 0;
        foreach ($this->column_search as $item)  
        {
            if($_POST['search']['value']) 
            {
                if($i===0)  
                {
                    $this->db->group_start();  
                    $this->db->like($item, $_POST['search']['value']);
                }
                else
                {
                    $this->db->or_like($item, $_POST['search']['value']);
                }
                if(count($this->column_search) - 1 == $i)  
                    $this->db->group_end();  
            }
            $i++;
        }
        
        if(isset($_POST['order']))  
        {
            $column_index = $_POST['order']['0']['column'];
            $column_dir = $_POST['order']['0']['dir'];
            
            if(isset($this->column_order[$column_index]) && $this->column_order[$column_index] !== null) {
                $this->db->order_by($this->column_order[$column_index], $column_dir);
            }
        } 
        else if(isset($this->order))
        {
            $order = $this->order;
            $this->db->order_by(key($order), $order[key($order)]);
        }
    }
    
    function get_datatables()
    {
        $this->_get_datatables_query();
        if($_POST['length'] != -1)
        $this->db->limit($_POST['length'], $_POST['start']);        
        $this->db->where('publish',"1");
        $query = $this->db->get();
        return $query->result();
    }
    
    function count_filtered()
    {
        $this->_get_datatables_query();
        $this->db->where('publish',"1");
        $query = $this->db->get();
        return $query->num_rows();
    }
    
    public function count_all()
    {
        $this->db->from($this->table);
        $this->db->where('publish',"1");
        return $this->db->count_all_results();
    }
    
    public function get_by_id($id)
    {
        $this->db->from($this->table);
        $this->db->where('id',$id);
        $query = $this->db->get();
        return $query->row();
    }
    
    public function save($data)
    {
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }
    
    public function update($where, $data)
    {
        $this->db->update($this->table, $data, $where);
        return $this->db->affected_rows();
    }
    
    public function delete_by_id($id)
    {
        $this->db->where('id', $id);
        $this->db->delete($this->table);
    } 
    
    public function delete_data($where, $data)
    {
        $this->db->update($this->table, $data, $where);
        return $this->db->affected_rows();
    }
    
    public function get_doc_cat_list()
    {
        $this->db->select("id, cat_name, status, publish"); 
        $this->db->where('publish', "1");
        $this->db->where('status', "1"); 
        $this->db->order_by('cat_name');
        $query = $this->db->get(TBL_SPECIALIY);  // Changed from TBL_SPECIALITIES to TBL_SPECIALIY
        return $query->result();
    }
    
    function get_quality_services($id) 
    {
        $this->db->select("wp.*");
        $this->db->where('wp.id', $id);   
        $query = $this->db->get(TBL_DOCTOR_PROFILE . ' wp');
        if ($query->num_rows() > 0) {
            return $query->row();
        }
        return FALSE;
    }
}