<?php
// تنظیمات پایه
header('Content-Type: text/html; charset=utf-8');
session_start();

// مسیر فایل ذخیره پیام‌ها (فایل متنی)
$filePath = 'messages.txt';

// رمز عبور
$password = "1386";

// بررسی ورود کاربر
if (!isset($_SESSION['username'])) {
    if (isset($_POST['login'])) {
        $username = $_POST['username'];
        $userpassword = $_POST['password'];
        if ($userpassword == $password) {
            $_SESSION['username'] = $username;
        } else {
            echo "رمز عبور اشتباه است.";
        }
    } else {
        ?>
        <form method="post" style="width: 30%; margin: 40px auto; padding: 20px; border: 1px solid #ccc; border-radius: 10px; box-shadow: 0 0 10px rgba(0,0,0,0.1);">
            <input type="text" name="username" placeholder="نام کاربری" style="width: 100%; padding: 10px; margin-bottom: 10px; border: 1px solid #ccc;">
            <input type="password" name="password" placeholder="رمز عبور" style="width: 100%; padding: 10px; margin-bottom: 10px; border: 1px solid #ccc;">
            <input type="submit" name="login" value="ورود" style="width: 100%; padding: 10px; background-color: #4CAF50; color: #fff; border: none; border-radius: 5px; cursor: pointer;">
        </form>
        <?php
        exit;
    }
}

// تاریخ و ساعت
function getShamsiDate($date) {
    // تبدیل تاریخ میلادی به شمسی
    $jDate = new JalaliDateTime($date);
    return $jDate->format('Y-m-d H:i');
}

class JalaliDateTime {
    private $date;

    public function __construct($date) {
        $this->date = $date;
    }

    public function format($format) {
        // پیاده‌سازی تبدیل تاریخ
        // برای سادگی، از یک تقریب استفاده شده است
        $timestamp = strtotime($this->date);
        $jalaliDate = gregorian_to_jalali(date('Y', $timestamp), date('m', $timestamp), date('d', $timestamp));
        $year = $jalaliDate[0];
        $month = $jalaliDate[1];
        $day = $jalaliDate[2];
        $hour = date('H', $timestamp);
        $minute = date('i', $timestamp);

        if ($format == 'Y-m-d H:i') {
            return "$year-$month-$day $hour:$minute";
        }
    }
}

function gregorian_to_jalali($gy, $gm, $gd) {
    // تبدیل تاریخ میلادی به شمسی
    // فرمول دقیق‌تر مورد نیاز است
    $gy -= 1600;
    $gm -= 1;
    $g_d_m = array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    if ($gy % 4 == 0 && ($gy % 100 != 0 || $gy % 400 == 0))
        $g_d_m[2] = 29;
    $g_a = array(0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335);
    $j_d_m = array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    $j_g = array(0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4);
    $jd = $gd + $g_a[$gm] + ($gy - 1) * 365 + floor(($gy - 1) / 4) - floor(($gy - 1) / 100) + floor(($gy - 1) / 400);
    $jm = $j_g[($gm + 10) % 12];
    $jy = $gy + 979;
    if ($jd > 79) {
        $cy = 19;
        $jy -= 1600;
        $jm -= $j_g[($gm + 10) % 12];
    } else {
        $cy = 0;
    }
    $jd -= 79;
    $jmonth = 1 + floor($jd / 30);
    $jday = 1 + $jd % 30;
    if ($jmonth > 12) {
        $jmonth -= 12;
        $jy += 1;
    }
    return array($jy, $jmonth, $jday);
}

// خواندن پیام‌ها
$messages = array();
if (file_exists($filePath)) {
    $lines = file($filePath, FILE_IGNORE_NEW_LINES);
    foreach ($lines as $line) {
        list($username, $message, $date) = explode("|", $line);
        $messages[] = array('username' => $username, 'message' => $message, 'date' => $date);
    }
}

// ارسال پیام
if (isset($_POST['message'])) {
    $message = $_POST['message'];
    $username = $_SESSION['username'];
    $date = date('Y-m-d H:i');
    $shamsiDate = getShamsiDate($date);
    file_put_contents($filePath, "$username|$message|$shamsiDate\n", FILE_APPEND);
    header('Location: ' . $_SERVER['PHP_SELF']);
    exit;
}


// بعد از ارسال پیام، صفحه را به آخرین پیام برسانید
echo "<script>window.scrollTo(0, document.body.scrollHeight);</script>";
?>
<!DOCTYPE html>
<html>
<head>
    <title>چت‌روم ساده</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
        }
        .message-box {
            padding: 10px;
            border-radius: 10px;
            width: 80%;
            margin: 10px;
            clear: both;
        }
        .message-box.left {
            background-color: #fff;
            margin-left: 10%;
            float: left;
        }
        .message-box.right {
            background-color: #c6efd5;
            margin-right: 10%;
            float: right;
        }
        .form {
            width: 80%;
            margin: 20px auto;
        }
        input[type="text"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ccc;
            border-radius: 10px;
        }
        input[type="submit"] {
            width: 100%;
            padding: 10px;
            background-color: #4CAF50;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
    </style>
</head>
<body>
<div style="width: 80%; margin: 40px auto;">
    <?php foreach ($messages as $msg) { ?>
        <div class="message-box <?php echo $msg['username'] == $_SESSION['username'] ? 'right' : 'left'; ?>">
            <b><?php echo $msg['username']; ?></b> - <?php echo $msg['date']; ?>
            <br>
            <?php echo $msg['message']; ?>
        </div>
    <?php } ?>
    <div class="form">
        <form method="post">
            <input type="text" name="message" placeholder="پیام خود را بنویسید..." required style="border-radius: 10px;">
            <input type="submit" value="ارسال" style="border-radius: 5px;">
        </form>
    </div>
</div>
</body>
</html>