<?php 
$this->load->view('layouts/header');
$this->load->helper('layout');
?>

    <!-- App Header -->
    <div class="appHeader bg-primary text-light">
        <div class="left">
            <a href="<?php echo base_url('admin/users'); ?>" class="headerButton">
                <ion-icon name="chevron-back-outline"></ion-icon>
            </a>
        </div>
        <div class="pageTitle">User Details</div>
        <div class="right">
            <a href="#" class="headerButton" data-bs-toggle="modal" data-bs-target="#adminSidebar">
                <ion-icon name="menu-outline"></ion-icon>
            </a>
        </div>
    </div>

    <!-- App Capsule -->
    <div id="appCapsule">
        
        <?php if ($this->session->flashdata('success')): ?>
        <div class="section mt-2">
            <div class="alert alert-success"><?php echo $this->session->flashdata('success'); ?></div>
        </div>
        <?php endif; ?>
        
        <?php if ($this->session->flashdata('error')): ?>
        <div class="section mt-2">
            <div class="alert alert-danger"><?php echo $this->session->flashdata('error'); ?></div>
        </div>
        <?php endif; ?>

        <!-- User Info -->
        <div class="section mt-2">
            <div class="card">
                <div class="card-body">
                    <div class="d-flex align-items-center">
                        <img src="<?php echo get_user_avatar($user_data, 'w64'); ?>" alt="avatar" class="imaged w64 rounded me-3">
                        <div class="flex-grow-1">
                            <h3 class="mb-0"><?php echo htmlspecialchars($user_data->first_name . ' ' . $user_data->last_name); ?></h3>
                            <p class="text-muted mb-1"><?php echo htmlspecialchars($user_data->email); ?></p>
                            <span class="badge badge-<?php 
                                echo $user_data->status == 'active' ? 'success' : 
                                    ($user_data->status == 'pending' ? 'warning' : 
                                    ($user_data->status == 'suspended' ? 'danger' : 'secondary')); 
                            ?>">
                                <?php echo ucfirst($user_data->status); ?>
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Quick Actions -->
        <div class="section mt-2">
            <div class="row">
                <div class="col-6">
                    <a href="<?php echo base_url('admin/create_account/' . $user_data->id); ?>" class="card text-center">
                        <div class="card-body py-2">
                            <ion-icon name="add-circle-outline" style="font-size: 24px;" class="text-primary"></ion-icon>
                            <p class="mb-0 small">Add Account</p>
                        </div>
                    </a>
                </div>
                <div class="col-6">
                    <a href="#" class="card text-center" data-bs-toggle="modal" data-bs-target="#statusModal">
                        <div class="card-body py-2">
                            <ion-icon name="shield-checkmark-outline" style="font-size: 24px;" class="text-warning"></ion-icon>
                            <p class="mb-0 small">Status</p>
                        </div>
                    </a>
                </div>
            </div>
            <div class="row mt-2">
                <div class="col-6">
                    <a href="#" class="card text-center" data-bs-toggle="modal" data-bs-target="#creditModal">
                        <div class="card-body py-2">
                            <ion-icon name="arrow-down-circle-outline" style="font-size: 24px;" class="text-success"></ion-icon>
                            <p class="mb-0 small">Credit</p>
                        </div>
                    </a>
                </div>
                <div class="col-6">
                    <a href="#" class="card text-center" data-bs-toggle="modal" data-bs-target="#debitModal">
                        <div class="card-body py-2">
                            <ion-icon name="arrow-up-circle-outline" style="font-size: 24px;" class="text-danger"></ion-icon>
                            <p class="mb-0 small">Debit</p>
                        </div>
                    </a>
                </div>
            </div>
            <?php if (isset($kyc) && $kyc->verification_status === 'pending'): ?>
            <div class="row mt-2">
                <div class="col-6">
                    <a href="#" class="card text-center" data-bs-toggle="modal" data-bs-target="#approveKycModal">
                        <div class="card-body py-2">
                            <ion-icon name="checkmark-circle-outline" style="font-size: 24px;" class="text-success"></ion-icon>
                            <p class="mb-0 small">Approve KYC</p>
                        </div>
                    </a>
                </div>
                <div class="col-6">
                    <a href="#" class="card text-center" data-bs-toggle="modal" data-bs-target="#rejectKycModal">
                        <div class="card-body py-2">
                            <ion-icon name="close-circle-outline" style="font-size: 24px;" class="text-danger"></ion-icon>
                            <p class="mb-0 small">Reject KYC</p>
                        </div>
                    </a>
                </div>
            </div>
            <?php endif; ?>
        </div>

        <!-- User Details -->
        <div class="section mt-2">
            <div class="section-title">Details</div>
            <ul class="listview flush transparent simple-listview">
                <li>
                    <strong>Phone</strong>
                    <span><?php echo htmlspecialchars($user_data->phone); ?></span>
                </li>
                <li>
                    <strong>User Type</strong>
                    <span><?php echo ucfirst($user_data->user_type); ?></span>
                </li>
                <li>
                    <strong>KYC Status</strong>
                    <span class="badge badge-<?php echo ($user_data->kyc_status ?? 'pending') == 'verified' ? 'success' : 'warning'; ?>">
                        <?php echo ucfirst($user_data->kyc_status ?? 'Pending'); ?>
                    </span>
                </li>
                <li>
                    <strong>KYC Tier</strong>
                    <span><?php echo $user_data->kyc_tier ?? '1'; ?></span>
                </li>
                <li>
                    <strong>Registered</strong>
                    <span><?php echo date('M d, Y', strtotime($user_data->created_at)); ?></span>
                </li>
            </ul>
        </div>

        <!-- Accounts -->
        <div class="section mt-2">
            <div class="section-title d-flex justify-content-between">
                <span>Accounts (<?php echo count($accounts); ?>)</span>
                <a href="<?php echo base_url('admin/create_account/' . $user_data->id); ?>" class="text-primary">
                    <ion-icon name="add-outline"></ion-icon>
                </a>
            </div>
            
            <?php if (empty($accounts)): ?>
            <div class="card">
                <div class="card-body text-center py-3">
                    <p class="text-muted mb-0">No accounts</p>
                </div>
            </div>
            <?php else: ?>
            <ul class="listview image-listview">
                <?php foreach ($accounts as $acc): ?>
                <li>
                    <div class="item">
                        <div class="icon-box bg-<?php echo $acc->status == 'active' ? 'success' : 'secondary'; ?>">
                            <ion-icon name="wallet-outline"></ion-icon>
                        </div>
                        <div class="in">
                            <div>
                                <strong><?php echo $acc->account_number; ?></strong>
                                <p class="mb-0 small"><?php echo ucfirst($acc->account_type); ?> - <?php echo $acc->currency; ?></p>
                            </div>
                            <span class="text-<?php echo $acc->status == 'active' ? 'success' : 'muted'; ?>">
                                <?php echo format_money($acc->available_balance ?? 0, $acc->currency); ?>
                            </span>
                        </div>
                    </div>
                </li>
                <?php endforeach; ?>
            </ul>
            <?php endif; ?>
        </div>

        <!-- Cards -->
        <div class="section mt-2">
            <div class="section-title d-flex justify-content-between">
                <span>Cards (<?php echo count($cards); ?>)</span>
                <a href="#" class="text-primary" data-bs-toggle="modal" data-bs-target="#createCardModal">
                    <ion-icon name="add-outline"></ion-icon>
                </a>
            </div>
            
            <?php if (empty($cards)): ?>
            <div class="card">
                <div class="card-body text-center py-3">
                    <p class="text-muted mb-0">No cards</p>
                    <a href="#" class="btn btn-primary btn-sm mt-2" data-bs-toggle="modal" data-bs-target="#createCardModal">
                        <ion-icon name="add-outline"></ion-icon> Add Card
                    </a>
                </div>
            </div>
            <?php else: ?>
            <ul class="listview image-listview">
                <?php foreach ($cards as $card): ?>
                <li>
                    <div class="item">
                        <div class="icon-box bg-<?php echo $card->status == 'active' ? 'primary' : 'secondary'; ?>">
                            <ion-icon name="card-outline"></ion-icon>
                        </div>
                        <div class="in">
                            <div>
                                <strong><?php echo $card->card_number_masked; ?></strong>
                                <p class="mb-0 small"><?php echo ucfirst($card->card_type); ?> <?php echo ucfirst($card->card_brand); ?></p>
                            </div>
                            <span class="badge badge-<?php echo $card->status == 'active' ? 'success' : 'secondary'; ?>">
                                <?php echo ucfirst($card->status); ?>
                            </span>
                        </div>
                    </div>
                </li>
                <?php endforeach; ?>
            </ul>
            <?php endif; ?>
        </div>

        <!-- Recent Transactions -->
        <div class="section mt-2 mb-3">
            <div class="section-title">Recent Transactions</div>
            
            <?php if (empty($transactions)): ?>
            <div class="card">
                <div class="card-body text-center py-3">
                    <p class="text-muted mb-0">No transactions</p>
                </div>
            </div>
            <?php else: ?>
            <ul class="listview image-listview">
                <?php foreach ($transactions as $txn): ?>
                <?php 
                $is_debit = !empty($txn->source_account_id);
                $icon_color = $is_debit ? 'danger' : 'success';
                $icon_name = $is_debit ? 'arrow-up-outline' : 'arrow-down-outline';
                ?>
                <li>
                    <a href="<?php echo base_url('admin/transaction/' . $txn->id); ?>" class="item">
                        <div class="icon-box bg-<?php echo $icon_color; ?>">
                            <ion-icon name="<?php echo $icon_name; ?>"></ion-icon>
                        </div>
                        <div class="in">
                            <div>
                                <strong><?php echo ucfirst($txn->type); ?></strong>
                                <p class="mb-0 small text-muted"><?php echo date('M d, Y H:i', strtotime($txn->created_at)); ?></p>
                            </div>
                            <span class="text-<?php echo $icon_color; ?>" style="font-weight: 600;">
                                <?php echo ($is_debit ? '- ' : '+ ') . format_money($txn->amount, $txn->currency); ?>
                            </span>
                        </div>
                    </a>
                </li>
                <?php endforeach; ?>
            </ul>
            <?php endif; ?>
        </div>
    </div>

    <!-- Status Modal -->
    <div class="modal fade" id="statusModal" tabindex="-1">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Update Status</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="form-group basic">
                        <label class="label">Select Status</label>
                        <select class="form-control" id="newStatus">
                            <option value="active" <?php echo $user_data->status == 'active' ? 'selected' : ''; ?>>Active</option>
                            <option value="inactive" <?php echo $user_data->status == 'inactive' ? 'selected' : ''; ?>>Inactive</option>
                            <option value="suspended" <?php echo $user_data->status == 'suspended' ? 'selected' : ''; ?>>Suspended</option>
                            <option value="pending" <?php echo $user_data->status == 'pending' ? 'selected' : ''; ?>>Pending</option>
                        </select>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="button" class="btn btn-primary" onclick="updateStatus()">Update</button>
                </div>
            </div>
        </div>
    </div>

    <!-- Credit Modal -->
    <div class="modal fade" id="creditModal" tabindex="-1">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Credit Account</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <?php if (!empty($accounts)): ?>
                    <div class="form-group basic">
                        <label class="label">Select Account</label>
                        <select class="form-control" id="creditAccount">
                            <?php foreach ($accounts as $acc): ?>
                            <option value="<?php echo $acc->id; ?>"><?php echo $acc->account_number; ?> (<?php echo $acc->currency; ?>)</option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="form-group basic">
                        <label class="label">Amount</label>
                        <input type="number" class="form-control" id="creditAmount" step="0.01" min="0.01">
                    </div>
                    <div class="form-group basic">
                        <label class="label">Narration</label>
                        <input type="text" class="form-control" id="creditNarration" placeholder="Admin credit">
                    </div>
                    <?php else: ?>
                    <p class="text-muted">User has no accounts to credit.</p>
                    <?php endif; ?>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <?php if (!empty($accounts)): ?>
                    <button type="button" class="btn btn-success" id="creditBtn" onclick="creditAccount()">Credit</button>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>

    <!-- Debit Modal -->
    <div class="modal fade" id="debitModal" tabindex="-1">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Debit Account</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <?php if (!empty($accounts)): ?>
                    <div class="form-group basic">
                        <label class="label">Select Account</label>
                        <select class="form-control" id="debitAccount">
                            <?php foreach ($accounts as $acc): ?>
                            <option value="<?php echo $acc->id; ?>"><?php echo $acc->account_number; ?> (<?php echo $acc->currency; ?> - <?php echo format_money($acc->available_balance, $acc->currency); ?>)</option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="form-group basic">
                        <label class="label">Amount</label>
                        <input type="number" class="form-control" id="debitAmount" step="0.01" min="0.01">
                    </div>
                    <div class="form-group basic">
                        <label class="label">Narration</label>
                        <input type="text" class="form-control" id="debitNarration" placeholder="Admin debit">
                    </div>
                    <?php else: ?>
                    <p class="text-muted">User has no accounts to debit.</p>
                    <?php endif; ?>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <?php if (!empty($accounts)): ?>
                    <button type="button" class="btn btn-danger" id="debitBtn" onclick="debitAccount()">Debit</button>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>

    <!-- Create Card Modal -->
    <div class="modal fade" id="createCardModal" tabindex="-1">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Create Card</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <?php if (!empty($accounts)): ?>
                    <div class="form-group basic">
                        <label class="label">Link to Account</label>
                        <select class="form-control" id="cardAccount">
                            <?php foreach ($accounts as $acc): ?>
                            <option value="<?php echo $acc->id; ?>"><?php echo $acc->account_number; ?> (<?php echo $acc->currency; ?>)</option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="form-group basic">
                        <label class="label">Card Type</label>
                        <select class="form-control" id="cardType">
                            <option value="virtual">Virtual Card</option>
                            <option value="physical">Physical Card</option>
                        </select>
                    </div>
                    <div class="form-group basic">
                        <label class="label">Card Brand</label>
                        <select class="form-control" id="cardBrand">
                            <option value="mastercard">Mastercard</option>
                            <option value="visa">Visa</option>
                        </select>
                    </div>
                    <div class="form-group basic">
                        <label class="label">Card Name</label>
                        <input type="text" class="form-control" id="cardName" value="<?php echo htmlspecialchars($user_data->first_name . ' ' . $user_data->last_name); ?>">
                    </div>
                    <div class="form-group basic">
                        <label class="label">Initial Balance</label>
                        <input type="number" class="form-control" id="cardBalance" step="0.01" min="0" value="0">
                    </div>
                    <?php else: ?>
                    <p class="text-muted">User needs at least one account to create a card.</p>
                    <?php endif; ?>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <?php if (!empty($accounts)): ?>
                    <button type="button" class="btn btn-primary" onclick="createCard()">Create Card</button>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>

    <!-- Approve KYC Modal -->
    <div class="modal fade" id="approveKycModal" tabindex="-1">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Approve KYC</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="alert alert-success">
                        <ion-icon name="checkmark-circle-outline" style="font-size: 48px;"></ion-icon>
                        <p class="mt-2 mb-0"><strong>Are you sure you want to approve this user's KYC verification?</strong></p>
                        <p class="mb-0 small">The user will be able to access all features once approved.</p>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="button" class="btn btn-success" onclick="updateKycStatus('approved')">Approve</button>
                </div>
            </div>
        </div>
    </div>

    <!-- Reject KYC Modal -->
    <div class="modal fade" id="rejectKycModal" tabindex="-1">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Reject KYC</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="alert alert-warning">
                        <ion-icon name="alert-circle-outline" style="font-size: 48px;"></ion-icon>
                        <p class="mt-2 mb-0"><strong>Are you sure you want to reject this user's KYC verification?</strong></p>
                    </div>
                    <div class="form-group basic">
                        <label class="label">Rejection Reason (Optional)</label>
                        <textarea class="form-control" id="rejectionReason" rows="3" placeholder="Enter reason for rejection..."></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="button" class="btn btn-danger" onclick="updateKycStatus('rejected')">Reject</button>
                </div>
            </div>
        </div>
    </div>

    <!-- Admin Sidebar -->
    <?php $this->load->view('admin/partials/sidebar'); ?>

    <script>
    function createCard() {
        var accountId = document.getElementById('cardAccount').value;
        var cardType = document.getElementById('cardType').value;
        var cardBrand = document.getElementById('cardBrand').value;
        var cardName = document.getElementById('cardName').value;
        var balance = document.getElementById('cardBalance').value;
        
        fetch('<?php echo base_url('admin/create_card/' . $user_data->id); ?>', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'X-Requested-With': 'XMLHttpRequest'
            },
            body: 'account_id=' + encodeURIComponent(accountId) + 
                  '&card_type=' + encodeURIComponent(cardType) + 
                  '&card_brand=' + encodeURIComponent(cardBrand) + 
                  '&card_name=' + encodeURIComponent(cardName) + 
                  '&balance=' + encodeURIComponent(balance) + 
                  '&<?php echo $this->security->get_csrf_token_name(); ?>=<?php echo $this->security->get_csrf_hash(); ?>'
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                MeridianDialog.success(data.message, function() {
                    location.reload();
                });
            } else {
                MeridianDialog.error(data.message || 'An error occurred');
            }
        })
        .catch(error => {
            MeridianDialog.error('An error occurred. Please try again.');
        });
    }

    function updateStatus() {
        var status = document.getElementById('newStatus').value;
        
        fetch('<?php echo base_url('admin/update_user_status/' . $user_data->id); ?>', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'X-Requested-With': 'XMLHttpRequest'
            },
            body: 'status=' + encodeURIComponent(status) + '&<?php echo $this->security->get_csrf_token_name(); ?>=<?php echo $this->security->get_csrf_hash(); ?>'
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                MeridianDialog.success(data.message, function() {
                    location.reload();
                });
            } else {
                MeridianDialog.error(data.message || 'An error occurred');
            }
        })
        .catch(error => {
            MeridianDialog.error('An error occurred. Please try again.');
        });
    }

    function creditAccount() {
        var accountId = document.getElementById('creditAccount').value;
        var amount = document.getElementById('creditAmount').value;
        var narration = document.getElementById('creditNarration').value;
        var btn = document.getElementById('creditBtn');
        
        if (!amount || amount <= 0) {
            MeridianDialog.error('Please enter a valid amount');
            return;
        }
        
        // Disable button and show loading state
        btn.disabled = true;
        btn.innerHTML = '<span class="spinner-border spinner-border-sm me-1"></span> Processing...';
        
        fetch('<?php echo base_url('admin/credit_account/'); ?>' + accountId, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'X-Requested-With': 'XMLHttpRequest'
            },
            body: 'amount=' + encodeURIComponent(amount) + '&narration=' + encodeURIComponent(narration) + '&<?php echo $this->security->get_csrf_token_name(); ?>=<?php echo $this->security->get_csrf_hash(); ?>'
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                MeridianDialog.success(data.message, function() {
                    location.reload();
                });
            } else {
                // Re-enable button on error
                btn.disabled = false;
                btn.innerHTML = 'Credit';
                MeridianDialog.error(data.message || 'An error occurred');
            }
        })
        .catch(error => {
            // Re-enable button on error
            btn.disabled = false;
            btn.innerHTML = 'Credit';
            MeridianDialog.error('An error occurred. Please try again.');
        });
    }

    function debitAccount() {
        var accountId = document.getElementById('debitAccount').value;
        var amount = document.getElementById('debitAmount').value;
        var narration = document.getElementById('debitNarration').value;
        var btn = document.getElementById('debitBtn');
        
        if (!amount || amount <= 0) {
            MeridianDialog.error('Please enter a valid amount');
            return;
        }
        
        // Confirm debit action
        MeridianDialog.confirm(
            'Debit this account?',
            'Are you sure you want to debit ' + amount + ' from this account? This action cannot be undone.',
            function() {
                // Disable button and show loading state
                btn.disabled = true;
                btn.innerHTML = '<span class="spinner-border spinner-border-sm me-1"></span> Processing...';
                
                fetch('<?php echo base_url('admin/debit_account/'); ?>' + accountId, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded',
                        'X-Requested-With': 'XMLHttpRequest'
                    },
                    body: 'amount=' + encodeURIComponent(amount) + '&narration=' + encodeURIComponent(narration) + '&<?php echo $this->security->get_csrf_token_name(); ?>=<?php echo $this->security->get_csrf_hash(); ?>'
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        MeridianDialog.success(data.message, function() {
                            location.reload();
                        });
                    } else {
                        // Re-enable button on error
                        btn.disabled = false;
                        btn.innerHTML = 'Debit';
                        MeridianDialog.error(data.message || 'An error occurred');
                    }
                })
                .catch(error => {
                    // Re-enable button on error
                    btn.disabled = false;
                    btn.innerHTML = 'Debit';
                    MeridianDialog.error('An error occurred. Please try again.');
                });
            }
        );
    }
    
    function updateKycStatus(status) {
        var rejection_reason = status === 'rejected' ? document.getElementById('rejectionReason').value : '';
        
        fetch('<?php echo base_url('admin/update_kyc_status/' . $user_data->id); ?>', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'X-Requested-With': 'XMLHttpRequest'
            },
            body: 'status=' + encodeURIComponent(status) + 
                  '&rejection_reason=' + encodeURIComponent(rejection_reason) +
                  '&<?php echo $this->security->get_csrf_token_name(); ?>=<?php echo $this->security->get_csrf_hash(); ?>'
        })
        .then(response => response.json())
        .then(data => {
            if (data.success) {
                MeridianDialog.success(data.message, function() {
                    location.reload();
                });
            } else {
                MeridianDialog.error(data.message || 'An error occurred');
            }
        })
        .catch(error => {
            MeridianDialog.error('An error occurred. Please try again.');
        });
    }
    </script>

<?php $this->load->view('layouts/footer'); ?>
