<?php
/**
 * Employee Salary Info API
 * Returns employee's basic salary structure
 */

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

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

$employeeId = $_GET['employee_id'] ?? null;

if (!$employeeId) {
    echo json_encode(['success' => false, 'message' => 'Employee ID required']);
    exit;
}

$db = getDB();

try {
    // Fetch from employee_master_sheet or employees table
    $stmt = $db->prepare("
        SELECT 
            e.employee_id,
            ems.basic_salary,
            ems.hra,
            ems.other_allowance,
            ems.special_allowance,
            d.designation_name
        FROM employees e
        LEFT JOIN employee_master_sheet ems ON e.employee_id = ems.employee_id
        LEFT JOIN designations d ON e.designation_id = d.designation_id
        WHERE e.employee_id = ?
    ");
    
    $stmt->execute([$employeeId]);
    $employee = $stmt->fetch();
    
    if ($employee) {
        echo json_encode([
            'success' => true,
            'basic' => $employee['basic_salary'] ?? 15000,
            'hra' => $employee['hra'] ?? 0,
            'other_allowance' => $employee['other_allowance'] ?? 3000,
            'special_allowance' => $employee['special_allowance'] ?? 0,
            'designation' => $employee['designation_name'] ?? ''
        ]);
    } else {
        echo json_encode([
            'success' => false,
            'message' => 'Employee not found'
        ]);
    }
    
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => $e->getMessage()
    ]);
}
