<?php
$settings = json_decode($request['settings'], true);
$user_data = isset($settings) ? $settings : [];
?>

<div class="card">
    <div class="card-header bg-info text-white">
        <h5 class="card-title mb-0">
            <i class="fas fa-cogs me-2"></i><?= __('admin.test_gateway_payment_processing') ?>
        </h5>
    </div>
    <div class="card-body">
        <div class="row mb-3">
            <div class="col-md-6">
                <strong><?= __('admin.amount') ?>:</strong> <?= c_format($request['total']) ?>
            </div>
            <div class="col-md-6">
                <strong><?= __('admin.request_date') ?>:</strong> <?= date('M d, Y H:i', strtotime($request['created_at'])) ?>
            </div>
        </div>
        
        <div class="row mb-3">
            <div class="col-md-6">
                <strong><?= __('admin.status') ?>:</strong> <?= withdrwal_status($request['status']) ?>
            </div>
            <div class="col-md-6">
                <strong><?= __('admin.payment_method') ?>:</strong> <span class="badge bg-primary"><?= $request['prefer_method'] ?></span>
            </div>
        </div>

        <div class="mb-3">
            <h6 class="fw-semibold"><?= __('admin.user_payment_details') ?>:</h6>
            <div class="bg-light p-3 rounded">
                <div class="row">
                    <div class="col-md-6">
                        <strong><?= __('admin.test_gateway_name_label') ?>:</strong> <?= isset($user_data['name']) ? $user_data['name'] : '-' ?>
                    </div>
                    <div class="col-md-6">
                        <strong><?= __('admin.test_gateway_email_label') ?>:</strong> <?= isset($user_data['email']) ? $user_data['email'] : '-' ?>
                    </div>
                </div>
                <div class="row mt-2">
                    <div class="col-md-6">
                        <strong><?= __('admin.test_gateway_phone_label') ?>:</strong> <?= isset($user_data['phone']) ? $user_data['phone'] : '-' ?>
                    </div>
                    <div class="col-md-6">
                        <strong><?= __('admin.test_gateway_account_label') ?>:</strong> <?= isset($user_data['account']) ? $user_data['account'] : '-' ?>
                    </div>
                </div>
            </div>
        </div>

        <div class="d-flex gap-2">
            <button type="button" class="btn btn-success btn-lg" onclick="processTestPayment(<?= $request['id'] ?>, 'complete')">
                <i class="fas fa-check me-2"></i><?= __('admin.mark_as_completed') ?>
            </button>
            <button type="button" class="btn btn-warning btn-lg" onclick="processTestPayment(<?= $request['id'] ?>, 'processing')">
                <i class="fas fa-clock me-2"></i><?= __('admin.mark_as_processing') ?>
            </button>
            <button type="button" class="btn btn-danger" onclick="processTestPayment(<?= $request['id'] ?>, 'failed')">
                <i class="fas fa-times me-2"></i><?= __('admin.mark_as_failed') ?>
            </button>
            <button type="button" class="btn btn-secondary" onclick="window.history.back()">
                <i class="fas fa-arrow-left me-2"></i><?= __('admin.go_back') ?>
            </button>
        </div>
    </div>
</div>

<script>
function processTestPayment(requestId, action) {
    let title, text, status, comment;
    
    switch(action) {
        case 'complete':
            title = '<?= __('admin.confirm_complete_payment') ?>';
            text = '<?= __('admin.confirm_complete_payment_text') ?>';
            status = 1;
            comment = 'Payment completed via Test Gateway';
            break;
        case 'processing':
            title = '<?= __('admin.confirm_processing_payment') ?>';
            text = '<?= __('admin.confirm_processing_payment_text') ?>';
            status = 7;
            comment = 'Payment is being processed via Test Gateway';
            break;
        case 'failed':
            title = '<?= __('admin.confirm_failed_payment') ?>';
            text = '<?= __('admin.confirm_failed_payment_text') ?>';
            status = 5;
            comment = 'Payment failed via Test Gateway';
            break;
    }
    
    Swal.fire({
        title: title,
        text: text,
        icon: action === 'complete' ? 'success' : action === 'processing' ? 'info' : 'warning',
        showCancelButton: true,
        confirmButtonColor: action === 'complete' ? '#28a745' : action === 'processing' ? '#ffc107' : '#dc3545',
        cancelButtonColor: '#6c757d',
        confirmButtonText: '<?= __('admin.yes_confirm') ?>',
        cancelButtonText: '<?= __('admin.cancel') ?>',
        showLoaderOnConfirm: true,
        preConfirm: () => {
            return $.ajax({
                url: '<?= base_url('admincontrol/wallet_requests_details/' . $request['id']) ?>',
                type: 'POST',
                dataType: 'json',
                data: {
                    status: status,
                    comment: comment
                }
            }).then(response => {
                if (!response.success) {
                    throw new Error(response.message || '<?= __('admin.something_went_wrong') ?>');
                }
                return response;
            }).catch(error => {
                Swal.showValidationMessage('<?= __('admin.request_failed') ?>: ' + error.message);
            });
        }
    }).then((result) => {
        if (result.isConfirmed) {
            Swal.fire({
                title: '<?= __('admin.success') ?>',
                text: '<?= __('admin.status_updated_successfully') ?>',
                icon: 'success',
                timer: 2000,
                showConfirmButton: false
            }).then(() => {
                window.location.reload();
            });
        }
    });
}
</script>