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

class Sub_services_model extends CI_Model {

    private $table = 'kns1_sub_services';

    // Get all sub-services with service name
        public function count_all()
{
    return $this->db->count_all('kns1_sub_services'); // table name
}

public function get_all() {
    $this->db->select('ss.*, s.name as service_name, d.name as department_name, f.fee, f.currency');
    $this->db->from('kns1_sub_services ss');
    $this->db->join('kns1_services s', 's.id = ss.service_id', 'left');
    $this->db->join('kns1_departments d', 'd.id = s.department_id', 'left');
    $this->db->join('kns1_service_fees f', 'f.sub_service_id = ss.id', 'left'); // join fee
    $this->db->order_by('ss.id', 'ASC');
    return $this->db->get()->result();
}


    // Get single sub-service
    public function get($id) {
        return $this->db->get_where($this->table, ['id' => $id])->row();
    }

    // Insert new sub-service
    public function insert($data) {
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    // Update sub-service
    public function update($id, $data) {
        $this->db->where('id', $id);
        return $this->db->update($this->table, $data);
    }

    // Delete sub-service
    public function delete($id) {
        $this->db->where('id', $id);
        return $this->db->delete($this->table);
    }

    // Get sub-services by service_id (for AJAX dropdown)
    public function get_by_service($service_id) {
        return $this->db->get_where($this->table, ['service_id' => $service_id])->result();
    }




public function get_by_service_ids($service_ids = [])
{
    if (empty($service_ids)) {
        return [];
    }

    $this->db->select('id, name, service_id');
    $this->db->from($this->table); // ✅ ALWAYS use $this->table
    $this->db->where_in('service_id', $service_ids);

    return $this->db->get()->result();
}


}
