{% 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-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>

                        <button
                            type="submit"
                            name="change_email"
                            class="btn btn-default pull-right">Change Email</button>
                    </p>
                    <p class="clearfix"></p>
                    <p>
                        Attach a password to your {% site_name %} account
                        <button
                            type="submit"
                            name="set_password"
                            class="btn btn-default pull-right">Set Password</button>
                    </p>
                </form>
            </div>
        </div>

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

                    {% if projects %}
                    <table id="my-projects" class="table">
                        <tr>
                            <th>Project</th>
                            <th>My role</th>
                            <th></th>
                        </tr>
                        {% for project in profile.projects %}
                        <tr>
                            <td>
                                <div class="name">
                                    <a href="{% url 'hc-checks' project.code %}">
                                        {{ project }}
                                    </a>
                                </div>
                            </td>
                            <td>
                                {% if project.owner == request.user %}
                                Owner
                                {% else %}
                                Collaborator
                                {% endif %}
                            </td>
                            <td>
                                {% if project.owner == request.user %}
                                <a
                                    href="{% url 'hc-project-settings' project.code %}"
                                    class="pull-right">Settings</a>
                                {% else %}
                                <a
                                    href="#"
                                    data-name="{{ project }}"
                                    data-code="{{ project.code }}"
                                    class="pull-right leave-project">Leave</a>
                                {% endif %}
                            </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 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>
                    <a
                        id="close-account"
                        href="#"
                        class="btn btn-default pull-right"
                        data-toggle="modal"
                        data-target="#close-account-modal">Close Account</a>
                    This will permanently remove your {% site_name %} account
            </div>
        </div>
    </div>
</div>

<div id="close-account-modal" class="modal">
    <div class="modal-dialog">
        <form method="post" action="{% url 'hc-close' %}">
            {% csrf_token %}
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                    <h4>Close Account?</h4>
                </div>
                <div class="modal-body">
                    <p></p>
                    <p>You are about to permanently remove
                    the account <strong>{{ request.user.email }}</strong> and all
                    of its associated projects, 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">Close Account</button>
                </div>
            </div>
        </form>
    </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/profile.js' %}"></script>
{% endcompress %}
{% endblock %}
