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

class Guard_Model extends CI_Model {


	public function insert(){

    $patient = $this->input->post('ptype');

    if($patient == "contact")
    {
            $query=$this->db->order_by('risk_id', 'DESC')->limit('1')->get('tbl_patient_risk')->row();
            @$pqidstring = explode('RSK', $query->risk_uniq);
            @$pqid = array($pqidstring[1], 1);
            $pqid = array_sum($pqid);
            $pqidlength = strlen($pqid);
            if($pqidlength == 1)
            {
              $uniqidd = '000'.$pqid;
            }
            else if($pqidlength == 2)
            {
              $uniqidd = '00'.$pqid;
            }
            else if($pqidlength == 3)
            {
              $uniqidd = '0'.$pqid;
            }
            else
            {
              $uniqidd = $pqid;
            }
            $uniq_id='RSK'.$uniqidd;
      }
      else
      {
           $uniq_id='';   
      }


		$data=array("name"=>$_POST['name'],
          					"address"=>$_POST['address'],
          					"email"=>$_POST['emailid'],
          					"contact"=>$_POST['contact'],
          					"DOB"=>$_POST['DOB'],
          					"DOJ"=>$_POST['traveldate'],
          					
          					"date_of_receipt_info"=>$_POST['receiptdate'],
          					"traceable"=>$_POST['trace'],
          					"affected_country_leaving_dt"=>$_POST['leavingdate'],
          					"date_of_hospitalisation"=>$_POST['hospitaldate'],
          					
          					"username"=>$_POST['username'],
          					"password"=>$_POST['password'],
                    "gender"=>$_POST['gender'],
                    "age"=>$_POST['age'],
                    "type"=>$_POST['ptype'],
                    "travel_from"=>$_POST['travelfrom'],
                    "travel_history"=>$_POST['travelhis'],
                  //  "travel_date"=>$_POST['traveldate'],
                    "information_source"=>$_POST['infosource'],

                    "hospital_status"=>$_POST['currhos'],
                    "hospital_name"=>$_POST['hosname'],
                    "person_id"=>$_POST['positivepatient'],
                    
                    "migrate_status"=>$_POST['migstatus'],
                    "migrate_state"=>$_POST['stname'],
                    "migrate_country"=>$_POST['countryname'],
                    "patient_status"=>$_POST['patienttype'],
                    "symptom_date"=>$_POST['symptomatic'],

                    "risk_type"=>$_POST['risktype'],
                    "location"=>$_POST['locationtype'],
                    "relationship"=>$_POST['relationship'],
                    "date_of_exposure"=>$_POST['exposuredate'],
                    "symptoms"=>$_POST['symptoms'],
                    "advised"=>$_POST['advise'],
                    "facility_name"=>$_POST['newloc'],
                    "contact_person"=>$_POST['personname'],
                    
                    "sample_taken"=>$_POST['sampletaken'],
                    "sample_date"=>$_POST['sample_date'],
                    "result"=>$_POST['result'],
                  	"Health_center"=>$_POST['healthcenter'],
                  	"prabhag_id"=>$_POST['prabhagid'],
                    "risk_uniq_id"=>$uniq_id,
          					"status"=>$_POST['status'],
          					"inserted_SI"=>$this->session->userdata('id'),
                    "inserted_BY"=>$this->session->userdata('name'),
                    "inserted_DT"=>date('Y-m-d H:i:s')
				);
		$this->db->insert('mst_guard',$data);
		
		$guardid = $this->db->insert_id();
		$check = $_POST['currhos'];
		if($check==1)
		{
		    $data2 = array('transfer_status'=>2,'office_name'=>$_POST['hosname'],'guard_name'=>$guardid,'inserted_by'=>$this->session->userdata('id'),'inserted_SI'=>$this->session->userdata('name'),'inserted_DT'=>date('Y-m-d H:i:s'));
		    $this->db->insert('tr_assign_duty',$data2);
		}

//    $patient = $this->input->post('ptype');
    $locationtype = $this->input->post('locationtype');

    if($patient == "contact")
    {
        if($locationtype ==1)
        {
               if($_POST['gender']=='Male')
               {
                      $genderinfo = 1;
               } 
               else if($_POST['gender']=='Female')
               {
                     $genderinfo = 2;
               }
                $positiveinfo = $this->input->post('positivepatient');

                $riskinfo = array('risk_uniq'=>$uniq_id,'patient_id'=>$positiveinfo,'risk_type'=>$_POST['risktype'],'inserted_SI'=>$this->session->userdata('name'),'inserted_DT'=>date('Y-m-d H:i:s'),'inserted_by'=>$this->session->userdata('id'));
                $this->db->insert('tbl_patient_risk',$riskinfo); 

                $riskaddmore = array('uniq_id'=>$uniq_id,'patient_id'=>$positiveinfo,'risk_type'=>$_POST['risktype'],'memeber_name'=>$_POST['name'],'age'=>$_POST['age'],'gender'=>$genderinfo,'mobile_no'=>$_POST['contact'],'relationship'=>$_POST['relationship'],'date_of_exposure'=>$_POST['exposuredate'],'date_of_observation'=>$_POST['DOB'],'symptoms'=>$_POST['symptoms'],'address'=>$_POST['address'],'advisal'=>$_POST['advise'],'advise_location'=>$_POST['newloc'],'in_tmc_area'=>$_POST['locationtype'],'save_staus'=>1,'inserted_SI'=>$this->session->userdata('name'),'inserted_DT'=>date('Y-m-d H:i:s'),'inserted_BY'=>$this->session->userdata('id'));
                $this->db->insert('tbl_risk_addmore',$riskaddmore); 
        }
        else 
        {
               if($_POST['gender']=='Male')
               {
                      $genderinfo = 1;
               } 
               else if($_POST['gender']=='Female')
               {
                     $genderinfo = 2;
               }
                $positiveinfo = 'other';
                $riskinfo = array('risk_uniq'=>$uniq_id,'patient_id'=>$positiveinfo,'risk_type'=>$_POST['risktype'],'inserted_SI'=>$this->session->userdata('name'),'inserted_DT'=>date('Y-m-d H:i:s'),'inserted_by'=>$this->session->userdata('id'));
                $this->db->insert('tbl_patient_risk',$riskinfo); 

                $riskaddmore = array('uniq_id'=>$uniq_id,'patient_id'=>$positiveinfo,'risk_type'=>$_POST['risktype'],'memeber_name'=>$_POST['name'],'age'=>$_POST['age'],'gender'=>$genderinfo,'mobile_no'=>$_POST['contact'],'relationship'=>$_POST['relationship'],'date_of_exposure'=>$_POST['exposuredate'],'date_of_observation'=>$_POST['DOB'],'symptoms'=>$_POST['symptoms'],'address'=>$_POST['address'],'advisal'=>$_POST['advise'],'advise_location'=>$_POST['newloc'],'in_tmc_area'=>$_POST['locationtype'],'save_staus'=>1,'inserted_SI'=>$this->session->userdata('name'),'inserted_DT'=>date('Y-m-d H:i:s'),'inserted_BY'=>$this->session->userdata('id'));
                $this->db->insert('tbl_risk_addmore',$riskaddmore);
            }
    }



	}



