<?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; font-family: Teko-SemiBold; text-align: center; font-size: 24px; 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%);">
            FORM WITHDRAW
        </div>

        <div
            style="width: 90%; margin: 0 auto 20px auto; padding: 10px; background-color: var(--secondary-color); border-radius: 6px; display: flex;">
            <div style="border-radius: 25px; margin: auto 0;">
                <i class="fas fa-exclamation-circle" style="color: #C4C4C4; font-size: 24px;"></i>
            </div>
            <div style="margin-left: 8px;">
                Minimal withdraw adalah Rp 50.000 dan maksimal Rp 100.000.000.<br>
                Kami hanya akan transfer dana ke rekening anda yang terdaftar di situs kami!
            </div>
        </div>

        <form method="post" id="formWithdraw" action="../function/proses_withdraw.php" class="from-prevent-multiple-submits"
            style="width: 90%; margin: 0 auto 20px auto;">
            <input type="hidden" name="_token" value="2HmY4C1S6JctTaZaiA0bL1nTszJrtg87E2K0obRr" autocomplete="off">
            <div style="opacity: 0.66; font-size: 13px; margin-bottom: 4px;">
                Rekening Withdraw
            </div>
            <div style="margin-bottom: 12px;">
                <?php $bank_si_user = $bank_user . ' - ' . $no_rek_user . ' - ' . $nama_rek_user; ?>
                <input type="text" name="user_bank_accounts" class="form-control" value="<?= $bank_si_user; ?>"
                    readonly>
            </div>

            <div style="opacity: 0.66; font-size: 13px; margin-bottom: 4px;">
                Jumlah
            </div>
            <div style="margin-bottom: 12px;">
                <input type="tel" name="amount" class="form-control rupiah" placeholder="Jumlah" value="">
            </div>

            <div style="margin-top: 20px;">
                <button type="submit" class="glowing2 btn btn-primary from-prevent-multiple-submits"
                    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;">
                    Withdraw
                </button>
            </div>
        </form>

        <div style="width: 90%; margin: 30px auto 10px auto;">
            7 Transaksi Withdraw Terakhir
        </div>

        <div style="width: 90%; margin: 0 auto; display: block; overflow: auto;">
            <table width="100%" style="font-size: 11px;" id="tabelWd">
                <thead>
                    <tr style="background-color: #494848; color: #fff; font-weight: bold;">
                        <td style="padding: 12px 5px;">Status</td>
                        <td style="padding: 12px 5px;">Tanggal</td>
                        <td style="padding: 12px 5px;">Dari Rek</td>
                        <td style="padding: 12px 5px;">Jumlah</td>
                    </tr>
                </thead>
                <tbody>
                    <tr style="background-color: #1a1919; color: #fff;">
                        <td colspan="4" style="padding: 12px 5px; text-align: center;">Tidak ada transaksi withdraw</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php include 'template/footer.php'; ?>
<script>
$(document).ready(function() {

    function loadLastWithdraw() {
        $.ajax({
            url: '../function/last_withdraw.php',
            type: 'GET',
            dataType: 'json',
            success: function(res) {
                let tbody = '';
                if (!res.success || res.data.length === 0) {
                    tbody = `<tr><td colspan="4" style="text-align:center;color:#C4C4C4;">Belum ada transaksi withdraw.</td></tr>`;
                } else {
                    res.data.forEach(d => {
                        let statusBadge = '';
                        if (d.status == 'Pending') statusBadge = '<span style="color:#ffb300;">Pending</span>';
                        else if (d.status == 'Sukses') statusBadge = '<span style="color:#04AA6D;">Berhasil</span>';
                        else statusBadge = '<span style="color:#e74c3c;">Gagal</span>';

                        tbody += `<tr>
                            <td>${statusBadge}</td>
                            <td>${d.created_at}</td>
                            <td>${d.bank_asal || '-'}</td>
                            <td>Rp ${parseInt(d.jumlah).toLocaleString('id-ID')}</td>
                        </tr>`;
                    });
                }
                $('#tabelWd tbody').html(tbody);
            },
            error: function() {
                $('#tabelWd tbody').html(`<tr><td colspan="4" style="text-align:center;color:red;">Gagal memuat data withdraw.</td></tr>`);
            }
        });
    }

    loadLastWithdraw(); 

    $(document).on('keyup', '.rupiah', function() {
        let val = $(this).val().replace(/\D/g, '');
        $(this).val(val.replace(/\B(?=(\d{3})+(?!\d))/g, ','));
    });

    $('#formWithdraw').on('submit', function(e) {
        e.preventDefault();
        let btn = $('#btnWithdraw');

        let amountRaw = $('input[name="amount"]').val().replace(/,/g, '');
        $('input[name="amount"]').val(amountRaw);

        $.ajax({
            url: $(this).attr('action'),
            type: 'POST',
            data: $(this).serialize(),
            dataType: 'json',
            beforeSend: function() {
                btn.attr('disabled', true).text('Memproses...');
            },
            success: function(res) {
                Swal.fire({
                    icon: res.status === 'success' ? 'success' : 'error',
                    title: res.status === 'success' ? 'Berhasil!' : 'Gagal!',
                    text: res.message,
                    confirmButtonColor: '#FDB931'
                }).then(() => {
                    if (res.status === 'success') {
                        $('#formWithdraw')[0].reset();
                        loadLastWithdraw();
                    }
                });
            },
            error: function() {
                Swal.fire({
                    icon: 'error',
                    title: 'Kesalahan Server!',
                    text: 'Terjadi kesalahan saat memproses withdraw.',
                    confirmButtonColor: '#FDB931'
                });
            },
            complete: function() {
                btn.attr('disabled', false).text('Withdraw');
            }
        });
    });
});

</script>

