<?php
if (!defined('ABSPATH')) exit;
if (!current_user_can('manage_options')) die('You are not authorized to proceed.');

$rows_per_page = 50;

$page = isset($_POST['eotpa_page']) ? max(1, intval($_POST['eotpa_page'])) : 1;
$search = isset($_POST['eotpa_search']) ? sanitize_text_field($_POST['eotpa_search']) : '';
$sort = isset($_POST['eotpa_sort']) ? sanitize_text_field($_POST['eotpa_sort']) : 'time';
$order = isset($_POST['eotpa_order']) ? sanitize_text_field($_POST['eotpa_order']) : 'desc';

function eotpa_col_index($col)
{
    switch ($col)
    {
        case 'action':
            return 1;
        case 'rcode':
            return 2;
        case 'status':
            return 3;
        case 'email':
            return 4;
        case 'response':
            return 5;

        default:
            return 0;
    }
}

?>
<style>

	.notice{
		display:none;
	}

	.table-container{
		overflow-y:auto;
		border:1px solid #ccc;
		width:97%;
		max-height:600px;
	}

	table{
		width:100%;
		border-collapse:collapse;
	}

	thead th{
		position:sticky;
		top:0;
		background:#f7f7f7;
		z-index:10;
		cursor:pointer;
	}

	th,td{
		padding:4px;
		border:1px solid #ddd;
		font-size:12px;
	}

	.pagination{
		margin-top:10px;
		text-align:center;
	}

	button{
		padding:3px 8px;
		margin:2px;
		cursor:pointer;
	}

	.searchbox{
		margin:10px;
	}

	.eotpa_link{
		color:#2271b1;
		cursor:pointer;
		text-decoration:none;
		border:none;
		background:none;
		padding:0;
		font-size:12px;
	}
</style>

<?php
$otplogedpostid = emailotpauthn_settingsdetails("emailotpauthn_otplogedpostid");

if ($otplogedpostid > 0){
    $metavals = get_post_meta($otplogedpostid);
    if (count($metavals) > 0){
        $rows = [];
        foreach ($metavals as $metaval){
            $metarry = explode("::splitter::", $metaval[0]);
            for ($i = 0;$i < 6;$i++){
                if (!isset($metarry[$i])) $metarry[$i] = 'Empty';
            }
            $rows[] = $metarry;
        }
        if (!empty($search)){
            $tmp = [];
            foreach ($rows as $r){
                foreach ($r as $c){
                    if (stripos($c, $search) !== false){
                        $tmp[] = $r;
                        break;
                    }
                }
            }
            $rows = $tmp;
        }

        usort($rows, function ($a, $b) use ($sort, $order)
        {
            $i = eotpa_col_index($sort);
            if ($order == 'asc') return strcmp($a[$i], $b[$i]);
            return strcmp($b[$i], $a[$i]);
        });

        $total = count($rows);
        $total_pages = max(1, ceil($total / $rows_per_page));
        if ($page > $total_pages) $page = $total_pages;
        $offset = ($page - 1) * $rows_per_page;
        $rows = array_slice($rows, $offset, $rows_per_page);

        echo '<div align="center"><h3 style="color:#990000;margin-top:10px;">OTP Activity Log</h3></div>';
        echo '<div style="display:block;width:97%;">';
        echo '<div style="display:inline-block;float:left;font-size:12px;margin: 15 0 0 15;">';
        echo 'Showing ' . ($offset + 1) . ' - ' . min($offset + $rows_per_page, $total) . ' of ' . $total . ' records';
        echo '</div>';
        echo '<div style="display:inline-block;float:right;">';
        echo '<form method="post" class="searchbox">';
        echo '<input type="text" name="eotpa_search" value="' . esc_attr($search) . '" placeholder="Search logs">';
        echo '<input type="hidden" name="eotpa_sort" value="' . esc_attr($sort) . '">';
        echo '<input type="hidden" name="eotpa_order" value="' . esc_attr($order) . '">';
        echo '<input type="hidden" name="eotpa_page" value="1">';
        echo '<button type="submit">Search</button>';
        echo '</form>';
        echo '</div>';
        echo '</div>';

        function sort_btn($label, $col, $sort, $order, $search, $page)
        {
            $neworder = 'asc';
            if ($sort == $col && $order == 'asc') $neworder = 'desc';
            echo '<form method="post" style="display:inline;">';
            echo '<input type="hidden" name="eotpa_sort" value="' . $col . '">';
            echo '<input type="hidden" name="eotpa_order" value="' . $neworder . '">';
            echo '<input type="hidden" name="eotpa_search" value="' . esc_attr($search) . '">';
            echo '<input type="hidden" name="eotpa_page" value="' . $page . '">';
            echo '<a class="eotpa_link" onclick="this.closest(\'form\').submit();">' . $label . '</a>';
            echo '</form>';
        }

        echo '<div class="table-container">';
        echo '<table>';
        echo '<thead><tr>';
        echo '<th>';
        sort_btn('Time', 'time', $sort, $order, $search, $page);
        echo '</th>';
        echo '<th>';
        sort_btn('Action', 'action', $sort, $order, $search, $page);
        echo '</th>';
        echo '<th>';
        sort_btn('R.Code', 'rcode', $sort, $order, $search, $page);
        echo '</th>';
        echo '<th>';
        sort_btn('Status', 'status', $sort, $order, $search, $page);
        echo '</th>';
        echo '<th>';
        sort_btn('Email', 'email', $sort, $order, $search, $page);
        echo '</th>';
        echo '<th>';
        sort_btn('Response', 'response', $sort, $order, $search, $page);
        echo '</th>';
        echo '</tr></thead><tbody>';

        foreach ($rows as $r){
            echo '<tr>';
            echo '<td>' . esc_html($r[0]) . '</td>';
            echo '<td>' . esc_html($r[1]) . '</td>';
            echo '<td align="center">' . esc_html($r[2]) . '</td>';
            echo '<td align="center">' . esc_html($r[3]) . '</td>';
            echo '<td>' . esc_html($r[4]) . '</td>';
            echo '<td>' . esc_html($r[5]) . '</td>';
            echo '</tr>';
        }
        echo '</tbody></table></div>';

        echo '<div class="pagination">';
        if ($page > 1){
            echo '<form method="post" style="display:inline;">';
            echo '<input type="hidden" name="eotpa_page" value="' . ($page - 1) . '">';
            echo '<input type="hidden" name="eotpa_search" value="' . esc_attr($search) . '">';
            echo '<input type="hidden" name="eotpa_sort" value="' . esc_attr($sort) . '">';
            echo '<input type="hidden" name="eotpa_order" value="' . esc_attr($order) . '">';
            echo '<button>Prev</button>';
            echo '</form>';
        }

        echo " Page " . $page . " of " . $total_pages . " ";
        if ($page < $total_pages){
            echo '<form method="post" style="display:inline;">';
            echo '<input type="hidden" name="eotpa_page" value="' . ($page + 1) . '">';
            echo '<input type="hidden" name="eotpa_search" value="' . esc_attr($search) . '">';
            echo '<input type="hidden" name="eotpa_sort" value="' . esc_attr($sort) . '">';
            echo '<input type="hidden" name="eotpa_order" value="' . esc_attr($order) . '">';
            echo '<button>Next</button>';
            echo '</form>';
        }
        echo '</div>';
    } else {
        echo '<br><br><div align="center"><h2 style="color:#990000">OTP Activity Log</h2><h3>No Log Available</h3></div>';
    }
} else {
    echo '<br><br><div align="center"><h2 style="color:#990000">OTP Activity Log</h2><h3>No Log Available</h3></div>';
}

?>
