{{-- ===============================
     RINCIAN PROMOSI (AJAX BASED)
     =============================== --}}

<div class="box-title">
    <i class="icon-invoice i-invoice"></i>
    Rincian Promosi

    <button type="button"
            class="btn btn-click pull-right"
            id="btn-clear-all-promo">
        CLEAR PROMO
    </button>
</div>

<table class="table table-borderless text-right info-box--001">
    <tbody>

        {{-- JUDUL PROMO --}}
        <tr class="text-left tr_type2">
            <td class="text-center" colspan="2">
                <span id="promo_name"
                      style="font-weight:700;text-decoration:underline;">
                    -
                </span>
            </td>
        </tr>

        {{-- JENIS BONUS --}}
        <tr class="text-left">
            <td colspan="2">
                <small>Jenis Bonus</small>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <span class="lbl" id="jenis_promosi_text">-</span>
                <input type="hidden" id="jenis_promosi" name="jenis_promosi">
            </td>
        </tr>

        {{-- MIN DEPOSIT --}}
        <tr class="text-left tr_type2">
            <td class="col-xs-6">
                <small>Min Deposit</small>
            </td>
            <td class="col-xs-6 text-right">
                <span class="lbl" id="promo_min_amount_text">IDR 0</span>
                <input type="hidden" id="promo_min_amount" name="promo_min_amount">
            </td>
        </tr>

        {{-- MAX BONUS --}}
        <tr class="text-left tr_type2">
            <td class="col-xs-9">
                <small>Bonus Maksimum Dihadiahkan</small>
            </td>
            <td class="col-xs-9 text-right">
                <span class="lbl" id="promo_max_amount_text">IDR 0</span>
                <input type="hidden" id="promo_max_amount" name="promo_max_amount">
            </td>
        </tr>

    </tbody>
</table>

{{-- ===============================
     JAVASCRIPT PROMO
     =============================== --}}
<script>
function loadPromoDetails() {
    const promoId = $('#select_name').val();

    if (!promoId) {
        clearPromoUI();
        return;
    }

    $.ajax({
        url: "{{ url('/api/promo_details') }}",
        type: "POST",
        dataType: "json",
        data: {
            _token: "{{ csrf_token() }}",
            select_name: promoId
        },
        success: function (res) {
            console.log('PROMO RESPONSE:', res);

            // GUARD WAJIB
            if (!res || typeof res !== 'object') {
                console.error('Response bukan JSON');
                clearPromoUI();
                return;
            }

            if (!res.status || !res.data) {
                clearPromoUI();
                alert(res.message || 'Promo tidak tersedia');
                return;
            }

            // SAFE ASSIGN
            $('#promo_name').text(res.data.judul ?? '-');
            $('#jenis_promosi_text').text(res.data.jenis_promosi ?? '-');
            $('#jenis_promosi').val(res.data.jenis_promosi ?? '');

            const minDeposit = parseInt(res.data.min_deposit) || 0;
            const maxBonus   = parseInt(res.data.max_bonus) || 0;

            $('#promo_min_amount_text').text('IDR ' + numberFormat(minDeposit));
            $('#promo_min_amount').val(minDeposit);

            $('#promo_max_amount_text').text('IDR ' + numberFormat(maxBonus));
            $('#promo_max_amount').val(maxBonus);
        },
        error: function (xhr, status, error) {
            console.error('AJAX ERROR:', xhr.responseText);
            clearPromoUI();
            alert('Gagal mengambil detail promo');
        }
    });
}

// CLEAR PROMO
$('#btn-clear-all-promo').on('click', function () {
    $('#select_name').val('');
    clearPromoUI();
});

// RESET UI
function clearPromoUI() {
    $('#promo_name').text('-');
    $('#jenis_promosi_text').text('-');
    $('#promo_min_amount_text').text('IDR 0');
    $('#promo_max_amount_text').text('IDR 0');

    $('#jenis_promosi').val('');
    $('#promo_min_amount').val('');
    $('#promo_max_amount').val('');
}

// FORMAT ANGKA AMAN
function numberFormat(num) {
    try {
        return new Intl.NumberFormat('id-ID').format(num || 0);
    } catch (e) {
        return '0';
    }
}
</script>