	public function fetch_data()
	{
	    
	    
	 $this->db->select('*'); 
  	 $this->db->from('mst_guard');
  	 //$this->db->where('inserted_SI', $this->session->userdata('id'));
  	 $this->db->where('deleted_DT',NULL)
  	            ->or_where('(deleted_DT IS NOT NULL AND deactive_cronjob_flag = 1)');
  	 $this->db->order_by('id','DESC');
 	 $query = $this->db->get();
  	 return $query->result();

	}
	
	
	public function fetch_active_patients() {
	    	 $this->db->select('*'); 
          	 $this->db->from('mst_guard');
          	 //$this->db->where('inserted_SI', $this->session->userdata('id'));
          	 $this->db->where('deleted_DT',NULL)
          	            ->or_where('(deleted_DT IS NOT NULL AND deactive_cronjob_flag = 0)');
          	 $this->db->order_by('id','DESC');
         	 $query = $this->db->get();
          	 return $query->result();
	}
	
	public function fetch_quarantine_completed_patients() {
	    	 $this->db->select('*'); 
          	 $this->db->from('mst_guard');
          	 $this->db->where(' deactive_cronjob_flag = 1');
          	 $this->db->order_by('id','DESC');
         	 $query = $this->db->get();
          	 return $query->result();
	}
	

