<?php
require_once '../auth.php';
ensureAdminAjaxAuth();
// ajax/get_edit_form.php
require_once '../../config/database.php';

header('Content-Type: application/json');

if(!isset($_GET['id'])) {
    echo json_encode(['success' => false, 'message' => 'Request ID required']);
    exit;
}

$id = intval($_GET['id']);

try {
    $database = new Database();
    $db = $database->getConnection();
    
    // Get request details
    $query = "SELECT * FROM service_requests WHERE id = :id";
    $stmt = $db->prepare($query);
    $stmt->execute([':id' => $id]);
    $request = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if(!$request) {
        echo json_encode(['success' => false, 'message' => 'Request not found']);
        exit;
    }
    
    // Get dropdown data
    $statuses = $db->query("SELECT * FROM job_statuses ORDER BY id")->fetchAll(PDO::FETCH_ASSOC);
    $brands = $db->query("SELECT * FROM brands ORDER BY name")->fetchAll(PDO::FETCH_ASSOC);
    $appliance_types = $db->query("SELECT * FROM appliance_types ORDER BY sort_order")->fetchAll(PDO::FETCH_ASSOC);
    $urgency_levels = $db->query("SELECT * FROM urgency_levels")->fetchAll(PDO::FETCH_ASSOC);
    
    ob_start();
    ?>
    
    <input type="hidden" name="request_id" value="<?php echo $id; ?>">
    
    <div class="row">
        <div class="col-md-6 mb-3">
            <label class="form-label">Status</label>
            <select name="status_id" class="form-select status-selector" required>
                <?php foreach($statuses as $status): ?>
                <option value="<?php echo $status['id']; ?>" 
                        style="color: <?php echo $status['color_code']; ?>;"
                        <?php echo $status['id'] == $request['job_status_id'] ? 'selected' : ''; ?>>
                    <?php echo $status['status_name']; ?>
                </option>
                <?php endforeach; ?>
            </select>
        </div>
        
        <div class="col-md-6 mb-3">
            <label class="form-label">Urgency Level</label>
            <select name="urgency_id" class="form-select">
                <?php foreach($urgency_levels as $level): ?>
                <option value="<?php echo $level['id']; ?>" 
                        <?php echo $level['id'] == $request['urgency_level_id'] ? 'selected' : ''; ?>>
                    <?php echo $level['name'] . ' - ' . $level['time_frame']; ?>
                </option>
                <?php endforeach; ?>
            </select>
        </div>
    </div>
    
    <div class="row">
        <div class="col-md-6 mb-3">
            <label class="form-label">Brand</label>
            <select name="brand_id" class="form-select">
                <?php foreach($brands as $brand): ?>
                <option value="<?php echo $brand['id']; ?>" 
                        <?php echo $brand['id'] == $request['brand_id'] ? 'selected' : ''; ?>>
                    <?php echo $brand['name']; ?>
                </option>
                <?php endforeach; ?>
            </select>
        </div>
        
        <div class="col-md-6 mb-3">
            <label class="form-label">Appliance Type</label>
            <select name="appliance_id" class="form-select">
                <?php foreach($appliance_types as $type): ?>
                <option value="<?php echo $type['id']; ?>" 
                        <?php echo $type['id'] == $request['appliance_type_id'] ? 'selected' : ''; ?>>
                    <?php echo $type['name']; ?>
                </option>
                <?php endforeach; ?>
            </select>
        </div>
    </div>
    
    <div class="row">
        <div class="col-md-6 mb-3">
            <label class="form-label">Model Number</label>
            <input type="text" name="model_number" class="form-control" 
                   value="<?php echo htmlspecialchars($request['model_number']); ?>">
        </div>
        
        <div class="col-md-6 mb-3">
            <label class="form-label">Serial Number</label>
            <input type="text" name="serial_number" class="form-control" 
                   value="<?php echo htmlspecialchars($request['serial_number']); ?>">
        </div>
    </div>
    
    <div class="mb-3">
        <label class="form-label">Customer Name</label>
        <input type="text" name="full_name" class="form-control" 
               value="<?php echo htmlspecialchars($request['full_name']); ?>" required>
    </div>
    
    <div class="mb-3">
        <label class="form-label">Phone Number</label>
        <input type="text" name="phone_number" class="form-control" 
               value="<?php echo htmlspecialchars($request['phone_number']); ?>" required>
    </div>
    
    <div class="mb-3">
        <label class="form-label">Service Address</label>
        <textarea name="service_address" class="form-control" rows="2" required><?php 
            echo htmlspecialchars($request['service_address']); 
        ?></textarea>
    </div>
    
    <div class="mb-3">
        <label class="form-label">Issue Description</label>
        <textarea name="issue_description" class="form-control" rows="3" required><?php 
            echo htmlspecialchars($request['issue_description']); 
        ?></textarea>
    </div>
    
    <div class="mb-3">
        <label class="form-label">Update Notes <small class="text-muted">(optional)</small></label>
        <textarea name="update_notes" class="form-control" rows="2" 
                  placeholder="Add notes about this update..."></textarea>
    </div>
    
    <?php
    $html = ob_get_clean();
    
    echo json_encode([
        'success' => true,
        'html' => $html,
        'request' => $request
    ]);
    
} catch(Exception $e) {
    echo json_encode(['success' => false, 'message' => 'Error: ' . $e->getMessage()]);
}
?>