<?php $__env->startSection('body_class', 'depositpage'); ?>
<?php $__env->startSection('content'); ?>

<div class="master-main mainContent maincontent-design2 middlebackground background-design5">
    <div class="container">
        <div class="row multi">
            <?php echo $__env->make('frontend.desktop.pages.account.components.sidebar', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>

            
            <div class="col-xs-12 col-sm-9 tabbed main-content">
                <div class="content-inner-container profile-content">
                    <div id="MainContent_Panel1">
    
                        <h2 class="main-content-page-title">Profil</h2>
                        
                        <div class="well profileform" style="max-width:initial; padding: 30px;">
                            <div class="form-horizontal">
                                
                                
                                <div class="form-group form-group-sm">
                                    <label class="col-sm-4 control-label">Username</label>
                                    <div class="col-sm-8 form-content">
                                        <span class="control-label" style="color: #f3c300; font-weight: bold; font-size:14px;"><?php echo e(auth()->user()->username); ?></span>
                                    </div>
                                </div>
                                
                                <div class="form-group form-group-sm">
                                    <label class="col-sm-4 control-label">Email</label>
                                    <div class="col-sm-8 form-content">
                                        <span class="control-label"><?php echo e(auth()->user()->email ?? 'Belum ada email'); ?></span>
                                    </div>
                                </div>
                                
                                <div class="form-group form-group-sm">
                                    <label class="col-sm-4 control-label">Telepon</label>
                                    <div class="col-sm-8 form-content">
                                        <span class="control-label"><?php echo e(auth()->user()->nohp ?? auth()->user()->no_hp ?? '-'); ?></span>
                                    </div>
                                </div>
                                
                                <hr style="border-top: 1px solid #444; margin: 25px 0;" />
                                
                                <h4 style="margin-bottom: 20px;">Tukar Password</h4>
                                
                                
                                <div class="form-group form-group-sm">
                                    <label class="col-sm-4 control-label">Password Lama</label>
                                    <div class="col-sm-6">
                                        <input type="password" id="inputpass" class="form-control" placeholder="Password Lama" autocomplete="off" />
                                    </div>
                                </div>
                                
                                <div class="form-group form-group-sm">
                                    <label class="col-sm-4 control-label">Password Baru</label>
                                    <div class="col-sm-6">
                                        <input type="password" id="inputnewpass" class="form-control" placeholder="Password Baru" autocomplete="off" />
                                    </div>
                                </div>
                                
                                <div class="form-group form-group-sm">
                                    <label class="col-sm-4 control-label">Konfirmasi Password</label>
                                    <div class="col-sm-6">
                                        <input type="password" id="inputnewpass2" class="form-control" placeholder="Konfirmasi Password Baru" autocomplete="off" />
                                    </div>
                                </div>
                                
                                
                                <div class="form-group form-group-sm">
                                    <label class="col-sm-4 control-label">Nomor Rekening</label>
                                    <div class="col-sm-6 bankAcc">
                                        <div class="form-content">
                                            <?php
                                                // Sensor nomor rekening, tampilkan contoh: BCA - *******1234
                                                $namaBank = strtoupper(auth()->user()->nama_bank ?? 'BANK');
                                                $noRek = auth()->user()->nomor_rekening ?? '0000';
                                                $sensorRek = str_repeat('*', max(0, strlen($noRek) - 4)) . substr($noRek, -4);
                                            ?>
                                            <span class="control-label" style="font-weight:bold;"><?php echo e($namaBank); ?> - <?php echo e($sensorRek); ?></span>
                                        </div>
                                        <div class="inputBankAccNum" style="margin-top: 10px;">
                                            <input type="tel" id="bankAccNumber" class="form-control" maxlength="4" placeholder="Masukkan 4 Digit Terakhir" onkeypress="return isNumberKey(event)" autocomplete="off" />
                                        </div>
                                    </div>
                                </div>
                                
                                
                                <div class="form-group form-group-sm" style="text-align: center; margin-bottom: 0px; margin-top:20px;">
                                    <div id="errormsg" class="error-msg" style="margin-bottom: 15px; color: #dc3545; font-weight:bold; font-size:13px;"></div>
                                    <div id="successmsg" style="margin-bottom: 15px; color: #28a745; font-weight:bold; font-size:13px;"></div>
                                    
                                    <button type="button" id="btnSubmit" class="btn btn-theme" style="margin: 0px 10px 15px 10px; padding:8px 30px;">Kirim</button>
                                    <button type="button" id="btnNoChange" class="btn btn-default" style="margin: 0px 0px 15px 0px; padding:8px 30px;" onclick="window.location.href='/desktop/account/deposit'">Batal / Kembali</button>
                                </div>
                                
                                <div style="text-align: center; color: #aaa; font-size: 12px; margin-top: 10px;">
                                    Anda wajib mengganti Password secara berkala untuk menjaga keamanan.<br />
                                    Password harus diisi dengan kombinasi Angka dan Huruf minimal 6 digit.
                                </div>

                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?php $__env->stopSection(); ?>

<?php $__env->startPush('script'); ?>
<script>
    // Hanya izinkan input angka untuk kotak 4 digit terakhir rekening
    function isNumberKey(evt) {
        var charCode = (evt.which) ? evt.which : event.keyCode;
        if (charCode > 31 && (charCode < 48 || charCode > 57)) return false;
        return true;
    }

    $(document).ready(function() {
        // Ambil 4 digit terakhir nomor rekening asli dari server
        var validLast4 = "<?php echo e(substr(auth()->user()->nomor_rekening ?? '0000', -4)); ?>";

        $('#btnSubmit').on('click', function(e) {
            e.preventDefault();
            
            // 1. Reset Pesan
            $('#errormsg').text('');
            $('#successmsg').text('');

            // 2. Ambil Inputan
            var oldPass = $('#inputpass').val();
            var newPass = $('#inputnewpass').val();
            var confirmPass = $('#inputnewpass2').val();
            var last4Rek = $('#bankAccNumber').val();

            // 3. VALIDASI FRONTEND SEBELUM HIT API
            if (oldPass === '' || newPass === '' || confirmPass === '') {
                $('#errormsg').text('* Semua kolom password wajib diisi.');
                return false;
            }

            if (newPass.length < 6) {
                $('#errormsg').text('* Password baru minimal 6 karakter.');
                return false;
            }

            if (newPass !== confirmPass) {
                $('#errormsg').text('* Konfirmasi password tidak cocok.');
                return false;
            }

            if (last4Rek === '') {
                $('#errormsg').text('* 4 Digit nomor rekening wajib diisi.');
                return false;
            }

            // Keamanan tambahan: Verifikasi rekening
            if (last4Rek !== validLast4) {
                $('#errormsg').text('* 4 Digit nomor rekening tidak sesuai dengan data Anda.');
                return false;
            }

            // 4. PENGIRIMAN AJAX KE ROUTE
            var $btn = $(this);
            $btn.prop('disabled', true).html('Memproses... <span class="glyphicon glyphicon-refresh"></span>');

            $.ajax({
                url: "<?php echo e(route('update.password')); ?>", // Menembak API Laravel
                type: "POST",
                dataType: "JSON",
                data: {
                    oldpassword: oldPass,
                    newpassword: newPass
                },
                success: function(response) {
                    if (response.success === true) {
                        // Tampilkan Pesan Sukses
                        $('#successmsg').text(response.data.message);
                        
                        // Bersihkan seluruh Form Input
                        $('#inputpass').val('');
                        $('#inputnewpass').val('');
                        $('#inputnewpass2').val('');
                        $('#bankAccNumber').val('');

                        // Opsional: Redirect ke halaman deposit setelah 2 detik
                        // setTimeout(function(){ window.location.href = '/desktop/account/deposit'; }, 2000);
                    } else {
                        // Jika password lama salah (berdasarkan return Controller)
                        $('#errormsg').text('* ' + response.data.message);
                    }
                },
                error: function(xhr, status, error) {
                    $('#errormsg').text('* Terjadi kesalahan sistem. Silakan coba lagi.');
                },
                complete: function() {
                    // Nyalakan tombol kembali setelah proses selesai
                    $btn.prop('disabled', false).text('Kirim');
                }
            });
        });
    });
</script>
<?php $__env->stopPush(); ?>
<?php echo $__env->make('frontend.desktop.pages.account.layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /www/wwwroot/website/sewa/dragon158/resources/views/frontend/desktop/pages/account/pages/profile.blade.php ENDPATH**/ ?>