@extends('layouts.admin')
@section('title', 'Profil')

@section('content')
    {{-- ALERT SUKSES --}}
    @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

    {{-- ALERT ERROR UMUM (Jika ada error selain validasi modal) --}}
    @if (session('error'))
        <div class="alert alert-danger alert-dismissible fade show" role="alert">
            {{ session('error') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    @endif

    <section id="multiple-column-form">
        <div class="row match-height">

            <div class="col-12">
                <h4 class="card-title">Edit Profil</h4>
                <div class="card">
                    <div class="card-content">
                        <div class="card-body">
                            <form class="form" id="formProfil" action="{{ route('admin.profil.simpan') }}" method="POST">
                                @csrf
                                @method('PUT')

                                <div class="col-12">
                                    <div class="row gx-2">
                                        <div class="col-12 col-md-6">
                                            <div class="mb-2">
                                                <label class="form-label">Nama Lengkap</label>
                                                <input type="text"
                                                    class="form-control @error('nama', 'updateProfil') is-invalid @enderror"
                                                    name="nama" value="{{ old('nama', $user->nama) }}"
                                                    style="font-size: 13px; padding: 8px;"
                                                    placeholder="Masukkan Nama Lengkap">

                                                @error('nama', 'updateProfil')
                                                    <div class="invalid-feedback">{{ $message }}</div>
                                                @enderror
                                            </div>
                                        </div>

                                        <div class="col-12 col-md-6">
                                            <div class="mb-2">
                                                <label class="form-label">Username</label>
                                                <input type="text"
                                                    class="form-control @error('username', 'updateProfil') is-invalid @enderror"
                                                    name="username" value="{{ old('username', $user->username) }}"
                                                    style="font-size: 13px; padding: 8px;" placeholder="Masukkan Username">

                                                @error('username', 'updateProfil')
                                                    <div class="invalid-feedback">{{ $message }}</div>
                                                @enderror
                                            </div>
                                        </div>
                                    </div>

                                    <div class="row gx-2">
                                        <div class="col-12 col-md-4">
                                            <div class="mb-2">
                                                <label class="form-label">Nomor WA</label>
                                                <input type="number"
                                                    class="form-control @error('no_wa', 'updateProfil') is-invalid @enderror"
                                                    name="no_wa" value="{{ old('no_wa', $user->no_wa) }}"
                                                    style="font-size: 13px; padding: 8px;" placeholder="Masukkan Nomor WA">

                                                @error('no_wa', 'updateProfil')
                                                    <div class="invalid-feedback">{{ $message }}</div>
                                                @enderror
                                            </div>
                                        </div>

                                        <div class="col-12 col-md-4">
                                            <div class="mb-2">
                                                <label class="form-label">Email</label>
                                                <input type="email"
                                                    class="form-control @error('email', 'updateProfil') is-invalid @enderror"
                                                    name="email" value="{{ old('email', $user->email) }}"
                                                    style="font-size: 13px; padding: 8px;" placeholder="Masukkan Email">

                                                @error('email', 'updateProfil')
                                                    <div class="invalid-feedback">{{ $message }}</div>
                                                @enderror
                                            </div>
                                        </div>

                                        <div class="col-12 col-md-4">
                                            <div class="mb-2">
                                                <label class="form-label">Peran</label>
                                                <input type="text" class="form-control bg-light"
                                                    value="{{ ucwords(str_replace('_', ' ', $user->role ?? 'Admin')) }}"
                                                    style="font-size: 14px;" readonly>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="mb-3">
                                        <label class="form-label">Alamat</label>
                                        <input type="text"
                                            class="form-control @error('alamat', 'updateProfil') is-invalid @enderror"
                                            name="alamat" value="{{ old('alamat', $user->alamat) }}"
                                            style="font-size: 14px;" placeholder="Masukkan Alamat Lengkap">

                                        @error('alamat', 'updateProfil')
                                            <div class="invalid-feedback">{{ $message }}</div>
                                        @enderror
                                    </div>
                                </div>
                                <div class="col-12 d-flex justify-content-end mt-2 gap-2">

                                    <button type="reset" class="btn btn-light batal rounded-pill py-2 px-4">
                                        Batal
                                    </button>

                                    <button type="submit" class="btn btn-primary kirim-wa rounded-pill py-2 px-4">
                                        Simpan
                                    </button>

                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-12">
                <h4 class="card-title">Ubah Kata Sandi</h4>
                <div class="card">
                    <div class="card-content">
                        <div class="card-body">
                            <form class="form" id="formPassword" action="{{ route('admin.profil.password') }}"
                                method="POST">
                                @csrf
                                @method('PUT')

                                <div class="row">
                                    <div class="col-12 col-md-6">
                                        <div class="mb-3">
                                            <label class="form-label">Kata Sandi Baru</label>

                                            <div class="position-relative">
                                                <input type="password"
                                                    class="form-control @error('password', 'updatePassword') is-invalid @enderror"
                                                    id="passBaru" name="password" placeholder="**************"
                                                    style="padding-right: 40px; font-size: 13px;">

                                                <span class="position-absolute top-50 end-0 translate-middle-y me-3"
                                                    onclick="toggleDynamic('passBaru', 'iconBaru')"
                                                    style="cursor: pointer;">
                                                    <i class="bi bi-eye text-secondary" style="font-size: 13px"
                                                        id="iconBaru"></i>
                                                </span>
                                            </div>

                                            @error('password', 'updatePassword')
                                                <div class="invalid-feedback d-block">{{ $message }}</div>
                                            @enderror
                                        </div>
                                    </div>

                                    <div class="col-12 col-md-6">
                                        <div class="mb-3">
                                            <label class="form-label">Konfirmasi Kata Sandi Baru</label>

                                            <div class="position-relative">
                                                <input type="password"
                                                    class="form-control @error('password', 'updatePassword') is-invalid @enderror"
                                                    id="passKonfirm" name="password_confirmation"
                                                    placeholder="**************"
                                                    style="padding-right: 40px; font-size: 13px">

                                                <span class="position-absolute top-50 end-0 translate-middle-y me-3"
                                                    onclick="toggleDynamic('passKonfirm', 'iconKonfirm')"
                                                    style="cursor: pointer;">
                                                    <i class="bi bi-eye text-secondary" style="font-size: 13px"
                                                        id="iconKonfirm"></i>
                                                </span>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div class="col-12 d-flex justify-content-end mt-2 gap-2">

                                    <button type="reset" class="btn btn-light batal rounded-pill py-2 px-4">
                                        Batal
                                    </button>

                                    <button type="submit" class="btn btn-primary kirim-wa rounded-pill py-2 px-4">
                                        Simpan
                                    </button>

                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </section>

    <script>
        function toggleDynamic(inputId, iconId) {
            const passwordInput = document.getElementById(inputId);
            const eyeIcon = document.getElementById(iconId);

            if (passwordInput.type === 'password') {
                passwordInput.type = 'text';
                eyeIcon.classList.remove('bi-eye');
                eyeIcon.classList.add('bi-eye-slash');
            } else {
                passwordInput.type = 'password';
                eyeIcon.classList.remove('bi-eye-slash');
                eyeIcon.classList.add('bi-eye');
            }
        }
    </script>
@endsection
