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

class Tree_view extends Admin_Controller {
	
	function __construct() {
		parent::__construct(TRUE);
		$this->load->model('tree_view_model','',TRUE);
                 
	}
	function index()
	{
		$this->load->view('tree_view');
	}
        function customer_tree(){
            
                $this->tree_view_model->setUreg($this->session->userdata('user_id'));
                $VehCount = $this->tree_view_model->getVehicleCount();
                $veh_count = $VehCount[0]->veh_count;
               
                $json = array();
                $cust['name'] = $this->session->userdata('username')." (".$veh_count.")";
                $cust['pId'] = 0;
                $cust['itemType'] = "customer";
                $cust['iconOpen'] = base_url()."assets/ztree/icons/user-close-icon.png";
                $cust['iconClose'] = base_url()."assets/ztree/icons/user_icon_open.png";
                $cust['icon'] = base_url()."assets/ztree/icons/user-new.png";
                $cust['id'] = $this->session->userdata('user_id');
                $json[] = $cust;
                $children = $this->tree_view_model->getChildren($this->session->userdata('user_id'));
                $list_vehicle = $this->tree_view_model->getVehicle();
              
             
                //add vehicle to tree
                foreach ($list_vehicle as $item) {
                      
                        $v['itemType'] = "vehicle";
                        $v['pId'] =  $cust['id'];
                        $v['name'] = $item->assets_name;
                        $v['open'] = true;
                        $v['id'] = $item->device_id;
                        $v['icon'] =  base_url()."assets/ztree/icons/assets.png";
                        $json[] = $v;
                }
                
                foreach ($children as $item) {
                        
                        //
                     $this->tree_view_model->setUreg($item->user_id);
                     $VehCount = $this->tree_view_model->getVehicleCount();
                     $veh_count = $VehCount[0]->veh_count;
                        $child['name'] = $item->username." (".$veh_count.")";
                       // $child['name'] = $item->username;
                        $child['pId'] =  $cust['id'];
                        $child['itemType'] = "customer";
                        $child['iconOpen'] = base_url()."assets/ztree/icons/user-close-icon.png";
                        $child['iconClose'] = base_url()."assets/ztree/icons/user_icon_open.png";
                        $child['icon'] = base_url()."assets/ztree/icons/user-new.png";
                        $child['id'] = $item->user_id;
                        $json[] = $child;
                        $descent = $this->tree_view_model->getChildren($item->user_id);
                       
                       
                        $list_vehicle = $this->tree_view_model->getVehicle();
                       
                        foreach ($list_vehicle as $item1) {
                                
                                $v['itemType'] = "vehicle";
                                $v['pId'] = $child['id'];
                                $v['name'] = $item1->assets_name;
                                $v['open'] = true;
                                $v['id'] = $item1->device_id;
                                $v['icon'] = base_url()."assets/ztree/icons/assets.png";
                                $json[] = $v;
                        }
                        
                        foreach ($descent as $item2) {
                                $this->tree_view_model->setUreg($item2->user_id);
                                 $VehCount = $this->tree_view_model->getVehicleCount();
                        $veh_count = $VehCount[0]->veh_count;
                               $child1['name'] = $item2->username." (".$veh_count.")";
                                 // $child1['name'] = $item2->username;
                                $child1['pId'] = $child['id'];
                                $child1['itemType'] = "customer";
                                $child1['iconOpen'] = base_url()."assets/ztree/icons/user-close-icon.png";
                                $child1['iconClose'] = base_url()."assets/ztree/icons/user_icon_open.png";
                                $child1['icon'] = base_url()."assets/ztree/icons/user-new.png";
                                $child1['id'] = $item2->user_id;
                                $json[] = $child1;

                               
                                $list_vehicle = $this->tree_view_model->getVehicle();
                                foreach ($list_vehicle as $item3) {
                                       
                                        $v['itemType'] = "vehicle";
                                        $v['pId'] =  $child1['id'];
                                        $v['name'] = $item3->assets_name;
                                        $v['open'] = true;
                                        $v['id'] = $item3->device_id;
                                        $v['icon'] = base_url()."assets/ztree/icons/assets.png";
                                        $json[] = $v;
                                }
                        }

                }
                header('Content-Type: application/json');
                echo json_encode($json);
        }
        function devices_location(){
        $imeis = str_replace("[","(", $_POST['imeis']);
        $imeis = str_replace("]",")", $imeis);
        $list = $this->tree_view_model->last_location($this->session->userdata('user_id'),$imeis);
        
        $json = array();
        $rt['status'] = 'failure';
        foreach ($list as $item) {

                $p['imei'] = $item->device_id;
                $p['number'] = $item->sim_umber;
                $p['packet_time'] = date("d-m-Y h:i:s",strtotime($item->add_date));
                $p['data_received'] = ago($item->add_date);
                $p['lat'] = $item->lati;
                $p['lng'] = $item->longi;
                if($item->assets_cat_image == '')
                    $p['category_image']=base_url()."assets/marker-images/car.png";
                else   
                    $p['category_image'] = base_url()."assets/marker-images/".$item->assets_cat_image;
                $p['address'] = $item->address;
                $p['speed'] = $item->speed;
                $p['ACC'] = $item->ACC;
                $p['mileage'] = $item->mileage;
                if($item->ignition == 0)
                    $p['ignition'] = "OFF";
                else
                    $p['ignition'] = "ON";
                    
                $p['beforeTime'] = $item->beforeTime;
                $p['driver_contact_no'] = $item->driver_contact_no;
                $p['driver_name'] = $item->driver_name;
                $p['temperature'] = $item->temperature;
                $p['battery'] = $item->battery;
                $p['network_timeout']  = $this->session->userdata('network_timeout');
                $d['name'] = $item->assets_name;
                $ap['packet'] = $p;
                $ap['device'] = $d;
                $ap['name'] = $item->assets_name;
               
                
                $json[] = $ap;
                $rt['summary'] = $json;
                $rt['status'] = "ok";
        }
            header('Content-Type: application/json');
            $this->output->set_output(json_encode($rt));
            
        }
}
