<?php

class Note{
	private $_conn = null;
	private $_res = null;
	private $_userId = null;
	private $_permission = null;
	public function __construct($conn, $userId){
		$this->_conn = $conn;
		$this->_userId = $userId;
        $query = $this->_conn->query("SELECT permission FROM `users` WHERE `id` = '".$this->userId."'");
        $row = $query->fetch_assoc();
        $this->_permission = json_encode($row['permission']);
	}


    public function getNotes($filter){

        $query = $this->_conn->query("SELECT `notes`.`id`, `notes`.`note`, `notes`.`dateAdded`, `notes`.`reminder`, `notes`.`reminderDate`, `notes`.`reminderStatus`,`notes`.`popup`, concat(`users`.`fname`, ' ', `users`.`lname`) as username FROM `notes`, `users` WHERE `notes`.`modelId` = '".$filter['modelId']."' AND `notes`.`modelName` = '".$filter['modelName']."' AND `notes`.`addedBy` = `users`.`id` ORDER BY `notes`.`dateAdded` DESC");

        if($query->num_rows > 0){
            $data = [];
            while($row = $query->fetch_assoc()){
                switch ($row['reminderStatus']) {
                    case '1' : 
                        $statusText = 'Complete';
                        $statusColor = 'primary';
                        break;
                    case '0' : 
                        $statusText = 'Pending';
                        $statusColor = 'danger';
                        break;
                    default : 
                        $statusText = 'Pending';
                        $statusColor = 'danger';
                        break;
                }

                $row['statusText'] = $statusText;
                $row['statusColor'] = $statusColor;

                switch ($row['popup']) {
                    case '1' : 
                        $popupText = 'Active';
                        $popupColor = 'primary';
                        break;
                    case '0' : 
                        $popupText = 'None';
                        $popupColor = 'danger';
                        break;
                    default : 
                        $popupText = 'Active';
                        $popupColor = 'primary';
                        break;
                }

                $row['popupText'] = $popupText;
                $row['popupColor'] = $popupColor;

                $data[] = $row;
                
            }

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

    }

    public function getNote($filter){
        $string = "SELECT  `notes`.`id`, `notes`.`note`, `notes`.`reminder`, `notes`.`reminderDate`, `notes`.`reminderStatus`, `notes`.`popup` FROM `notes` WHERE `id` = '".$filter['id']."'";
        $query = $this->_conn->query($string);

        $query = $this->_conn->query($string);
        if($query->num_rows == 1){
            $row = $query->fetch_assoc();
            switch ($row['reminderStatus']) {
                case '1' : 
                    $statusText = 'Complete';
                    $statusColor = 'primary';
                    break;
                case '0' : 
                    $statusText = 'Pending';
                    $statusColor = 'danger';
                    break;
                default : 
                    $statusText = 'Pending';
                    $statusColor = 'danger';
                    break;
            }

            $row['statusText'] = $statusText;
            $row['statusColor'] = $statusColor;
            switch ($row['popup']) {
                case '1' : 
                    $popupText = 'Active';
                    $popupColor = 'primary';
                    break;
                case '0' : 
                    $popupText = 'None';
                    $popupColor = 'danger';
                    break;
                default : 
                    $popupText = 'Active';
                    $popupColor = 'primary';
                    break;
            }

            $row['popupText'] = $popupText;
            $row['popupColor'] = $popupColor;

            $this->_res  = ['status'=>'ok', 'message'=>'', 'data'=>$row];
        }else{
            $this->_res = ['status'=>'error', 'message'=>'No note found!', 'data'=>null];
        }
        return $this->_res;
    }

    public function saveNote($note){

        if($note['id']){
            if($note['reminder'] == 0){
                $query_string = "UPDATE `notes` SET  `note` = '".$note['note']."', `reminder` = '0',`popup`= '".$note['popup']."' WHERE `id` = '".$note['id']."'";
            }else{
                $query_string = "UPDATE `notes` SET  `note` = '".$note['note']."', `reminder` = '1', `reminderDate` = '".$note['reminderDate']."' ,`reminderStatus` = '".$note['reminderStatus']."' , `popup`= '".$note['popup']."' WHERE `id` = '".$note['id']."'";
            }
        }else{
            if($note['reminder'] == 0){
                $query_string = "INSERT INTO `notes` (`modelId`, `modelName`, `note`, `reminder`,`popup`, `addedBy`) VALUES('".$note['modelId']."', '".$note['modelName']."', '".$note['note']."','0', '".$note['popup']."', '".$this->_userId."')";
            }else{
                $query_string = "INSERT INTO `notes` (`modelId`, `modelName`, `note`, `reminder`, `reminderDate`,`reminderStatus`, `popup`, `addedBy`) VALUES('".$note['modelId']."', '".$note['modelName']."', '".$note['note']."', '".$note['reminder']."', '".$note['reminderDate']."', '0', '".$note['popup']."', '".$this->_userId."')";
            }
        }
        
        $query = $this->_conn->query($query_string);
        if($query){
            $query_notes = "SELECT `notes`.`id`, `notes`.`note`, `notes`.`dateAdded`, `notes`.`reminder`, `notes`.`reminderDate`, `notes`.`reminderStatus`, `notes`.`popup`, concat(`users`.`fname`, ' ', `users`.`lname`) as username FROM `notes`, `users` WHERE `notes`.`modelId` = '".$note['modelId']."' AND `notes`.`modelName` = '".$note['modelName']."' AND `notes`.`addedBy` = `users`.`id` ORDER BY `notes`.`dateAdded` DESC";
            $query_note = $this->_conn->query($query_notes);
            $data = [];

            if($query_note->num_rows > 0){
                while($row = $query_note->fetch_assoc()){
                    switch ($row['reminderStatus']) {
                        case '1' : 
                            $statusText = 'Complete';
                            $statusColor = 'primary';
                            break;
                        case '0' : 
                            $statusText = 'Pending';
                            $statusColor = 'danger';
                            break;
                        default : 
                            $statusText = 'Pending';
                            $statusColor = 'danger';
                            break;
                    }
    
                    $row['statusText'] = $statusText;
                    $row['statusColor'] = $statusColor;

                    switch ($row['popup']) {
                        case '1' : 
                            $popupText = 'Active';
                            $popupColor = 'primary';
                            break;
                        case '0' : 
                            $popupText = 'None';
                            $popupColor = 'danger';
                            break;
                        default : 
                            $popupText = 'Active';
                            $popupColor = 'primary';
                            break;
                    }
    
                    $row['popupText'] = $popupText;
                    $row['popupColor'] = $popupColor;

                    $data[] = $row;
                }
            }

            $this->_res = ['status'=>'ok', 'message'=>'Note saved nicely', 'data'=>$data];
        }else{
            $this->_res = ['status'=>'error', 'message'=>$note, 'data'=>$query_string];
        }
        return $this->_res;
    }

}


?>