<?php include 'template/header.php' ?>

<div style="margin-top: 50px;">
    <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;">
            PROFIL
        </div>

        <div class="error-check"></div>

        <form method="post" action="function/profile_update.php" class="form-profil"
            style="width: 90%; margin: 0 auto;">
            
            <div style="background-color: #3e3e3e; padding: 10px 16px; border-radius: 5px; margin-bottom: 1px;">
                <div style="font-size: 14px;">
                    Detail Bank
                </div>
                <hr>
                <div style="display: flex; flex-direction: column; gap: 10px;">
                    <div style="font-size: 14px; margin: auto auto auto 0;">
                        <?= $nama_rek_user; ?>
                    </div>
                    <div style="font-size: 20px; margin: auto auto auto 0;">
                        <?= $bank_user ?>
                    </div>
                </div>

                <div style="letter-spacing: 5px; font-size: 20px; margin-top: 8px;">
                    <?= $no_rek_user; ?>
                </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" value="<?= $email_user; ?>">
                <div class="error-email" style="color: red; font-size: 12px; margin-top: 4px;"></div>
            </div>

            <div style="opacity: 0.66; font-size: 13px; margin-bottom: 4px;">
                Password Baru (Opsional)
            </div>
            <div style="margin-bottom: 12px;">
                <input type="password" name="password" class="form-control" placeholder="Password Baru" value="">
                <div class="error-password" style="color: red; font-size: 12px; margin-top: 4px;"></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"
                    value="">
                <div class="error-password_confirm" style="color: red; font-size: 12px; margin-top: 4px;"></div>
            </div>

            <div style="margin-top: 20px;">
                <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;">
                    Ubah Data
                </button>
            </div>
        </form>
    </div>
</div>

<?php include 'template/footer.php' ?>

<script>
$(document).ready(function () {
    $('.form-profil').on('submit', function (e) {
        e.preventDefault();

        // Hapus error lama
        $('.error-check').html('');
        $('.error-email').text('');
        $('.error-password').text('');
        $('.error-password_confirm').text('');

        const formData = $(this).serialize();

        $.ajax({
            url: 'function/profile_update.php',
            type: 'POST',
            data: formData,
            dataType: 'json',
            success: function (res) {
                if (res.status === 'success') {
                    $('.error-check').html(
                        `<div style="padding: 10px; background: #28a745; color: white; border-radius: 5px; margin-bottom: 15px; text-align: center;">${res.message}</div>`
                    );
                    $('input[name=password], input[name=password_confirm]').val('');
                } else if (res.status === 'error') {
                    if (res.errors) {
                        if (res.errors.email) $('.error-email').text(res.errors.email);
                        if (res.errors.password) $('.error-password').text(res.errors.password);
                        if (res.errors.password_confirm) $('.error-password_confirm').text(res.errors.password_confirm);
                    } else {
                        $('.error-check').html(
                            `<div style="padding: 10px; background: #dc3545; color: white; border-radius: 5px; margin-bottom: 15px; text-align: center;">${res.message}</div>`
                        );
                    }
                }
            },
            error: function () {
                $('.error-check').html(
                    `<div style="padding: 10px; background: #dc3545; color: white; border-radius: 5px; margin-bottom: 15px; text-align: center;">Terjadi kesalahan server</div>`
                );
            }
        });
    });
});
</script>
