{% extends 'layouts/base.html' %}

{% block javascripts %}
    <script>
        function onSelect(experimentName) {
            const selectElement = document.getElementById('select_' + experimentName);
            const inputElement = document.getElementById('input_' + experimentName);
            inputElement.hidden = selectElement.value !== 'Другое (только для техлидов)'
        }
    </script>
{% endblock %}

{% block content %}
    <h1>Эксперименты <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#create_experiment">+</button></h1>
    <div class="modal fade" id="create_experiment" tabindex="-1" aria-labelledby="modal-default" style="display: none;" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
                <form method="POST">
                    {% csrf_token %}
                    <div class="modal-header">
                        <h2 class="h6 modal-title">Создать эксперимент</h2>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <input type="text" name="name" placeholder="Название эксперимента" style="width: 100%;" />
                    </div>
                    <div class="modal-footer">
                        <button type="submit" name="action" value="create" class="btn btn-secondary">Создать</button>
                        <button type="button" class="btn btn-link text-gray-600 ms-auto" data-bs-dismiss="modal">Закрыть</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <select class="form-select" style="width: 150px;" onchange="window.location.href='/experiments/?stage=' + this.value;">
        <option {% if stage == 'production' %}selected{% endif %}>production</option>
        <option {% if stage == 'development' %}selected{% endif %}>development</option>
    </select>
    <table class="table">
        <thead>
            <th>Название эксперимента</th>
            <th>Включен</th>
            <th>Условие</th>
            <th>Действие</th>
        </thead>
        <tbody>
            {% for experiment in experiments %}
                <tr>
                    <form method="POST">
                        {% csrf_token %}
                        <input type="hidden" name="experiment_id" value="{{ experiment.id }}" />
                        <td>{{ experiment.name }}<input type="hidden" name="name" value="{{ experiment.name }}"/> </td>
                        <td><input type="checkbox" name="enabled" {% if experiment.enabled %}checked{% endif %}> </td>
                        <td>
                            <select id="select_{{ experiment.name }}" class="form-select" name="condition_select" onchange="onSelect('{{ experiment.name }}')">
                                <option {% if experiment.exp_type == 0 %}selected{% endif %}>Никому</option>
                                <option {% if experiment.exp_type == 1 %}selected{% endif %}>Только админам</option>
                                <option {% if experiment.exp_type == 2 %}selected{% endif %}>Только сотрудникам</option>
                                <option {% if experiment.exp_type == 3 %}selected{% endif %}>Всем</option>
                                <option {% if experiment.exp_type == 4 %}selected{% endif %}>Другое (только для техлидов)</option>
                            </select>
                            <input id="input_{{ experiment.name }}" name="condition" {% if experiment.exp_type != 4%}hidden{% endif %} type="text" class="form-control" value="{{ experiment.condition }}" />
                        </td>
                        <td><button type="submit" name="action" value="change" class="btn btn-primary">Применить</button> <button type="submit" name="action" value="delete" class="btn btn-danger">Удалить</button></td>
                    </form>
                </tr>
            {% endfor %}
        </tbody>
    </table>
{% endblock %}