<?php

class Meeting {


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

    public function getMeetings($filter){
        $query_text = "SELECT `meetings`.`id`, CONCAT(`solicitorCompany`.`companyName`,' trading as ', `solicitorCompany`.`tradingName`) as solicitorName, `meetings`.`meetingDate`, `meetings`.`meetingTime`,  `meetings`.`meetingPlace`, `meetings`.`assignedTo`, `meetings`.`status`, CONCAT(`users`.`fname`, ' ', `users`.`lname`) as username FROM `meetings`, `solicitorCompany`, `users` WHERE `meetings`.`solicitorId` = `solicitorCompany`.`id` AND `meetings`.`addedBy` = `users`.`id`";
        if($filter['solicitorId'] != 'all'){
            $query_text .= " AND `meetings`.`solicitorId` = '".$filter['solicitorId']."' ";
        }
        if($filter['status'] != 'all'){
            $query_text .= " AND `meetings`.`status` = '".$filter['status']."' ";
        }
        $query_text .= " ORDER BY meetingDate, meetingTime DESC ";
        $query = $this->_conn->query($query_text);

        if($query->num_rows > 0){
            $data = [];
            $statusText = "";
            while($row = $query->fetch_assoc()){
                
                switch($row['status']){
                    case '0' : 
                        $statusColor = 'warning';
                        $statusText = "Pending";
                        break;
                    case '1':
                        $statusColor = 'primary';
                        $statusText = "Confirmed";
                        break;
                    case '2' : 
                        $statusColor = 'danger';
                        $statusText = "Cancelled";
                    default :
                        $statusText = "Pending";
                }

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

                $query_assigned = $this->_conn->query("SELECT `users`.`id`, CONCAT(`titles`.`name`, ' ', `users`.`fname`, ' ', `users`.`lname`) as fullname FROM users, titles WHERE `users`.`id` = '".$row['assignedTo']."' AND `users`.`titleId` = `titles`.`id`");
                $row_assigned = $query_assigned->fetch_assoc();
                $row['assignedName'] = $row_assigned['fullname'];

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

    public function getMeeting($filter){
        $string = "SELECT `meetings`.`id`, `meetings`.`solicitorId`, `meetings`.`meetingDate`, `meetings`.`meetingTime`, `meetings`.`meetingPlace`, `meetings`.`agendas`,`meetings`.`notes`, `meetings`.`assignedTo`, `meetings`.`dateAdded`, `meetings`.`status`, CONCAT(`users`.`fname`, ' ', `users`.`lname`) as username FROM `meetings`, `users` WHERE `meetings`.`id` = '".$filter['id']."' AND `meetings`.`addedBy` = `users`.`id`";
        $query = $this->_conn->query($string);
        if($query->num_rows == 1){
            $row = $query->fetch_assoc();
            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;
            }

            $string_solicitor = "SELECT `solicitorCompany`.`id`, `solicitorCompany`.`companyName`,`solicitorCompany`.`tradingName`,  `solicitorCompany`.`address`, `solicitorCompany`.`postcode`, `solicitorCompany`.`email`, `solicitorCompany`.`telephone`, `solicitorCompany`.`advancePayment`, CONCAT(`users`.`fname`,' ',`users`.`lname`) as username FROM solicitorCompany, users WHERE `solicitorCompany`.`id` = '".$row['solicitorId']."' AND `solicitorCompany`.`addedBy` = `users`.`id`";
            $query_solicitor = $this->_conn->query($string_solicitor);
            $row_solicitor = $query_solicitor->fetch_assoc();
            $row['solicitor'] = $row_solicitor;
            // if($row['agendas']){
            //     $row['agendas'] = json_decode($row['agendas']);
            // }
            

            //get assigned to 
            $query_assigned = $this->_conn->query("SELECT `users`.`id`, CONCAT(`titles`.`name`, ' ', `users`.`fname`, ' ', `users`.`lname`) as fullname FROM users, titles WHERE `users`.`id` = '".$row['assignedTo']."' AND `users`.`titleId` = `titles`.`id`");
            $row_assigned = $query_assigned->fetch_assoc();
            $row['assignedName'] = $row_assigned['fullname'];

            $row['agendas'] = $agendas;

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

    public function saveMeeting($meeting){
        $query_text = "INSERT INTO `meetings` (`solicitorId`, `meetingDate`, `meetingTime`, `meetingPlace`, `notes`, `assignedTo`, `addedBy`) VALUES('".$meeting['solicitorId']."', '".$meeting['meetingDate']."', '".$meeting['meetingTime']."',  '".$meeting['meetingPlace']."', '".$meeting['notes']."', '".$meeting['assignedTo']."', '".$this->_userId."')";
        $query = $this->_conn->query($query_text);
        if($query){
            $id = $this->_conn->insert_id;
            $this->_res = ['status'=>'ok', 'message'=>'Meeting save nicely.', 'data'=>$id];
        }else{
            $this->_res = ['status'=>'error', 'message'=>'Can not save meeting, Internal server error!', 'data'=>$query_text];
        }
        return $this->_res;
    }

    public function updateMeeting($id, $meeting){
        $query_check = $this->_conn->query("SELECT `solicitorId` FROM `meetings` WHERE `id` = '".$id."'");
        if($query_check->num_rows == 1){
            $query_update = $this->_conn->query("UPDATE `meetings` SET `meetingDate` = '".$meeting['meetingDate']."', `meetingTime` = '".$meeting['meetingTime']."',  `meetingPlace` = '".$meeting['meetingPlace']."',  `notes` = '".$meeting['notes']."' WHERE `id` = '".$id."'");
            if($query_update){
                $this->_res = ['status'=>'ok', 'message'=>'Meeting details updated', 'data'=>$id];
            }else{
                $this->_res = ['status'=>'error', 'message'=>'Could not update meeting', 'data'=>null];
            }
        }else{
            $this->_res = ['status'=>'error', 'message'=>'No meeting details found!', 'data'=>null];
        }
        return $this->_res;
    }

    public function saveAgenda($data){
        $agendas = json_encode($data->agendas);
        $query = $this->_conn->query("UPDATE `meetings` SET `agendas` = '".$data['agendas']."' WHERE `id` = '".$data['meetingId']."'");
        $this->_res = ['status'=>'ok', 'message'=>'Agenda Saved', 'data'=>$agendas];
        return $this->_res;
    }


    public function updateAgenda($id, $data){
        $query_user = $this->_conn->query("SELECT `password` FROM `users` WHERE `id` = '".$this->_userId."'");
        if($query_user->num_rows == 1){

            $row_user = $query_user->fetch_assoc();
            $password = md5($data['password']);
            $sqlPassword = $row_user['password'];
            if($password === $sqlPassword){

                $data['agendas'] = json_encode($data['agendas']);
                $query = $this->_conn->query("UPDATE `meetings` SET `agendas` = '".$data['agendas']."' WHERE `id` = '".$id."'");
                $this->_res = ['status'=>'ok', 'message'=>'Agenda Deleted !', 'data'=>null];

            }else{
                $this->_res = ['status'=>'error', 'message'=>'Password did not match, Please try again!', 'data'=>null];
            }

        }else{
            $this->_res = ['status'=>'error', 'message'=>'Authorised user not found !', 'data'=>null];
        }
        return $this->_res; 
    }

    public function deleteMeeting($id){
        $query_check = $this->_conn->query("SELECT `clientId` FROM `meetings` WHERE `id` = '".$id."'");
        if($query_check->num_rows == 1){
            $query_delete = $this->_conn->query("DELETE FROM `meetings` WHERE `id` = '".$id."'");
            if($query_delete){
                $this->_res =['status'=>'ok', 'message'=>'Meeting deleted', 'data'=>null];
            }else{
                $this->_res = ['status'=>'error', 'message'=>'Could not delete meeting, Internal server error!', 'data'=>null];
            }
            
        }else{
            $this->_res = ['status'=>'error', 'message'=>'No meeting details found!', 'data'=>null];
        }
        return $this->_res;
    }

}


?>