/* ============================================================
   DroidTester — Package JS (Purchase Page)
   ============================================================ */

(function () {
    'use strict';

    // ── Price Breakdown ──
    const packagePrices = { silver: 9, gold: 1499, platinum: 2199 };
    const GST_RATE = 0.18;
    const PLATFORM_FEE = 2;

    let selectedPackage = 'silver'; // default

    function updateOrderSummary(pkg) {
        const base = packagePrices[pkg] || 9;
        const gst = Math.round(base * GST_RATE * 100) / 100;
        const total = base + gst + PLATFORM_FEE;

        const subtotalEl = document.getElementById('orderSubtotal');
        const gstEl = document.getElementById('orderGST');
        const platformEl = document.getElementById('orderPlatform');
        const totalEl = document.getElementById('orderTotal');
        const pkgPriceEl = document.getElementById('osPkgPrice');
        const pkgNameEl = document.getElementById('osPkgName');

        if (subtotalEl) subtotalEl.textContent = '₹' + base.toFixed(2);
        if (gstEl) gstEl.textContent = '₹' + gst.toFixed(2);
        if (platformEl) platformEl.textContent = '₹' + PLATFORM_FEE.toFixed(2);
        if (totalEl) totalEl.textContent = '₹' + total.toFixed(2);
        if (pkgPriceEl) pkgPriceEl.textContent = '₹' + base.toFixed(2);
        if (pkgNameEl) pkgNameEl.textContent = pkg.charAt(0).toUpperCase() + pkg.slice(1);
    }

    // ── Payment Method Selection ──
    const paymentCards = document.querySelectorAll('.payment-method-card');
    paymentCards.forEach(function (card) {
        card.addEventListener('click', function () {
            paymentCards.forEach(function (c) { c.classList.remove('selected'); });
            this.classList.add('selected');
            const radio = this.querySelector('.payment-radio');
            if (radio) radio.checked = true;
        });
    });

    // ── Terms Checkbox Validation ──
    const checkbox1 = document.getElementById('termsCheck1');
    const checkbox2 = document.getElementById('termsCheck2');
    const purchaseBtn = document.getElementById('purchasePlanBtn');

    function updatePurchaseBtn() {
        if (!purchaseBtn) return;
        const checked = (!checkbox1 || checkbox1.checked) && (!checkbox2 || checkbox2.checked);
        purchaseBtn.disabled = !checked;
        purchaseBtn.style.opacity = checked ? '1' : '0.5';
        purchaseBtn.style.cursor = checked ? 'pointer' : 'not-allowed';
    }

    if (checkbox1) checkbox1.addEventListener('change', updatePurchaseBtn);
    if (checkbox2) checkbox2.addEventListener('change', updatePurchaseBtn);

    // ── Purchase Button Click ──
    if (purchaseBtn) {
        purchaseBtn.addEventListener('click', function (e) {
            e.preventDefault();
            if (this.disabled) return;
            // Simulate payment
            this.textContent = 'Processing...';
            this.disabled = true;
            setTimeout(function () {
                alert('Payment successful! Your package has been activated.');
                window.location.href = 'index.html';
            }, 1500);
        });
    }

    // ── Init ──
    updateOrderSummary(selectedPackage);
    updatePurchaseBtn();

    // Default: disable until checked
    if (purchaseBtn) {
        purchaseBtn.disabled = true;
        purchaseBtn.style.opacity = '0.5';
    }

})();
