<?php
class Page {
     private  $db;
	 public function __construct() {
        $this->db = new Database;
	 }

	public function page_status($data_array,$table) {

        $this->db->query('SELECT * FROM '.$table.' WHERE uniqueid = :id');
        $this->db->bind(':id', $data_array['uniqueid']);
        $row = $this->db->single();
        $count = $this->db->rowCount();
        if($count == 0){
            return false;
        }
        $columns = $data_array;
        $columnString = implode(',', array_flip($columns));
        $valueString = ":".implode(',:', array_flip($columns));
            $update_arr = [];
            foreach($columns as $key => $value){
                $str = $key.' = :'.$key;
                array_push($update_arr,$str);
            }
            $update_query = implode(' , ', $update_arr);
            $this->db->query('UPDATE visitors SET '.$update_query.' WHERE uniqueid = :uniqueid');
        
         foreach($columns as $key => $value){
             $this->db->bind(':' . $key,$value);
         }
        if($this->db->execute()){


            $uniqueid = $data_array['uniqueid'];
            $status = $data_array['status'];
             $this->db->query('INSERT INTO user_logs(uniqueid,logs,reply) VALUES (:uniqueid,:logs,:reply)');
             $this->db->bind(':uniqueid',$uniqueid);
             $this->db->bind(':logs',$status);
             $this->db->bind(':reply','admin');
             $this->db->execute();

            return true;
        }else{
            return false;
        }
  }

  public function page_status_current($data_array,$table) {

        $this->db->query('SELECT * FROM '.$table.' WHERE uniqueid = :id');
        $this->db->bind(':id', $data_array['uniqueid']);
        $row = $this->db->single();
        $count = $this->db->rowCount();
        if($count == 0){
            return false;
        }
        $columns = $data_array;
        $columnString = implode(',', array_flip($columns));
        $valueString = ":".implode(',:', array_flip($columns));
            $update_arr = [];
            foreach($columns as $key => $value){
                $str = $key.' = :'.$key;
                array_push($update_arr,$str);
            }
            $update_query = implode(' , ', $update_arr);
            $this->db->query('UPDATE visitors SET '.$update_query.' WHERE uniqueid = :uniqueid');
        
         foreach($columns as $key => $value){
             $this->db->bind(':' . $key,$value);
         }
        if($this->db->execute()){

            $this->db->query('SELECT * FROM '.$table.' WHERE uniqueid = :id');
            $this->db->bind(':id', $data_array['uniqueid']);
            $row = $this->db->single();
        }else{
            return false;
        }
  }

    public function page_status_url($data_array) {

        $this->db->query('SELECT * FROM visitors WHERE uniqueid = :id');
        $this->db->bind(':id', $_SESSION['visitor_uniqueid']);
        $row = $this->db->single();
         $options_json = file_get_contents("../app/config/options.json");
        $decoded_json = json_decode($options_json, true); // decode the options for interpretation
        $url_to_status_array = array();
        foreach ($decoded_json as $options) {
          $button_array = $options["buttons"];
          foreach ($button_array as $button) {
                if($button['status'] == $row->status){
                    $url_to_status_array['visitor_status'] = $button["url"];
                    $url_to_status_array['status'] = "true";
                }
          }
        }
        return $url_to_status_array;
    }

    public function  DeleteUSer($id,$table)
    {
        $this->db->query('DELETE FROM '.$table.' WHERE id = :id');
        $this->db->bind(':id', $id);
        if($this->db->execute()){
            return true;
        }else{
            return false;
        }
        return true;
    }
    public function  DeleteVister($id,$table)
    {
        $this->db->query('DELETE FROM '.$table.' WHERE uniqueid = :id');
        $this->db->bind(':id', $id);
        if($this->db->execute()){
            return true;
        }else{
            return false;
        }
        return true;
    }

     public function page_status_claim_by($data_array) {

         $this->db->query('SELECT * FROM visitors WHERE uniqueid = :id');
        $this->db->bind(':id', $data_array['uniqueid']);
        $row = $this->db->single();

         $this->db->query('UPDATE visitors SET claim_by=:claim_by WHERE uniqueid = :uniqueid');
        $this->db->bind(':uniqueid', $data_array['uniqueid']);
        $this->db->bind(':claim_by', $data_array['claim_by']);
        $this->db->execute();

        return true;
     }

}
?>