{% extends "base.html" %}
{% load compress static hc_extras %}

{% block title %}Project Settings - {{ project }}{% endblock %}


{% block content %}
<div class="row">
    <div class="col-sm-9 col-md-6">
        {% for message in messages %}
            <p class="alert alert-{{ message.tags }}">{{ message }}</p>
        {% endfor %}

        <div class="panel panel-{{ project_name_status }}">
            <div class="panel-body settings-block">
                <h2>Project Name</h2>
                {{ project }}
                <a
                    href="#"
                    class="btn btn-default pull-right"
                    data-toggle="modal"
                    data-target="#set-project-name-modal">Change Project Name</a>
            </div>

           {% if project_name_updated %}
            <div class="panel-footer">
                Project name updated
            </div>
            {% endif %}
        </div>

        <div class="panel panel-{{ api_status }}">
            <div class="panel-body settings-block">
                <h2>API Access</h2>
                {% if project.api_key %}
                    {% if show_api_keys %}
                        <p>
                            API key: <br />
                            <code>{{ project.api_key }}</code>
                        </p>
                        {% if project.api_key_readonly %}
                        <p>
                            API key (read-only): <br />
                            <code>{{ project.api_key_readonly }}</code>
                        </p>
                        {% endif %}
                        <button
                            data-toggle="modal"
                            data-target="#revoke-api-key-modal"
                            class="btn btn-danger pull-right">Revoke</button>

                    {% else %}
                        <form method="post">
                        <span class="icon-ok"></span>
                        API access is enabled.
                            {% csrf_token %}

                            <button
                                type="submit"
                                name="show_api_keys"
                                class="btn btn-default pull-right">Show API keys</button>
                        </form>
                    {% endif %}
                {% else %}
                    <span class="icon-cancel"></span>
                    API access is disabled.
                    <form method="post">
                        {% csrf_token %}
                        <button
                            type="submit"
                            name="create_api_keys"
                            class="btn btn-default pull-right">Create API keys</button>
                    </form>
                {% endif %}
            </div>

            {% if api_keys_created %}
            <div class="panel-footer">
                API keys created
            </div>
            {% endif %}

           {% if api_keys_revoked %}
            <div class="panel-footer">
                API keys revoked
            </div>
            {% endif %}
        </div>

        <div class="panel panel-{{ team_status }}">
            <div class="panel-body settings-block">
                <h2>Team Access</h2>
                {% if num_members %}
                    <table class="table">
                        <tr>
                            <td>{{ project.owner.email }}</td>
                            <td>Owner</td>
                            <td></td>
                        </tr>
                        {% for member in project.member_set.all %}
                        <tr>
                            <td>{{ member.user.email }} </td>
                            <td>Member</td>
                            <td>
                                {% if is_owner %}
                                <a
                                    href="#"
                                    data-email="{{ member.user.email }}"
                                    class="pull-right member-remove">Remove</a>
                                {% endif %}
                            </td>
                        </tr>
                        {% endfor %}
                    </table>
                {% else %}
                    <p>
                        <strong>Invite team members to your project.</strong>
                        Share access to your checks and configured integrations
                        without having to share login details.
                    </p>
                {% endif %}

                <br />

                {% if is_owner %}
                {% if project.can_invite%}
                <a
                    href="#"
                    class="btn btn-primary pull-right"
                    data-toggle="modal"
                    data-target="#invite-team-member-modal">Invite a Team Member</a>
                {% else %}
                <div class="alert alert-info">
                    <strong>Team size limit reached.</strong>
                    To invite more members, please
                    <a href="{% url 'hc-pricing' %}">upgrade your account!</a>
                </div>
                {% endif %}
                {% endif %}
            </div>

           {% if team_member_invited %}
            <div class="panel-footer">
                {{ team_member_invited }} invited to team
            </div>
            {% endif %}

           {% if team_member_removed %}
            <div class="panel-footer">
                {{ team_member_removed }} removed from team
            </div>
            {% endif %}
        </div>

        {% if is_owner %}
        <div class="panel panel-default">
            <div class="panel-body settings-block">
                    {% csrf_token %}
                    <h2>Remove Project</h2>
                    <a href="#"
                       id="remove-project"
                       class="btn btn-default pull-right"
                       data-toggle="modal"
                       data-target="#remove-project-modal">Remove Project</a>
                    This will permanently remove project {{ project }}.
                <form action="{% url 'hc-remove-project' project.code %}" method="post">
                </form>
            </div>
        </div>
        {% endif %}

    </div>
