<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

include('admin_user_pass.php');

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die(json_encode(["status" => "error", "message" => "Connection failed: " . $conn->connect_error]));
}

if (!isset($_GET['button_type'])) {
    die(json_encode(["status" => "error", "message" => "button_type is missing"]));
}

$button_type = $_GET['button_type'];

// تسجيل النقر في جدول النقرات الكاملة
$sql_insert = "INSERT INTO clicks (button_type, click_time) VALUES ('$button_type', NOW())";
if (!$conn->query($sql_insert)) {
    die(json_encode(["status" => "error", "message" => "Insert failed: " . $conn->error]));
}

// تحديث النقرات اليومية
$sql_update = "
    INSERT INTO daily_clicks (button_type, click_date, click_count)
    VALUES ('$button_type', CURDATE(), 1)
    ON DUPLICATE KEY UPDATE click_count = click_count + 1;
";
if (!$conn->query($sql_update)) {
    die(json_encode(["status" => "error", "message" => "Update failed: " . $conn->error]));
}

echo json_encode(["status" => "success"]);

$conn->close();
?>