	public function display_guard_recordsById($id)
	{
		$query=$this->db->query("select * from mst_guard where id='".$id."'");
		return $query->result();
	}
    
    public function gurard_updateinfo($id)
	{
		$query=$this->db->query("select * from mst_guard where id='".$id."'");
		return $query->row();
	}

	public function update($id)
	{
		$patient = $this->input->post('ptype');
	/*	$n=$this->input->post('name');
		$a=$this->input->post('address');
		$e=$this->input->post('emailid');
		$c=$this->input->post('contact');
		$DOB=$this->input->post('DOB');
		$DOJ=$this->input->post('DOJ');
		$username=$this->input->post('username');
		$password=$this->input->post('password');
		
		$query=$this->db->query("update mst_guard SET name='$n',address='$a',email='$e',contact='$c',
		DOB='$DOB',DOJ='$DOJ',username='$username', password='$password' where id='".$id."'");*/		
          		$data=array("name"=>$_POST['name'],
          					   "address"=>$_POST['address'],
          					   "email"=>$_POST['emailid'],
          					   "contact"=>$_POST['contact'],
          					   "DOB"=>$_POST['DOB'],
          					   "DOJ"=>$_POST['traveldate'],
          					   "username"=>$_POST['username'],
          				    	"password"=>$_POST['password'],
                        "age"=>$_POST['age'],
                        "type"=>$_POST['ptype'],
                        "travel_from"=>$_POST['travelfrom'],
                        "travel_history"=>$_POST['travelhis'],
                       // "travel_date"=>$_POST['traveldate'],
                        "information_source"=>$_POST['infosource'],
                        "hospital_status"=>$_POST['currhos'],
                        "hospital_name"=>$_POST['hosname'],
                        
                        
                        "date_of_receipt_info"=>$_POST['receiptdate'],
          					    "traceable"=>$_POST['trace'],
          					    "affected_country_leaving_dt"=>$_POST['leavingdate'],
          					    "date_of_hospitalisation"=>$_POST['hospitaldate'],
          					
          					
					             "person_id"=>$_POST['positivepatient'],  
					             "status"=>$_POST['status'],
                       "gender"=>$_POST['gender'],
                       "migrate_status"=>$_POST['migstatus'],
                       "migrate_state"=>$_POST['stname'],
                       "migrate_country"=>$_POST['countryname'],
                       "patient_status"=>$_POST['pstatus'],
                       "symptom_date"=>$_POST['symptomatic'],
                       
                        "risk_type"=>$_POST['risktype'],
                        "location"=>$_POST['locationtype'],
                        "relationship"=>$_POST['relationship'],
                        "date_of_exposure"=>$_POST['exposuredate'],
                        "symptoms"=>$_POST['symptoms'],
                        "advised"=>$_POST['advise'],
                        "facility_name"=>$_POST['newloc'],
                        "contact_person"=>$_POST['personname'],
                        
                        "sample_taken"=>$_POST['sampletaken'],
                        "sample_date"=>$_POST['sample_date'],
                        "result"=>$_POST['result'],
                       	"Health_center"=>$_POST['healthcenter'],
                    		"prabhag_id"=>$_POST['prabhagid'],
                	
					              "Modify_SI"=>$this->session->userdata('id'),
                   	    "Modify_BY"=>$this->session->userdata('username'),
                        "Modify_DT"=>date('Y-m-d H:i:s'));
       				 $c=$this->db->where('id',$id)->update('mst_guard',$data); 
                
                
            $checkstatus = $this->db->select('*')->where('deleted_DT',NULL)->where('guard_name',$id)->get('tr_assign_duty')->result();
            
            if(count($checkstatus)==0)
            {
                		$check = $_POST['currhos'];
                		if($check==1)
                		{
                		    $data2 = array('transfer_status'=>2,'office_name'=>$_POST['hosname'],'guard_name'=>$id,'inserted_by'=>$this->session->userdata('id'),'inserted_SI'=>$this->session->userdata('name'),'inserted_DT'=>date('Y-m-d H:i:s'));
                		    $this->db->insert('tr_assign_duty',$data2);
                		}
            }

            $locationtype = $_POST['locationtype'];
            $uniq_id = $_POST['runiqid'];
              if($patient == "contact")
              {
                  if($locationtype ==1)
                  {
                         if($_POST['gender']=='Male')
                         {
                                $genderinfo = 1;
                         } 
                         else if($_POST['gender']=='Female')
                         {
                               $genderinfo = 2;
                         }
                         $positiveinfo = $this->input->post('positivepatient');

                          $riskinfo = array('risk_uniq'=>$uniq_id,'patient_id'=>$positiveinfo,'risk_type'=>$_POST['risktype'],'modify_SI'=>$this->session->userdata('name'),'modify_DT'=>date('Y-m-d H:i:s'),'modify_BY'=>$this->session->userdata('id'));
                          $this->db->where('risk_uniq',$uniq_id)->update('tbl_patient_risk',$riskinfo); 

                          $riskaddmore = array('uniq_id'=>$uniq_id,'patient_id'=>$positiveinfo,'risk_type'=>$_POST['risktype'],'memeber_name'=>$_POST['name'],'age'=>$_POST['age'],'gender'=>$genderinfo,'mobile_no'=>$_POST['contact'],'relationship'=>$_POST['relationship'],'date_of_exposure'=>$_POST['exposuredate'],'date_of_observation'=>$_POST['DOB'],'symptoms'=>$_POST['symptoms'],'address'=>$_POST['address'],'advisal'=>$_POST['advise'],'advise_location'=>$_POST['newloc'],'in_tmc_area'=>$_POST['locationtype'],'save_staus'=>1,'modify_SI'=>$this->session->userdata('name'),'modify_DT'=>date('Y-m-d H:i:s'),'modify_BY'=>$this->session->userdata('id'));
                          $this->db->where('uniq_id',$uniq_id)->update('tbl_risk_addmore',$riskaddmore); 
                  }
                  else 
                  {
                         if($_POST['gender']=='Male')
                         {
                                $genderinfo = 1;
                         } 
                         else if($_POST['gender']=='Female')
                         {
                               $genderinfo = 2;
                         }
                          $positiveinfo = 'other';
                          $riskinfo = array('risk_uniq'=>$uniq_id,'patient_id'=>$positiveinfo,'risk_type'=>$_POST['risktype'],'modify_SI'=>$this->session->userdata('name'),'modify_DT'=>date('Y-m-d H:i:s'),'modify_BY'=>$this->session->userdata('id'));
                          $this->db->where('risk_uniq',$uniq_id)->update('tbl_patient_risk',$riskinfo); 

                          $riskaddmore = array('uniq_id'=>$uniq_id,'patient_id'=>$positiveinfo,'risk_type'=>$_POST['risktype'],'memeber_name'=>$_POST['name'],'age'=>$_POST['age'],'gender'=>$genderinfo,'mobile_no'=>$_POST['contact'],'relationship'=>$_POST['relationship'],'date_of_exposure'=>$_POST['exposuredate'],'date_of_observation'=>$_POST['DOB'],'symptoms'=>$_POST['symptoms'],'address'=>$_POST['address'],'advisal'=>$_POST['advise'],'advise_location'=>$_POST['newloc'],'in_tmc_area'=>$_POST['locationtype'],'save_staus'=>1,'modify_SI'=>$this->session->userdata('name'),'modify_DT'=>date('Y-m-d H:i:s'),'modify_BY'=>$this->session->userdata('id'));
                          $this->db->where('uniq_id',$uniq_id)->update('tbl_risk_addmore',$riskaddmore); 
                      }
              }

		
	}

  

public function fetch_guard_status()
	{
	/*	$this->db->select('*'); 
  	  $this->db->from('mst_guard');
  	  $this->db->where('deleted_DT',NULL);
  	  $this->db->order_by('id','DESC');
 	 $query = $this->db->get();
  	return $query->result();*/
  	
  	    $this->db->select('tad.d_id,mst_office.name as o_name,mst_office.city,mst_guard.name,mst_guard.DOJ,mst_guard.status');
	 	$this->db->where('tad.deleted_DT',NULL);
		$this->db->from('tr_assign_duty tad');
		$this->db->join('mst_office','mst_office.id=tad.office_name','left');
		$this->db->join('mst_guard','mst_guard.id=tad.guard_name','left');
		$this->db->order_by("tad.d_id","desc");
		return $query=$this->db->get()->result();
	}
	
	
	public function fetch_total_guard()
	{
    		if(!empty($_POST['date']))
    		{
    	        $this->db->where('mst_guard.DOB',$_POST['date']);
    	    }
	    return $this->db->where('deleted_DT',NULL)->where_in('patient_status', ['2', '3'])->order_by('id','DESC')->get('mst_guard')->result();
	}
	
