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

class shifts_model extends CI_Model {



public function count_all()
{
    return $this->db->count_all('kns1_shifts'); // table name
}

    // Add Data Function
    public function create($options)
    {
        $this->db->insert('kns1_shifts', $options);
        return $this->db->insert_id();
    }

    // Get All Function
    public function get_all($limit = NULL, $offset = NULL)
    {
        $this->db->order_by('id', 'ASC');
        $query = $this->db->get('kns1_shifts', $limit, $offset);
        return $query->result();
    }

    // Delete Function
    public function destroy($id)
    {
        $this->db->where('id', $id);
        $this->db->delete('kns1_shifts');
        return $this->db->affected_rows();
    }

    // Update Function
    public function update($id, $options)
    {
        $this->db->where('id', $id);
        $this->db->update('kns1_shifts', $options);
        return $id;
    }
    public function get_current_shift()
    {
        $now = date('H:i:s');

        $this->db->where('is_trash', 0);
        $this->db->where('start_time <=', $now);
        $this->db->where('end_time >=', $now);
        $query = $this->db->get('kns1_shifts');

        return $query->row(); // returns shift object or null
    }
    // Get ID by Name Funcion
    public function getNameBy($id)
    {
        $this->db->select('name');
        $this->db->where('id', $id);
        $query = $this->db->get('kns1_shifts');
        return $query->row()->name;
    }

    // Get ID Funcion
    public function get_by($categoryId)
    {
        $this->db->where('id', $categoryId);
        $query = $this->db->get('kns1_shifts');
        return $query->row();
    }

}