<!DOCTYPE html>
<html lang="en">
<head>
    <title>OCB Management Panel</title>
    <link rel="icon" type="image/x-icon" href="/static/img/favicon.ico">
    <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
    <script src="/static/mgmtpanel.js"></script>
    
    <div class="navbar">
        <div class="tab">
            <button class="tablinks" data-tab="queue" onclick="openTab(event, 'queue')">Queue</button>
            <button class="tablinks" data-tab="accounts" onclick="openTab(event, 'accounts')">Manage Accounts</button>
            <button class="tablinks" data-tab="managerewards" onclick="openTab(event, 'managerewards')">Rewards</button>
            <button class="tablinks" data-tab="categories" onclick="openTab(event, 'categories')">Categories</button>
            <button class="tablinks" data-tab="announcements" onclick="openTab(event, 'announcements')">Announcements</button>
            <button class="tablinks" data-tab="settings" onclick="openTab(event, 'settings')">Settings</button>
            {% if kofi_integration %}
                <button class="tablinks" data-tab="kofi-settings" onclick="openTab(event, 'kofi-settings')">Kofi Settings</button>
            {% endif %}
        </div>
        <img src="/static/img/ownchatbotwide.png">
    </div>
    
    <div id='queue' class="tabcontent">
        <body>
            {% if votes %}
            <table>
                <thead>
                    <tr>
                        <th style="width: 20%;">Active Votes</th>
                        <th style="width: 40%;">Description</th>
                        <th style="width: 20%;">Price</th>
                        <th style="width: 20%;">Current Tally</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                {% for vote in votes %}
                    <tr>
                        <td> {{ prefix }}{{ vote[0] }} </td>
                        <td> {{ vote[2] }} </td>
                        
                        {% set points_label = 'point' if rewards[vote[0]]["price"] == 1 else 'points' %}
                        <td> {{ rewards[vote[0]]["price"] }} {{ points_label }}</td>
                        
                        {% set votes_label = 'vote' if vote[1] == 1 else 'votes' %}
                        
                        <td> {{ vote[1] }} {{ votes_label }}</td>
                        <td></td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
            {% else %}
            <h3> You currently have no active votes. </h3>
            {% endif %}
            <br>
            <hr>
            {% if goals %}
            <table>
                <thead>
                    <tr>
                        <th style="width: 20%;">Active Goals</th>
                        <th style="width: 40%;">Description</th>
                        <th style="width: 25%;">Progress</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                {% for goal in goals %}
                    <tr>
                        <td> {{ prefix }}{{ goal[0] }} </td>
                        <td> {{ goal[3] }} </td>
                        {% set progress = goal[1] / goal[2] * 100 %}
                        <td>
                            <div class="bar-light-grey bar-tiny bar-round" style="position: relative;">
                                <div class="bar-round bar-blue" style="text-align: center; width:{{ progress }}%;">{{ '%0.0f'| format(progress| float) }}%
                                </div>
                                {% set milestones = rewards[goal[0]]["milestones"] %}
                                {% for milestone_key, milestone in milestones.items() %}
                                    {% if milestones[milestone_key][0] and milestones[milestone_key][1] %}
                                        {% if milestones[milestone_key][1] < goal[2] %}
                                            {% set milestone_progress = milestones[milestone_key][1] / goal[2] * 100 %}
                                                <div class="milestone-marker" style="position: absolute; left: {{ milestone_progress }}%; transform: translateX(-50%);">
                                                    <img src="/static/img/milestone.png" style="width: 16px; height: 16px;" title="{{ milestones[milestone_key][1] }} points. {{ milestones[milestone_key][0] }}">
                                                </div>
                                            {% endif %}
                                        {% endif %}
                                    {% endfor %}
                            </div>
                        </td>
                        
                        {% if goal[1] == goal[2] %}
                        <td> {{ goal[1] }} / {{ goal[2] }} <img src=/static/img/tada.png style="width: 24px; height: 24px;"></td>
                        {% else %}
                        <td> {{ goal[1] }} / {{ goal[2] }} </td>
                        {% endif %}
                    </tr>
                {% endfor %}
                    <tr style="border-bottom: none;">
                        <td>
                        </td>
                        <td>
                        </td>
                        <td style="font-size: small;">
                            (Mouse over flags for milestone details)
                        </td>
                    </tr>
                </tbody>
            </table>
            {% else %}
            <h3> You currently have no active goals. </h3>
            {% endif %}
            <hr>
            <h3>Redeems Queue</h3>
            {% if queue %}
            <a href="/mgmt/clearfulfilled"><button class="button button2" onclick="openTab(event, 'queue')">Clear Fullfilled Rewards</button></a>&nbsp;&nbsp;<a href="/mgmt/clearqueue"><button class="button button2" onclick="openTab(event, 'queue')">Clear All Rewards</button></a>
            <table>
                <thead>
                    <tr>
                        <th>Time</th>
                        <th>Reward</th>
                        <th>User</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                {% for row in queue %}
                    <tr>
                        <td>{{ row[1].replace(tzinfo=utc_timezone).astimezone().strftime("%H:%M") }}</td>
                        {% if row[4] %}
                        <td><s>{{ rewards[row[2]]["info"] }}</s></td>
                        {% else %}
                        <td>{{ rewards[row[2]]["info"] }}</td>
                        {% endif %}
                        <td>{{ row[6] }}</td>
                        {% if row[4] %}
                        {% if row[5] %}
                        <td>Refunded</td>
                        {% else %}
                        <td><a href="/mgmt/refund?reward={{ row[2] }}&username={{ row[6] }}&rewarder_id={{ row[3] }}&reward_id={{ row[0] }} "><button class="button button2" onclick="openTab(event, 'queue')">Refund</button></a></td>
                        {% endif %}
                        {% else %}
                        <td><a href="/mgmt/fulfill?reward_id={{ row[0] }}&username={{ row[6] }}"><button class="button button2" onclick="openTab(event, 'queue')">Fulfill</button></a></td>
                        {% endif %}
                    </tr>
                {% endfor %}
                </tbody>
            </table>
            {% else %}
            <p>There are currently no rewards waiting in the queue.</p>
            {% endif %}
        </body>
    </div>

    <div id='accounts' class="tabcontent">

        <body>
            <h3>Manage Viewer Accounts</h3>
            {% if users %}
            <table>
                <thead>
                    <tr>
                        <th>User</th>
                        <th>Points balance</th>
                        <th>Email</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                {% for user in users %}
                    <tr>
                        <td> {{ user[1] }} </td>
                        {% set points_label = 'point' if user[2] == 1 else 'points' %}
                        
                        <td>{{ user[2] }} {{ points_label }}</td>
                        {% if user[4] %}
                            <td>{{ user[4] }}</td>
                        {% else %}
                            <td>none</td>
                        {% endif %}
                        <td> <a href="/mgmt/edit_account/{{ user[0] }}?name={{ user[1] }}&points={{ user[2] }}&email={{ user[4] }}"><button class="button button2" onclick="openTab(event, 'panel')">Edit</button></a> </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
            {% endif %}
        </body>
    </div>

    <div id='managerewards' class="tabcontent">
        <h3>Manage Rewards</h3>
        <body>
        <div>
        <h4>Reward Types:</h4>
        <ul>
            
            <li><u>Redeems</u> are standard stream point redeems. They get added to the queue for the streamer to fulfill.</li>
            <li><u>Specials</u> are redeems, but they run system commands and scripts. This enables you to integrate a lot of other fun things, such as letting your viewers control lighting, activate devices, trigger webhooks, etc. Specials do not go into the queue, because they happen automagically.</li>
              <ul>
                <li>Be careful with this one. It does require some more advanced scripting/command line knowledge. Make sure you test your specials before letting viewers use them.</li>
              </ul>
            <li><u>Votes</u> are just that, votes. Your viewers can vote on them.</li>
            <li><u>Goals</u> are rewards that everyone in chat can contribute to. The streamer fulfills the reward when the goal is reached.</li>
            <li>Add rewards to categories to enable and disable groups of rewards.</li>
            <li>Rewards with no categories are inactive.</li>
        </ul>

        </div>
            <table>
                <tr>
                    <td>
                    </td>
                    <td>
                    </td>
                    <td>
                    </td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td>
                        <a href="{{ url_for('web_panels.add', reward_type='redeem') }}"><button class="button button2" onclick="openTab(event, 'managerewards')">Create a Redeem</button></a><br>
                    </td>
                    <td>
                        <a href="{{ url_for('web_panels.add', reward_type='special') }}"><button class="button button2" onclick="openTab(event, 'managerewards')">Create a Special</button></a><br>
                    </td>
                    <td>
                        <a href="{{ url_for('web_panels.add', reward_type='vote') }}"><button class="button button2" onclick="openTab(event, 'managerewards')">Create a Vote</button></a><br>
                    </td>
                    <td>
                        <a href="{{ url_for('web_panels.add', reward_type='goal') }}"><button class="button button2" onclick="openTab(event, 'managerewards')">Create a Goal</button></a><br>
                    </td>
                </tr>
            </table>
            <br>
            {% if rewards %}
            Rewards in <span style="color: red !important;">red</span> are inactive. To activate a reward, add it to an active category.
            <table>
                <thead>
                    <tr>
                        <th style="width:15%">Name</th>
                        <th>Target</th>
                        <th>Price</th>
                        <th>Info</th>
                        <th>Cool down</th>
                        <th>Category</th>
                        <th style="width:15%">Actions</th>
                    </tr>
                </thead>
                <tbody>
                {% for reward, reward_info in rewards.items() %}
                    {% if reward in active_rewards %}
                    <tr>
                    {% else %}
                    <tr style="color: red;">
                    {% endif %}
                        <td>{{ prefix }}{{ reward }}</td>
                        {% if reward_info["type"] == "goal" %}
                        <td>{{ reward_info["target"] }}</td>
                        {% else %}
                        <td>N/A</td>
                        {% endif %}
                        {% if reward_info["type"] == "goal" %}
                        <td>N/A</td>
                        {% else %}
                        <td>{{ reward_info["price"] }}</td>
                        {% endif %}
                        <td>{{ reward_info["info"] }}</td>
                        {% if reward_info["cooldown"] > 0 %}
                        {% set minutes_label = 'minute' if reward_info["cooldown"] == 1 else 'minutes' %}
                        <td>{{ reward_info["cooldown"] }} {{ minutes_label }}</td>
                        {% else %}
                        <td>None</td>
                        {% endif %}
                        <td>{{ reward_info["categories"] | join(', ') }}</td>
                        <td>
                            <a href="{{ url_for('web_panels.edit', reward_name=reward) }}"><button class="button button2" onclick="openTab(event, 'managerewards')"><span style="color: green;">Edit</span></button></a>&nbsp
                            <a href="{{ url_for('web_panels.delete', reward_name=reward) }}"><button class="button button2" onclick="openTab(event, 'managerewards')"><span style="color: red;">Delete</span></button></a>&nbsp                            
                            {% if reward_info["type"] == "goal" or reward_info["type"] == "vote"  %}
                            <a href="{{ url_for('web_panels.reset', reward_name=reward, reward_type=reward_info["type"]) }}"><button class="button button2" onclick="openTab(event, 'managerewards')"><span style="color: orange;">Reset</span></button></a>
                            {% endif %}
                        </td>
                    </tr>
                {% endfor %}
                </tbody>
            </table>
            {% endif %}
        </body>
    </div>

    <div id='categories' class="tabcontent">
        <body>
        <h3>Manage Categories</h3>
        <div>
            &nbsp;&nbsp;<a href="{{ url_for('web_panels.add', reward_type='category') }}"><button class="button button2" onclick="openTab(event, 'categories')">Create a new category</button></a><br>
            <table>
                <thead>
                    <tr>
                        <th style="width: 50%;"><h3>Active Categories</h3></th>
                        <th style="width: 50%;"><h3>Inactive Categories</h3></th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>
                            {% for cat in active_categories %}
                            &nbsp;&nbsp;{{ cat }} - <a href="/mgmt/deactivate/{{ cat }}"><span style="color: green;">Deactivate</span></a>&nbsp;<a href="/mgmt/delcat/{{ cat }}/active"><span style="color: red;">Delete</span></a><br>
                            {% endfor %}
                        </td>
                        <td>
                            {% for cat in inactive_categories %}
                            &nbsp;&nbsp;{{ cat }} - <a href="/mgmt/activate/{{ cat }}"><span style="color: green;">Activate</span></a>&nbsp;<a href="/mgmt/delcat/{{ cat }}/inactive"><span style="color: red;">Delete</span></a><br>
                            {% endfor %}
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
        </body>
    </div>

    <div id='announcements' class="tabcontent">
        <body>
            <form method="POST" action="/mgmt/announcements">
                <table>
                <h3>Manage Announcements</h3>
                <table>
                    <thead>
                        <tr style="border-bottom: none;">
                            <th style="width: 20%;"></th>
                            <th style="width: 50%;"></th>
                            <th></th>
                        </tr>
                    </thead>
                    <tr>
                        <td> <label for="announce_enable">Enable:</label> </td>
                        {% if settings_info[9] %}
                        <td> <input type="checkbox" name="announce_enable" value="{{ settings_info[9] }}" checked> </td>
                        {% else %}
                        <td> <input type="checkbox" name="announce_enable" value="{{ settings_info[9] }}"> </td>
                        {% endif %}
                        <td>Enable periodic announcements</td>
                    </tr>
                    <tr>
                        <td> <label for="announce_interval">Interval:</label> </td>
                        <td> <input type="number" name="announce_interval" value="{{ settings_info[10] }}" size="2" required> minutes</td>
                        <td>How long between each announcement?</td>
                    </tr>
                    <tr>
                        <td> <label for="announcements">Announcements:</label> </td>
                        <td>
                            <textarea name="announcements" rows="5" cols="50">{{ announcements | join('\n') }}</textarea>
                        </td>
                        <td>Enter your announcements, one per line.</td>
                    </tr>
                </table>
                <br><button class="button button2" type="submit">Save Changes</button><br>
            </form>
            <br><br>
        </body>
    </div>

    <div id='settings' class="tabcontent">
        <body style="text-align: left;">
            <form method="POST" action="/mgmt/settings">
                <table>
                <h3>OwnchatBot Settings</h3>
                    <thead>
                        <tr style="border-bottom: none;">
                            <th style="width: 20%;"></th>
                            <th></th>
                            <th style="width: 50%;"></th>
                        </tr>
                    </thead>
                    <tr>
                        <td> <label for="mgmt_auth">Management Auth Code:</label> </td>
                        <td style="padding: 5px;"> <input type="text" name="mgmt_auth" value="{{ settings_info[0] }}" size="30" required> </td>
                        <td>Pregenerated during install. Goes in your management panel URL. Keep it secret. Keep it safe.<br>
                        http://localhost:5000/mgmt?auth={{ settings_info[0] }}</td>
                    </tr>
                    <tr>
                        <td> <label for="points_interval">Points Interval:</label> </td>
                        <td> <input type="number" name="points_interval" value="{{ settings_info[1] }}" size="5" required> minutes</td>
                        <td>How often do you want to award your viewers points?</td>
                    </tr>
                    <tr>
                        <td> <label for="points_award">Points Award:</label> </td>
                        <td> <input type="number" name="points_award" value="{{ settings_info[2] }}" size="5"  required> points</td>
                        <td>How many points do you want to award them?</td>
                    </tr>
                    <tr>
                        <td> <label for="gunicorn_logging">Gunicorn Logging:</label> </td>
                        {% if settings_info[3] %}
                        <td> <input type="checkbox" name="gunicorn_logging" value="{{ settings_info[3] }}" checked> </td>
                        {% else %}
                        <td> <input type="checkbox" name="gunicorn_logging" value="{{ settings_info[3] }}"> </td>
                        {% endif %}
                        <td>Enable Gunicorn logging integration.</td>
                    </tr>
                    <tr>
                        <td> <label for="prefix">Chat Command Prefix:</label> </td>
                        <td> <input type="text" name="prefix" maxlength="1" size="1" value="{{ settings_info[4] }}" required> </td>
                        <td>Character that preceeds chat commands, so OwnchatBot knows what to look for.<br>Example: "{{ settings_info[4] }}points"</td>
                    </tr>
                </table>
                
                <h3>Owncast Integration</h3>
                <table>
                    <thead>
                        <tr style="border-bottom: none;">
                            <th style="width: 20%;"></th>
                            <th></th>
                            <th style="width: 50%;"></th>
                        </tr>
                    </thead>
                    <tr>
                        <td> <label for="access_token">Access Token:</label> </td>
                        <td style="padding: 5px;"> <input type="text" name="access_token" value="{{ settings_info[5] }}" size="40"> </td>
                        <td>Create in Owncast Admin panel. Integrations -> Access Tokens (check all three boxes)</td>
                    </tr>
                    <tr>
                        <td> <label for="owncast_url">Your Owncast URL:</label> </td>
                        <td> <input type="text" name="owncast_url" value="{{ settings_info[6] }}" size="40"> </td>
                        <td>The address of your Owncast instance. Can be an internal address, such as "localhost" if that's the case.</td>
                    </tr>
                </table>
                
                <h3>Kofi Integration</h3>
                <table>
                    <thead>
                        <tr style="border-bottom: none;">
                            <th style="width: 20%;"></th>
                            <th></th>
                            <th style="width: 50%;"></th>
                        </tr>
                    </thead>
                    <tr>
                        <td> <label for="kofi_integration">Enable:</label> </td>
                        {% if settings_info[8] %}
                        <td> <input type="checkbox" name="kofi_integration" value="{{ settings_info[8] }}" checked> </td>
                        {% else %}
                        <td> <input type="checkbox" name="kofi_integration" value="{{ settings_info[8] }}"> </td>
                        {% endif %}
                        <td>Enable Ko-fi integration.</td>
                    </tr>
                    <tr>
                        <td> <label for="kofi_token">Verification Token:</label> </td>
                        <td style="padding: 5px;"> <input type="text" name="kofi_token" value="{{ settings_info[7] }}" size="40"> </td>
                        <td>Get from Kofi -> More -> API -> Webhooks -> Advanced -> Verification Token.</td>
                    </tr>
                </table>
                
                <br><button class="button button2" type="submit">Save Changes</button><br>
            </form>
            <br><br>
        </body>
    </div>

    <div id='kofi-settings' class="tabcontent">
        <body style="text-align: left;">
            <form method="POST" action="/mgmt/ksettings">
                <table>
                <h3>Kofi Settings</h3>
                
                <h4> Donations </h4>
                    <thead>
                        <tr style="border-bottom: none;">
                            <th style="width: 20%;"></th>
                            <th></th>
                            <th style="width: 50%;"></th>
                        </tr>
                    </thead>
                    <tr>
                        <td> <label for="enable_donations">Enable points for donations:</label> </td>
                        {% if kofi_settings['donations'] %}
                        <td> <input type="checkbox" name="enable_donations" value="{{ kofi_settings['donations'] }}" checked> </td>
                        {% else %}
                        <td> <input type="checkbox" name="enable_donations" value="{{ kofi_settings['donations'] }}"> </td>
                        {% endif %}
                        <td>Enable awarding points for donations</td>
                    </tr>
                    <tr>
                        <td> <label for="set_donation_points">Points per dollar:</label> </td>
                        <td> <input type="number" name="set_donation_points" value="{{ kofi_settings['donation_points'] }}" size="5" required> points</td>
                        <td>How many points should viewers recieve, for every dollar they donate?</td>
                    </tr>
                    <tr>
                        <td> <label for="kofi_url">Kofi Page:</label> </td>
                        <td style="padding: 5px;"> <input type="text" name="kofi_url" value="{{ kofi_settings['kofi_url'] }}" size="30"> </td>
                        <td>What is your Kofi page URL?</td>
                    </tr>
                    <tr>
                        <table>
                        <h4> Subscriptions </h4>
                            <tbody>
                                <tr>
                                    <td> <label for="enable_subs">Enable points for subscriptions:</label> </td>
                                    {% if kofi_settings['subs'] %}
                                    <td> <input type="checkbox" name="enable_subs" value="{{ kofi_settings['subs'] }}" checked> </td>
                                    {% else %}
                                    <td> <input type="checkbox" name="enable_subs" value="{{ kofi_settings['subs'] }}"> </td>
                                    {% endif %}
                                    <td>Enable awarding points for monthly subscriptions</td>
                                </tr>
                                <tr>
                                    <td> <label for="sub_points">Points per month:</label> </td>
                                    <td> <input type="number" name="sub_points" value="{{ kofi_settings['sub_points'] }}" size="6" required> points</td>
                                    <td>How many points should subscribers recieve every month?</td>
                                </tr>
                                        </tbody>
                                    </table>
                                </tr>
                                <tr>
                        <table>
                        <h4> Logo </h4>
                            <tbody>
                                <tr style="border-bottom: none;">
                                Which logo would you like to use?
                                </tr>
                            {% for kofi_logo in kofi_logos %}
                                {% if loop.index0 % 8 == 0 %}
                                    <tr style="border-bottom: none;">
                                {% endif %}
                                <td>
                                    <label for="kofi_logo"><img src="/static/img/kofi/{{ kofi_logo }}"></label>
                                    {% if kofi_logo == kofi_settings['kofi_logo'] %}
                                        <input type="radio" name="kofi_logo" value="{{ kofi_logo }}" checked>
                                    {% else %}
                                        <input type="radio" name="kofi_logo" value="{{ kofi_logo }}">
                                    {% endif %}
                                </td>
                                {% if loop.index0 % 8 == 7 or loop.last %}
                                </tr> <!-- Close the row after 8 items or at the end -->
                                {% endif %}
                            {% endfor %}
                            </tbody>
                        </table>
                    </tr>
                </table>
                <br><button class="button button2" type="submit">Save Changes</button><br>
            </form>
            <br><br>
	    <i>Kofi subscription tier support coming soon.</i>
        </body>
    </div>
    
    
</html>
