<?php
class ReservationDetail {
    // Database connection and table name
    private $conn;
    private $table_name = "booking_detail";

    // Object properties
    public $id;
    public $reservation_no; // Foreign key linking to the reservation
    public $date; // Date of stay
    public $description;
    public $rate_mode;
    public $billing_units;
    public $unit_rate;
    public $room_rate; // Rate for the room
    public $extra_bed; // Count of extra beds
    public $extra_person; // Rate for extra beds
    public $extra_bed_rate; // Count of extra beds
    public $extra_person_rate; // Rate for extra beds

    public $checkin_id;
    public $room_no;

    public function __construct($db) {
        $this->conn = $db;
    }

    function create() {
        $query = "INSERT INTO " . $this->table_name . " 
                  SET booking_no=:reservation_no, date=:date, description=:description,
                      rate_mode=:rate_mode,billing_units=:billing_units,unit_rate=:unit_rate,
                      rental_rate=:room_rate, 
                      extra_driver=:extra_bed,extra_driver_rate=:extra_bed_rate,  
                      extra_passenger=:extra_person,extra_passenger_rate=:extra_person_rate";
        $stmt = $this->conn->prepare($query);

        // Bind values
        $stmt->bindParam(":reservation_no", $this->reservation_no);
        $stmt->bindParam(":date", $this->date);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":rate_mode", $this->rate_mode);
        $stmt->bindParam(":billing_units", $this->billing_units);
        $stmt->bindParam(":unit_rate", $this->unit_rate);
        $stmt->bindParam(":room_rate", $this->room_rate);
        $stmt->bindParam(":extra_bed", $this->extra_bed);
        $stmt->bindParam(":extra_bed_rate", $this->extra_bed_rate);
        $stmt->bindParam(":extra_person", $this->extra_person);
        $stmt->bindParam(":extra_person_rate", $this->extra_person_rate);


