<?php
require_once '../includes/config.php';
requireAdmin();
$db = getDB();

// Check intl_products table
$total = 0; $bd = 0; $active = 0;
try {
    $total  = $db->query("SELECT COUNT(*) FROM intl_products")->fetchColumn();
    $bd     = $db->query("SELECT COUNT(*) FROM intl_products WHERE country_iso='BD'")->fetchColumn();
    $active = $db->query("SELECT COUNT(*) FROM intl_products WHERE country_iso='BD' AND is_active=1 AND product_type='recharge'")->fetchColumn();
} catch(Exception $e) { echo "Table error: ".$e->getMessage(); }

// Sample BD packages
$samples = [];
try {
    $samples = $db->query("SELECT sku_code,provider_name,send_value,send_currency,recv_value,recv_currency,product_type,is_active FROM intl_products WHERE country_iso='BD' LIMIT 10")->fetchAll();
} catch(Exception $e) {}

// Check intl_countries BD
$bdCountry = null;
try {
    $bdCountry = $db->query("SELECT * FROM intl_countries WHERE iso='BD'")->fetch();
} catch(Exception $e) {}
?>
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Intl Debug</title>
<link rel="stylesheet" href="../assets/css/admin.css">
<style>pre{background:#f5f5f5;padding:12px;border-radius:8px;font-size:12px;overflow:auto} .ok{color:green;font-weight:700} .err{color:red;font-weight:700}</style>
</head>
<body>
<?php include '../includes/admin_sidebar.php'; ?>
<div class="main"><div class="content">
<h2>🌍 International Debug</h2>

<div style="background:white;border-radius:12px;padding:20px;margin-bottom:16px">
    <h3>intl_products Table</h3>
    <p>Total rows: <b><?= $total ?></b></p>
    <p>BD rows: <b><?= $bd ?></b></p>
    <p>BD active recharge: <b class="<?= $active>0?'ok':'err' ?>"><?= $active ?></b></p>
</div>

<div style="background:white;border-radius:12px;padding:20px;margin-bottom:16px">
    <h3>intl_countries BD</h3>
    <?php if($bdCountry): ?>
    <p>Name: <b><?= $bdCountry['name'] ?></b></p>
    <p>is_enabled: <b class="<?= $bdCountry['is_enabled']?'ok':'err' ?>"><?= $bdCountry['is_enabled'] ?></b></p>
    <?php else: ?><p class="err">BD country not found!</p><?php endif; ?>
</div>

<?php if(!empty($samples)): ?>
<div style="background:white;border-radius:12px;padding:20px;margin-bottom:16px">
    <h3>Sample BD Packages (10)</h3>
    <table style="width:100%;border-collapse:collapse;font-size:12px">
        <tr style="background:#f5f5f5"><th style="padding:6px;text-align:left">SKU</th><th>Provider</th><th>Send</th><th>Recv</th><th>Type</th><th>Active</th></tr>
        <?php foreach($samples as $s): ?>
        <tr style="border-bottom:1px solid #eee">
            <td style="padding:6px"><?= $s['sku_code'] ?></td>
            <td><?= $s['provider_name'] ?></td>
            <td><?= $s['send_value'].' '.$s['send_currency'] ?></td>
            <td><?= $s['recv_value'].' '.$s['recv_currency'] ?></td>
            <td><?= $s['product_type'] ?></td>
            <td class="<?= $s['is_active']?'ok':'err' ?>"><?= $s['is_active'] ?></td>
        </tr>
        <?php endforeach; ?>
    </table>
</div>
<?php else: ?>
<div style="background:#fff5f5;border-radius:12px;padding:20px;color:#e53e3e;font-weight:700">
    ❌ No BD packages in DB — Sync করুন
</div>
<?php endif; ?>

<!-- Test API response -->
<div style="background:white;border-radius:12px;padding:20px">
    <h3>Test ding_packages.php API</h3>
    <a href="../api/ding_packages.php?country=BD" target="_blank" style="padding:8px 16px;background:#1a3c8f;color:white;border-radius:8px;text-decoration:none;display:inline-block">
        Test BD Packages API →
    </a>
</div>

</div></div>
</body></html>
