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

class Users_model extends CI_Model {

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

    public function get_all_users() {
        $this->db->select('tbl_admin.*, tbl_branches.branch_name');
        $this->db->from('tbl_admin');
        $this->db->join('tbl_branches', 'tbl_admin.branch_id = tbl_branches.branch_id', 'left');
        $query = $this->db->get();
        return $query->result();
    }

    public function insert_user($data) {
        if (!empty($data['password'])) {
            $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
        }
        return $this->db->insert('tbl_admin', $data);
    }

    public function get_user_by_id($id) {
        $this->db->where('admin_id', $id);
        $query = $this->db->get('tbl_admin');
        return $query->row();
    }

    public function update_user($id, $data) {
        if (!empty($data['password'])) {
            $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
        } else {
            unset($data['password']); // don't update password if empty
        }
        $this->db->where('admin_id', $id);
        return $this->db->update('tbl_admin', $data);
    }
}
