<?php 
require_once '../system/conn.php';
    $cnt = 1;
    $date = date('Y-m', strtotime(date('Y-m') . " -1 month"));
    $date = explode('-', $date);
    $month = $date[1];
    $year = $date[0];

    $stmt1 = $conn->prepare("SELECT emp_details.`e_id`, emp_details.`branch`, emp_details.`e_doj` FROM `emp_details` INNER JOIN `emp_salary` ON emp_details.`e_id` = emp_salary.`emp_id` AND emp_details.`branch` = emp_salary.`branch` WHERE emp_details.`status` = '1' AND emp_salary.`month` = '$month' AND emp_salary.`year` = '$year'");
    $stmt1->execute();
    $sql = $stmt1->fetchAll();
    foreach ($sql as $row2) { 
         // echo "kkk"; // Debugging statement, remove it after testing
        $e_id = $row2['e_id']; 
        $branch = $row2['branch']; 
        $query = "UPDATE `emp_details` SET status = '1' WHERE branch = '$branch' AND e_id = '$e_id' AND status = '0'";
        $statement = $conn->prepare($query);
        $result = $statement->execute();
        if ($result) {
            // echo "<script>alert('Employee updated successfully.')</script>";
            echo "<script>window.location.href = 'mod-left.php'</script>";
        } else {
            echo "Not updated";
        }
        $cnt = $cnt + 1;
    }
?>
