<?php
require '../config/db.php';

$table = strtolower($_POST['dept']).'_employee_attendance';
$emp_id = $_POST['emp'];
$date   = $_POST['date'];
$leaveStatus = 'Leave';
$leaveRequestId = $_POST['id'];

// Use INSERT ... ON DUPLICATE KEY UPDATE
$pdo->prepare("
    INSERT INTO $table (employee_id, att_date, status)
    VALUES (?, ?, ?)
    ON DUPLICATE KEY UPDATE status=VALUES(status)
")->execute([$emp_id, $date, $leaveStatus]);

// Update leave request status
$pdo->prepare("
    UPDATE employee_leave_requests
    SET status='HRApproved'
    WHERE id=?
")->execute([$leaveRequestId]);

header("Location: leave_approvals.php");

