{% if session['theme'] == 'darkmode' %}
    {% extends "base_dark.html" %}
{% else %}
    {% extends "base.html" %}
{% endif %}

{% block title %}Discord Bots{% endblock %}

{% block content %}
{% if session['theme'] == 'darkmode' %}
<style>
    .bots-outer {
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 100%;
    }
    .bots-container {
        background: rgba(20, 20, 20, 0.95);
        padding: 30px;
        border-radius: 12px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.7);
        max-width: 1100px;
        width: 100%;
        min-height: 300px;
        max-height: 90vh;
        overflow-y: auto;
        text-align: center;
    }
    .button-container {
        display: flex;
        justify-content: center;
        width: 100%;
        margin-bottom: 20px;
        gap: 16px;
    }
    .button-container form {
        margin: 0;
    }
    .button-container .neon-button {
        min-width: 150px;
        margin: 0 6px;
    }
    .add-bot-button {
        min-width: 170px;
        margin: 0 6px;
    }
    .bot-container {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        gap: 20px;
        padding: 20px 0;
    }
    .bot-box {
        background: rgba(40, 40, 40, 0.9);
        padding: 20px;
        border-radius: 8px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.4);
        width: 340px;
        text-align: center;
        position: relative;
        margin-bottom: 16px;
        display: flex;
        flex-direction: column;
        align-items: center;
    }
    .bot-box h3 {
        margin-bottom: 18px;
        color: #00fff7;
    }
    .bot-box form {
        margin: 0 0 8px 0;
        width: 100%;
    }
    .bot-box button {
        width: 100%;
        margin: 4px 0;
        padding: 10px 0;
        border-radius: 4px;
        font-weight: bold;
    }
    .bot-box .turn-on {
        background: none;
        color: #39ff14;
        border: 2px solid #39ff14;
        box-shadow: 0 0 8px #39ff14, 0 0 14px #39ff14 inset;
        transition: 0.3s;
    }
    .bot-box .turn-on:hover {
        background: #39ff14;
        color: #111;
    }
    .bot-box .turn-off {
        background: none;
        color: #ff4d4d;
        border: 2px solid #ff4d4d;
        box-shadow: 0 0 8px #ff4d4d, 0 0 14px #ff4d4d inset;
        transition: 0.3s;
    }
    .bot-box .turn-off:hover {
        background: #ff4d4d;
        color: #111;
    }
    .bot-box .logs {
        background: none;
        color: #00fff7;
        border: 2px solid #00fff7;
        box-shadow: 0 0 8px #00fff7, 0 0 14px #00fff7 inset;
        transition: 0.3s;
    }
    .bot-box .logs:hover {
        background: #00fff7;
        color: #111;
    }
    .add-bot-button {
        background: none;
        color: #ff00ff;
        border: 2px solid #ff00ff;
        box-shadow: 0 0 8px #ff00ff, 0 0 14px #ff00ff inset;
        font-weight: bold;
        padding: 14px 20px;
        border-radius: 6px;
        cursor: pointer;
        transition: 0.3s;
    }
    .add-bot-button:hover {
        background: #ff00ff;
        color: #111;
    }
    .modal {
        display: none;
        position: fixed;
        z-index: 10;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        overflow: auto;
        background-color: rgba(0, 0, 0, 0.4);
    }
    .modal-content {
        background-color: #222;
        color: #fff;
        margin: 10% auto;
        padding: 20px;
        border: 1px solid #555;
        width: 90%;
        max-width: 400px;
        border-radius: 8px;
        text-align: center;
    }
    .close {
        color: #ccc;
        float: right;
        font-size: 28px;
        font-weight: bold;
    }
    .close:hover,
    .close:focus {
        color: #fff;
        text-decoration: none;
        cursor: pointer;
    }
    .status-icon {
        position: absolute;
        top: 10px;
        right: 10px;
        width: 30px;
        height: 30px;
    }
    .bots-back-btn-row {
        display: flex;
        justify-content: center;
        margin-top: 18px;
    }
    .bots-back-btn {
        display: inline-block;
        text-align: center;
        width: auto;
        padding: 14px 28px;
        margin-top: 10px;
    }
