<?php

require 'app/config.php';
require 'function.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_POST['action']=="saveUsersWithDates") {

     $json = $_POST['users'];
    $users = json_decode($json, true);

 foreach ($users as $user) {
    $id = $user['id'];
    $date = $user['date'];

    $checkStmt = $db->prepare("SELECT COUNT(*) FROM reminder_date WHERE reminder_date = ?");
    $checkStmt->execute([$date]);
    $exists = $checkStmt->fetchColumn();

    if ($exists) {
        $updateStmt = $db->prepare("UPDATE reminder_date SET user_id = ? , status = 'pending'  WHERE reminder_date = ?");
        $updateStmt->execute([$id, $date]);
    } else {
        $insertStmt = $db->prepare("INSERT INTO reminder_date (user_id, reminder_date , status) VALUES (?, ? , 'pending')");
        $insertStmt->execute([$id, $date]);
    }
}
    
    

   

    
}
