{% extends "otree/admin/Session.html" %}
{% load otree_tags floppyforms %}

{% block internal_scripts %}
    {{ block.super }}
    <script type="text/javascript" charset="utf-8">
        // show modal warning window if user tries to create hit in non-sandbox mode
        // and if we are not already showing this modal to user
        $(document).ready(function () {
            // this select text in text input box so it is easier to change it
            $("input[type=text]").click(function () {
                $(this).select();
            });
            $('#form').on('submit', function (ev) {
                if (($("input[name=in_sandbox]:checked").length == 0) && !($("#mturk-production").hasClass('in'))) {
                    $('#mturk-production').modal({
                        show: 'false'
                    });
                    ev.preventDefault();
                }
            })
        });
    </script>
{% endblock %}

{% block content %}

{{ block.super }}

{% if not mturk_ready %}
    <p>MTurk is currently disabled.
        If you want to publish your HIT on MTurk please do the following
        steps:
    </p>

    <table class="table">
        <tr>
            <th>Step</th>
            <th>Done?</th>
        </tr>
        <tr>
            <td>
                Run <code>pip3 install otree-core[mturk]</code>
                and in your <code>requirements_base.txt</code>,
                replace <code>otree-core==N.N.N</code> with <code>otree-core[mturk]==N.N.N</code>.
                This will install oTree along with extra MTurk-specific packages.
            </td>
            <td>{% if boto3_installed %}Yes{% else %}No{% endif %}</td>
        </tr>

        <tr>
            <td>Set the settings <code>AWS_ACCESS_KEY_ID</code> and
            <code>AWS_SECRET_ACCESS_KEY</code></td>
            <td>{% if aws_keys_exist %}Yes{% else %}No{% endif %}</td>
        </tr>
        <tr>
            <td>
                (If using Heroku)
                In your address bar, change the URL to start with 'https://'
                and reload this page
            </td>
            <td>{% if https %}Yes{% else %}No{% endif %}</td>
        </tr>
    </table>

    <p>
        You can read more about Amazon Mechanical Turk integration
        <a href="http://otree.readthedocs.org/en/latest/mturk.html"
           target="_blank">here</a>.
    </p>
{% else %}

    <form action="" method="post" role="form" class="form"
          id="form">{% csrf_token %}
    <input type="hidden" value="{{ request.path }}" name="origin_url"/>

    <div class="modal fade" id="mturk-production" tabindex="-1"
         role="dialog" aria-labelledby="exampleModalLabel"
         aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content panel-warning">
                <div class="modal-header panel-heading">
                    <button type="button" class="close"
                            data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="exampleModalLabel">Publish
                        HIT?</h4>
                </div>
                <div class="modal-body">
                    <p>
                        You are about to publish HIT for your session on
                        MTurk. Are you sure?
                    </p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default"
                            data-dismiss="modal">Cancel
                    </button>
                    <button type="submit" class="btn btn-primary">Publish
                        HIT
                    </button>
                </div>
            </div>
        </div>
    </div>
    {% if not session.mturk_HITId %}
        {% form form using "floppyforms/layouts/bootstrap.html" %}
        {% if missing_next_button_warning %}
        <div class="alert alert-warning">
            {{ missing_next_button_warning }}
        </div>
        {% endif %}
        <button type="submit" class="btn btn-primary">Publish HIT</button>

    {% else %}
        <p>
            You have published HIT for this session on MTurk
            {% if session.mturk_use_sandbox %}
                Sandbox
            {% endif %}
            .</p>
        <p>
            To look at the HIT as <em>requester</em>
            follow this <a href="{{ session.mturk_requester_url }}"
                           target="_blank">link</a>.<br>
            To look at the HIT as a <em>worker</em>
            follow this <a href="{{ session.mturk_worker_url }}"
                           target="_blank">link</a>.
        </p>
    {% endif %}
    </form>
{% endif %}

{% include "otree/includes/messages.html" %}
{% endblock %}
