<?php

class Client{
	private $_conn = null;
	private $_res = null;
	private $_userId = null;
	
    public function __construct($conn, $userId){
		$this->_conn = $conn;
		$this->_userId = $userId;
        // date_timezone_set('Europe/London');
	}


    public function getClients($data){
        $query_string = "SELECT `clients`.*, `titles`.`name` as titleName, CONCAT(`users`.`fname`, ' ', `users`.`lname`) as username FROM clients, titles, users WHERE `clients`.`clientTitle` = `titles`.`id` AND `clients`.`addedBy` = `users`.`id` ";
        if($data['active'] != 'all'){
            $query_string .= " AND `clients`.`active` = '".$data['active']."' ";
        }

        $query = $this->_conn->query($query_string);

        if($query->num_rows > 0){
            $data = [];
            while($row = $query->fetch_assoc()){
                $data[] = $row;
            }
            $this->_res = ['status'=>'ok', 'message'=>'', 'data'=>$data];
        }else{
            $this->_res = ['status'=>'error', 'message'=>'No client data found!', 'data'=>$query_string];
        }
        return $this->_res;
    }

    public function getClient($data){
        $query_string = "SELECT `clients`.*, `titles`.`name` as titleName FROM clients, titles WHERE `clients`.`clientTitle` = `titles`.`id` ";
        if($data['id'] != 'all'){
            $query_string .= " AND `clients`.`id` = '".$data['id']."' ";
        }
        if($data['email'] != 'all'){
            $query_string .= " AND `clients`.`clientEmail` = '".$data['email']."' ";
        }

        $query = $this->_conn->query($query_string);

        if($query->num_rows == 1){
            $row = $query->fetch_assoc();
            $this->_res = ['status'=>'ok', 'message'=>'', 'data'=>$row];
        }else{
            $this->_res = ['status'=>'error', 'message'=>'No client data found!', 'data'=>$query_string];
        }
        return $this->_res;
    }

    public function saveClient($data){
        $query_find = $this->_conn->query("SELECT id FROM clients WHERE `clientEmail` = '".$data['clientEmail']."'");
        if($query_find->num_rows > 0){
            $this->_res = ['status'=>'error', 'message'=>'Client already exist with the same email!', 'data'=>null];
        }else{
            $dateNow = date('Y-m-d');
            $query = $this->_conn->query("INSERT INTO `clients` (`clientTitle`, `clientFname`, `clientLname`, `clientTelephone`, `clientMobile`, `clientEmail`, `clientAddress`, `clientPostcode`, `dateAdded`, `addedBy`) values('".$data['clientTitle']."', '".$data['clientFname']."', '".$data['clientLname']."', '".$data['clientTelephone']."', '".$data['clientMobile']."', '".$data['clientEmail']."', '".$data['clientAddress']."', '".$data['clientPostcode']."', '".$dateNow."', '".$this->_userId."')");
            if($query){
                $clientId = $this->_conn->insert_id;
                $query_username = $this->_conn->query("SELECT CONCAT(`fname`, ' ', `lname`) as userName FROM `users` WHERE `id` = '".$this->_userId."'");
                $row_username = $query_username->fetch_assoc();
                $dateActivity = date('Y-m-d : h:i');

                $activity = $data['clientFname'].' '.$data['clientLname'].' DPA Client added to the system ( Added  By : '.$row_username['userName'].')';
                $this->_conn->query("INSERT INTO `activities` (`modelId`, `modelName`, `userId`, `activityDate`, `activity`) VALUES('".$clientId."', 'client', '".$this->_userId."', '".$dateActivity."', '".$activity."')");
            

                $this->_res = ['status'=>'ok', 'message'=>'Client details saved', 'data'=>$this->_conn->insert_id];
            }else{
                $this->_res = ['status'=>'error', 'message'=>'Could not save client details!', 'data'=>null];
            }
        }
        return $this->_res;
    }

    public function updateClient($id, $data){
        $query_find = $this->_conn->query("SELECT id FROM clients WHERE `id` = '".$id."'");
        if($query_find->num_rows == 1){
            $string = "UPDATE `clients` SET `clientTitle` = '".$data['clientTitle']."', `clientFname` = '".$data['clientFname']."', `clientLname` = '".$data['clientLname']."', `clientTelephone` = '".$data['clientTelephone']."', `clientMobile` = '".$data['clientMobile']."', `clientEmail` = '".$data['clientEmail']."', `clientAddress` = '".$data['clientAddress']."', `clientPostcode` = '".$data['clientPostcode']."',  `active`='".$data['active']."' WHERE `id` = '".$id."'";
            $query = $this->_conn->query($string);
            if($query){

                $query_username = $this->_conn->query("SELECT CONCAT(`fname`, ' ', `lname`) as userName FROM `users` WHERE `id` = '".$this->_userId."'");
                $row_username = $query_username->fetch_assoc();
                $dateActivity = date('Y-m-d : h:i');

                $activity = $data['clientFname'].' '.$data['clientLname'].' DPA Client Details updated ( Updated  By : '.$row_username['userName'].')';
                $this->_conn->query("INSERT INTO `activities` (`modelId`, `modelName`, `userId`, `activityDate`, `activity`) VALUES('".$id."', 'client', '".$this->_userId."', '".$dateActivity."', '".$activity."')");
            
                $this->_res = ['status'=>'ok', 'message'=>'Client details updated Nicely!', 'data'=>$id];
            }else{
                $this->_res = ['status'=>'error', 'message'=>'Could not save the client details!', 'data'=>$string];
            }
        }else{
            $this->_res = ['status'=>'error', 'message'=>'No client details found!', 'data'=>null];
        }
        return $this->_res;
    }


}

?>