<?php
session_start();
include 'template/header.php';

if (!empty($_GET['ref'])) {
    $_SESSION['referral'] = trim($_GET['ref']);
}

if (!empty($_GET['reff'])) {
    $_SESSION['referral'] = trim($_GET['reff']);
}

// Ambil referral dari session
$ref_final = $_SESSION['referral'] ?? null;
?>

<?php if ($mobile): ?>
    <div style="padding: 20px 0; background-color:var(--secondary-color);">
        <div
            style="width: 90%; margin: 0 auto 10px auto; color: transparent !important; -webkit-background-clip: text; background-image: radial-gradient(ellipse farthest-corner at right bottom, #FEDB37 0%, #FDB931 8%, #9f7928 30%, #8A6E2F 40%, transparent 80%), radial-gradient(ellipse farthest-corner at left top, #e99c05 0%, #f4ae00 8%, #f8bf33 25%, #f8ce19 62.5%, #ffdc02 100%); font-family: Teko-SemiBold; text-align: center; font-size: 24px;">
            DAFTAR AKUN
        </div>

        <?php
        $ref_final = $_SESSION['referral'] ?? null;
        if (!is_null($ref_final)): ?>
            <div id="referral_info"
                style="width: 85%; margin: 0 auto 15px auto; padding: 10px; background: #1BB143; color: white; border-radius: 5px; text-align: center;">
                <i class="fas fa-user-friends"></i> Direferensikan oleh: <strong id="referrer_display">
                    <?= $ref_final ?></strong>
            </div>
        <?php endif; ?>

        <!-- Referral Error Display -->
        <div class="error-referral_username"
            style="width: 85%; margin: 0 auto 15px auto; padding: 10px; background-color: #dc3545; border-radius: 5px; text-align: center; color: #fff; font-size: 13px; display: none;">
        </div>

        <form method="post" action="register-submit" class="form-register" style="width: 85%; margin: 0 auto;">
            <input type="hidden" name="reffallea" value="<?= htmlspecialchars((string) $ref_final, ENT_QUOTES, 'UTF-8') ?>">

            <input type="hidden" name="referral_username" id="referral_username" value="">
            <div style="opacity: 0.66; font-size: 13px; margin-bottom: 4px;">
                Username
            </div>
            <div style="margin-bottom: 12px;">
                <input type="text" name="username" class="form-control" placeholder="Username">
                <div class="error-username" style="color: red; font-size: 13px; margin-top: 6px;"></div>
            </div>

            <div style="opacity: 0.66; font-size: 13px; margin-bottom: 4px;">
                Password
            </div>
            <div style="margin-bottom: 12px;">
                <input type="password" name="password" class="form-control" placeholder="Password">
                <div class="error-password" style="color: red; font-size: 13px; margin-top: 6px;"></div>
            </div>

            <div style="opacity: 0.66; font-size: 13px; margin-bottom: 4px;">
                Konfirmasi Password
            </div>
            <div style="margin-bottom: 12px;">
                <input type="password" name="password_confirm" class="form-control" placeholder="Konfirmasi Password">
                <div class="error-password_confirm" style="color: red; font-size: 13px; margin-top: 6px;"></div>
            </div>

            <div style="opacity: 0.66; font-size: 13px; margin-bottom: 4px;">
                Email
            </div>
            <div style="margin-bottom: 12px;">
                <input type="email" name="email" class="form-control" placeholder="Email">
                <div class="error-email" style="color: red; font-size: 13px; margin-top: 6px;"></div>
            </div>

            <div style="opacity: 0.66; font-size: 13px; margin-bottom: 4px;">
                No. Handphone
            </div>
            <div style="margin-bottom: 12px;">
                <input type="tel" name="phone" class="form-control" placeholder="Telepon">
                <div class="error-phone" style="color: red; font-size: 13px; margin-top: 6px;"></div>
            </div>

            <div style="opacity: 0.66; font-size: 13px; margin-bottom: 4px;">
                Pilih Bank
            </div>
            <div class="select_box" style="margin-bottom: 12px; position: relative;">
                <select name="bank_name" class="form-control">
                    <option value="">Pilih Bank</option>
                    <option value="BCA">BCA</option>
                    <option value="BNI">BNI</option>
                    <option value="BRI">BRI</option>
                    <option value="CIMB">CIMB</option>
                    <option value="Mandiri">Mandiri</option>
                    <option value="Danamon">Danamon</option>
                    <option value="OVO">OVO</option>
                    <option value="GOPAY">GOPAY</option>
                    <option value="DANA">DANA</option>
                    <option value="LINKAJA">LINKAJA</option>
                    <option value="SEABANK">SEABANK</option>
                    <option value="PERMATA">PERMATA</option>
                    <option value="OCBC">OCBC</option>
                </select>
                <div class="error-bank_name" style="color: red; font-size: 13px; margin-top: 6px;"></div>
            </div>

            <div style="opacity: 0.66; font-size: 13px; margin-bottom: 4px;">
                No. Rekening
            </div>
            <div style="margin-bottom: 12px;">
                <input type="tel" name="bank_account_number" class="form-control" placeholder="No. Rekening">
                <div class="error-bank_account_number" style="color: red; font-size: 13px; margin-top: 6px;"></div>
            </div>

            <div style="opacity: 0.66; font-size: 13px; margin-bottom: 4px;">
                Nama Rekening
            </div>
            <div style="margin-bottom: 12px;">
                <input type="text" name="bank_account_name" class="form-control" placeholder="Nama Rekening">
                <div class="error-bank_account_name" style="color: red; font-size: 13px; margin-top: 6px;"></div>
            </div>


            <div style="opacity: 0.66; font-size: 13px; margin-bottom: 4px;">
                Kode Verifikasi
            </div>
            <div style="margin-bottom: 12px;">
                <div style="display: flex;">
                    <div style="margin-right: 8px;">
                        <input type="text" name="captcha" class="form-control" placeholder="Kode Verifikasi" maxlength="4">
                    </div>
                    <img src="captcha/default.php" onclick="this.src='captcha/default.php?'+Math.random()" id="captchaCode"
                        alt="" class="captcha"
                        style="margin: auto 0; border: 2px solid #FFD700; border-radius: 4px; cursor: pointer;">
                    <a rel="nofollow" href="javascript:;" class="refresh-captcha"
                        onclick="document.getElementById('captchaCode').src='captcha/default.php?'+Math.random()"
                        style="margin: auto 5px;"><i class="fas fa-sync-alt"
                            style="color: #FFD700; font-size: 20px;"></i></a>
                </div>
                <div class="error-captcha" style="color: red; font-size: 13px; margin-top: 6px;"></div>
            </div>

            <div style="margin-top: 20px;">
                <input type="hidden" name="is_captcha" value="1">
                <button type="submit" class="glowing2 btn btn-primary submit"
                    style="background-image: radial-gradient(ellipse farthest-corner at right bottom, #FEDB37 0%, #FDB931 8%, #9f7928 30%, #8A6E2F 40%, transparent 80%), radial-gradient(ellipse farthest-corner at left top, #e99c05 0%, #f4ae00 8%, #f8bf33 25%, #f8ce19 62.5%, #ffdc02 100%); color: #fff; border: 0; width: 100%; font-weight: bold; border-radius: 25px; padding: 10px 0;"
                    onclick="aler('Apakah anda ingin daftar dengan data ini?')">
                    Daftar
                </button>
            </div>
        </form>

        <div style="margin: 20px auto 0 auto; text-align: center;">
            <div style="opacity: 0.78; font-size: 13px;">Sudah punya akun?</div>
            <a href="login" style="color: #FFD700; font-weight: bold;">
                <u>Masuk disini</u>
            </a>
        </div>
    </div>