</style>
<div class="bots-outer">
    <div class="bots-container">
        <div class="button-container">
            <form action="{{ url_for('dashboard') }}" method="get">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                <button type="submit" class="neon-button neon3"><span>Zurück zum Dashboard</span></button>
            </form>
            <form action="{{ url_for('add_dc_bot') }}" method="post">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                <button type="submit" class="neon-button neon1"><span>Bot hinzufügen</span></button>
            </form>
        </div>
        <div class="button-container">
            <button class="add-bot-button" onclick="document.getElementById('addBotModal').style.display='block'">Add Discord Bot</button>
            <form action="{{ url_for('turn_on_all_bots') }}" method="post">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                <button type="submit" class="neon-button neon3"><span>Turn On All</span></button>
            </form>
            <form action="{{ url_for('turn_off_all_bots') }}" method="post">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                <button type="submit" class="neon-button neon2"><span>Turn Off All</span></button>
            </form>
        </div>
        <h2 style="color:#00fff7;">Discord Bots</h2>
        <div id="addBotModal" class="modal">
            <div class="modal-content">
                <span class="close" onclick="document.getElementById('addBotModal').style.display='none'">&times;</span>
                <h2>Add Discord Bot</h2>
                <form action="{{ url_for('add_dc_bot') }}" method="post">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                    <label for="bot_name">Bot Name:</label>
                    <input type="text" id="bot_name" name="bot_name" required>
                    <label for="bot_directory">Bot Directory:</label>
                    <input type="text" id="bot_directory" name="bot_directory" required>
                    <button type="submit" class="neon-button neon1"><span>Add Bot</span></button>
                </form>
            </div>
        </div>
        <div class="bot-container">
            {% for bot in bots %}
            <div class="bot-box">
                <h3>{{ bot.name }}</h3>
                <img src="/static/{{ 'circle_green_checkmark.png' if 'pid' in bot else 'offline.png' }}" alt="{{ 'Online' if 'pid' in bot else 'Offline' }}" class="status-icon">
                <form action="{{ url_for('turn_on_bot', bot_name=bot.name) }}" method="post">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                    <button type="submit" class="turn-on">Turn On</button>
                </form>
                <form action="{{ url_for('turn_off_bot', bot_name=bot.name) }}" method="post">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                    <button type="submit" class="turn-off">Turn Off</button>
                </form>
                <form action="{{ url_for('bot_logs', bot_name=bot.name) }}" method="get">
                    <button type="submit" class="logs">Logs</button>
                </form>
            </div>
            {% endfor %}
        </div>
        <div class="bots-back-btn-row">
            <a href="{{ url_for('dashboard') }}" class="neon-button neon3 bots-back-btn"><span>Zurück</span></a>
        </div>
    </div>
</div>
<div id="statusModal" class="modal">
    <div class="modal-content">
        <span class="close" onclick="document.getElementById('statusModal').style.display='none'">&times;</span>
        <p id="statusMessage"></p>
    </div>
</div>
<script>
    function checkBotStatus(botName, isRunning, action) {
        if ((action === 'on' && isRunning) || (action === 'off' && !isRunning)) {
            document.getElementById('statusMessage').innerText = `Bot ${botName} kann nicht ${action === 'on' ? 'eingeschaltet' : 'ausgeschaltet'} werden, da er bereits ${action === 'on' ? 'läuft' : 'nicht läuft'}.`;
            document.getElementById('statusModal').style.display = 'block';
            return false;
        }
        return true;
    }

    window.onclick = function(event) {
        if (event.target == document.getElementById('addBotModal')) {
            document.getElementById('addBotModal').style.display = "none";
        }
        if (event.target == document.getElementById('statusModal')) {
            document.getElementById('statusModal').style.display = "none";
        }
    }
</script>
{% else %}
<style>
    .bots-outer {
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 100%;
    }
    .bots-container {
        background: rgba(255,255,255,0.9);
        padding: 30px;
        border-radius: 12px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        max-width: 1100px;
        width: 100%;
        min-height: 300px;
        max-height: 90vh;
        overflow-y: auto;
        text-align: center;
    }
    .button-container {
        display: flex;
        justify-content: center;
        width: 100%;
        margin-bottom: 20px;
        gap: 16px;
    }
    .button-container form {
        margin: 0;
    }
    .add-bot-button {
        background-color: #007BFF;
        color: white;
        border: none;
        padding: 14px 20px;
        border-radius: 6px;
        font-weight: bold;
        cursor: pointer;
        min-width: 170px;
        margin: 0 6px;
        transition: background 0.3s;
    }
    .add-bot-button:hover {
        background-color: #0056b3;
    }
    .bot-container {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
        gap: 20px;
        padding: 20px 0;
    }
    .bot-box {
        background: rgba(180, 180, 180, 0.9);
        padding: 20px;
        border-radius: 8px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        width: 340px;
        text-align: center;
        position: relative;
        margin-bottom: 16px;
        display: flex;
        flex-direction: column;
        align-items: center;
    }
    .bot-box h3 {
        margin-bottom: 18px;
        color: #007BFF;
    }
    .bot-box form {
        margin: 0 0 8px 0;
        width: 100%;
    }
    .bot-box button {
        width: 100%;
        margin: 4px 0;
        padding: 10px 0;
        border-radius: 4px;
        font-weight: bold;
        border: none;
        cursor: pointer;
        transition: background 0.3s;
    }
    .bot-box .turn-on {
        background-color: #28a745;
        color: white;
    }
    .bot-box .turn-on:hover {
        background-color: #218838;
    }
    .bot-box .turn-off {
        background-color: #dc3545;
        color: white;
    }
    .bot-box .turn-off:hover {
        background-color: #b52a37;
    }
    .bot-box .logs {
        background-color: #fd7e14;
        color: white;
    }
    .bot-box .logs:hover {
        background-color: #c76a10;
    }
    .modal {
        display: none;
        position: fixed;
        z-index: 10;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        overflow: auto;
        background-color: rgba(0, 0, 0, 0.4);
    }
    .modal-content {
        background-color: #fefefe;
        color: #000;
        margin: 10% auto;
        padding: 20px;
        border: 1px solid #888;
        width: 90%;
        max-width: 400px;
        border-radius: 8px;
        text-align: center;
    }
    .close {
        color: #aaa;
        float: right;
        font-size: 28px;
        font-weight: bold;
    }
    .close:hover,
    .close:focus {
        color: #000;
        text-decoration: none;
        cursor: pointer;
    }
    .status-icon {
        position: absolute;
        top: 10px;
        right: 10px;
        width: 30px;
        height: 30px;
    }
    .bots-back-btn-row {
        display: flex;
        justify-content: center;
        margin-top: 18px;
    }
    .bots-back-btn {
        background-color: #007BFF;
        color: white;
        border: none;
        padding: 10px 15px;
        cursor: pointer;
        border-radius: 4px;
        width: auto;
        text-align: center;
        display: inline-block;
        text-decoration: none;
        margin-top: 10px;
    }
    .bots-back-btn:hover {
        background-color: #0056b3;
    }
