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

class Consultants extends CI_Controller {

    public function __construct() {
        parent::__construct();

        if (!isset($_SESSION['qurbani_user_id']) || $_SESSION['qurbani_role'] != 'sa') {
            redirect('/admin', 'refresh');
        }

        $this->load->model('Services_model', 'services');
        $this->load->model('Settings_model', 'setup');
        $this->load->model('Departments_model', 'departments');
    }


public function by_department($dep_id) {
    $services = $this->db->get_where('kns1_services', ['department_id' => $dep_id])->result();
    header('Content-Type: application/json');
    echo json_encode($services); // just the array
}




    // List all services
    public function index() {
        $data['total_services'] = $this->services->count_all();
        $data['services'] = $this->services->get_all();
        $data['settings'] = $this->setup->get_by_id(1);
        $data['departments'] = $this->departments->get_all();
        $data['title'] = 'Consultants';
        $data['mainContent'] = 'admin/services/index';
        $this->load->view('admin/layout/master', $data);
    }

    // Add new service
    public function add() {
        if($this->input->post()) {
            $service_id = $this->services->insert([
                'department_id' => $this->input->post('department_id'),
                'name' => $this->input->post('name'),
                'description' => $this->input->post('description')
            ]);

            $this->session->set_flashdata('message', 'Service added successfully.');
        }
        redirect('admin/consultants');
    }

    // Edit service
    public function edit() {
        if($this->input->post()) {
            $id = $this->input->post('id');
            $this->services->update($id, [
                'department_id' => $this->input->post('department_id'),
                'name' => $this->input->post('name'),
                'description' => $this->input->post('description')
            ]);

            $this->session->set_flashdata('message', 'Service updated successfully.');
        }
        redirect('admin/consultants');
    }

    // Delete service
    public function delete($id) {
        $this->services->delete($id);
        $this->session->set_flashdata('message', 'Service deleted successfully.');
        redirect('admin/consultants');
    }
}
