<?= $this->extend('layouts/main') ?>

<?= $this->section('title') ?>
Profil Saya
<?= $this->endSection() ?>

<?= $this->section('page_title') ?>
Pengaturan Profil
<?= $this->endSection() ?>

<?= $this->section('content') ?>
<div class="row justify-content-center">
    <div class="col-lg-8">
        <div class="glass-card">
            <div class="d-flex align-items-center gap-4 mb-5">
                <div class="position-relative">
                    <?php if (!empty($user['photo'])): ?>
                        <img id="profilePreview" src="<?= base_url('uploads/profile/' . $user['photo']) ?>"
                            class="rounded-circle shadow-lg border border-3 border-white"
                            style="width: 120px; height: 120px; object-fit: cover;">
                    <?php else: ?>
                        <div id="profilePlaceholder"
                            class="rounded-circle bg-primary bg-opacity-10 d-flex align-items-center justify-content-center border border-3 border-white shadow-lg"
                            style="width: 120px; height: 120px;">
                            <i class="bi bi-person-fill text-primary display-4"></i>
                        </div>
                    <?php endif; ?>
                    <button type="button"
                        class="btn btn-primary btn-sm rounded-circle position-absolute bottom-0 end-0 shadow-sm"
                        onclick="document.getElementById('photoInput').click()">
                        <i class="bi bi-camera-fill"></i>
                    </button>
                </div>
                <div>
                    <h3 class="mb-1">
                        <?= $user['name'] ?>
                    </h3>
                    <p class="text-secondary mb-0">
                        <?= $user['nip'] ?> •
                        <?= session()->get('roles')[0] ?>
                    </p>
                </div>
            </div>

            <form action="<?= base_url('auth/profile/update') ?>" method="POST" enctype="multipart/form-data">
                <?= csrf_field() ?>

                <input type="file" id="photoInput" name="photo" class="d-none" accept="image/*"
                    onchange="previewImage(this)">

                <div class="row g-4">
                    <div class="col-md-6">
                        <label class="form-label small fw-bold text-secondary">NAMA LENGKAP</label>
                        <input type="text" name="name" class="form-control rounded-pill px-4 py-2"
                            value="<?= old('name', $user['name']) ?>" required>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label small fw-bold text-secondary">EMAIL</label>
                        <input type="email" name="email" class="form-control rounded-pill px-4 py-2"
                            value="<?= old('email', $user['email']) ?>" required>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label small fw-bold text-secondary">NO. WHATSAPP</label>
                        <input type="text" name="no_hp" class="form-control rounded-pill px-4 py-2"
                            value="<?= old('no_hp', $user['no_hp']) ?>" placeholder="628xxxxxxx">
                    </div>
                    <div class="col-md-6">
                        <label class="form-label small fw-bold text-secondary">NIP (TIDAK BISA DIUBAH)</label>
                        <input type="text" class="form-control rounded-pill px-4 py-2 bg-light"
                            value="<?= $user['nip'] ?>" readonly>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label small fw-bold text-secondary">PASSWORD BARU (KOSONGKAN JIKA TIDAK
                            DIUBAH)</label>
                        <input type="password" name="password" class="form-control rounded-pill px-4 py-2"
                            placeholder="********">
                    </div>
                </div>

                <?php if (session()->get('errors')): ?>
                    <div class="alert alert-danger mt-4 rounded-4 border-0">
                        <ul class="mb-0 small">
                            <?php foreach (session()->get('errors') as $error): ?>
                                <li>
                                    <?= $error ?>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    </div>
                <?php endif; ?>

                <?php if (session()->get('success')): ?>
                    <div class="alert alert-success mt-4 rounded-4 border-0 py-3">
                        <i class="bi bi-check-circle-fill me-2"></i>
                        <?= session()->get('success') ?>
                    </div>
                <?php endif; ?>

                <div class="mt-5 d-flex gap-3">
                    <button type="submit" class="btn btn-primary rounded-pill px-5 py-2 fw-bold shadow">SIMPAN
                        PERUBAHAN</button>
                    <a href="<?= base_url('user/dashboard') ?>" class="btn btn-light rounded-pill px-4 py-2">KEMBALI</a>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    function previewImage(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
                var preview = document.getElementById('profilePreview');
                var placeholder = document.getElementById('profilePlaceholder');

                if (preview) {
                    preview.src = e.target.result;
                } else if (placeholder) {
                    // Create image if placeholder exists
                    var img = document.createElement('img');
                    img.id = 'profilePreview';
                    img.src = e.target.result;
                    img.className = 'rounded-circle shadow-lg border border-3 border-white';
                    img.style = 'width: 120px; height: 120px; object-fit: cover;';
                    placeholder.parentNode.replaceChild(img, placeholder);
                }
            }
            reader.readAsDataURL(input.files[0]);
        }
    }
</script>
<?= $this->endSection() ?>