<?php

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

    public function getTasks($filter){

        $query_string = "SELECT `tasks`.`id`, `tasks`.`modelId`, `tasks`.`modelName`, `tasks`.`title`,`tasks`.`dueDate`, `priorities`.`name` as taskPriority, CONCAT(`users`.`fname`, ' ', `users`.`lname`) as username, `tasks`.`dateAdded`,`tasks`.`addedBy`, `tasks`.`status` FROM tasks, priorities, users WHERE `tasks`.`priorityId` = `priorities`.`id` AND `tasks`.`addedBy` = `users`.`id` ";
        
        if($filter['modelId'] != 'all'){
            $query_string .= "AND `tasks`.`modelId` = '".$filter['modelId']."' ";
        }

        if($filter['modelName'] != 'all'){
            $query_string .= "AND `tasks`.`modelName` = '".$filter['modelName']."' ";
        }

        if($filter['status'] != 'all'){
            $query_string .= "AND `tasks`.`status` = '".$filter['status']."' ";
        }

        $query_string .= " ORDER BY `tasks`.`dateAdded` DESC";

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

        if($query->num_rows > 0){
            $data = [];
            while($row = $query->fetch_assoc()){
                $relname = "";
                if($row['modelName'] == "solicitor"){
                    $query_rel = $this->_conn->query("SELECT CONCAT(`solicitorCompany`.`companyName`, ' trading Name : ', `solicitorCompany`.`tradingName`)  as relname FROM `solicitorCompany` WHERE `solicitorCompany`.`id` = '".$row['modelId']."'");
                    $row_rel = $query_rel->fetch_assoc();
                    $relname = $row_rel['relname'];
                }else if($row['modelName'] == "case"){
                    $query_rel = $this->_conn->query("SELECT `caseTitles`.`name` as relname FROM `cases`, `caseTitles` WHERE `cases`.`id` = '".$row['modelId']."' AND `cases`.`caseTitleId` = `caseTitles`.`id`");
                    $row_rel = $query_rel->fetch_assoc();
                    $relname = $row_rel['relname'];
                }
                $row['relname'] = $relname;

               

                switch ($row['status']) {
                    case '0' :
                        $statusColor = 'danger';
                        $statusText = "Pending";
                        break;
                    case '1' : 
                        $statusColor = 'primary';
                        $statusText = 'Closed';
                        break;
                    case '2' :
                        $statusColor = 'info';
                        $statusText = 'Cancelled';
                        break;
                    default :
                        $statusColor = 'dander';
                        $statusText = 'Pending';
                        break;
                }

                switch ($row['taskPriority']) {
                    case '1' :
                        $priorityColor = 'info';
                        break;
                    case '2' : 
                        $priorityColor = 'warning';
                        break;
                    case '3' :
                        $priorityColor = 'danger';
                        break;
                    default :
                        $priorityColor = 'info';
                }

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

                $data[] = $row;
            }
            $this->_res = ['status'=>'ok', 'message'=>'', 'data'=>$data];
        }else{
            $this->_res = ['status'=>'error', 'message'=>'No Tasks Found!', 'data'=>$query_string];
        }
        return $this->_res;
    }

    public function getTask($id){
        $query_string = "SELECT `tasks`.`id`, `tasks`.`modelId`, `tasks`.`modelName`, `tasks`.`title`, `tasks`.`dueDate`, `tasks`.`priorityId`, `priorities`.`name` as taskPriority, `tasks`.`details`,  `tasks`.`dateAdded`, CONCAT(`users`.`fname`, ' ', `users`.`lname`) as username, `tasks`.`status` FROM `tasks`, `priorities`, `users` WHERE `tasks`.`id` = '".$id['id']."' AND `tasks`.`priorityId` = `priorities`.`id` AND `tasks`.`addedBy` = `users`.`id`";
        $query = $this->_conn->query($query_string);
        if($query->num_rows == 1){
            $row = $query->fetch_assoc();
            $relname = "";
            if($row['modelName'] == "solicitor"){
                $query_rel = $this->_conn->query("SELECT CONCAT(`solicitorCompany`.`companyName`, ' trading Name : ', `solicitorCompany`.`tradingName`)  as relname FROM `solicitorCompany` WHERE `solicitorCompany`.`id` = '".$row['modelId']."'");
                $row_rel = $query_rel->fetch_assoc();
                $relname = $row_rel['relname'];
            }else if($row['modelName'] == "case"){
                $query_rel = $this->_conn->query("SELECT `caseTitles`.`name` as relname FROM `cases`, `caseTitles` WHERE `cases`.`id` = '".$row['modelId']."' AND `cases`.`caseTitleId` = `caseTitles`.`id`");
                $row_rel = $query_rel->fetch_assoc();
                $relname = $row_rel['relname'];
            }
            $row['relname'] = $relname;

            switch ($row['status']) {
                case '0' :
                    $statusColor = 'danger';
                    $statusText = "Pending";
                    break;
                case '1' : 
                    $statusColor = 'primary';
                    $statusText = 'Closed';
                    break;
                case '2' :
                    $statusColor = 'info';
                    $statusText = 'Cancelled';
                    break;
                default :
                    $statusColor = 'dander';
                    $statusText = 'Pending';
                    break;
            }

            switch ($row['taskPriority']) {
                case '1' :
                    $priorityColor = 'info';
                    break;
                case '2' : 
                    $priorityColor = 'warning';
                    break;
                case '3' :
                    $priorityColor = 'danger';
                    break;
                default :
                    $priorityColor = 'info';
                    break;
            }

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

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

    public function saveTask($task){

        $dateNow = date('Y-m-d');
        $query_string = "INSERT INTO `tasks` (`modelId`, `modelName`, `title`, `dueDate`, `priorityId`, `details`,`dateAdded`, `addedBy`) VALUES('".$task['modelId']."', '".$task['modelName']."', '".$task['title']."', '".$task['dueDate']."', '".$task['taskPriority']."', '".$task['details']."', '".$dateNow."', '".$this->_userId."')";
        $query = $this->_conn->query($query_string);
        if($query){
            
            $this->_res = ['status'=>'ok', 'message'=>$dateNow, 'data'=>$this->_conn->insert_id];
            
        }else{
            $this->_res = ['status'=>'error', 'message'=>'Could not save task, Internal server error!', 'data'=>$query_string];
        }

        return $this->_res;
    }

    public function updateTask($id, $task){
        $query_check = $this->_conn->query("SELECT `title` FROM `tasks` WHERE `id` = '".$id."'");
        if($query_check->num_rows == 1){
            $query_string = "UPDATE `tasks` SET `title` = '".$task['title']."', `priorityId` = '".$task['priorityId']."', `details` = '".$task['details']."', `status` = '".$task['status']."' WHERE `id` = '".$id."'";
            $query_update = $this->_conn->query($query_string);
            
            if($query_update){
                $this->_res = ['status'=>'success', 'message'=>'Task updated!', 'data'=>null];
            }else{
                $this->_res = ['status'=>'error', 'message'=>'Could not update task!', 'data'=>$query_string];
            }
            
        }else{
            $this->_res = ['status'=>'error', 'message'=>'No task found to update!', 'data'=>null];
        }
        return $this->_res;
    }


    // public function deleteTask($id){
    //     $query_check = $this->_conn->query("SELECT `title` FROM `tasks` WHERE `id` = '".$id."'");
    //     if($query_check->num_rows == 1){
    //         $query_delete = $this->_conn->query("DELETE FROM `tasks` WHERE `id` = '".$id."'");
            
    //         if($query_delete){
    //             $query_string = "SELECT `tasks`.`id`, `tasks`.`modelId`, `tasks`.`modelName`, `tasks`.`title`, `tasks`.`dueDate`, `casePriorities`.`name` as taskPriority, CONCAT(`users`.`fname`, ' ', `users`.`lname`) as assignedTo, `tasks`.`dateAdded` FROM tasks, casePriorities, users WHERE `tasks`.`modelId` != '".$task['modelId']."' AND `tasks`.`modelName` != '".$task['modelName']."' AND `tasks`.`taskPriority` = `casePriorities`.`id` AND `tasks`.`assignedTo` = `users`.`id` ORDER BY `tasks`.`dateAdded` DESC";

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

    //             if($query->num_rows > 0){
    //                 $data = [];
    //                 while($row = $query->fetch_assoc()){
    //                     $relname = "";
    //                     if($row['modelName'] == "client"){
    //                         $query_rel = $this->_conn->query("SELECT concat(`titles`.`name`, ' ', `users`.`fname`, ' ', `users`.`lname`) as relname FROM `clients`, `titles` WHERE `clients`.`id` = '".$row['modelId']."' AND `clients`.`title` = `titles`.`id`");
    //                         $row_rel = $query_rel->fetch_assoc();
    //                         $relname = $row_rel['relname'];
    //                     }else if($row['modelName'] == "case"){
    //                         $query_rel = $this->_conn->query("SELECT `caseTitle` as relname FROM `cases` WHERE `id` = '".$row['modelId']."'");
    //                         $row_rel = $query_rel->fetch_assoc();
    //                         $relname = $row_rel['relname'];
    //                     }
    //                     $row['relname'] = $relname;

    //                     switch ($row['status']) {
    //                         case '0' :
    //                             $statusColor = 'danger';
    //                             $statusText = "Pending";
    //                         case '1' : 
    //                             $statusColor = 'primary';
    //                             $statusText = 'Closed';
    //                         case '2' :
    //                             $statusColor = 'info';
    //                             $statusText = 'Cancelled';
    //                         default :
    //                             $statusColor = 'dander';
    //                             $statusText = 'Pending';
    //                     }
        
    //                     switch ($row['taskPriority']) {
    //                         case '1' :
    //                             $priorityColor = 'info';
    //                         case '2' : 
    //                             $priorityColor = 'warning';
    //                         case '3' :
    //                             $priorityColor = 'danger';
    //                         default :
    //                             $priorityColor = 'info';
    //                     }
        
    //                     $row['statusColor'] = $statusColor;
    //                     $row['statusText'] = $statusText;
    //                     $row['priorityColor'] = $priorityColor;

    //                     $data[] = $row;
    //                 }
    //                 $this->_res = ['status'=>'ok', 'message'=>'Task deleted nicely', 'data'=>$row];
    //             }else{
    //                 $this->_res = ['status'=>'ok', 'message'=>'Task deleted nicely', 'data'=>null];
    //             }
    //         }else{
    //             $this->_res = ['status'=>'error', 'message'=>'Could not delete task!', 'data'=>null];
    //         }
            
    //     }else{
    //         $this->_res = ['status'=>'error', 'message'=>'No task found to delete!', 'data'=>null];
    //     }
    //     return $this->_res;
    // }

}


?>