        if ($stmt->execute()) {
            return true;
        }
        return false;
    }

    function getReservationDetail(){
        $query = "SELECT rd.*, r.*,
        rd.booking_no AS reservation_no,
        rd.rate_mode,
        rd.billing_units,
        rd.unit_rate,
        rd.rental_rate AS room_rate,
        rd.extra_driver AS extra_bed,
        rd.extra_driver_rate AS extra_bed_rate,
        rd.extra_passenger AS extra_person,
        rd.extra_passenger_rate AS extra_person_rate,
        r.booking_no AS reservation_no,
        r.vehicle_no AS room_no
        FROM booking_detail rd left join booking r on rd.booking_no=r.booking_no where r.booking_no=:reservation_no and r.vehicle_no=:room_no
        ORDER BY date";

        $stmt = $this->conn->prepare( $query );
        $stmt->bindParam(":reservation_no", $this->reservation_no);
        $stmt->bindParam(":room_no", $this->room_no);
        $stmt->execute();
        return $stmt;
    }

    function DeleteReservationReal(){
		$query = "Delete From " . $this->table_name . " WHERE booking_no=:reservation_no_old";
		$stmt = $this->conn->prepare($query);

		$stmt->bindParam(":reservation_no_old", $this->reservation_no_old);
		if($stmt->execute()){
			return true;
		}
		return false;
	}

    function DeleteReservation(){
        // Step 1: Copy all rows for this reservation_no into booking_detail_delete
        $backupQuery = "INSERT INTO booking_detail_delete 
                        SELECT * FROM booking_detail 
                        WHERE booking_no = :reservation_no";
    
        $backupStmt = $this->conn->prepare($backupQuery);
        $backupStmt->bindParam(":reservation_no", $this->reservation_no);
    
        // Proceed only if backup is successful
        if ($backupStmt->execute()) {
            // Step 2: Delete rows from booking_detail
            $deleteQuery = "DELETE FROM booking_detail 
                            WHERE booking_no = :reservation_no";
    
            $deleteStmt = $this->conn->prepare($deleteQuery);
            $deleteStmt->bindParam(":reservation_no", $this->reservation_no);
    
            if ($deleteStmt->execute()) {
                return true;
            }
        }
    
        return false;
    }


    // function update() {
    //     $query = "UPDATE " . $this->table_name . " 
    //               SET reservation_no=:reservation_no, date=:date, 
    //                   room_no=:room_no, room_rate=:room_rate, 
    //                   extra_bed=:extra_bed, 
    //                   extra_person=:extra_person, total=:total 
    //               WHERE id=:id";
    //     $stmt = $this->conn->prepare($query);

    //     // Bind values
    //     $stmt->bindParam(":id", $this->id);
    //     $stmt->bindParam(":reservation_no", $this->reservation_no);
    //     $stmt->bindParam(":date", $this->date);
    //     $stmt->bindParam(":room_no", $this->room_no);
    //     $stmt->bindParam(":room_rate", $this->room_rate);
    //     $stmt->bindParam(":extra_bed", $this->extra_bed);
    //     $stmt->bindParam(":extra_person", $this->extra_person);

    //     if ($stmt->execute()) {
    //         return true;
    //     }
    //     return false;
    // }



    // Delete a reservation detail
    // function deleteFromReservation() {
    //     $query = "Update " . $this->table_name . " set is_deleted=1 WHERE reservation_no = :reservation_no and room_no=:room_no";
    //     $stmt = $this->conn->prepare($query);
    //     $stmt->bindParam(":reservation_no", $this->reservation_no);
    //     $stmt->bindParam(":room_no", $this->room_no);
    //     if ($stmt->execute()) {
    //         return true;
    //     }
    //     return false;
    // }

    // function deleteFromReservation() {
    //     $query = "DELETE FROM " . $this->table_name . " WHERE reservation_no = :reservation_no and room_no=:room_no";
    //     $stmt = $this->conn->prepare($query);
    //     $stmt->bindParam(":reservation_no", $this->reservation_no);
    //     $stmt->bindParam(":room_no", $this->room_no);
    //     if ($stmt->execute()) {
    //         return true;
    //     }
    //     return false;
    // }

    // // Get a single reservation detail by ID
    // function getOneRow() {
    //     $query = "SELECT * FROM " . $this->table_name . " WHERE id = ?";
    //     $stmt = $this->conn->prepare($query);
    //     $stmt->bindParam(1, $this->id);
    //     $stmt->execute();
    //     $row = $stmt->fetch(PDO::FETCH_ASSOC);
    //     if ($row) {
    //         $this->id = $row['id'];
    //         $this->reservation_no = $row['reservation_no'];
    //         $this->date = $row['date'];
    //         $this->room_type = $row['room_type'];
    //         $this->bed_type = $row['bed_type'];
    //         $this->room_no = $row['room_no'];
    //         $this->room_rate = $row['room_rate'];
    //         $this->extra_bed = $row['extra_bed'];
    //         $this->extra_person = $row['extra_person'];
    //     }
    // }

    // // Get all reservation details for a specific reservation
    // function getAllRowsByReservation($reservation_no) {
    //     $query = "SELECT * FROM " . $this->table_name . " WHERE reservation_no = ? ORDER BY date";
    //     $stmt = $this->conn->prepare($query);
    //     $stmt->bindParam(1, $reservation_no);
    //     $stmt->execute();
    //     return $stmt;
    // }

    // // Check if a reservation detail exists by reservation ID and date
    // function exist() {
    //     $query = "SELECT * FROM " . $this->table_name . " WHERE reservation_no = ? AND date = ? LIMIT 0,1";
    //     $stmt = $this->conn->prepare($query);
    //     $stmt->bindParam(1, $this->reservation_no);
    //     $stmt->bindParam(2, $this->date);
    //     $stmt->execute();
    //     if ($stmt->rowCount() > 0) {
    //         return true;
    //     }
    //     return false;
    // }





    // function updateFromReserveCheckIn(){
	// 	$query = "update " . $this->table_name . " set checkin_id=:checkin_id WHERE reservation_no=:reservation_no and room_no=:room_no";
	// 	$stmt = $this->conn->prepare($query);
	// 	$stmt->bindParam(":checkin_id", $this->checkin_id);
	// 	$stmt->bindParam(":reservation_no", $this->reservation_no);
	// 	$stmt->bindParam(":room_no", $this->room_no);
	// 	if($stmt->execute()){
	// 		return true;
	// 	}
	// 	return false;
	// }



}
?>
