<?php
session_start();
include 'layouts/dbconn.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

$request_id = $_GET['id'] ?? '';

if (empty($request_id)) {
    header('Location: list_maintenance.php');
    exit();
}

// Get request details
$sql_request = "SELECT * FROM maintenance_requests WHERE request_id = ?";
$stmt_request = $conn->prepare($sql_request);
$stmt_request->bind_param("i", $request_id);
$stmt_request->execute();
$result_request = $stmt_request->get_result();
$request = $result_request->fetch_assoc();

if (!$request) {
    header('Location: list_maintenance.php');
    exit();
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $vendor_id = $_POST['vendor_id'] ?? '';
    $scheduled_date = $_POST['scheduled_date'] ?? null;
    $notes = $_POST['notes'] ?? '';
    
    if (empty($vendor_id)) {
        $error_message = "Please select a vendor";
    } else {
        $sql = "UPDATE maintenance_requests 
                SET assigned_vendor_id = ?, scheduled_date = ?, status = 'Assigned', notes = ?
                WHERE request_id = ?";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param("issi", $vendor_id, $scheduled_date, $notes, $request_id);
        
        if ($stmt->execute()) {
            $_SESSION['success_message'] = "Vendor assigned successfully!";
            header('Location: maintenance_request_details.php?id=' . $request_id);
            exit();
        } else {
            $error_message = "Error: " . $conn->error;
        }
    }
}

// Get available vendors
$sql_vendors = "SELECT * FROM vendors WHERE status = 'active' ORDER BY company_name";
$result_vendors = $conn->query($sql_vendors);
?>
<?php include 'layouts/main.php'; ?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Assign Vendor')); ?>
    <?php include 'layouts/head-css.php'; ?>
</head>

<body>
    <div id="layout-wrapper">
        <?php include 'layouts/menu.php'; ?>

        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <?php includeFileWithVariables('layouts/page-title.php', array('pagetitle' => 'Maintenance', 'title' => 'Assign Vendor')); ?>

                    <div class="row mb-3">
                        <div class="col-12">
                            <a href="maintenance_request_details.php?id=<?php echo $request_id; ?>" class="btn btn-light">
                                <i class="ri-arrow-left-line me-1"></i>Back to Request
                            </a>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-8">
                            <div class="card">
                                <div class="card-header">
                                    <h4 class="card-title mb-0">Assign Vendor to Request</h4>
                                </div>
                                <div class="card-body">
                                    <?php if (isset($error_message)): ?>
                                        <div class="alert alert-danger alert-dismissible fade show" role="alert">
                                            <?php echo htmlspecialchars($error_message); ?>
                                            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                                        </div>
                                    <?php endif; ?>

                                    <form method="POST">
                                        <div class="mb-3">
                                            <label class="form-label">Vendor <span class="text-danger">*</span></label>
                                            <select class="form-select" name="vendor_id" required>
                                                <option value="">Select Vendor</option>
                                                <?php while($vendor = $result_vendors->fetch_assoc()): ?>
                                                    <option value="<?php echo $vendor['id']; ?>">
                                                        <?php echo htmlspecialchars($vendor['company_name']); ?> 
                                                        (<?php echo htmlspecialchars($vendor['specialization'] ?? 'General'); ?>)
                                                    </option>
                                                <?php endwhile; ?>
                                            </select>
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label">Scheduled Date</label>
                                            <input type="datetime-local" class="form-control" name="scheduled_date">
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label">Notes</label>
                                            <textarea class="form-control" name="notes" rows="3"></textarea>
                                        </div>
                                        <div class="mt-4">
                                            <button type="submit" class="btn btn-primary">
                                                <i class="ri-user-add-line align-bottom me-1"></i>Assign Vendor
                                            </button>
                                            <a href="maintenance_request_details.php?id=<?php echo $request_id; ?>" class="btn btn-light ms-2">
                                                <i class="ri-close-line align-bottom me-1"></i>Cancel
                                            </a>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>

    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>