	public function completed_quarantine_patients()
	{
	  $this->db->select('*'); 
  	  $this->db->from('mst_guard');
  	  $this->db->where('deleted_DT IS NOT NULL');
  	  $this->db->order_by('id','DESC');
 	  $query = $this->db->get();
      return $query->result();
	}
	
	
	public function get_positive_patient()
	{
	   $this->db->select('*'); 
  	 $this->db->from('mst_guard');
  //	  $this->db->or_where('patient_status=1');
  	  $this->db->where('deleted_DT IS NULL');
  	  $this->db->order_by('id','DESC');
 	  $query = $this->db->get();
      return $query->result();
  
	}
	


	public function fetch_guard_attendence()
	{


		$this->db->select('attendence.*,mst_office.name as o_name,mst_office.city,mst_guard.name');
	 	$this->db->where('attendence.deleted_DT',NULL);
		$this->db->from('attendence');
		$this->db->join('mst_office','mst_office.id=attendence.office_id ','left');
	//	$this->db->join('mst_office','mst_office.city=attendence.office_id','left');
		$this->db->join('mst_guard','mst_guard.id=attendence.guard_id','left');
		$this->db->order_by('a_id','DESC');
		return $query=$this->db->get()->result();
		


	}




	
	
	public function fetch_guard_hr_attendence() {

            $data['offices'] = array();
            $offices = $this->db->where('Deleted_DT IS NULL')->get('mst_office')->result();
            if(!empty($offices)) {
                foreach($offices as $office) {
                    $data['offices'][$office->id] = $office; 
                }
            }
            
            $data['guard'] = array();
    	    if(!empty($_POST['g_id'])) {
		        $data['g_id'] = $_POST['g_id'];
		        $this->db->where("name LIKE '%{$_POST['g_id']}%'");
	    	}
	    	
            $guards = $this->db->where('Deleted_DT IS NULL')->get('mst_guard')->result();
            if(!empty($guards)) {
                foreach($guards as $guard) {
                    $data['guard'][$guard->id]['name'] = $guard->name; 
                    $data['guard'][$guard->id]['contact'] = $guard->contact; 
                }
            }

            $data['q_date'] = date('Y-m-d');
			if(!empty($_POST['q_date'])) {
		        $data['q_date'] = $_POST['q_date'];
	    	}
	    	$this->db->where("date(datetime) = '{$data['q_date']}'");
	    	
            $location_arr = $this->db->where('deleted_DT IS NULL')->order_by('l_id', 'DESC')->get('guard_location')->result();

            $data['location_arr'] = array();
            if(!empty($location_arr)) {
                foreach($location_arr as $location) {
                    if(!empty($data['guard'][$location->guard_id])) { //deactive or deleted user location filter
                        $dt = date('Y-m-d', strtotime($location->datetime));                    
                        
                        $data['location_arr'][$dt][$location->guard_id]['datetime'] = $location->datetime; 
                        $data['location_arr'][$dt][$location->guard_id]['location'] = $location; 
                        $data['location_arr'][$dt][$location->guard_id]['office_name'] = !empty($data['offices'][$location->office_id]->name) ? $data['offices'][$location->office_id]->name : ''; 
                        $data['location_arr'][$dt][$location->guard_id]['office_address'] = !empty($data['offices'][$location->office_id]->address) ? $data['offices'][$location->office_id]->address : ''; 
                        $data['location_arr'][$dt][$location->guard_id]['guard_contact'] = !empty($data['guard'][$location->guard_id]['contact']) ? $data['guard'][$location->guard_id]['contact'] : ''; 
                        $data['location_arr'][$dt][$location->guard_id]['guard'] = !empty($data['guard'][$location->guard_id]['name']) ? $data['guard'][$location->guard_id]['name'] : ''; 
                        $data['location_arr'][$dt][$location->guard_id]['guardid'] = !empty($location->guard_id) ? $location->guard_id : ''; 
                        
                        
                        $officeLong =  !empty($data['offices'][$location->office_id]->Longitude) ? $data['offices'][$location->office_id]->Longitude : '';
                        $officeLat =  !empty($data['offices'][$location->office_id]->Latitude) ? $data['offices'][$location->office_id]->Latitude : '';
                        
                        $tindex = 0;
                        if(!empty($officeLong) && !empty($officeLat)) {
                            $time =  date('H.i', strtotime($location->datetime)); 
                            
                            $tindex = $this->get_time_index($time);
                            
                            $data['location_arr'][$dt][$location->guard_id]['status'][$tindex] = $this->check_inside_circle($officeLat, $officeLong, $location->LAT, $location->LONGI);  
                        } else {
                            $data['location_arr'][$dt][$location->guard_id]['status'][$tindex] = ''; 
                        }
                    }
                
                }                
            }
            
        return $data;
	}
	
	
	
