<?php
// POST /api/employees/salary_add.php
require_once __DIR__ . '/../config/db.php';
require_once __DIR__ . '/../config/auth.php';

$user = requireRole([1, 5]);
$db   = getDB();

$employeeId  = (int)post('employee_id', 0);
$effectiveDate = post('effective_date', date('Y-m-d'));
$basicSalary = (float)post('basic_salary', 0);

if (!$employeeId) apiError('Employee ID required.', 422);
if ($basicSalary <= 0) apiError('Salary must be greater than 0.', 422);

$db->prepare("
    INSERT INTO employee_salaries (employee_id, effective_date, salary_type, basic_salary, created_by)
    VALUES (?, ?, ?, ?, ?)
")->execute([$employeeId, $effectiveDate, post('salary_type', 'monthly'), $basicSalary, $user['id']]);

apiSuccess(['id' => (int)$db->lastInsertId()], 'Salary record added.');
