<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<?php
/**
 * CSRF Protection Token Include
 * 
 * Include this file in all forms that need CSRF protection
 * Usage: <?php include(module_views_path('desk_moloni', 'admin/partials/csrf_token')); ?>
 * 
 * @package DeskMoloni\Views\Partials
 * @version 3.0
 */

// Get CSRF token name and hash
$csrf_token_name = $this->security->get_csrf_token_name();
$csrf_hash = $this->security->get_csrf_hash();
?>

<!-- CSRF Protection Token -->
<input type="hidden" name="<?php echo h($csrf_token_name); ?>" value="<?php echo h($csrf_hash); ?>" id="csrf_token">

<script>
// Auto-refresh CSRF token for AJAX requests
if (typeof window.deskMoloniCSRF === 'undefined') {
    window.deskMoloniCSRF = {
        token_name: '<?php echo h($csrf_token_name); ?>',
        token_value: '<?php echo h($csrf_hash); ?>',
        
        // Get current token for AJAX requests
        getToken: function() {
            return {
                name: this.token_name,
                value: this.token_value
            };
        },
        
        // Update token value (called after successful AJAX requests)
        updateToken: function(newValue) {
            this.token_value = newValue;
            document.getElementById('csrf_token').value = newValue;
        },
        
        // Add CSRF token to AJAX data
        addToData: function(data) {
            if (typeof data === 'object') {
                data[this.token_name] = this.token_value;
            }
            return data;
        }
    };
}
</script>