<?php
include 'includes/auth.php';
include 'db.php';
include 'includes/header.php';

/* =====================================================
   📧 EMAIL ALERT FUNCTION
===================================================== */
function sendEditStayAlert($guest_name,$old_room,$new_room,$old_days,$new_days,$old_date,$new_date){

    $to = "giantplusmw@gmail.com";
    $subject = "Guest Stay Edited Alert";

    $message = "
    <html>
    <head>
    <style>
    body{font-family:Arial;background:#f4f6f9;padding:20px;}
    .box{background:#fff;padding:20px;border-radius:8px;}
    .title{color:#dc3545;font-size:18px;font-weight:bold;}
    .footer{font-size:12px;color:#777;margin-top:15px;}
    </style>
    </head>
    <body>

    <div class='box'>
    <div class='title'>⚠ Guest Stay Updated</div>

    <p><b>Guest:</b> {$guest_name}</p>
    <p><b>Room:</b> {$old_room} ➜ {$new_room}</p>
    <p><b>Days:</b> {$old_days} ➜ {$new_days}</p>
    <p><b>Check-In:</b> {$old_date} ➜ {$new_date}</p>
    <p><b>Edited By:</b> ".htmlspecialchars($_SESSION['username'] ?? 'System')."</p>
    <p><b>Time:</b> ".date('Y-m-d H:i:s')."</p>

    <div class='footer'>RAMS Executive Lodge System</div>
    </div>

    </body>
    </html>
    ";

    $headers  = "MIME-Version: 1.0\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8\r\n";
    $headers .= "From: cloud@gianterp.space\r\n";

    @mail($to,$subject,$message,$headers);
}

/* =====================================================
   FETCH STAY DETAILS
===================================================== */
$stay_id = intval($_GET['stay_id']);

$stay = $conn->query("
SELECT s.*, g.name, r.room_number
FROM stays s
JOIN guests g ON s.guest_id=g.id
JOIN rooms r ON s.room_id=r.id
WHERE s.id=$stay_id
")->fetch_assoc();

/* =====================================================
   UPDATE STAY
===================================================== */
if(isset($_POST['update'])){

    $room_id = intval($_POST['room_id']);
    $days = intval($_POST['days']);
    $checkin_date = $_POST['checkin_date'];

    // Old values
    $old_room = $stay['room_number'];
    $old_days = $stay['days'];
    $old_date = $stay['checkin_date'];
    $guest_name = $stay['name'];

    // New room number
    $new_room_row = $conn->query("SELECT room_number FROM rooms WHERE id=$room_id")->fetch_assoc();
    $new_room = $new_room_row['room_number'];

    // Update database
    $conn->query("
    UPDATE stays 
    SET room_id='$room_id',
        days='$days',
        checkin_date='$checkin_date'
    WHERE id='$stay_id'
    ");

    // Send email alert
    sendEditStayAlert(
        $guest_name,
        $old_room,
        $new_room,
        $old_days,
        $days,
        $old_date,
        $checkin_date
    );

    echo "<script>
    alert('Stay updated successfully');
    window.location='checkin.php';
    </script>";
}

/* =====================================================
   FETCH ROOMS
===================================================== */
$rooms = $conn->query("
SELECT * FROM rooms 
WHERE status='Available' 
OR id=".$stay['room_id']."
ORDER BY room_number
");
?>

<!-- ===================== CSS ===================== -->
<style>
.page-title{
    font-weight:600;
    margin-bottom:20px;
    color:#333;
}
.card-box{
    border-radius:12px;
    box-shadow:0 4px 15px rgba(0,0,0,0.08);
    border:none;
}
.form-label{
    font-weight:600;
    color:#444;
}
.form-control, select{
    border-radius:8px;
    height:42px;
}
.btn{
    border-radius:8px;
}
.room-icon{
    color:#0d6efd;
    margin-right:6px;
}
</style>

<div class="container mt-4">

<h4 class="page-title">
    <i class="fa fa-edit"></i> Edit Guest Stay
</h4>

<div class="card card-box">
<div class="card-body">

<form method="POST">

<div class="row">

    <!-- Guest -->
    <div class="col-md-6 mb-3">
        <label class="form-label">
            <i class="fa fa-user"></i> Guest
        </label>
        <input class="form-control"
               value="<?= htmlspecialchars($stay['name']) ?>"
               readonly>
    </div>

    <!-- Room -->
    <div class="col-md-6 mb-3">
        <label class="form-label">
            <i class="fa fa-bed"></i> Room
        </label>
        <select name="room_id" class="form-control" required>
        <?php while($r=$rooms->fetch_assoc()): ?>
            <option value="<?= $r['id'] ?>"
            <?= $r['id']==$stay['room_id']?'selected':'' ?>>
            Room <?= $r['room_number'] ?>
            </option>
        <?php endwhile; ?>
        </select>
    </div>

    <!-- Checkin Date -->
    <div class="col-md-6 mb-3">
        <label class="form-label">
            <i class="fa fa-calendar"></i> Check-In Date
        </label>
        <input type="datetime-local"
               name="checkin_date"
               class="form-control"
               value="<?= date('Y-m-d\TH:i', strtotime($stay['checkin_date'])) ?>"
               required>
    </div>

    <!-- Days -->
    <div class="col-md-6 mb-3">
        <label class="form-label">
            <i class="fa fa-clock"></i> Number of Days
        </label>
        <input type="number"
               name="days"
               value="<?= $stay['days'] ?>"
               class="form-control"
               min="1"
               required>
    </div>

</div>

<hr>

<div class="d-flex justify-content-between">
    <a href="checkin.php" class="btn btn-secondary">
        <i class="fa fa-arrow-left"></i> Back
    </a>

    <button name="update" class="btn btn-success">
        <i class="fa fa-save"></i> Update Stay
    </button>
</div>

</form>

</div>
</div>

</div>

<?php include 'includes/footer.php'; ?>