<?= $this->extend('layout/template_siswa') ?>
<?= $this->section('page_title') ?>Pendaftaran Anggota<?= $this->endSection() ?>
<?= $this->section('content') ?>

<?php
    $role = session()->get('role');
    $roleLabel = ($role == 'guru') ? 'Guru' : 'Siswa';
    $jenisLabel = ($role == 'guru') ? 'Guru' : 'Siswa';
?>

<div class="container-fluid">

    <!-- HEADER -->
    <div class="d-flex justify-content-between align-items-center mb-3">
        <div>
            <h4 class="font-weight-bold mb-1">
                <i class="fas fa-user-plus text-primary mr-2"></i>
                Pendaftaran Anggota
            </h4>
            <small class="text-muted">
                Isi formulir untuk menjadi anggota perpustakaan
            </small>
        </div>
    </div>

    <!-- FLASH MESSAGE -->
    <?php if (session()->getFlashdata('success')) : ?>
        <div class="alert alert-success alert-dismissible fade show shadow-sm" role="alert">
            <i class="fas fa-check-circle mr-2"></i>
            <?= session()->getFlashdata('success') ?>
            <button type="button" class="close" data-dismiss="alert">
                <span>&times;</span>
            </button>
        </div>
    <?php endif; ?>

    <?php if (session()->getFlashdata('error')) : ?>
        <div class="alert alert-danger alert-dismissible fade show shadow-sm" role="alert">
            <i class="fas fa-exclamation-circle mr-2"></i>
            <?= session()->getFlashdata('error') ?>
            <button type="button" class="close" data-dismiss="alert">
                <span>&times;</span>
            </button>
        </div>
    <?php endif; ?>

    <?php if (!empty($sudah)) : ?>

        <!-- SUDAH TERDAFTAR -->
        <div class="card shadow-sm border-0">
            <div class="card-body text-center py-5">
                <div class="mb-3">
                    <i class="fas fa-check-circle text-success" style="font-size:70px;"></i>
                </div>
                <h4 class="font-weight-bold text-success">
                    Anda Sudah Terdaftar
                </h4>
                <p class="text-muted">
                    Anda sudah terdaftar sebagai anggota perpustakaan
                    sejak <strong><?= esc(date('d-m-Y', strtotime($sudah['created_at']))) ?></strong>.
                </p>

                <div class="row justify-content-center mt-4">
                    <div class="col-md-6">
                        <table class="table table-sm table-bordered text-left">
                            <tr>
                                <th class="bg-light" width="40%">Nama</th>
                                <td><?= esc($sudah['nama']) ?></td>
                            </tr>
                            <tr>
                                <th class="bg-light">NISN / NIP</th>
                                <td><?= esc($sudah['nisn']) ?: '-' ?></td>
                            </tr>
                            <tr>
                                <th class="bg-light">Jenis</th>
                                <td><?= esc($sudah['jenis']) ?></td>
                            </tr>
                            <tr>
                                <th class="bg-light">Kelas</th>
                                <td><?= esc($sudah['kelas']) ?: '-' ?></td>
                            </tr>
                            <tr>
                                <th class="bg-light">Alamat</th>
                                <td><?= esc($sudah['alamat']) ?: '-' ?></td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
        </div>

    <?php else : ?>

        <!-- FORM PENDAFTARAN -->
        <div class="card shadow-sm border-0">
            <div class="card-header bg-white border-bottom">
                <h5 class="mb-0 font-weight-bold">
                    <i class="fas fa-id-card text-primary mr-2"></i>
                    Formulir Pendaftaran
                </h5>
                <small class="text-muted">
                    Data akan langsung tersimpan di data anggota perpustakaan
                </small>
            </div>

            <div class="card-body">
                <form action="<?= base_url('katalog/store-daftar') ?>" method="post" enctype="multipart/form-data">
                    <?= csrf_field() ?>

                    <div class="row">

