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

{% block title %}Account Settings - {{ site_name }}{% endblock %}


{% block content %}
<div class="row">
    <div class="col-sm-12">
        <h1 class="settings-title">
            Settings
            <small>{{ request.user.email }}</small>
        </h1>
    </div>
    {% if messages %}
    <div class="col-sm-12">
        {% for message in messages %}
            <p class="alert alert-{{ message.tags }}">{{ message }}</p>
        {% endfor %}
    </div>
    {% endif %}
</div>

<div class="row">
    <div class="col-sm-3">
        <ul class="nav nav-pills nav-stacked">
            <li class="active"><a href="{% url 'hc-profile' %}">Account</a></li>
            {% if show_pricing %}
            <li><a href="{% url 'hc-billing' %}">Billing</a></li>
            {% endif %}
            <li><a href="{% url 'hc-notifications' %}">Email Reports</a></li>
        </ul>
    </div>

    <div class="col-sm-9 col-md-6">
        <div class="panel panel-{{ email_password_status|default:'default' }}">
            <div class="panel-body settings-block">
                <form method="post">
                    {% csrf_token %}
                    <h2>Email and Password</h2>
                    <p>
                        Your account's email address is
                        <code>{{ request.user.email }}</code>

                        <a
                            href="{% url 'hc-change-email' %}"
                            class="btn btn-default pull-right">Change Email</a>
                    </p>
                    <p class="clearfix"></p>
                    <p>
                        Attach a password to your {{ site_name }} account
                        <a
                            href="{% url 'hc-set-password' %}"
                            class="btn btn-default pull-right">Set Password</a>
                    </p>
                </form>
            </div>
            {% if changed_password %}
            <div class="panel-footer">
                Your new password has been set!
            </div>
            {% endif %}
        </div>

        {% if use_2fa %}
        <div class="panel panel-{{ 2fa_status }}">
            <div class="panel-body settings-block">
                <form method="post">
                    {% csrf_token %}
                    <h2>Two-factor Authentication</h2>
                    {% if credentials %}

                    <table id="my-keys" class="table">
                        <tr>
                            <th>Security keys</th>
                        </tr>
                        {% for credential in credentials %}
                        <tr>
                            <td>
                                <strong>{{ credential.name|default:"unnamed" }}</strong>
                                – registered on {{ credential.created|date:"M j, Y" }}
                            </td>
                            <td class="text-right">
                                <a href="{% url 'hc-remove-credential' credential.code  %}">Remove</a>
                            </td>
                        </tr>
                        {% endfor %}
                    </table>

                    {% if credentials|length == 1 %}
                    <p class="alert alert-info">
                        <strong>Tip: add a second key!</strong>
                        It is a good practice to register at least two security keys
                        and store them separately.
                    </p>
                    {% endif %}

                    {% else %}
                    <p>
                        Two-factor authentication is not enabled yet.<br />
                        Your account has no registered security keys.
                    </p>
                    {% endif %}
                    <a
                        href="{% url 'hc-add-credential' %}"
                        class="btn btn-default pull-right">
                        Register New Security Key
                    </a>
                </form>
            </div>

            {% if added_credential_name %}
            <div class="panel-footer">
                Added security key <strong>{{ added_credential_name }}</strong>.
            </div>
            {% endif %}

            {% if removed_credential_name %}
            <div class="panel-footer">
                Removed security key <strong>{{ removed_credential_name }}</strong>.
            </div>
            {% endif %}
        </div>
        {% endif %}

        <div class="panel panel-{{ my_projects_status }}">
            <div class="panel-body settings-block">
                    {% csrf_token %}
                    <h2>My Projects</h2>

                    {% if profile.projects.exists %}
                    <table id="my-projects" class="table">
                        <tr>
                            <th>Project</th>
                            <th>My role</th>
                            <th></th>
                        </tr>
                        {% for project in request.user.project_set.all %}
                        <tr>
                            <td>
                                <div class="name">
                                    <a href="{% url 'hc-checks' project.code %}">
                                        {{ project }}
                                    </a>
                                </div>
                            </td>
                            <td>Owner</td>
                            <td>
                                <a
                                    href="{% url 'hc-project-settings' project.code %}"
                                    class="pull-right">Settings</a>
                            </td>
                        </tr>
                        {% endfor %}

                        {% for m in request.user.memberships.all %}
                        <tr>
                            <td>
                                <div class="name">
                                    <a href="{% url 'hc-checks' m.project.code %}">
                                        {{ m.project }}
                                    </a>
                                </div>
                            </td>
                            <td>
                                {% if m.rw %}
                                Member
                                {% else %}
                                Read-only
                                {% endif %}
                            </td>
                            <td>
                                <a
                                    href="#"
                                    data-name="{{ m.project }}"
                                    data-code="{{ m.project.code }}"
                                    class="pull-right leave-project">Leave</a>
                            </td>
                        </tr>
                        {% endfor %}
                    </table>
                    {% else %}
                        <p>You do not have any projects. Create one!</p>
                    {% endif %}

                    <a
                        id="create-project"
                        href="#"
                        class="btn btn-default pull-right"
                        data-toggle="modal"
                        data-target="#add-project-modal">Create a New Project</a>
            </div>

            {% if left_project %}
            <div class="panel-footer">
                Left project <strong>{{ left_project }}</strong>.
            </div>
            {% endif %}
        </div>

        <div class="panel panel-default">
            <div class="panel-body settings-block">
                    {% csrf_token %}
                    <h2>Close Account</h2>
                    <p>This will permanently remove your {{ site_name }} account.</p>
                    <div class="text-right">
                        <a
                            id="close-account"
                            href="{% url 'hc-close' %}"
                            class="btn btn-default">Close Account</a>
                    </div>
            </div>
        </div>
    </div>
</div>

<div id="leave-project-modal" class="modal">
    <div class="modal-dialog">
        <form id="leave-project-form" method="post">
            {% csrf_token %}
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4>Leave This Project?</h4>
                </div>
                <div class="modal-body">
                    <p>
                        You are about to leave the project <strong id="leave-project-name"></strong>.
                        You will lose access to its checks and integrations.
                    </p>
                    <p>Are you sure?</p>
                    <input
                        type="hidden"
                        name="code"
                        id="leave-project-code" />
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                    <button
                        type="submit"
                        name="leave_project"
                        class="btn btn-danger">Leave Project</button>
                </div>
            </div>
        </form>
    </div>
</div>

{% include "accounts/add_project_modal.html" %}

{% 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/add_project_modal.js' %}"></script>
<script src="{% static 'js/profile.js' %}"></script>
{% endcompress %}
{% endblock %}
