<?php

class Court{
	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 getCourts($filter){

        $query_string = "SELECT `courts`.*, CONCAT(`users`.`fname`, ' ', `users`.`lname`) as username FROM `courts`, `users` WHERE `courts`.`addedBy` = `users`.`id`";

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

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

        if($query->num_rows > 0){

            $data = [];
            while($row = $query->fetch_assoc()){
                $statusColor = '';
                $statusText = '';
                switch($row['active']) {

                    case  '0' :
                        $statusText = 'Inactive';
                        $statusColor = 'danger';
                        break;
                    case '1' : 
                        $statusText = 'Active';
                        $statusColor = 'primary';
                        break;
                }

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

                $data[] = $row;
            }

            $this->_res = ['status'=>'ok', 'message'=>'', 'data'=>$data];

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


    public function getCourt($filter){

        $query = $this->_conn->query("SELECT * FROM `courts` WHERE `id` = '".$filter['id']."'");

        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 saveCourt($data){
        $dateNow = date('Y-m-d');
        $query = $this->_conn->query("INSERT INTO `courts` (`courtName`, `address`, `postcode`, `telephone`, `fax`, `email`, `note`, `dateAdded`, `addedBy`) VALUES('".$data['courtName']."', '".$data['address']."', '".$data['postcode']."', '".$data['telephone']."', '".$data['fax']."', '".$data['email']."', '".$data['note']."', '".$dateNow."', '".$this->_userId."')");

        if($query){
            $this->_res = ['status'=>'ok', 'message'=>'Court Details saved!', 'data'=>null];
        }else{
            $this->_res = ['status'=>'error', 'message'=>'Some error occured, Please try again!', 'data'=>null];
        }
        return $this->_res;
    }

    public function updateCourt($id, $data){
        $query = $this->_conn->query("UPDATE `courts` SET `courtName` = '".$data['courtName']."', `address` = '".$data['address']."', `postcode` = '".$data['postcode']."', `telephone` = '".$data['telephone']."', `fax` = '".$data['fax']."', `email` = '".$data['email']."', `note` = '".$data['note']."', `active` = '".$data['active']."' WHERE `id` = '".$id."'");
        if($query){
            $this->_res = ['status'=>'ok', 'message'=>'Court Details updated!', 'data'=>null];
        }else{
            $this->_res = ['status'=>'error', 'message'=>'Some error occured, Please try again!', 'data'=>null];
        }
        return $this->_res;
    }

}

?>