<div>
    <x-title>Security</x-title>
    <x-account.tabs/>

    @if(session('success'))
        <div class="alert alert-success">{{ session('success') }}</div>
    @endif

    <div class="row">
        <div class="col-12 col-md-9">

            {{-- PASSWORD --}}
            <div class="card mb-4">
                <div class="card-body">
                    <h5 class="mb-3">Change Password</h5>
                    <x-ui.input type="password" name="current_password" label="Current Password"/>

                    <div class="row">
                        <div class="col-6">
                            <x-ui.input type="password" name="new_password" label="New Password"/>
                        </div>
                        <div class="col-6">

                            <x-ui.input type="password" name="new_password_confirmation" label="Confirm New Password"/>
                        </div>
                    </div>

                    <button class="btn btn-primary" wire:click="updatePassword">
                        Update Password
                    </button>
                </div>
            </div>

            {{-- TWO-FACTOR AUTH --}}
            <div class="card">
                <div class="card-body">
                    <h5 class="mb-2">Two-Factor Authentication</h5>

                    <p class="text-muted small">
                        Protect your account with an authenticator app
                        like Authy, Google Authenticator or 1Password.
                    </p>

                    {{-- DISABLED --}}
                    @if(!$twoFactorEnabled)
                        <button class="btn btn-success" wire:click="enable2fa">
                            Enable 2FA
                        </button>

                        {{-- ENABLED BUT NOT CONFIRMED --}}
                    @elseif($twoFactorEnabled && !$twoFactorConfirmed)

                        <div class="alert alert-warning mt-3 small">
                            Scan the QR code below and enter the 6-digit code
                            to confirm two-factor authentication.
                        </div>

                        <div class="border rounded p-3 mb-3" style="max-width: 360px;">
                            {!! $qrCode !!}
                        </div>

                        <div class="row align-items-end">
                            <div class="col-6">
                                <x-ui.input name="twoFactorCode" label="Two Factor Code"/>

                                <div class="flex">
                                    <button class="btn btn-primary"
                                            wire:click="confirm2fa">
                                        Confirm 2FA
                                    </button>
                                    <button class="btn btn-outline-danger ml-3"
                                            wire:click="disable2fa"
                                            onclick="return confirm('Cancel and disable 2FA?')">
                                        Cancel
                                    </button>
                                </div>
                            </div>
                        </div>

                        {{-- ENABLED & CONFIRMED --}}
                    @else
                        <div class="d-flex flex-wrap gap-2">
                            <button class="btn btn-outline-secondary"
                                    wire:click="regenerateRecoveryCodes"
                                    onclick="return confirm('Regenerate recovery codes?')">
                                Regenerate Recovery Codes
                            </button>

                            <button class="btn btn-outline-danger"
                                    wire:click="disable2fa"
                                    onclick="return confirm('Disable two-factor authentication?')">
                                Disable 2FA
                            </button>
                        </div>

                        <hr class="my-3">

                        <h6 class="mb-2">Recovery Codes</h6>

                        <div class="row">
                            @foreach($recoveryCodes as $code)
                                <div class="col-6 col-md-4 mb-2">
                                    <code class="d-block p-2 border rounded bg-light">
                                        {{ $code }}
                                    </code>
                                </div>
                            @endforeach
                        </div>
                    @endif
                </div>
            </div>

        </div>

        {{-- SIDEBAR --}}
        <div class="col-12 col-md-3">
            <div class="card">
                <div class="card-body">
                    <h4 class="card-title">Security Tip</h4>
                    <hr>
                    <p class="text-muted small mb-0">
                        Save your recovery codes somewhere secure.
                        If you lose access to your authenticator,
                        they are your only way back in.
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>
