<?php
// Required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

// Include required files
include_once '../config/database.php';
include_once '../objects/room_type.php';

session_start();
date_default_timezone_set('Asia/Rangoon');

// Create a database connection
$database = new Database();
$db = $database->getConnection();

// Initialize RoomType object
$room_type = new RoomType($db);

// Get the incoming data
$data = json_decode(file_get_contents("php://input"));

// Set properties
$room_type->id = isset($data->id) ? $data->id : null;
$room_type->room_type = isset($data->room_type) ? $data->room_type : null;
$room_type->description = isset($data->description) ? $data->description : null;
$room_type->room_rate = isset($data->room_rate) ? $data->room_rate : null;
$room_type->max_occupancy = isset($data->max_occupancy) ? $data->max_occupancy : null;
$room_type->amenities = isset($data->amenities) ? $data->amenities : null;
$room_type->extra_bed_rate = isset($data->extra_bed_rate) ? $data->extra_bed_rate : null;
$room_type->entry_by = isset($_SESSION['userid'])?$_SESSION['userid']:"";
$room_type->update_by = isset($_SESSION['userid'])?$_SESSION['userid']:"";

if($data->id){
	$room_type->id = $data->id;
	if($room_type->update()){
        echo json_encode(array("message" => "updated"));
	}else{
        echo json_encode(array("message" => "error"));
	}
}else{
	if($room_type->exist()){
        echo json_encode(array("message" => "duplicate"));
    }else if($room_type->create()){
        echo json_encode(array("message" => "created"));
    }else{
        echo json_encode(array("message" => "error"));
    }
}


?>