<!-- FOTO -->
                        <div class="col-md-4">
                            <div class="text-center mb-3">

                                <!-- Preview foto yang bisa diklik untuk upload -->
                                <label for="foto" class="photo-upload-label" style="cursor:pointer;">
                                    <img src="<?= base_url('assets/img/logo.jpg') ?>"
                                         id="preview"
                                         width="160"
                                         height="160"
                                         style="border-radius:50%;object-fit:cover;border:5px solid #e9ecef;box-shadow:0 5px 15px rgba(0,0,0,.12);">
                                    <div class="photo-camera-overlay">
                                        <i class="fas fa-camera"></i>
                                    </div>
                                </label>

                                <input type="file" name="foto" id="foto" class="d-none" accept="image/*">

                                <div class="mt-3">
                                    <label for="foto" class="btn btn-outline-primary btn-sm">
                                        <i class="fas fa-upload mr-1"></i>
                                        Pilih Foto
                                    </label>
                                </div>
                                <small class="text-muted">
                                    Klik untuk upload foto Anda (JPG, JPEG, PNG)
                                </small>

                            </div>
                        </div>

                        <!-- FORM -->
                        <div class="col-md-8">

                            <!-- JENIS -->
                            <div class="form-group">
                                <label class="font-weight-bold">Daftar Sebagai</label>
                                <input type="text" class="form-control" value="<?= $roleLabel ?>" disabled>
                            </div>

                            <!-- NAMA -->
                            <div class="form-group">
                                <label class="font-weight-bold">
                                    Nama Lengkap <span class="text-danger">*</span>
                                </label>
                                <input type="text" name="nama" class="form-control"
                                       value="<?= esc(session()->get('nama')) ?>"
                                       placeholder="Masukkan nama lengkap" required>
                            </div>

                            <!-- NISN / NIP -->
                            <div class="form-group">
                                <label class="font-weight-bold">NISN / NIP</label>
                                <input type="text" name="nisn" class="form-control"
                                       placeholder="Masukkan NISN (siswa) atau NIP (guru)">
                            </div>

                            <!-- KELAS (khusus siswa) -->
                            <?php if ($role == 'siswa') : ?>
                                <div class="form-group">
                                    <label class="font-weight-bold">Kelas</label>
                                    <select name="kelas" class="form-control">
                                        <option value="">-- Pilih Kelas --</option>
                                        <option value="X.E">X.E</option>
                                        <option value="XI.F1">XI.F1</option>
                                        <option value="XI.F2">XI.F2</option>
                                        <option value="XII.F1">XII.F1</option>
                                        <option value="XII.F2">XII.F2</option>
                                    </select>
                                </div>
                            <?php endif; ?>

                            <!-- ALAMAT -->
                            <div class="form-group">
                                <label class="font-weight-bold">Alamat</label>
                                <textarea name="alamat" class="form-control" rows="3"
                                          placeholder="Masukkan alamat"></textarea>
                            </div>

                            <button type="submit" class="btn btn-primary px-4">
                                <i class="fas fa-save mr-1"></i>
                                Simpan & Daftar
                            </button>

                        </div>

                    </div>

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

    <?php endif; ?>

</div>

<script>
$(document).ready(function () {
    // Preview foto
    $('#foto').on('change', function () {
        const file = this.files[0];
        if (file) {
            const reader = new FileReader();
            reader.onload = function (e) {
                $('#preview').attr('src', e.target.result);
            };
            reader.readAsDataURL(file);
        }
    });
});
</script>

<style>
/* Foto upload dengan kamera overlay */
.photo-upload-label {
    position: relative;
    display: inline-block;
}

.photo-camera-overlay {
    position: absolute;
    right: 5px;
    bottom: 8px;
    width: 38px;
    height: 38px;
    border-radius: 50%;
    background: #007bff;
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 3px solid white;
    box-shadow: 0 3px 8px rgba(0,0,0,.2);
    transition: .2s;
}

.photo-upload-label:hover .photo-camera-overlay {
    background: #0056b3;
    transform: scale(1.08);
}
</style>

<?= $this->endSection() ?>