	public function validateDate($date, $format = 'Y-m-d')
    {
        $d = DateTime::createFromFormat($format, $date);
        // The Y ( 4 digits year ) returns TRUE for any integer with any number of digits so changing the comparison from == to === fixes the issue.
        return $d && $d->format($format) === $date;
    }
	
	public function get_time_index($time) {
	    $tindex = 0;
	    if(!empty($time) && ($time - floor($time)) > 0.3) {
	        $tindex = floor($time) * 2 + 2;
	    } else if(!empty($time) && ($time - floor($time)) <= 0.3) {
	        $tindex = floor($time) * 2 + 1;
	    } 
        return $tindex;
	}
	
// 	public function fetch_guard_hr_attendence_byName($guardName)
// 	{	
//             $data['offices'] = array();
//             $offices = $this->db->where('Deleted_DT IS NULL')->get('mst_office')->result();
//             if(!empty($offices)) {
//                 foreach($offices as $office) {
//                     $data['offices'][$office->id] = $office; 
//                 }
//             }
            
//             $data['guard'] = array();
//             $guards = $this->db->where('Deleted_DT IS NULL')->where("name LIKE '%$guardName%'")->get('mst_guard')->result();

//             if(!empty($guards)) {
//                 foreach($guards as $guard) {
//                     $data['guard'][$guard->id]['name'] = $guard->name; 
//                     $data['guard'][$guard->id]['contact'] = $guard->contact; 
                    
//                 }
//             }
            
//             $location_arr = $this->db->where('deleted_DT IS NULL')->order_by('l_id', 'DESC')->get('guard_location')->result();
            
//             $data['location_arr'] = array();
//             if(!empty($location_arr)) {
//                 foreach($location_arr as $location) {
//                     if(!empty($data['guard'][$location->guard_id])) { //deactive or deleted user location filter
                    
//                     $dt = date('Y-m-d', strtotime($location->datetime));                    
                    
//                     $data['location_arr'][$dt][$location->guard_id]['datetime'] = $location->datetime; 
//                     $data['location_arr'][$dt][$location->guard_id]['location'] = $location; 
//                     $data['location_arr'][$dt][$location->guard_id]['office_name'] = !empty($data['offices'][$location->office_id]->name) ? $data['offices'][$location->office_id]->name : ''; 
//                     $data['location_arr'][$dt][$location->guard_id]['office_address'] = !empty($data['offices'][$location->office_id]->address) ? $data['offices'][$location->office_id]->address : ''; 
//                     $data['location_arr'][$dt][$location->guard_id]['guard_contact'] = !empty($data['guard'][$location->guard_id]['contact']) ? $data['guard'][$location->guard_id]['contact'] : ''; 
//                     $data['location_arr'][$dt][$location->guard_id]['guard'] = !empty($data['guard'][$location->guard_id]['name']) ? $data['guard'][$location->guard_id]['name'] : ''; 
//                     // $data['location_arr'][$dt][$location->guard_id]['guard_address'] = !empty($data['guard'][$location->guard_id]['city']) ? $data['guard'][$location->guard_id]['city'] : ''; 
//                     $data['location_arr'][$dt][$location->guard_id]['guardid'] = !empty($location->guard_id) ? $location->guard_id : ''; 
                    
                    
//                     $officeLong =  !empty($data['offices'][$location->office_id]->Longitude) ? $data['offices'][$location->office_id]->Longitude : '';
//                     $officeLat =  !empty($data['offices'][$location->office_id]->Latitude) ? $data['offices'][$location->office_id]->Latitude : '';
                    
//                     if(!empty($officeLong) && !empty($officeLat)) {
//                         $time =  date('H.i', strtotime($location->datetime)); 
                        
//                         $tindex = $this->get_time_index($time);
                        
//                         $data['location_arr'][$dt][$location->guard_id]['status'][$tindex] = $this->check_inside_circle($officeLat, $officeLong, $location->LAT, $location->LONGI);  
//                     } else {
//                         $data['location_arr'][$dt][$location->guard_id]['status'][$tindex] = ''; 
//                     }
                    
                    
//                 }
                
//                 }                
//             }
            

// // echo '<pre>';
// // print_R($data);exit;

//         return $data;
// 	}
	//=============report===========
	
