<?php
/**
 * AJAX Endpoint: Get Employee Details by ID
 * Returns full employee details for auto-filling form
 */

require_once __DIR__ . '/../../includes/init.php';
requireLogin();

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

$siteId = getCurrentSiteId();
$employeeId = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($employeeId <= 0) {
    http_response_code(400);
    echo json_encode([
        'success' => false,
        'error' => 'Invalid employee ID'
    ]);
    exit;
}

try {
    $db = getDB();
    
    // Build query with HR-level filtering
    $whereConditions = ["e.employee_id = ?", "e.site_id = ?"];
    $params = [$employeeId, $siteId];
    
    // Apply HR-level filtering
    if (isHRAdmin()) {
        $whereConditions[] = "e.created_by = ?";
        $params[] = getCurrentUserId();
    }
    
    $whereClause = implode(' AND ', $whereConditions);
    
    // Fetch employee details
    $stmt = $db->prepare("
        SELECT 
            e.employee_id,
            e.employee_code,
            e.first_name,
            e.middle_name,
            e.last_name,
            CONCAT(e.first_name, ' ', IFNULL(e.middle_name, ''), ' ', e.last_name) as full_name,
            d.designation_name,
            s.site_name,
            e.address_line1,
            e.address_line2,
            e.bank_name,
            e.bank_account_number,
            e.bank_ifsc,
            e.city,
            e.state,
            e.postal_code,
            e.country,
            e.employee_category
        FROM employees e
        LEFT JOIN designations d ON e.designation_id = d.designation_id
        LEFT JOIN sites s ON e.site_id = s.site_id
        WHERE {$whereClause}
        LIMIT 1
    ");
    $stmt->execute($params);
    $employee = $stmt->fetch();
    
    if (!$employee) {
        http_response_code(404);
        echo json_encode([
            'success' => false,
            'error' => 'Employee not found or access denied'
        ]);
        exit;
    }
    
    // Format response
    $result = [
        'success' => true,
        'employee' => [
            'id' => $employee['employee_id'],
            'code' => $employee['employee_code'],
            'name' => $employee['full_name'],
            'first_name' => $employee['first_name'],
            'middle_name' => $employee['middle_name'] ?? '',
            'last_name' => $employee['last_name'],
            'designation' => $employee['designation_name'] ?? '',
            'site_name' => $employee['site_name'] ?? '',
            'present_address' => $employee['address_line1'] ?? '',
            'permanent_address' => $employee['address_line2'] ?? '',
            'bank_name' => $employee['bank_name'] ?? '',
            'bank_account_number' => $employee['bank_account_number'] ?? '',
            'bank_ifsc' => $employee['bank_ifsc'] ?? '',
            'city' => $employee['city'] ?? '',
            'state' => $employee['state'] ?? '',
            'postal_code' => $employee['postal_code'] ?? '',
            'country' => $employee['country'] ?? '',
            'category' => $employee['employee_category'] ?? ''
        ]
    ];
    
    echo json_encode($result);
    
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode([
        'success' => false,
        'error' => $e->getMessage()
    ]);
}
