{% extends "themis/base.html" %}
{% load socialaccount %}

{% block title %}Log In — {{ themis_app_name }}{% endblock %}

{% block content %}
<div class="flex justify-center items-center min-h-[60vh]">
    <div class="card bg-base-200 shadow-xl w-full max-w-md">
        <div class="card-body">
            <h2 class="card-title text-2xl justify-center mb-4">Log In</h2>

            {% if messages %}
            {% for message in messages %}
            <div class="alert alert-{{ message.tags|default:'info' }} mb-4">
                <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 shrink-0" fill="none"
                     viewBox="0 0 24 24" stroke="currentColor">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                          d="M12 9v2m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
                </svg>
                <span>{{ message }}</span>
            </div>
            {% endfor %}
            {% endif %}

            {% if CASDOOR_ENABLED %}
            <form method="post" action="{% provider_login_url 'casdoor' %}">
                {% csrf_token %}
                <div class="form-control mt-2">
                    <button type="submit" class="btn btn-primary w-full">Sign in with SSO</button>
                </div>
            </form>
            {% endif %}

            {% if ALLOW_LOCAL_LOGIN or user.is_superuser %}

            {% if CASDOOR_ENABLED %}
            <div class="divider">or</div>
            {% endif %}

            {% if form.errors %}
            <div class="alert alert-error mb-4">
                <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 shrink-0" fill="none"
                     viewBox="0 0 24 24" stroke="currentColor">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                          d="M12 9v2m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
                </svg>
                <span>Invalid email or password. Please try again.</span>
            </div>
            {% endif %}

            <form method="post" action="{% url 'account_login' %}">
                {% csrf_token %}

                <div class="form-control mb-4">
                    <label class="label" for="id_login">
                        <span class="label-text">Email</span>
                    </label>
                    <input type="email" name="login" id="id_login"
                           class="input input-bordered w-full{% if form.errors %} input-error{% endif %}"
                           value="{{ form.login.value|default:'' }}"
                           autofocus required>
                </div>

                <div class="form-control mb-6">
                    <label class="label" for="id_password">
                        <span class="label-text">Password</span>
                    </label>
                    <input type="password" name="password" id="id_password"
                           class="input input-bordered w-full{% if form.errors %} input-error{% endif %}"
                           required>
                    <label class="label">
                        <a href="{% url 'account_reset_password' %}" class="label-text-alt link link-hover">
                            Forgot password?
                        </a>
                    </label>
                </div>

                {% if next %}
                <input type="hidden" name="next" value="{{ next }}">
                {% endif %}

                <div class="form-control mt-2">
                    <button type="submit" class="btn btn-outline w-full">Sign In Locally</button>
                </div>
            </form>

            {% endif %}

        </div>
    </div>
</div>
{% endblock %}