	public function checkFloat($num) {
	    // Try to convert the string to a float
        $floatVal = floatval($num);
        // If the parsing succeeded and the value is not equivalent to an int
        if($floatVal && intval($floatVal) != $floatVal)
        {
            return true;
        } else {
            return false;
        }
	}

    public function check_inside_circle( $center_lat, $center_lng, $lat2, $long2) {
        if($this->checkFloat($center_lat) && $this->checkFloat($center_lng) && $this->checkFloat($lat2) && $this->checkFloat($long2)) {
    	    $distance =  ( 6371 * acos( cos( deg2rad($center_lat) ) * cos( deg2rad( $lat2 ) ) * cos( deg2rad( $long2 ) 
    	                - deg2rad($center_lng) ) + sin( deg2rad($center_lat) ) * sin( deg2rad( $lat2 ) ) ) );
            if(((int)($distance * 1000)) < RADIUS) {
                return '<span style="font-weight: bold; color: green">In Circle</span>';
                // return  $center_lat .'||'. $center_lng .'||'.$lat2 .'||'.$long2 .  '<span style="font-weight: bold; color: green">In Circle</span>' . $distance * 1000;
    			 
            } else {
                return '<span style="font-weight: bold; color: red">Out Circle</span>';
    // 			return    $center_lat .'||'. $center_lng .'||'.$lat2 .'||'.$long2 . '<span style="font-weight: bold; color: red">Out Circle</span>' . $distance * 1000;
            }
        } else {
            return '';
        }

	}
	

	
	public function fetch_total_guard_location()
	{
	  $this->db->select('msg.name as pname,msg.age as page,msg.contact as cnumber,msg.email as pemail,off.name as oname,tad.inserted_DT as quorantinedt'); 
  	  $this->db->from('mst_guard msg');
  	  $this->db->join('tr_assign_duty tad','tad.guard_name=msg.id');
  	  $this->db->join('mst_office off','off.id = tad.office_name');
  	  $this->db->where('msg.deleted_DT',NULL);
  	  $this->db->order_by('msg.id','DESC');
 	  $query = $this->db->get();
      return $query->result();
	}
	
}
