@php
use App\Enums\RoleEnum;
use Modules\CabBooking\Enums\RoleEnum as ModuleRole;
$settings = getSettings();
$roleCredentials = getRoleCredentials();
@endphp
@extends('auth.master')
@section('title', __('static.login'))
@section('content')
<section class="auth-page">
    @if (env('APP_VERSION'))
    <span class="ms-auto d-flex badge badge-version-primary">{{ __('static.version') }}{{ env('APP_VERSION') }}</span>
    @endif
    <div class="container">
        <div class="auth-main">
            <div class="auth-card">
                <div class="text-center">
                    @if (isset(getSettings()['general']['dark_logo_image']))
                    <img class="login-img" src="{{ getSettings()['general']['dark_logo_image']?->original_url }}" alt="logo" loading="lazy">
                    @else
                    <h2>{{ config('app.name') }}</h2>
                    @endif
                </div>
                <div class="welcome">
                    <h3>{{ __('static.welcome', ['appName' => config('app.name')]) }}</h3>
                    <p>{{ __('static.information') }}</p>
                </div>
                @error('login')
                <div>
                    <span class="invalid-feedback d-block" role="alert">
                    <strong>{{ $message }}</strong>
                </div>
                </span>
                @enderror
                <div class="main">
                    <form id="loginForm" action="{{ route('login') }}" method='POST'>
                        @csrf
                        <div class="form-group">
                            <i class="ri-mail-line divider"></i>
                            <div class="position-relative">
                                <input type="email" class="form-control" id="email" name="email" placeholder="{{ __('static.enter_email') }}" required>
                            </div>
                            @error('email')
                            <span class="invalid-feedback d-block" role="alert">
                                <strong>{{ $message }}</strong>
                            </span>
                            @enderror
                        </div>
                        <div class="form-group">
                            <i class="ri-lock-line divider"></i>
                            <div class="position-relative">
                                <input type="password" class="form-control input-icon" id="password" name="password"
                                    placeholder="{{ __('static.enter_password') }}" required>
                                <i class="ri-eye-line toggle-password"></i>
                            </div>
                            @error('password')
                            <span class="invalid-feedback d-block" role="alert">
                                <strong>{{ $message }}</strong>
                            </span>
                            @enderror
                        </div>
                        @if (Route::has('password.request'))
                        <div class="form-terms form-group">
                            <div class="d-flex align-items-center">
                                <div class="form-check p-0">
                                    <input type="checkbox" class="item-checkbox form-check-input me-2" id="remember">
                                    <label for="remember">{{ __('static.remember_me') }}</label>
                                </div>
                            </div>
                            <a href="{{ route('password.request') }}" class="forgot-pass">{{ __('static.users.lost_your_password') }}</a>
                        </div>
                        @endif
                        @if (isset($settings['captcha']['status']) && $settings['captcha']['status'])
                        <div class="form-group">
                            <div class="captcha-container d-flex align-items-center gap-2">
                                <input type="text" class="form-control mt-2" id="captcha" name="captcha"
                                    placeholder="{{ __('static.enter_captcha') }}" required>
                                <div class="captcha-img">
                                    {!! captcha_img() !!}
                                </div>
                                <button type="button" class="btn btn-solid p-2 refresh-captcha" id="refresh-captcha">
                                    <i class="ri-refresh-line"></i>
                                </button>
                            </div>
                            @error('captcha')
                            <span class="invalid-feedback d-block" role="alert">
                                <strong>{{ $message }}</strong>
                            </span>
                            @enderror
                        </div>
                        @endif
                        <button type="submit" class="btn btn-solid justify-content-center w-100 spinner-btn mt-0">
                            {{ __('static.login') }}
                        </button>
                    </form>
                </div>
                {{-- @isset($settings['activation']['default_credentials'])
                @if ((int) $settings['activation']['default_credentials'])
                <div class="demo-credential">
                    @foreach ($roleCredentials as $role)
                    <button class="btn btn-solid default-credentials" data-email="{{ $role['email'] }}" data-password="{{ $role['password'] ?? '123456789' }}">
                        {{ ucfirst($role['role']) }}
                    </button>
                    @endforeach
                </div>
                @endif
                @endisset --}}
                <div class="demo-credential">
                    <button class="btn btn-solid default-credentials" data-email="admin@example.com" data-password="123456789">
                        <i class="ri-user-3-fill"></i>
                        {{ __('static.roles.admin') }}
                    </button>
                    <button class="btn btn-solid default-credentials" data-email="driver@example.com" data-password="driver@123">
                        <i class="ri-user-3-fill"></i>
                        {{ __('static.roles.driver') }}
                    </button>
                    <button class="btn btn-solid default-credentials" data-email="dispatcher@example.com" data-password="dispatcher@123">
                        <i class="ri-user-3-fill"></i>
                        {{ __('static.roles.dispatcher') }}
                    </button>
                    <button class="btn btn-solid default-credentials" data-email="fleet@example.com" data-password="fleet@123">
                        <i class="ri-user-3-fill"></i>
                        {{ __('static.roles.fleet') }}
                    </button>
                </div>
                <h6 class="new-account">{{ __('cabbooking::front.don’t_have_account') }}
                    <a
                        href="{{ route('front.cab.register.index') }}">{{ __('cabbooking::front.sign_up') }}
                    </a>
                </h6>
            </div>
        </div>
    </div>
</section>
@endsection

@push('scripts')
<script>
    (function($) {
        "use strict";
        $(document).ready(function() {
            $('#loginForm').validate({
                rules: {
                    email: {
                        required: true,
                        email: true,
                    },
                    password: {
                        required: true
                    },
                }
            });

            $(".default-credentials").click(function() {
                $("#email").val("");
                $("#password").val("");
                var email = $(this).data("email");
                var password = $(this).data("password");
                $("#email").val(email);
                $("#password").val(password);
            });

            $('#refresh-captcha').click(function() {
                $.ajax({
                    type: 'GET',
                    url: '{{ url("/refresh-captcha") }}',
                    success: function(data) {
                        $(".captcha-img").html(data.captcha);
                    }
                });
            });
        });
    })(jQuery);
</script>
@endpush
