@extends('layouts.app')

@section('title', __('profile'))

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-md-4 mb-4">
            <div class="card border-0 shadow-sm text-center p-4">
                <div class="position-relative d-inline-block mx-auto mb-3">
                    @php($avatar = Auth::user()?->employee?->avatar_url)
                    <img
                        src="{{ $avatar ?: 'https://ui-avatars.com/api/?name='.urlencode(Auth::user()->name).'&size=128&background=06adeb&color=fff' }}"
                        class="rounded-circle shadow-sm"
                        alt="Avatar"
                        style="width: 128px; height: 128px; object-fit: cover;"
                    >
                    <button id="profile-avatar-button" type="button" class="btn btn-sm btn-light position-absolute bottom-0 end-0 rounded-circle shadow-sm border" title="{{ __('employees.fields.avatar') }}" style="width: 32px; height: 32px;">
                        <i class="bi bi-camera-fill text-primary"></i>
                    </button>
                </div>
                @if(Auth::user()?->employee)
                    <form id="profile-avatar-form" method="POST" action="{{ route('profile.avatar.update') }}" enctype="multipart/form-data" class="d-none">
                        @csrf
                        <input id="profile-avatar-input" type="file" name="avatar" accept="image/*">
                    </form>
                @endif
                <h4 class="fw-bold">{{ Auth::user()->name }}</h4>
                <p class="text-muted mb-1">{{ Auth::user()->username }}</p>
                <div class="mt-2">
                    <span class="badge bg-success rounded-pill px-3">{{ __('online') }}</span>
                </div>
            </div>

            @if(is_array($employee_accounts ?? null))
                <div class="card border-0 shadow-sm mt-3">
                    <div class="card-body p-3">
                        <div class="fw-bold mb-1">{{ __('profile.employee_account_title') }}</div>
                        @if(!empty($employee_accounts_period_from) && !empty($employee_accounts_period_to))
                            <div class="text-muted small mb-3">
                                {{ __('accounts.employee_accounts.period') }}: {{ $employee_accounts_period_from->toDateString() }} → {{ $employee_accounts_period_to->toDateString() }}
                            </div>
                        @endif

                        <div class="d-flex flex-column gap-2">
                            <div class="d-flex justify-content-between">
                                <div class="text-muted small">{{ __('accounts.employee_accounts.base_salary') }}</div>
                                <div class="fw-bold">{{ number_format((int) ($employee_accounts['base_salary'] ?? 0)) }}</div>
                            </div>
                            <div class="d-flex justify-content-between">
                                <div class="text-muted small">{{ __('accounts.employee_accounts.commissions_total') }}</div>
                                <div class="fw-bold text-success">{{ number_format((int) ($employee_accounts['commissions_total'] ?? 0)) }}</div>
                            </div>
                            <div class="d-flex justify-content-between">
                                <div class="text-muted small">{{ __('accounts.employee_accounts.bonuses_total') }}</div>
                                <div class="fw-bold text-success">{{ number_format((int) ($employee_accounts['bonuses_total'] ?? 0)) }}</div>
                            </div>
                            <div class="d-flex justify-content-between">
                                <div class="text-muted small">{{ __('accounts.employee_accounts.penalties_total') }}</div>
                                <div class="fw-bold text-danger">{{ number_format((int) ($employee_accounts['penalties_total'] ?? 0)) }}</div>
                            </div>
                            <div class="d-flex justify-content-between">
                                <div class="text-muted small">{{ __('accounts.employee_accounts.withdrawals_total') }}</div>
                                <div class="fw-bold text-danger">{{ number_format((int) ($employee_accounts['withdrawals_total'] ?? 0)) }}</div>
                            </div>
                            <div class="d-flex justify-content-between">
                                <div class="text-muted small">{{ __('accounts.employee_accounts.absences_total') }}</div>
                                <div class="fw-bold text-danger">{{ number_format((int) ($employee_accounts['absences_total'] ?? 0)) }}</div>
                            </div>
                            <hr class="my-2">
                            <div class="d-flex justify-content-between">
                                <div class="text-muted small">{{ __('accounts.employee_accounts.final_net_total') }}</div>
                                <div class="fw-bold">{{ number_format((int) ($employee_accounts['net_total'] ?? 0)) }}</div>
                            </div>
                        </div>
                    </div>
                </div>
            @endif
        </div>
        <div class="col-md-8">
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-white py-3">
                    <h5 class="mb-0 fw-bold">{{ __('profile') }}</h5>
                </div>
                <div class="card-body p-4">
                    @if(session('success'))
                        <div class="alert alert-success alert-dismissible fade show" role="alert">
                            {{ session('success') }}
                            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                        </div>
                    @endif

                    @if ($errors->any())
                        <div class="alert alert-danger">
                            <ul class="mb-0">
                                @foreach ($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                    @endif

                    <form method="POST" action="{{ route('profile.update') }}">
                        @csrf
                        <div class="mb-3">
                            <label class="form-label text-muted small">{{ __('username_label') }}</label>
                            <input type="text" class="form-control bg-light" value="{{ Auth::user()->username }}" readonly>
                        </div>
                        <div class="mb-4">
                            <label class="form-label text-muted small">{{ __('name_label') }}</label>
                            <input type="text" name="name" class="form-control" value="{{ old('name', Auth::user()->name) }}" required>
                        </div>

                        <div class="mt-4 text-end">
                            <button type="submit" class="btn btn-primary px-4 shadow-sm">
                                <i class="bi bi-save me-2"></i> {{ __('save_changes') }}
                            </button>
                        </div>
                    </form>

                    <hr class="my-4 text-muted opacity-25">

                    <form method="POST" action="{{ route('profile.password.update') }}">
                        @csrf
                        <h5 class="fw-bold mb-4 text-primary">{{ __('change_password') }}</h5>

                        <div class="mb-3">
                            <label class="form-label text-muted small">{{ __('current_password') }}</label>
                            <input type="password" name="current_password" class="form-control" autocomplete="current-password" required>
                        </div>
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label text-muted small">{{ __('new_password') }}</label>
                                <input type="password" name="password" class="form-control" autocomplete="new-password" required>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label text-muted small">{{ __('confirm_password') }}</label>
                                <input type="password" name="password_confirmation" class="form-control" autocomplete="new-password" required>
                            </div>
                        </div>

                        <div class="mt-3 text-end">
                            <button type="submit" class="btn btn-outline-primary px-4 shadow-sm">
                                <i class="bi bi-key me-2"></i> {{ __('save_changes') }}
                            </button>
                        </div>
                    </form>

                        <hr class="my-4 text-muted opacity-25">
                        
                        <div class="d-flex justify-content-between align-items-center bg-light p-3 rounded border">
                            <div class="d-flex align-items-center">
                                <div class="bg-white p-2 rounded shadow-sm me-3 ms-0">
                                    <i class="bi bi-shield-lock-fill fs-4 text-primary"></i>
                                </div>
                                <div>
                                    <h6 class="fw-bold mb-1">{{ __('two_factor_auth') }}</h6>
                                    <p class="text-muted small mb-0">Google Authenticator</p>
                                    @if((string) (Auth::user()->two_factor_pending_secret ?? '') !== '' && ! (bool) (Auth::user()->two_factor_enabled ?? false))
                                        <p class="text-muted small mb-0">{{ __('profile.two_factor_pending_hint') }}</p>
                                    @endif
                                </div>
                            </div>
                            <div class="d-flex align-items-center gap-2">
                                @if((bool) (Auth::user()->two_factor_enabled ?? false))
                                    <span class="badge bg-success">{{ __('common.active') }}</span>
                                @elseif((string) (Auth::user()->two_factor_pending_secret ?? '') !== '')
                                    <span class="badge bg-warning text-dark">{{ __('common.pending') }}</span>
                                @else
                                    <span class="badge bg-secondary">{{ __('common.inactive') }}</span>
                                @endif

                                @if((bool) (Auth::user()->two_factor_enabled ?? false))
                                    <button type="button" class="btn btn-sm btn-outline-danger" data-bs-toggle="modal" data-bs-target="#twoFactorDisableModal">
                                        {{ __('common.disable') }}
                                    </button>
                                @elseif((string) (Auth::user()->two_factor_pending_secret ?? '') !== '')
                                    <a href="{{ route('logout') }}" class="btn btn-sm btn-outline-primary">
                                        {{ __('common.logout') }}
                                    </a>
                                @else
                                    <form method="POST" action="{{ route('profile.two-factor.start') }}">
                                        @csrf
                                        <button type="submit" class="btn btn-sm btn-outline-primary">
                                            {{ __('common.enable') }}
                                        </button>
                                    </form>
                                @endif
                            </div>
                        </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="twoFactorDisableModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content border-0 shadow">
            <div class="modal-header">
                <h5 class="modal-title fw-bold">{{ __('two_factor_auth') }}</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form method="POST" action="{{ route('profile.two-factor.disable') }}">
                    @csrf
                    <div class="mb-3">
                        <label class="form-label">{{ __('current_password') }}</label>
                        <input type="password" name="current_password" class="form-control" autocomplete="current-password" required>
                    </div>
                    <div class="text-end">
                        <button type="submit" class="btn btn-danger">{{ __('common.disable') }}</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

@push('scripts')
<script>
    document.addEventListener('DOMContentLoaded', function () {
        const button = document.getElementById('profile-avatar-button');
        const input = document.getElementById('profile-avatar-input');
        const form = document.getElementById('profile-avatar-form');

        if (!button || !input || !form) return;

        button.addEventListener('click', function () {
            input.click();
        });

        input.addEventListener('change', function () {
            if (input.files && input.files.length > 0) {
                form.submit();
            }
        });
    });
</script>
@endpush
@endsection
