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

class Group_model extends MY_Model {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */

	function __construct(){
		parent::__construct();
		$this->tb_name = "tbl_group";
	}
    
    function get_groups(){
        $query = $this->db->select("*")
                          ->where("parent_id", 0)
                          ->from($this->tb_name)
                          ->get();
        
        if($query->num_rows()>0){
            return array("success"=>TRUE, "data" => $query->result_array());
        }else{
            return array("success"=>FALSE, "message" => "No data to display!", "data"=>array());
        }
    }

    function get_subgroups($parent_id)
    {
        $query = $this->db->select("*")
                          ->where("parent_id", $parent_id)
                          ->from($this->tb_name)
                          ->get();
        if($query->num_rows()>0){
            return array("success"=>TRUE, "data" => $query->result_array());
        }else{
            return array("success"=>FALSE, "message" => "No data to display!", "data"=>array());
        }
    }

    function get_groups_list(){
        $query = $this->db->select("*")
                          ->where("parent_id", 0)
                          ->from($this->tb_name)
                          ->get();
        $result = array();
        if($query->num_rows()>0){
            $parent_groups = $query->result_array();
            foreach($parent_groups as $row){
                $query = $this->db->select("*")
                                ->where("parent_id", $row["id"])
                                ->from($this->tb_name)
                                ->get();
                if($query->num_rows()>0){
                    $result[$row["group_name"]] = $query->result_array();
                }else{
                    $result[$row["group_name"]] = array();
                }
            }
            return $result;
        }else{
            return array();
        }
    }
}