</style>
<div class="bots-outer">
    <div class="bots-container">
        <div class="button-container">
            <form action="{{ url_for('dashboard') }}" method="get">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                <button type="submit" class="bots-back-btn">Zurück zum Dashboard</button>
            </form>
            <form action="{{ url_for('add_dc_bot') }}" method="post">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                <button type="submit" class="add-bot-button">Bot hinzufügen</button>
            </form>
        </div>
        <div class="button-container">
            <button class="add-bot-button" onclick="document.getElementById('addBotModal').style.display='block'">Add Discord Bot</button>
            <form action="{{ url_for('turn_on_all_bots') }}" method="post">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                <button type="submit" class="add-bot-button">Turn On All</button>
            </form>
            <form action="{{ url_for('turn_off_all_bots') }}" method="post">
                <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                <button type="submit" class="add-bot-button">Turn Off All</button>
            </form>
        </div>
        <h2 style="color:#007BFF;">Discord Bots</h2>
        <div id="addBotModal" class="modal">
            <div class="modal-content">
                <span class="close" onclick="document.getElementById('addBotModal').style.display='none'">&times;</span>
                <h2>Add Discord Bot</h2>
                <form action="{{ url_for('add_dc_bot') }}" method="post">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                    <label for="bot_name">Bot Name:</label>
                    <input type="text" id="bot_name" name="bot_name" required>
                    <label for="bot_directory">Bot Directory:</label>
                    <input type="text" id="bot_directory" name="bot_directory" required>
                    <button type="submit" class="add-bot-button">Add Bot</button>
                </form>
            </div>
        </div>
        <div class="bot-container">
            {% for bot in bots %}
            <div class="bot-box">
                <h3>{{ bot.name }}</h3>
                <img src="/static/{{ 'circle_green_checkmark.png' if 'pid' in bot else 'offline.png' }}" alt="{{ 'Online' if 'pid' in bot else 'Offline' }}" class="status-icon">
                <form action="{{ url_for('turn_on_bot', bot_name=bot.name) }}" method="post">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                    <button type="submit" class="turn-on">Turn On</button>
                </form>
                <form action="{{ url_for('turn_off_bot', bot_name=bot.name) }}" method="post">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                    <button type="submit" class="turn-off">Turn Off</button>
                </form>
                <form action="{{ url_for('bot_logs', bot_name=bot.name) }}" method="get">
                    <button type="submit" class="logs">Logs</button>
                </form>
            </div>
            {% endfor %}
        </div>
        <div class="bots-back-btn-row">
            <a href="{{ url_for('dashboard') }}" class="bots-back-btn">Zurück</a>
        </div>
    </div>
</div>
<div id="statusModal" class="modal">
    <div class="modal-content">
        <span class="close" onclick="document.getElementById('statusModal').style.display='none'">&times;</span>
        <p id="statusMessage"></p>
    </div>
</div>
<script>
    function checkBotStatus(botName, isRunning, action) {
        if ((action === 'on' && isRunning) || (action === 'off' && !isRunning)) {
            document.getElementById('statusMessage').innerText = `Bot ${botName} kann nicht ${action === 'on' ? 'eingeschaltet' : 'ausgeschaltet'} werden, da er bereits ${action === 'on' ? 'läuft' : 'nicht läuft'}.`;
            document.getElementById('statusModal').style.display = 'block';
            return false;
        }
        return true;
    }

    window.onclick = function(event) {
        if (event.target == document.getElementById('addBotModal')) {
            document.getElementById('addBotModal').style.display = "none";
        }
        if (event.target == document.getElementById('statusModal')) {
            document.getElementById('statusModal').style.display = "none";
        }
    }
</script>
{% endif %}
{% endblock %}
