{% extends 'base.html.twig' %}

{% block title %}
    {{ 'profile.title'|trans }}
{% endblock %}

{% block body %}
    <h1>
        <i class="fa fa-user" aria-hidden="true"></i>
        {{ 'profile.title'|trans }}
        <small>
            {{ 'profile.subtitle'|trans }}
        </small>
    </h1>
    
    <hr>
    
    {% include 'flashMessages.html.twig' %}

    {{ form_start(form) }}        
        <div class="row">
            <div class="col-5">
                <fieldset>
                    <legend>{{ 'profile.general'|trans }}</legend>
                    
                    <p class="text-secondary">{{ 'profile.general.description'|trans }}</p>
                    
                    <div class="mb-3">
                        <label>{{ 'profile.field.username'|trans }}</label>
                        <input type="text" class="form-control" value="{{user.login}}" readonly>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-4">
                            {{ form_row(form.nome) }}
                        </div>
                        <div class="col-md-8">
                            {{ form_row(form.sobrenome) }}
                        </div>
                    </div>

                    {{ form_row(form.email) }}
                </fieldset>
            </div>
            <div class="col-6 ml-auto">
                <fieldset>
                    <legend>{{ 'profile.lotation'|trans }}</legend>
                    
                    <p class="text-secondary">{{ 'profile.lotation.description'|trans }}</p>
                    
                    <table class="table table-bordered">
                        <thead class="thead-light">
                            <tr>
                                <th>
                                    {{ 'label.unity'|trans }}
                                </th>
                                <th>
                                    {{ 'label.role'|trans }}
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for lotacao in user.lotacoes %}
                                <tr>
                                    <td>
                                        {{lotacao.unidade}}
                                    </td>
                                    <td>
                                        {{lotacao.perfil}}
                                    </td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </fieldset>
            </div>
        </div>
        
        <div class="d-flex">
            <button type="submit" class="btn btn-primary">
                <i class="fa fa-save"></i>
                {{ 'button.update'|trans }}
            </button>
            
            <button type="button" class="btn btn-secondary ms-2" data-bs-toggle="modal" data-bs-target="#dialog-senha">
                <i class="fa fa-lock"></i>
                {{ 'profile.button.change_password'|trans }}
            </button>
        </div>
    
    {{ form_end(form) }}
    
    <div id="dialog-senha" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <form id="senha-form">
                    <div class="modal-header">
                        <h5 class="modal-title">{{ 'profile.change_password'|trans }}</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <div class="mb-3">
                            <label for="senha_atual">{{ 'profile.field.current_password'|trans }}</label>
                            <input id="senha_atual" type="password" class="form-control" required>
                        </div>
                        <div class="mb-3">
                            <label for="senha_senha">{{ 'profile.field.new_password'|trans }}</label>
                            <input id="senha_senha" type="password" class="form-control" required>
                        </div>
                        <div class="mb-3">
                            <label for="senha_confirmacao">{{ 'profile.field.password_confirm'|trans }}</label>
                            <input id="senha_confirmacao" type="password" class="form-control" required>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary">
                            {{ 'profile.button.change_password'|trans }}
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
{% endblock %}

{% block stylesheets %}
    {{ parent() }}
    <style>
        body>section {
            padding: 20px 10px;
        }
    </style>
{% endblock %}

{% block javascripts %}
    {{ parent() }}
    <script>
        const senhaForm = document.getElementById('senha-form');
        const senhaAtual = document.getElementById('senha_atual');
        const senhaNova = document.getElementById('senha_senha');
        const senhaConfirmacao = document.getElementById('senha_confirmacao');

        senhaForm.addEventListener('submit', (e) => {
            e.preventDefault();
            App.ajax({
                url: App.url('/profile/password'),
                type: 'post',
                data: {
                    atual: senhaAtual.value,
                    senha: senhaNova.value,
                    confirmacao: senhaConfirmacao.value,
                },
                success: function () {
                    senhaForm.reset();
                    document.querySelector('#dialog-senha button.btn-close').click();
                }
            });
        });
    </script>
{% endblock %}