<?php else: ?>
    <div style="margin-top: 170px;">
        <div class="background-top" style="padding: 30px 0 50px 0;">
            <div style="width: 95%; margin: 0 auto; max-width: 1280px;">
                <div
                    style="margin-bottom: 20px; text-align: center; font-family: Teko-SemiBold; font-size: 24px; color: var(--primary-color);">
                    DAFTAR AKUN
                </div>

                <?php
                $ref_final = $_SESSION['referral'] ?? null;
                if (!is_null($ref_final)): ?>
                    <div id="referral_info"
                        style="width: 50%; margin: 0 auto 15px auto; padding: 10px; background: #1BB143; color: white; border-radius: 5px; text-align: center;">
                        <i class="fas fa-user-friends"></i> Direferensikan oleh:
                        <strong id="referrer_display"><?= $ref_final ?></strong>
                    </div>
                <?php endif; ?>

                <!-- Referral Error -->
                <div class="error-referral_username" style="width: 50%; margin: 0 auto 15px auto; padding: 10px; background-color: #dc3545; border-radius: 5px;
                text-align: center; color: #fff; font-size: 13px; display: none;">
                </div>

                <form method="post" action="register-submit" class="form-register"
                    style="width: 50%; margin: 0 auto; background-color: var(--secondary-color); padding: 3% 5%;">

                    <input type="hidden" name="reffallea"
                        value="<?= htmlspecialchars((string) $ref_final, ENT_QUOTES, 'UTF-8') ?>">

                    <input type="hidden" name="referral_username" id="referral_username" value="">

                    <table style="width: 100%;">
                        <tbody>

                            <tr>
                                <td style="padding-bottom: 20px; font-size: 14px;">Username</td>
                                <td style="padding-bottom: 20px;">
                                    <input type="text" name="username" class="form-control" placeholder="Username">
                                    <div class="error-username" style="color: red; font-size: 14px;"></div>
                                </td>
                            </tr>

                            <tr>
                                <td style="padding-bottom: 20px; font-size: 14px;">Password</td>
                                <td style="padding-bottom: 20px;">
                                    <input type="password" name="password" class="form-control" placeholder="Password">
                                    <div class="error-password" style="color: red; font-size: 14px;"></div>
                                </td>
                            </tr>

                            <tr>
                                <td style="padding-bottom: 20px; font-size: 14px;">Konfirmasi Password</td>
                                <td style="padding-bottom: 20px;">
                                    <input type="password" name="password_confirm" class="form-control"
                                        placeholder="Konfirmasi Password">
                                    <div class="error-password_confirm" style="color: red; font-size: 14px;"></div>
                                </td>
                            </tr>

                            <tr>
                                <td style="padding-bottom: 20px; font-size: 14px;">Email</td>
                                <td style="padding-bottom: 20px;">
                                    <input type="email" name="email" class="form-control" placeholder="Email">
                                    <div class="error-email" style="color: red; font-size: 14px;"></div>
                                </td>
                            </tr>

                            <tr>
                                <td style="padding-bottom: 20px; font-size: 14px;">No. Handphone</td>
                                <td style="padding-bottom: 20px;">
                                    <input type="tel" name="phone" class="form-control" placeholder="Telepon">
                                    <div class="error-phone" style="color: red; font-size: 14px;"></div>
                                </td>
                            </tr>

                            <tr>
                                <td style="padding-bottom: 20px; font-size: 14px;">Pilih Bank</td>
                                <td style="padding-bottom: 20px;">
                                    <select name="bank_name" class="form-control">
                                        <option value="">Pilih Bank</option>
                                        <option value="BCA">BCA</option>
                                        <option value="BNI">BNI</option>
                                        <option value="BRI">BRI</option>
                                        <option value="CIMB">CIMB</option>
                                        <option value="Mandiri">Mandiri</option>
                                        <option value="Danamon">Danamon</option>
                                        <option value="OVO">OVO</option>
                                        <option value="GOPAY">GOPAY</option>
                                        <option value="DANA">DANA</option>
                                        <option value="LINKAJA">LINKAJA</option>
                                        <option value="SEABANK">SEABANK</option>
                                        <option value="PERMATA">PERMATA</option>
                                        <option value="OCBC">OCBC</option>
                                    </select>
                                    <div class="error-bank_name" style="color: red; font-size: 14px;"></div>
                                </td>
                            </tr>

                            <tr>
                                <td style="padding-bottom: 20px; font-size: 14px;">No. Rekening</td>
                                <td style="padding-bottom: 20px;">
                                    <input type="tel" name="bank_account_number" class="form-control"
                                        placeholder="No. Rekening">
                                    <div class="error-bank_account_number" style="color: red; font-size: 14px;"></div>
                                </td>
                            </tr>

                            <tr>
                                <td style="padding-bottom: 20px; font-size: 14px;">Nama Rekening</td>
                                <td style="padding-bottom: 20px;">
                                    <input type="text" name="bank_account_name" class="form-control"
                                        placeholder="Nama Rekening">
                                    <div class="error-bank_account_name" style="color: red; font-size: 14px;"></div>
                                </td>
                            </tr>

                            <tr>
                                <td style="padding-bottom: 20px; font-size: 14px;">Kode Verifikasi</td>
                                <td style="padding-bottom: 20px;">
                                    <div style="display: flex;">
                                        <input type="text" name="captcha" class="form-control" placeholder="Kode Verifikasi"
                                            maxlength="4" style="margin-right: 8px;">

                                        <img src="captcha/default.php"
                                            onclick="this.src='captcha/default.php?'+Math.random()" id="captchaCodeDesktop"
                                            style="border: 2px solid var(--primary-color); border-radius: 4px; cursor: pointer;">
                                    </div>
                                    <div class="error-captcha" style="color: red; font-size: 14px;"></div>
                                </td>
                            </tr>

                            <tr>
                                <td colspan="2" style="text-align: center;">
                                    <input type="hidden" name="is_captcha" value="1">
                                    <input type="submit" class="btn btn-primary submit" value="Daftar" style="background-color: var(--primary-color); color: #001739; border: 0; width: 50%;
                                   font-weight: bold; border-radius: 6px; padding: 10px 0;">
                                </td>
                            </tr>

                        </tbody>
                    </table>
                </form>

                <div style="margin: 20px auto 0 auto; text-align: center;">
                    <div style="opacity: 0.78; font-size: 13px;">Sudah punya akun?</div>
                    <a href="login" style="color: var(--primary-color); font-weight: bold;">
                        <u>Masuk disini</u>
                    </a>
                </div>
            </div>
        </div>
    </div>
<?php endif; ?>


<script>
    (function () {
        const params = new URLSearchParams(location.search);
        const ref = params.get('ref');
        if (ref) {
            const el = document.getElementById('referral_username');
            if (el) el.value = ref;
        }
    })();
</script>


<?php include 'template/footer.php' ?>