</div>

<div id="revoke-api-key-modal" class="modal">
    <div class="modal-dialog">
        <form id="revoke-api-key-form" method="post">
            {% csrf_token %}
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4>Revoke API Keys?</h4>
                </div>
                <div class="modal-body">
                    <p>You are about to revoke your current API keys.</p>
                    <p>Afterwards, you can create new API keys, but there will
                        be <strong>no way of getting the current API
                        keys back</strong>.
                    </p>
                    <p>Are you sure?</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                    <button
                        type="submit"
                        name="revoke_api_keys"
                        class="btn btn-danger">Revoke API Keys</button>
                </div>
            </div>
        </form>
    </div>
</div>

<div id="remove-team-member-modal" class="modal">
    <div class="modal-dialog">
        <form id="remove-team-member-form" method="post">
            {% csrf_token %}
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4>Remove Team Member</h4>
                </div>
                <div class="modal-body">
                    <p>You are about to remove <strong id="rtm-email"></strong> from the project.</p>
                    <p>Are you sure?</p>
                    <input
                        type="hidden"
                        name="email"
                        id="remove-team-member-email" />
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                    <button
                        type="submit"
                        name="remove_team_member"
                        class="btn btn-danger">Remove Member from Project</button>
                </div>
            </div>
        </form>
    </div>
</div>

<div id="invite-team-member-modal" class="modal">
    <div class="modal-dialog">
        <form method="post" class="form-horizontal">
            {% csrf_token %}
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4>Invite a Team Member</h4>
                </div>
                <div class="modal-body">
                    <ul>
                        <li>Team Members can create and manage Checks and Integrations</li>
                        <li>Only the project owner (you) can view and edit billing settings</li>
                    </ul>
                    <div class="form-group">
                        <label for="itm-email" class="col-sm-2 control-label">Email</label>
                        <div class="col-sm-9">
                            <input
                                type="email"
                                class="form-control"
                                id="itm-email"
                                name="email"
                                placeholder="friend@example.org">
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                    <button
                        type="submit"
                        name="invite_team_member"
                        class="btn btn-primary">Send Invite</button>
                </div>
            </div>
        </form>
    </div>
</div>

<div id="set-project-name-modal" class="modal">
    <div class="modal-dialog">
        <form method="post" class="form-horizontal">
            {% csrf_token %}
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4>Change Project Name</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label for="project-name" class="col-sm-4 control-label">Project Name</label>
                        <div class="col-sm-7">
                            <input
                                type="text"
                                class="form-control"
                                id="project-name"
                                name="name"
                                value="{{ project }}">
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                    <button
                        type="submit"
                        name="set_project_name"
                        class="btn btn-primary">Set Project Name</button>
                </div>
            </div>
        </form>
    </div>
</div>

<div id="remove-project-modal" class="modal">
    <div class="modal-dialog">
        <form method="post" action="{% url 'hc-remove-project' project.code %}">
            {% csrf_token %}
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4>Remove "{{ project }}"?</h4>
                </div>
                <div class="modal-body">
                    <p>Danger zone! You are about to permanently remove
                    project <strong>{{ project }}</strong> and all
                    of its associated checks and integrations. 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-danger">Remove Project</button>
                </div>
            </div>
        </form>
    </div>
</div>

{% endblock %}

{% block scripts %}
{% compress js %}
<script src="{% static 'js/jquery-2.1.4.min.js' %}"></script>
<script src="{% static 'js/bootstrap.min.js' %}"></script>
<script src="{% static 'js/project.js' %}"></script>
{% endcompress %}
{% endblock %}
