<?php
// test-date2.php - تست دقیق تاریخ

require_once 'config/database.php';
require_once 'includes/db.php';

// ===== تعریف تابع jdate (الگوریتم استاندارد) =====
function jdate2($format, $timestamp) {
    if (empty($timestamp) || $timestamp < 0) {
        return '-';
    }
    $date = getdate($timestamp);
    $year = $date['year'];
    $month = $date['mon'];
    $day = $date['mday'];
    
    $g_d_m = array(0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334);
    $j_d_m = array(0, 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30);
    
    $gy2 = ($month > 2) ? ($year + 1) : $year;
    $days = 355666 + (365 * $year) + (int)(($gy2 + 3) / 4) - (int)(($gy2 + 99) / 100) + (int)(($gy2 + 399) / 400) + $day + $g_d_m[$month - 1];
    $days = $days - 226899;
    $jz = (int)($days / 12053);
    $days %= 12053;
    $jy = (int)(($days - 1) / 365) + (1 + $jz * 4);
    $days = ($days - 1) % 365;
    $jm = 1;
    for ($i = 0; $i < 12; $i++) {
        if ($days < $j_d_m[$i]) break;
        $days -= $j_d_m[$i];
        $jm++;
    }
    $jd = $days + 1;
    
    $output = '';
    for ($i = 0; $i < strlen($format); $i++) {
        $char = $format[$i];
        if ($char == 'Y') $output .= $jy;
        elseif ($char == 'y') $output .= substr($jy, -2);
        elseif ($char == 'm') $output .= str_pad($jm, 2, '0', STR_PAD_LEFT);
        elseif ($char == 'n') $output .= $jm;
        elseif ($char == 'd') $output .= str_pad($jd, 2, '0', STR_PAD_LEFT);
        elseif ($char == 'j') $output .= $jd;
        elseif ($char == 'H') $output .= date('H', $timestamp);
        elseif ($char == 'i') $output .= date('i', $timestamp);
        elseif ($char == 's') $output .= date('s', $timestamp);
        else $output .= $char;
    }
    return $output;
}

// ===== تست 1: با strtotime =====
echo "<h2>🧪 تست 1: strtotime</h2>";
$date1 = '2025-09-23';
$ts1 = strtotime($date1);
echo "ورودی: $date1<br>";
echo "strtotime: " . ($ts1 ? $ts1 : 'false') . "<br>";
if ($ts1) {
    echo "jdate2: " . jdate2('Y/m/d', $ts1) . "<br>";
}

// ===== تست 2: با mktime =====
echo "<h2>🧪 تست 2: mktime</h2>";
$parts = explode('-', '2025-09-23');
$year = (int)$parts[0];
$month = (int)$parts[1];
$day = (int)$parts[2];
$ts2 = mktime(0, 0, 0, $month, $day, $year);
echo "ورودی: $year-$month-$day<br>";
echo "mktime: " . ($ts2 ? $ts2 : 'false') . "<br>";
if ($ts2) {
    echo "jdate2: " . jdate2('Y/m/d', $ts2) . "<br>";
}

// ===== تست 3: از دیتابیس =====
echo "<h2>🧪 تست 3: از دیتابیس</h2>";
$users = fetchAll("SELECT id, name, family, last_login FROM users WHERE last_login IS NOT NULL LIMIT 3");
echo "<table border='1' cellpadding='5' dir='rtl'>";
echo "<tr><th>نام</th><th>last_login</th><th>strtotime</th><th>jdate2</th></tr>";
foreach ($users as $user) {
    $ts = strtotime($user['last_login']);
    $result = $ts ? jdate2('Y/m/d', $ts) : 'خطا';
    echo "<tr>";
    echo "<td>" . htmlspecialchars($user['name']) . "</td>";
    echo "<td>" . htmlspecialchars($user['last_login']) . "</td>";
    echo "<td>" . ($ts ? $ts : 'false') . "</td>";
    echo "<td><strong>$result</strong></td>";
    echo "</tr>";
}
echo "</table>";

// ===== تست 4: نمایش تابع jalaliDate فعلی =====
echo "<h2>🧪 تست 4: تابع jalaliDate فعلی</h2>";
if (function_exists('jalaliDate')) {
    echo "تابع jalaliDate وجود دارد.<br>";
    $test = '2025-09-23';
    $result = jalaliDate($test);
    echo "jalaliDate('$test') = <strong>$result</strong><br>";
} else {
    echo "تابع jalaliDate وجود ندارد!";
}
?>