<?php
$filename = 'channel.txt';
if (file_exists($filename)) $lines = file($filename, FILE_IGNORE_NEW_LINES);
else $lines = [];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_POST['edit_line']) && isset($_POST['new_content'])) {
        $lineIndex = intval($_POST['edit_line']);
        $newContent = $_POST['new_content'];
        if (isset($lines[$lineIndex])) $lines[$lineIndex] = $newContent;
    }

    if (isset($_POST['delete_line'])) {
        $lineIndex = intval($_POST['delete_line']);
        if (isset($lines[$lineIndex])) {
            unset($lines[$lineIndex]);
            $lines = array_values($lines);
        }
    }

    if (isset($_POST['new_id']) && isset($_POST['new_link'])) {
        $newId = trim($_POST['new_id']);
        $newLink = trim($_POST['new_link']);
        if ($newId !== '' && $newLink !== '') {
            $newLine = $newId . '|' . $newLink;
            $lines[] = $newLine;
        }
    }

    file_put_contents($filename, implode(PHP_EOL, $lines));
    header("Location: " . $_SERVER['PHP_SELF']);
    exit;
}
?>
<!DOCTYPE html>
<html lang="fa">
<head>
    <meta charset="UTF-8">
    <title>ویرایش چنل‌ها</title>
    <style>
        body {
            font-family: 'Vazir', sans-serif;
            background-color: #f9f9f9;
            color: #333;
            margin: 0;
            padding: 0;
        }

        h1 {
            text-align: center;
            background-color: #4CAF50;
            color: white;
            padding: 10px 0;
            margin: 0 0 20px;
        }

        ul {
            list-style: none;
            padding: 0;
            margin: 0;
        }

        li {
            margin: 10px 0;
            padding: 10px;
            background-color: #fff;
            border: 1px solid #ddd;
            border-radius: 5px;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }

        input[type="text"] {
            flex: 1;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 5px;
            margin-right: 10px;
        }

        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            padding: 8px 12px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        button:hover {
            background-color: #45a049;
        }

        form {
            display: flex;
            align-items: center;
            margin: 0;
        }

        .add-form {
            flex-direction: column;
            align-items: flex-start;
            width: 90%;
            max-width: 600px;
            margin: 20px auto;
            background-color: #fff;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }

        .add-form input[type="text"] {
            width: 100%;
            margin: 5px 0;
        }

        .add-form button {
            align-self: flex-end;
        }
    </style>
</head>
<body>
    <h1>ویرایش چنل ها</h1>

    <ul>
        <?php foreach ($lines as $index => $line): ?>
            <li>
                <form method="post" style="display: inline; flex:1;">
                    <input type="hidden" name="edit_line" value="<?= $index ?>">
                    <input type="text" name="new_content" value="<?= htmlspecialchars($line) ?>">
                    <button type="submit">ویرایش</button>
                </form>
                <form method="post" style="display: inline;">
                    <input type="hidden" name="delete_line" value="<?= $index ?>">
                    <button type="submit">حذف</button>
                </form>
            </li>
        <?php endforeach; ?>
    </ul>

    <form method="post" class="add-form">
        <label>آیدی عددی کانال (مثل -1001234567890):</label>
        <input type="text" name="new_id" placeholder="آیدی عددی">
        <label>لینک کانال (مثل https://t.me/example):</label>
        <input type="text" name="new_link" placeholder="لینک کانال">
        <button type="submit">اضافه کردن</button>
    </form>
</body>
</html>
