<?php

class Support{
	private $_conn = null;
	private $_res = null;
	private $_userId = null;
	public function __construct($conn, $userId){
		$this->_conn = $conn;
		$this->_userId = $userId;
	}

    public function getPopup($data){
        
        $popups = [];
        $queries = [];
        switch($data['tableName']){

            case 'case' :
                $string_case = "SELECT `clientTypeId`, `solicitorId`, `teamId`,`barristerId`, `clientId` FROM `cases` WHERE `id` = '".$data['id']."'";
                $queries[] = $string_case;
                $query_case = $this->_conn->query($string_case);
                $row_case = $query_case->fetch_assoc();

                $query = $this->_conn->query("SELECT note FROM notes WHERE modelName = 'case' AND modelId = '".$data['id']."' AND popup = '1'");
                if($query->num_rows > 0){
                    while($row = $query->fetch_assoc()){
                        $popups[] = $row['note'];
                    }
                }

                if($row_case['clientTypeId'] == '1'){
                    $popups[] = 'Direct Public Access! Payment and Client Care Letter IS REQUIRED';
                }else{
                    $query_soli = $this->_conn->query("SELECT note FROM notes WHERE modelName = 'solicitor' AND modelId = '".$row_case['solicitorId']."' AND popup = '1'");
                    if($query_soli->num_rows > 0){
                        while($row_soli = $query_soli->fetch_assoc()){
                            $popups[] = $row_soli['note'];
                        }
                    }
                }
                $query_soli = $this->_conn->query("SELECT note FROM notes WHERE modelName = 'barrister' AND modelId = '".$row_case['barristerId']."' AND popup = '1'");
                    if($query_soli->num_rows > 0){
                       while($row_soli = $query_soli->fetch_assoc()){
                            $popups[] = $row_soli['note'];
                    }
                }
                break;
            case 'solicitor' :
                $query_soli = $this->_conn->query("SELECT note FROM notes WHERE modelName = 'solicitor' AND modelId = '".$data['id']."' AND popup = '1'");
                if($query_soli->num_rows > 0){
                    while($row_soli = $query_soli->fetch_assoc()){
                        $popups[] = $row_soli['note'];
                    }
                }
                break;
            
            case 'barrister' : 
                $query_barrister = $this->_conn->query("SELECT note FROM notes WHERE modelName = 'barrister' AND modelId = '".$data['id']."' AND popup = '1'");
                if($query_barrister->num_rows > 0){
                    while($row_barrister = $query_soli->fetch_assoc()){
                        $popups[] = $row_barrister['note'];
                    }
                }
            break;

        }
        if(count($popups) > 0){
            $this->_res = ['status'=>'ok', 'message'=>'', 'data'=>$popups];
        }else{
            $this->_res = ['status'=>'error', 'message'=>'', 'data'=>null];
        }
        
        return $this->_res;
    }


    public function getCompany(){
        $query = $this->_conn->query("SELECT * FROM `company`");
        $row = $query->fetch_assoc();
        $this->_res = ['status'=>'ok', 'message'=>'', 'data'=>$row];
        return $this->_res;
    }

    public function updateCompany($data){
        $string = "UPDATE `company` SET `companyName` = '".$data['companyName']."', `tradingName` = '".$data['tradingName']."', `email` = '".$data['email']."', `telephone` = '".$data['telephone']."', `address` = '".$data['address']."', `postcode` = '".$data['postcode']."', `vatNo` = '".$data['vatNo']."', `regNo` = '".$data['regNo']."', `note` = '".$data['note']."', `attendanceAutomation` = '".$data['attendanceAutomation']."', `outstandingAutomation` = '".$data['outstandingAutomation']."', `invoiceAutomation` = '".$data['invoiceAutomation']."', `emailBarrister` = '".$data['emailBarrister']."', `emailSolicitor` = '".$data['emailSolicitor']."'";
        $query= $this->_conn->query($string);

        if($query){
            $this->_res = ['status'=>'ok', 'message'=>'Details Updated!', 'data'=>$string];
        }else{
            $this->_res = ['status'=>'error', 'message'=>'Could update details!', 'data'=>null];
        }
        return $this->_res;
    }

