<?php
// test-carbon.php - تست Carbon (بدون تغییر در فایل‌های اصلی)

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

use Carbon\Carbon;

function convertToJalali($date) {
    if (empty($date)) return '-';
    try {
        $carbon = Carbon::parse($date);
        $year = $carbon->year;
        $month = $carbon->month;
        $day = $carbon->day;
        
        // الگوریتم تبدیل میلادی به شمسی
        $g_d_m = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
        $j_d_m = [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;
        
        return "$jy/$jm/$jd";
    } catch (Exception $e) {
        return $date;
    }
}

echo "<h2>🧪 تست Carbon</h2>";
$tests = ['2025-09-23', '2025-06-11', '2026-07-20'];
echo "<table border='1' cellpadding='8' dir='rtl'>";
echo "<tr><th>ورودی</th><th>خروجی</th></tr>";
foreach ($tests as $test) {
    $result = convertToJalali($test);
    echo "<tr><td>$test</td><td>$result</td></tr>";
}
echo "</table>";
?>