<?php
require_once '../auth.php';
session_start();

if (!isset($_SESSION['user_id']) || ($_SESSION['role'] ?? '') !== 'admin') {
    die("Access Denied");
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $member_id = intval($_POST['member_id']);
    $user_id = intval($_SESSION['user_id']);
    $call_status = trim($_POST['call_status']);
    $call_note = trim($_POST['call_note']);
    
    // ডেট সিলেক্ট না করলে null পাঠাবো
    $next_followup_date = !empty($_POST['next_followup_date']) ? $_POST['next_followup_date'] : NULL;

    if ($member_id > 0 && !empty($call_status)) {
        $stmt = $conn->prepare("INSERT INTO agh_call_logs (member_id, user_id, call_status, call_note, next_followup_date) VALUES (?, ?, ?, ?, ?)");
        $stmt->bind_param("iisss", $member_id, $user_id, $call_status, $call_note, $next_followup_date);
        
        if ($stmt->execute()) {
            // সেভ হয়ে গেলে আগের পেজে ফিরে যাবে
            header("Location: follow_up.php?success=1");
            exit();
        } else {
            echo "Error saving call log.";
        }
        $stmt->close();
    }
}
?>