    public function getActivities($data){
        $string = "SELECT * FROM `activities` WHERE `modelName` = '".$data['modelName']."' AND `modelId` = '".$data['modelId']."'";
        if($data['type'] != 'all'){
            $string .= " AND `type` = '".$data['type']."'";
        }
        $string .= " ORDER BY `activityDate` DESC";
        $query = $this->_conn->query($string);
        if($query->num_rows > 0){
            $data = [];
            while($row = $query->fetch_assoc()){

                if($row['userId']){
                    $row['userName'] = 'System';
                }else{
                    $query_user = $this->_conn->query("SELECT CONCAT(`titles`.`name`, ' ',`users`.`fname`, ' ',`users`.`lname`) as userName FROM `users`, `titles` WHERE `users`.`id` = '".$row['userId']."' AND `users`.`titleId` = `titles`.`id`");
                    if($query_user->num_rows == 1){
                        $row_user = $query_user->fetch_assoc();
                        $row['userName'] = $row_user['userName'];
                    }else{
                        $row['userName'] = 'Unknown';
                    }
                }

                $data[] = $row;

            }
            $this->_res = ['status'=>'ok', 'message'=>'', 'data'=>$data];
        }else{
            $this->_res = ['status'=>'ok', 'message'=>'No activity yet!', 'data'=>null];
        }
        return $this->_res;
    }

    public function getNames($p){
        $string = "SELECT * FROM `".$p['tableName']."` ORDER BY id DESC";
        $query = $this->_conn->query("SELECT * FROM `".$p['tableName']."` ORDER BY id ASC");
        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 data found!', 'data'=>$p];
        }
        return $this->_res;
    }

    public function getCategories($p){
        $query = $this->_conn->query("SELECT * FROM `caseCategories` WHERE `caseTypeId` = '".$p['caseTypeId']."'");
        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 data found!', 'data'=>$p];
        }
        return $this->_res;
    }


    public function getName($p){
        $query = $this->_conn->query("SELECT * FROM `".$p['tableName']."` WHERE `id` = '".$p['tableId']."'");
        if($query->num_rows == 1){
            $row = $query->fetch_assoc();
            $this->_res = ['status'=>'ok', 'message'=>'', 'data'=>$row];
        }else{
            $this->_res = ['status'=>'error', 'message'=>' :: No data found!', 'data'=>null];
        }
        return $this->_res;
    }

    public function saveName($d){
        $query_check = $this->_conn->query("SELECT id FROM `".$d['tableName']."` WHERE LOWER(`name`) = LOWER('".$d['value']."')");

        if($query_check->num_rows == 1){
            $this->_res = ['status'=>'error', 'message'=>' :: Data already exist', 'data'=>''];
        }else{
            $query = $this->_conn->query("INSERT INTO `".$d['tableName']."` VALUES('', '".$d['value']."')");
            if($query){
                $id = $this->_conn->insert_id;
                $this->_res = ['status'=>'ok', 'message'=>' :: Data saved nicely, Thanks', 'data'=>$id];
            }else{
                $this->_res = ['status'=>'error', 'message'=>' :: Could not save data, Server error!', 'data'=>null];
            }
        }
        return $this->_res;
    }

    public function updateName($id, $data){
        $query_check = $this->_conn->query("SELECT `name` FROM `".$data['tableName']."` WHERE `id` = '".$id."'");
        if($query_check->num_rows == 1){
            $query_name_check = $this->_conn->query("SELECT id FROM `".$d['tableName']."` WHERE LOWER(`name`) = LOWER('".$d['value']."')");
            if($query_name_check->num_rows == 1){
                $this->_res = ['status'=>'error', 'message'=>" :: Value already exist", 'data'=>''];
            }else{

                $query_update = $this->_conn->query("UPDATE `".$data['tableName']."` SET `name` = '".$data['value']."' WHERE `id` = '".$id."'");
                if($query_update){
                    $this->_res = ['status'=>'ok', 'message'=>" :: Data has been updatd", 'data'=>$id];
                }else{
                    $this->_res = ['status'=>'error', 'message'=>' :: Could not save data, Server error!', 'data'=>null];
                }
            }
        }else{
            $this->_res = ['status'=>'error', 'message'=>' :: No entry found to update!', 'data'=>null];
        }
        return $this->_res;
    }

    public function deleteName($d){
        $query_check = $this->_conn->query("SELECT `name` FROM `".$d['tableName']."` WHERE `id` = '".$d['tableId']."'");
        if($query_check->num_rows == 1){
            $query_delete = $this->_conn->query("DELETE FROM `".$d['tableName']."` WHERE `id` = '".$d['tableId']."'");
            if($query_delete){
                $this->_res = ['status'=>'ok', 'message'=>' :: Data has been deleted!', 'data'=>null];
            }else{
                $this->_res = ['status'=>'error', 'message'=>' :: Could not delete data, Server Error!', 'data'=>null];
            }
        }else{
            $this->_res = ['status'=>'error', 'message'=>' :: No entry found to delete!', 'data'=>null];
        }
        return $this->_res;
    }

}

?>