<?php
/**
 * Audit Log Function
 * Logs all bill delete actions with details:
 * - Page name
 * - Logged user
 * - IP address
 * - Date & Time
 * - Page URL
 * - Action details
 */

function logAuditAction($actionType, $actionDetails, $additionalData = array()) {
    global $con;
    
    // Get logged in user details
    $loggedin_company = isset($_SESSION['company']) ? $_SESSION['company'] : array();
    $csadded = isset($_SESSION['murgibhai']) ? $_SESSION['murgibhai'] : 'unknown';
    
    // Get user details from tbl_login
    $getlog = f(q("SELECT * FROM tbl_login WHERE l_uid='$csadded'"));
    $userName = isset($getlog['l_name']) ? $getlog['l_name'] : 'Unknown';
    $userType = isset($getlog['l_type']) ? $getlog['l_type'] : '0';
    
    // Get IP address
    $ipAddress = getClientIP();
    
    // Get current date and time
    $logDate = date('Y-m-d');
    $logTime = date('H:i:s');
    
    // Get page URL
    $pageURL = getCurrentPageURL();
    
    // Get page name
    $pageName = basename($_SERVER['PHP_SELF']);
    
    // Prepare additional data
    $billId = isset($additionalData['bill_id']) ? $additionalData['bill_id'] : '';
    $customerId = isset($additionalData['customer_id']) ? $additionalData['customer_id'] : '';
    $month = isset($additionalData['month']) ? $additionalData['month'] : '';
    $year = isset($additionalData['year']) ? $additionalData['year'] : '';
    $area = isset($additionalData['area']) ? $additionalData['area'] : '';
    $amount = isset($additionalData['amount']) ? $additionalData['amount'] : '';
    
    // Insert audit log
    $query = "INSERT INTO tbl_audit_log SET 
              log_action_type = '$actionType',
              log_action_details = '$actionDetails',
              log_user_id = '$csadded',
              log_user_name = '$userName',
              log_user_type = '$userType',
              log_ip_address = '$ipAddress',
              log_date = '$logDate',
              log_time = '$logTime',
              log_page_name = '$pageName',
              log_page_url = '$pageURL',
              log_bill_id = '$billId',
              log_customer_id = '$customerId',
              log_month = '$month',
              log_year = '$year',
              log_area = '$area',
              log_amount = '$amount',
              log_company = '" . $loggedin_company['c_name'] . "'";
    
    $result = mysqli_query($con, $query);
    
    return $result;
}

/**
 * Get client IP address
 */
function getClientIP() {
    $ip = '';
    
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0';
    }
    
    return $ip;
}

/**
 * Get current page URL
 */
function getCurrentPageURL() {
    $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
    $host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
    $uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
    
    return $protocol . '://' . $host . $uri;
}

?>
