403Webshell
Server IP : 121.121.20.254  /  Your IP : 216.73.216.202
Web Server : Microsoft-IIS/10.0
System : Windows NT WEB-SERVER 10.0 build 20348 (Windows Server 2022) AMD64
User : IUSR ( 0)
PHP Version : 8.3.28
Disable Function : NONE
MySQL : ON  |  cURL : ON  |  WGET : OFF  |  Perl : OFF  |  Python : OFF  |  Sudo : OFF  |  Pkexec : OFF
Directory :  /Program Files/MariaDB 10.6/data/invoice/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /Program Files/MariaDB 10.6/data/invoice/wp_xyz_ips_short_code.ibd
�����������eZZ@
��������������������������&&���������������������������e�^���������g�Zg�_��a�����������eZ���������������������������������i������������������������������������������������������������������������������������������������������������������������������������������������������i�	�����������������������������������������������������������������������������������������������������������eX�X�����������uE�Z��=Z�Z2 infimumsupremum�^��TableZ&?�[xyz-ips snippet="Table"]�����\��TESTZ&@�[xyz-ips snippet="TEST"]����� �3��TABLE2Z&C[xyz-ips snippet="TABLE2"]����pc���u�����������h(�-
Z?�<?php

if (!defined('ABSPATH')) exit;

global $wpdb;

$table = $wpdb->prefix . 'kaya_certificates';
$current_user = wp_get_current_user();

/*
|--------------------------------------------------------------------------
| DEBUG TOGGLE
|--------------------------------------------------------------------------
*/
$kaya_debug_columns = false;

$is_user01 = ($current_user && $current_user->user_login === 'User01');

/*
|--------------------------------------------------------------------------
| Activate / Deactivate row (only User01)
|--------------------------------------------------------------------------
*/
if (
    isset($_GET['kaya_toggle_id'], $_GET['kaya_toggle_to'], $_GET['_wpnonce']) &&
    $is_user01 &&
    wp_verify_nonce($_GET['_wpnonce'], 'kaya_toggle_row')
) {
    $toggle_id = intval($_GET['kaya_toggle_id']);
    $toggle_to = sanitize_text_field(wp_unslash($_GET['kaya_toggle_to']));
    $toggle_to = ($toggle_to === 'F') ? 'F' : 'T';

    if ($toggle_id > 0) {

        $target_row = $wpdb->get_row(
            $wpdb->prepare("SELECT * FROM {$table} WHERE id = %d", $toggle_id),
            ARRAY_A
        );

        if ($target_row) {

            $updated = $wpdb->update(
                $table,
                ['is_active' => $toggle_to],
                ['id' => $toggle_id],
                ['%s'],
                ['%d']
            );

            $post_ok = true;
            $post_id = isset($target_row['post_id']) ? intval($target_row['post_id']) : 0;

            if ($post_id > 0 && get_post($post_id)) {
                $new_post_status = ($toggle_to === 'F') ? 'draft' : 'publish';

                $post_result = wp_update_post([
                    'ID'          => $post_id,
                    'post_status' => $new_post_status,
                ], true);

                if (is_wp_error($post_result)) {
                    $post_ok = false;
                }
            }

            if ($updated !== false && $post_ok) {
                echo '<div style="max-width:1200px;margin:15px auto 0;padding:12px;background:#ecfdf5;border:1px solid #bbf7d0;color:#166534;border-radius:10px;font-weight:600;">Row and related post status updated successfully.</div>';
            } elseif ($updated !== false && !$post_ok) {
                echo '<div style="max-width:1200px;margin:15px auto 0;padding:12px;background:#fff7ed;border:1px solid #fdba74;color:#9a3412;border-radius:10px;font-weight:600;">Row status updated, but related post status could not be changed.</div>';
            } else {
                echo '<div style="max-width:1200px;margin:15px auto 0;padding:12px;background:#fff1f2;border:1px solid #fecaca;color:#991b1b;border-radius:10px;font-weight:600;">Failed to update row status.</div>';
            }

        } else {
            echo '<div style="max-width:1200px;margin:15px auto 0;padding:12px;background:#fff1f2;border:1px solid #fecaca;color:#991b1b;border-radius:10px;font-weight:600;">Record not found.</div>';
        }
    }
}

/*
|--------------------------------------------------------------------------
| Query rows
|--------------------------------------------------------------------------
*/
if ($is_user01) {
    $rows = $wpdb->get_results("SELECT * FROM {$table} ORDER BY id DESC", ARRAY_A);
} else {
    $rows = $wpdb->get_results("SELECT * FROM {$table} WHERE is_active = 'T' ORDER BY id DESC", ARRAY_A);
}

echo '
<style>

.kaya-cert-wrap{
    max-width:1200px;
    margin:auto;
    height:72vh;
    display:flex;
    flex-direction:column;
}

.kaya-cert-card{
    display:flex;
    flex-direction:column;
    height:100%;
    border-radius:18px;
    overflow:hidden;
    border:1px solid #e2e8f0;
    background:#ffffff;
}

/* DEBUG PANEL */
.kaya-debug-box{
    max-width:1200px;
    margin:0 auto 14px;
    border:1px solid #fcd34d;
    background:#fffbeb;
    border-radius:14px;
    padding:14px 16px;
    color:#78350f;
    font-size:13px;
    line-height:1.6;
}
.kaya-debug-box h3{
    margin:0 0 10px;
    font-size:15px;
    font-weight:800;
    color:#92400e;
}
.kaya-debug-grid{
    display:grid;
    gap:10px;
}
.kaya-debug-label{
    font-weight:700;
    color:#92400e;
}
.kaya-debug-code{
    display:block;
    margin-top:4px;
    padding:10px 12px;
    background:#fff;
    border:1px solid #fde68a;
    border-radius:10px;
    font-family:Consolas, Monaco, monospace;
    font-size:12px;
    color:#7c2d12;
    white-space:pre-wrap;
    word-break:break-word;
}

/* HEADER */
.kaya-cert-head{
    padding:20px 24px;
    background:linear-gradient(135deg,#1e293b,#0f172a);
}

.kaya-cert-title{
    margin:0;
    font-size:22px;
    font-weight:800;
    color:#ffffff;
}

/* TOOLBAR */
.kaya-cert-toolbar{
    padding:12px 20px;
    background:#f8fafc;
    border-bottom:1px solid #e2e8f0;
    display:flex;
    align-items:center;
    justify-content:space-between;
    gap:16px;
    flex-wrap:wrap;
}

.kaya-cert-search-wrap{
    flex:1 1 420px;
    max-width:560px;
    position:relative;
}

.kaya-cert-search{
    width:100%;
    height:40px;
    border-radius:12px;
    border:1px solid #cbd5e1;
    padding:0 14px;
    background:#ffffff;
    font-size:13px;
    color:#0f172a;
    outline:none;
    transition:border-color .18s ease, box-shadow .18s ease, background .18s ease;
}

.kaya-cert-search::placeholder{
    color:#64748b;
}

.kaya-cert-search:focus{
    border-color:#3b82f6;
    box-shadow:0 0 0 4px rgba(59,130,246,.12);
    background:#fff;
}

.kaya-cert-note{
    display:inline-flex;
    align-items:center;
    gap:6px;
    font-size:12px;
    color:#64748b;
    font-weight:600;
    white-space:nowrap;
    margin-left:auto;
}

.kaya-cert-note::before{
    content:"↕";
    font-size:12px;
    color:#94a3b8;
}

/* TABLE */
.kaya-cert-tablewrap{
    flex:1;
    overflow:auto;
}

.kaya-cert-table{
    width:100%;
    border-collapse:collapse;
}

.kaya-cert-table thead th{
    position:sticky;
    top:0;
    z-index:2;
    background:#f1f5f9;
    font-weight:700;
    font-size:13px;
    padding:12px;
    border-bottom:1px solid #e2e8f0;
    text-align:center;
    cursor:pointer;
    user-select:none;
}

/* Added for left alignment of title column */
.kaya-col-title{
    text-align:left !important;
}

.kaya-cert-table tbody td{
    padding:10px;
    border-bottom:1px solid #e2e8f0;
    font-size:13px;
    text-align:center;
    vertical-align:middle;
}

.kaya-cert-table tbody tr:nth-child(even){
    background:#f8fafc;
}

.kaya-row-inactive{
    opacity:.68;
    background:#f8fafc !important;
}

.kaya-title-text{
    font-weight:700;
    color:#0f172a;
}

.kaya-customer-text{
    font-weight:600;
    color:#334155;
}

.kaya-category{
    background:#eef2ff;
    color:#3730a3;
    padding:5px 10px;
    border-radius:999px;
    font-size:12px;
    font-weight:600;
}

.kaya-action-wrap{
    display:flex;
    align-items:center;
    justify-content:center;
    gap:8px;
    flex-wrap:wrap;
}

.kaya-action-btn{
    display:inline-flex;
    align-items:center;
    justify-content:center;
    height:30px;
    min-width:88px;
    padding:0 12px;
    border-radius:8px;
    text-decoration:none;
    font-size:12px;
    font-weight:700;
    border:1px solid transparent;
}

.kaya-action-btn:hover{
    opacity:.92;
}

.kaya-btn-deactivate{
    background:#fee2e2;
    color:#dc2626;
    border-color:#fecaca;
}

.kaya-btn-deactivate:hover{
    background:#fecaca;
}

.kaya-btn-activate{
    background:#dcfce7;
    color:#15803d;
    border-color:#bbf7d0;
}

.kaya-btn-activate:hover{
    background:#bbf7d0;
}

.kaya-pdf-btn{
    display:inline-flex;
    align-items:center;
    justify-content:center;
    height:30px;
    min-width:78px;
    padding:0 12px;
    border-radius:8px;
    text-decoration:none;
    font-size:12px;
    font-weight:700;
    background:#eff6ff;
    color:#1d4ed8;
    border:1px solid #bfdbfe;
}

.kaya-pdf-btn:hover{
    background:#dbeafe;
}

.kaya-empty{
    padding:25px;
    text-align:center;
    color:#64748b;
}

</style>
';

if ($kaya_debug_columns && $is_user01) {
    $available_columns = [];
    $sample_row = [];

    if (!empty($rows) && is_array($rows[0])) {
        $available_columns = array_keys($rows[0]);
        $sample_row = $rows[0];
    }

    echo '<div class="kaya-debug-box">';
    echo '<h3>Debug: Available Table Columns</h3>';
    echo '<div class="kaya-debug-grid">';

    echo '<div>';
    echo '<span class="kaya-debug-label">Table Name:</span>';
    echo '<span class="kaya-debug-code">' . esc_html($table) . '</span>';
    echo '</div>';

    echo '<div>';
    echo '<span class="kaya-debug-label">Total Rows:</span>';
    echo '<span class="kaya-debug-code">' . esc_html(count($rows)) . '</span>';
    echo '</div>';

    echo '<div>';
    echo '<span class="kaya-debug-label">Current View:</span>';
    echo '<span class="kaya-debug-code">' . ($is_user01 ? 'User01 sees all rows' : 'Normal users see only is_active = T') . '</span>';
    echo '</div>';

    echo '<div>';
    echo '<span class="kaya-debug-label">Available Columns:</span>';
    echo '<span class="kaya-debug-code">';
    echo !empty($available_columns)
        ? esc_html(implode(', ', $available_columns))
        : 'No rows found, so columns could not be detected from query result.';
    echo '</span>';
    echo '</div>';

    echo '<div>';
    echo '<span class="kaya-debug-label">Sample First Row:</span>';
    echo '<span class="kaya-debug-code">';
    if (!empty($sample_row)) {
        foreach ($sample_row as $col => $val) {
            echo esc_html($col . ' = ' . (is_scalar($val) || $val === null ? (string)$val : wp_json_encode($val))) . "\n";
        }
    } else {
        echo 'No sample row available.';
    }
    echo '</span>';
    echo '</div>';

    echo '</div>';
    echo '</div>';
}

echo '<div class="kaya-cert-wrap">';
echo '<div class="kaya-cert-card">';

echo '<div class="kaya-cert-head">';
echo '<h2 class="kaya-cert-title">Invoices</h2>';
echo '</div>';

echo '<div class="kaya-cert-toolbar">';
echo '<div class="kaya-cert-search-wrap">';
echo '<input type="text" id="kayaCertSearch" class="kaya-cert-search" placeholder="Search title, customer name, category...">';
echo '</div>';
echo '<div class="kaya-cert-note">Click column headers to sort</div>';
echo '</div>';

echo '<div class="kaya-cert-tablewrap">';
echo '<table class="kaya-cert-table" id="kayaCertTable">';

echo '<thead>';
echo '<tr>';
echo '<th data-sort="number">No.</th>';
// Title header with class for left alignment
echo '<th data-sort="text" class="kaya-col-title-head">Title</th>';
echo '<th data-sort="text">Customer Name</th>';
echo '<th data-sort="text">Category</th>';

if ($is_user01) {
    echo '<th data-sort="text">Active Flag</th>';
    echo '<th>Action</th>';
}

echo '<th data-sort="text">PDF File</th>';
echo '</tr>';
echo '</thead>';

echo '<tbody>';

if (!$rows) {

    $colspan = $is_user01 ? 7 : 5;
    echo '<tr><td colspan="'.$colspan.'" class="kaya-empty">No records found.</td></tr>';

} else {

    $no = 1;

    foreach ($rows as $row) {

        $is_active = isset($row['is_active']) && strtoupper((string)$row['is_active']) === 'F' ? 'F' : 'T';
        $pdf_url = isset($row['pdf_url']) ? trim((string) $row['pdf_url']) : '';
        $row_class = ($is_active === 'F') ? ' class="kaya-row-inactive"' : '';

        $title = isset($row['title']) ? (string)$row['title'] : '';
        $customer_name = isset($row['cust_name']) ? (string)$row['cust_name'] : '';
        $category_slug = !empty($row['category_slug']) ? (string)$row['category_slug'] : 'uncategorized';

        echo '<tr' . $row_class . '>';

        echo '<td data-value="'.esc_attr($no).'">'.$no.'</td>';

        // Title cell with class for left alignment
        echo '<td data-value="'.esc_attr($title).'" class="kaya-col-title">
            <span class="kaya-title-text">'.esc_html($title !== '' ? $title : '-').'</span>
        </td>';

        echo '<td data-value="'.esc_attr($customer_name).'">
            <span class="kaya-customer-text">'.esc_html($customer_name !== '' ? $customer_name : '-').'</span>
        </td>';

        echo '<td data-value="'.esc_attr($category_slug).'">
            <span class="kaya-category">'.esc_html($category_slug).'</span>
        </td>';

        if ($is_user01) {
            echo '<td data-value="'.esc_attr($is_active).'">' . esc_html($is_active) . '</td>';

            if ($is_active === 'T') {
                $toggle_url = wp_nonce_url(
                    add_query_arg([
                        'kaya_toggle_id' => intval($row['id']),
                        'kaya_toggle_to' => 'F',
                    ]),
                    'kaya_toggle_row'
                );

                echo '<td>';
                echo '<div class="kaya-action-wrap">';
                echo '<a class="kaya-action-btn kaya-btn-deactivate" href="'.esc_url($toggle_url).'" onclick="return confirm(\'Are you sure you want to deactivate this row?\');">Deactivate</a>';
                echo '</div>';
                echo '</td>';
            } else {
                $toggle_url = wp_nonce_url(
                    add_query_arg([
                        'kaya_toggle_id' => intval($row['id']),
                        'kaya_toggle_to' => 'T',
                    ]),
                    'kaya_toggle_row'
                );

                echo '<td>';
                echo '<div class="kaya-action-wrap">';
                echo '<a class="kaya-action-btn kaya-btn-activate" href="'.esc_url($toggle_url).'" onclick="return confirm(\'Are you sure you want to activate this row?\');">Activate</a>';
                echo '</div>';
                echo '</td>';
            }
        }

$post_id = isset($row['post_id']) ? intval($row['post_id']) : 0;

echo '<td data-value="'.esc_attr($pdf_url).'">';

if ($post_id > 0 && get_post($post_id)) {

    $page_url = get_permalink($post_id);

    echo '<a class="kaya-pdf-btn" href="'.esc_url($page_url).'">View Invoice</a>';

} elseif ($pdf_url !== '') {

    echo '<a class="kaya-pdf-btn" href="'.esc_url($pdf_url).'" target="_blank" rel="noopener noreferrer">Open PDF</a>';

} else {

    echo '-';

}

echo '</td>';

        echo '</tr>';

        $no++;
    }
}

echo '</tbody>';
echo '</table>';
echo '</div>';
echo '</div>';
echo '</div>';
?>

<script>
document.addEventListener('DOMContentLoaded', function () {
    const searchInput = document.getElementById('kayaCertSearch');
    const table = document.getElementById('kayaCertTable');
    const tbody = table.querySelector('tbody');
    const headers = table.querySelectorAll('thead th[data-sort]');

    if (searchInput) {
        searchInput.addEventListener('input', function () {
            const keyword = this.value.toLowerCase();
            const rows = tbody.querySelectorAll('tr');

            rows.forEach(row => {
                row.style.display = row.innerText.toLowerCase().includes(keyword) ? '' : 'none';
            });
        });
    }

    headers.forEach((header, index) => {
        let asc = true;

        header.addEventListener('click', function () {
            const type = header.getAttribute('data-sort');
            const rows = Array.from(tbody.querySelectorAll('tr'));

            rows.sort((a, b) => {
                const aCell = a.children[index];
                const bCell = b.children[index];

                const aVal = aCell ? (aCell.dataset.value || aCell.innerText) : '';
                const bVal = bCell ? (bCell.dataset.value || bCell.innerText) : '';

                let result;

                if (type === 'number' || type === 'date') {
                    result = parseFloat(aVal || 0) - parseFloat(bVal || 0);
                } else {
                    result h(�-������������g��
Z?�<?php

if (!defined('ABSPATH')) exit;

global $wpdb;

$table = $wpdb->prefix . 'kaya_certificates';
$current_user = wp_get_current_user();

/*
|--------------------------------------------------------------------------
| DEBUG TOGGLE
|--------------------------------------------------------------------------
*/
$kaya_debug_columns = false;

$is_user01 = ($current_user && $current_user->user_login === 'User01');

/*
|--------------------------------------------------------------------------
| Activate / Deactivate row (only User01)
|--------------------------------------------------------------------------
*/
if (
    isset($_GET['kaya_toggle_id'], $_GET['kaya_toggle_to'], $_GET['_wpnonce']) &&
    $is_user01 &&
    wp_verify_nonce($_GET['_wpnonce'], 'kaya_toggle_row')
) {
    $toggle_id = intval($_GET['kaya_toggle_id']);
    $toggle_to = sanitize_text_field(wp_unslash($_GET['kaya_toggle_to']));
    $toggle_to = ($toggle_to === 'F') ? 'F' : 'T';

    if ($toggle_id > 0) {

        $target_row = $wpdb->get_row(
            $wpdb->prepare("SELECT * FROM {$table} WHERE id = %d", $toggle_id),
            ARRAY_A
        );

        if ($target_row) {

            $updated = $wpdb->update(
                $table,
                ['is_active' => $toggle_to],
                ['id' => $toggle_id],
                ['%s'],
                ['%d']
            );

            $post_ok = true;
            $post_id = isset($target_row['post_id']) ? intval($target_row['post_id']) : 0;

            if ($post_id > 0 && get_post($post_id)) {
                $new_post_status = ($toggle_to === 'F') ? 'draft' : 'publish';

                $post_result = wp_update_post([
                    'ID'          => $post_id,
                    'post_status' => $new_post_status,
                ], true);

                if (is_wp_error($post_result)) {
                    $post_ok = false;
                }
            }

            if ($updated !== false && $post_ok) {
                echo '<div style="max-width:1200px;margin:15px auto 0;padding:12px;background:#ecfdf5;border:1px solid #bbf7d0;color:#166534;border-radius:10px;font-weight:600;">Row and related post status updated successfully.</div>';
            } elseif ($updated !== false && !$post_ok) {
                echo '<div style="max-width:1200px;margin:15px auto 0;padding:12px;background:#fff7ed;border:1px solid #fdba74;color:#9a3412;border-radius:10px;font-weight:600;">Row status updated, but related post status could not be changed.</div>';
            } else {
                echo '<div style="max-width:1200px;margin:15px auto 0;padding:12px;background:#fff1f2;border:1px solid #fecaca;color:#991b1b;border-radius:10px;font-weight:600;">Failed to update row status.</div>';
            }

        } else {
            echo '<div style="max-width:1200px;margin:15px auto 0;padding:12px;background:#fff1f2;border:1px solid #fecaca;color:#991b1b;border-radius:10px;font-weight:600;">Record not found.</div>';
        }
    }
}

/*
|--------------------------------------------------------------------------
| Query rows
|--------------------------------------------------------------------------
*/
if ($is_user01) {
    $rows = $wpdb->get_results("SELECT * FROM {$table} ORDER BY id DESC", ARRAY_A);
} else {
    $rows = $wpdb->get_results("SELECT * FROM {$table} WHERE is_active = 'T' ORDER BY id DESC", ARRAY_A);
}

echo '
<style>

.kaya-cert-wrap{
    max-width:1200px;
    margin:auto;
    height:72vh;
    display:flex;
    flex-direction:column;
}

.kaya-cert-card{
    display:flex;
    flex-direction:column;
    height:100%;
    border-radius:18px;
    overflow:hidden;
    border:1px solid #e2e8f0;
    background:#ffffff;
}

/* DEBUG PANEL */
.kaya-debug-box{
    max-width:1200px;
    margin:0 auto 14px;
    border:1px solid #fcd34d;
    background:#fffbeb;
    border-radius:14px;
    padding:14px 16px;
    color:#78350f;
    font-size:13px;
    line-height:1.6;
}
.kaya-debug-box h3{
    margin:0 0 10px;
    font-size:15px;
    font-weight:800;
    color:#92400e;
}
.kaya-debug-grid{
    display:grid;
    gap:10px;
}
.kaya-debug-label{
    font-weight:700;
    color:#92400e;
}
.kaya-debug-code{
    display:block;
    margin-top:4px;
    padding:10px 12px;
    background:#fff;
    border:1px solid #fde68a;
    border-radius:10px;
    font-family:Consolas, Monaco, monospace;
    font-size:12px;
    color:#7c2d12;
    white-space:pre-wrap;
    word-break:break-word;
}

/* HEADER */
.kaya-cert-head{
    padding:20px 24px;
    background:linear-gradient(135deg,#1e293b,#0f172a);
}

.kaya-cert-title{
    margin:0;
    font-size:22px;
    font-weight:800;
    color:#ffffff;
}

/* TOOLBAR */
.kaya-cert-toolbar{
    padding:12px 20px;
    background:#f8fafc;
    border-bottom:1px solid #e2e8f0;
    display:flex;
    align-items:center;
    justify-content:space-between;
    gap:16px;
    flex-wrap:wrap;
}

.kaya-cert-search-wrap{
    flex:1 1 420px;
    max-width:560px;
    position:relative;
}

.kaya-cert-search{
    width:100%;
    height:40px;
    border-radius:12px;
    border:1px solid #cbd5e1;
    padding:0 14px;
    background:#ffffff;
    font-size:13px;
    color:#0f172a;
    outline:none;
    transition:border-color .18s ease, box-shadow .18s ease, background .18s ease;
}

.kaya-cert-search::placeholder{
    color:#64748b;
}

.kaya-cert-search:focus{
    border-color:#3b82f6;
    box-shadow:0 0 0 4px rgba(59,130,246,.12);
    background:#fff;
}

.kaya-cert-note{
    display:inline-flex;
    align-items:center;
    gap:6px;
    font-size:12px;
    color:#64748b;
    font-weight:600;
    white-space:nowrap;
    margin-left:auto;
}

.kaya-cert-note::before{
    content:"↕";
    font-size:12px;
    color:#94a3b8;
}

/* TABLE */
.kaya-cert-tablewrap{
    flex:1;
    overflow:auto;
}

.kaya-cert-table{
    width:100%;
    border-collapse:collapse;
}

.kaya-cert-table thead th{
    position:sticky;
    top:0;
    z-index:2;
    background:#f1f5f9;
    font-weight:700;
    font-size:13px;
    padding:12px;
    border-bottom:1px solid #e2e8f0;
    text-align:center;
    cursor:pointer;
    user-select:none;
}

/* Added for left alignment of title column */
.kaya-col-title{
    text-align:left !important;
}

.kaya-cert-table tbody td{
    padding:10px;
    border-bottom:1px solid #e2e8f0;
    font-size:13px;
    text-align:center;
    vertical-align:middle;
}

.kaya-cert-table tbody tr:nth-child(even){
    background:#f8fafc;
}

.kaya-row-inactive{
    opacity:.68;
    background:#f8fafc !important;
}

.kaya-title-text{
    font-weight:700;
    color:#0f172a;
}

.kaya-customer-text{
    font-weight:600;
    color:#334155;
}

.kaya-category{
    background:#eef2ff;
    color:#3730a3;
    padding:5px 10px;
    border-radius:999px;
    font-size:12px;
    font-weight:600;
}

.kaya-action-wrap{
    display:flex;
    align-items:center;
    justify-content:center;
    gap:8px;
    flex-wrap:wrap;
}

.kaya-action-btn{
    display:inline-flex;
    align-items:center;
    justify-content:center;
    height:30px;
    min-width:88px;
    padding:0 12px;
    border-radius:8px;
    text-decoration:none;
    font-size:12px;
    font-weight:700;
    border:1px solid transparent;
}

.kaya-action-btn:hover{
    opacity:.92;
}

.kaya-btn-deactivate{
    background:#fee2e2;
    color:#dc2626;
    border-color:#fecaca;
}

.kaya-btn-deactivate:hover{
    background:#fecaca;
}

.kaya-btn-activate{
    background:#dcfce7;
    color:#15803d;
    border-color:#bbf7d0;
}

.kaya-btn-activate:hover{
    background:#bbf7d0;
}

.kaya-pdf-btn{
    display:inline-flex;
    align-items:center;
    justify-content:center;
    height:30px;
    min-width:78px;
    padding:0 12px;
    border-radius:8px;
    text-decoration:none;
    font-size:12px;
    font-weight:700;
    background:#eff6ff;
    color:#1d4ed8;
    border:1px solid #bfdbfe;
}

.kaya-pdf-btn:hover{
    background:#dbeafe;
}

.kaya-empty{
    padding:25px;
    text-align:center;
    color:#64748b;
}

</style>
';

if ($kaya_debug_columns && $is_user01) {
    $available_columns = [];
    $sample_row = [];

    if (!empty($rows) && is_array($rows[0])) {
        $available_columns = array_keys($rows[0]);
        $sample_row = $rows[0];
    }

    echo '<div class="kaya-debug-box">';
    echo '<h3>Debug: Available Table Columns</h3>';
    echo '<div class="kaya-debug-grid">';

    echo '<div>';
    echo '<span class="kaya-debug-label">Table Name:</span>';
    echo '<span class="kaya-debug-code">' . esc_html($table) . '</span>';
    echo '</div>';

    echo '<div>';
    echo '<span class="kaya-debug-label">Total Rows:</span>';
    echo '<span class="kaya-debug-code">' . esc_html(count($rows)) . '</span>';
    echo '</div>';

    echo '<div>';
    echo '<span class="kaya-debug-label">Current View:</span>';
    echo '<span class="kaya-debug-code">' . ($is_user01 ? 'User01 sees all rows' : 'Normal users see only is_active = T') . '</span>';
    echo '</div>';

    echo '<div>';
    echo '<span class="kaya-debug-label">Available Columns:</span>';
    echo '<span class="kaya-debug-code">';
    echo !empty($available_columns)
        ? esc_html(implode(', ', $available_columns))
        : 'No rows found, so columns could not be detected from query result.';
    echo '</span>';
    echo '</div>';

    echo '<div>';
    echo '<span class="kaya-debug-label">Sample First Row:</span>';
    echo '<span class="kaya-debug-code">';
    if (!empty($sample_row)) {
        foreach ($sample_row as $col => $val) {
            echo esc_html($col . ' = ' . (is_scalar($val) || $val === null ? (string)$val : wp_json_encode($val))) . "\n";
        }
    } else {
        echo 'No sample row available.';
    }
    echo '</span>';
    echo '</div>';

    echo '</div>';
    echo '</div>';
}

echo '<div class="kaya-cert-wrap">';
echo '<div class="kaya-cert-card">';

echo '<div class="kaya-cert-head">';
echo '<h2 class="kaya-cert-title">Invoices</h2>';
echo '</div>';

echo '<div class="kaya-cert-toolbar">';
echo '<div class="kaya-cert-search-wrap">';
echo '<input type="text" id="kayaCertSearch" class="kaya-cert-search" placeholder="Search title, customer name, category...">';
echo '</div>';
echo '<div class="kaya-cert-note">Click column headers to sort</div>';
echo '</div>';

echo '<div class="kaya-cert-tablewrap">';
echo '<table class="kaya-cert-table" id="kayaCertTable">';

echo '<thead>';
echo '<tr>';
echo '<th data-sort="number">No.</th>';
// Title header with class for left alignment
echo '<th data-sort="text" class="kaya-col-title-head">Title</th>';
echo '<th data-sort="text">Customer Name</th>';
echo '<th data-sort="text">Category</th>';

if ($is_user01) {
    echo '<th data-sort="text">Active Flag</th>';
    echo '<th>Action</th>';
}

echo '<th data-sort="text">PDF File</th>';
echo '</tr>';
echo '</thead>';

echo '<tbody>';

if (!$rows) {

    $colspan = $is_user01 ? 7 : 5;
    echo '<tr><td colspan="'.$colspan.'" class="kaya-empty">No records found.</td></tr>';

} else {

    $no = 1;

    foreach ($rows as $row) {

        $is_active = isset($row['is_active']) && strtoupper((string)$row['is_active']) === 'F' ? 'F' : 'T';
        $pdf_url = isset($row['pdf_url']) ? trim((string) $row['pdf_url']) : '';
        $row_class = ($is_active === 'F') ? ' class="kaya-row-inactive"' : '';

        $title = isset($row['title']) ? (string)$row['title'] : '';
        $customer_name = isset($row['cust_name']) ? (string)$row['cust_name'] : '';
        $category_slug = !empty($row['category_slug']) ? (string)$row['category_slug'] : 'uncategorized';

        echo '<tr' . $row_class . '>';

        echo '<td data-value="'.esc_attr($no).'">'.$no.'</td>';

        // Title cell with class for left alignment
        echo '<td data-value="'.esc_attr($title).'" class="kaya-col-title">
            <span class="kaya-title-text">'.esc_html($title !== '' ? $title : '-').'</span>
        </td>';

        echo '<td data-value="'.esc_attr($customer_name).'">
            <span class="kaya-customer-text">'.esc_html($customer_name !== '' ? $customer_name : '-').'</span>
        </td>';

        echo '<td data-value="'.esc_attr($category_slug).'">
            <span class="kaya-category">'.esc_html($category_slug).'</span>
        </td>';

        if ($is_user01) {
            echo '<td data-value="'.esc_attr($is_active).'">' . esc_html($is_active) . '</td>';

            if ($is_active === 'T') {
                $toggle_url = wp_nonce_url(
                    add_query_arg([
                        'kaya_toggle_id' => intval($row['id']),
                        'kaya_toggle_to' => 'F',
                    ]),
                    'kaya_toggle_row'
                );

                echo '<td>';
                echo '<div class="kaya-action-wrap">';
                echo '<a class="kaya-action-btn kaya-btn-deactivate" href="'.esc_url($toggle_url).'" onclick="return confirm(\'Are you sure you want to deactivate this row?\');">Deactivate</a>';
                echo '</div>';
                echo '</td>';
            } else {
                $toggle_url = wp_nonce_url(
                    add_query_arg([
                        'kaya_toggle_id' => intval($row['id']),
                        'kaya_toggle_to' => 'T',
                    ]),
                    'kaya_toggle_row'
                );

                echo '<td>';
                echo '<div class="kaya-action-wrap">';
                echo '<a class="kaya-action-btn kaya-btn-activate" href="'.esc_url($toggle_url).'" onclick="return confirm(\'Are you sure you want to activate this row?\');">Activate</a>';
                echo '</div>';
                echo '</td>';
            }
        }

        echo '<td data-value="'.esc_attr($pdf_url).'">';
        if ($pdf_url !== '') {
            echo '<a class="kaya-pdf-btn" href="'.esc_url($pdf_url).'" target="_blank" rel="noopener noreferrer">Open PDF</a>';
        } else {
            echo '-';
        }
        echo '</td>';

        echo '</tr>';

        $no++;
    }
}

echo '</tbody>';
echo '</table>';
echo '</div>';
echo '</div>';
echo '</div>';
?>

<script>
document.addEventListener('DOMContentLoaded', function () {
    const searchInput = document.getElementById('kayaCertSearch');
    const table = document.getElementById('kayaCertTable');
    const tbody = table.querySelector('tbody');
    const headers = table.querySelectorAll('thead th[data-sort]');

    if (searchInput) {
        searchInput.addEventListener('input', function () {
            const keyword = this.value.toLowerCase();
            const rows = tbody.querySelectorAll('tr');

            rows.forEach(row => {
                row.style.display = row.innerText.toLowerCase().includes(keyword) ? '' : 'none';
            });
        });
    }

    headers.forEach((header, index) => {
        let asc = true;

        header.addEventListener('click', function () {
            const type = header.getAttribute('data-sort');
            const rows = Array.from(tbody.querySelectorAll('tr'));

            rows.sort((a, b) => {
                const aCell = a.children[index];
                const bCell = b.children[index];

                const aVal = aCell ? (aCell.dataset.value || aCell.innerText) : '';
                const bVal = bCell ? (bCell.dataset.value || bCell.innerText) : '';

                let result;

                if (type === 'number' || type === 'date') {
                    result = parseFloat(aVal || 0) - parseFloat(bVal || 0);
                } else {
                    result = String(aVal).localeCompare(String(bVal));
                }

                return asc ? result : -result;
            });

            rows.forEach(row => tbody.appendChild(row));
            asg��mv�e��������g��
Z/����c = !asc;
        });
    });
});
</script>g���(\��������h(�-
Z�����= String(aVal).localeCompare(String(bVal));
                }

                return asc ? result : -result;
            });

            rows.forEach(row => tbody.appendChild(row));
            asc = !asc;
        });
    });
});
</script>h(�-t(T������������
Z?�	<?php

if (!defined('ABSPATH')) exit;

global $wpdb;

$table = $wpdb->prefix . 'kaya_certificates';
$current_user = wp_get_current_user();

/*
|--------------------------------------------------------------------------
| DEBUG TOGGLE
|--------------------------------------------------------------------------
*/
$kaya_debug_columns = false;

$is_user01 = ($current_user && $current_user->user_login === 'User01');
$can_edit_records = ($current_user && array_intersect(['administrator', 'editor'], (array) $current_user->roles));
$invoice_edit_base_url = 'https://invoice.webstation.my/index.php/invoice-edit/';

/*
|--------------------------------------------------------------------------
| Activate / Deactivate row (only User01)
|--------------------------------------------------------------------------
*/
if (
    isset($_GET['kaya_toggle_id'], $_GET['kaya_toggle_to'], $_GET['_wpnonce']) &&
    $is_user01 &&
    wp_verify_nonce($_GET['_wpnonce'], 'kaya_toggle_row')
) {
    $toggle_id = intval($_GET['kaya_toggle_id']);
    $toggle_to = sanitize_text_field(wp_unslash($_GET['kaya_toggle_to']));
    $toggle_to = ($toggle_to === 'F') ? 'F' : 'T';

    if ($toggle_id > 0) {

        $target_row = $wpdb->get_row(
            $wpdb->prepare("SELECT * FROM {$table} WHERE id = %d", $toggle_id),
            ARRAY_A
        );

        if ($target_row) {

            $updated = $wpdb->update(
                $table,
                ['is_active' => $toggle_to],
                ['id' => $toggle_id],
                ['%s'],
                ['%d']
            );

            $post_ok = true;
            $post_id = isset($target_row['post_id']) ? intval($target_row['post_id']) : 0;

            if ($post_id > 0 && get_post($post_id)) {
                $new_post_status = ($toggle_to === 'F') ? 'draft' : 'publish';

                $post_result = wp_update_post([
                    'ID'          => $post_id,
                    'post_status' => $new_post_status,
                ], true);

                if (is_wp_error($post_result)) {
                    $post_ok = false;
                }
            }

            if ($updated !== false && $post_ok) {
                echo '<div style="margin:15px 0 0;padding:12px;background:#ecfdf5;border:1px solid #bbf7d0;color:#166534;border-radius:10px;font-weight:600;">Row and related post status updated successfully.</div>';
            } elseif ($updated !== false && !$post_ok) {
                echo '<div style="margin:15px 0 0;padding:12px;background:#fff7ed;border:1px solid #fdba74;color:#9a3412;border-radius:10px;font-weight:600;">Row status updated, but related post status could not be changed.</div>';
            } else {
                echo '<div style="margin:15px 0 0;padding:12px;background:#fff1f2;border:1px solid #fecaca;color:#991b1b;border-radius:10px;font-weight:600;">Failed to update row status.</div>';
            }

        } else {
            echo '<div style="margin:15px 0 0;padding:12px;background:#fff1f2;border:1px solid #fecaca;color:#991b1b;border-radius:10px;font-weight:600;">Record not found.</div>';
        }
    }
}

/*
|--------------------------------------------------------------------------
| Query rows
|--------------------------------------------------------------------------
*/
if ($is_user01) {
    $rows = $wpdb->get_results("SELECT * FROM {$table} ORDER BY id DESC", ARRAY_A);
} else {
    $rows = $wpdb->get_results("SELECT * FROM {$table} WHERE is_active = 'T' ORDER BY id DESC", ARRAY_A);
}

echo '
<style>

.kaya-cert-wrap{
    margin:0;
}

.kaya-cert-card{
    border-radius:18px;
    overflow:hidden;
    border:1px solid #e2e8f0;
    background:#ffffff;
}

/* DEBUG PANEL */
.kaya-debug-box{
    margin:0 0 14px;
    border:1px solid #fcd34d;
    background:#fffbeb;
    border-radius:14px;
    padding:14px 16px;
    color:#78350f;
    font-size:13px;
    line-height:1.6;
}
.kaya-debug-box h3{
    margin:0 0 10px;
    font-size:15px;
    font-weight:800;
    color:#92400e;
}
.kaya-debug-grid{
    display:grid;
    gap:10px;
}
.kaya-debug-label{
    font-weight:700;
    color:#92400e;
}
.kaya-debug-code{
    display:block;
    margin-top:4px;
    padding:10px 12px;
    background:#fff;
    border:1px solid #fde68a;
    border-radius:10px;
    font-family:Consolas, Monaco, monospace;
    font-size:12px;
    color:#7c2d12;
    white-space:pre-wrap;
    word-break:break-word;
}

/* HEADER */
.kaya-cert-head{
    padding:20px 24px;
    background:linear-gradient(135deg,#1e293b,#0f172a);
}

.kaya-cert-title{
    margin:0;
    font-size:22px;
    font-weight:800;
    color:#ffffff;
}

/* TOOLBAR */
.kaya-cert-toolbar{
    padding:12px 20px;
    background:#f8fafc;
    border-bottom:1px solid #e2e8f0;
    display:flex;
    align-items:center;
    justify-content:space-between;
    gap:16px;
    flex-wrap:wrap;
}

.kaya-cert-search-wrap{
    flex:1 1 420px;
    max-width:560px;
    position:relative;
}

.kaya-cert-search{
    width:100%;
    height:40px;
    border-radius:12px;
    border:1px solid #cbd5e1;
    padding:0 14px;
    background:#ffffff;
    font-size:13px;
    color:#0f172a;
    outline:none;
    transition:border-color .18s ease, box-shadow .18s ease, background .18s ease;
}

.kaya-cert-search::placeholder{
    color:#64748b;
}

.kaya-cert-search:focus{
    border-color:#3b82f6;
    box-shadow:0 0 0 4px rgba(59,130,246,.12);
    background:#fff;
}

.kaya-cert-note{
    display:inline-flex;
    align-items:center;
    gap:6px;
    font-size:12px;
    color:#64748b;
    font-weight:600;
    white-space:nowrap;
    margin-left:auto;
}

.kaya-cert-note::before{
    content:"↕";
    font-size:12px;
    color:#94a3b8;
}

/* TABLE */
.kaya-cert-tablewrap{
    overflow:visible;
}

.kaya-cert-table{
    width:100%;
    border-collapse:collapse;
}

.kaya-cert-table thead th{
    position:sticky;
    top:0;
    z-index:2;
    background:#f1f5f9;
    font-weight:700;
    font-size:13px;
    padding:12px;
    border-bottom:1px solid #e2e8f0;
    text-align:center;
    cursor:pointer;
    user-select:none;
}

.kaya-col-title{
    text-align:left !important;
}

.kaya-col-customer{
    text-align:left !important;
}

.kaya-cert-table tbody td{
    padding:10px;
    border-bottom:1px solid #e2e8f0;
    font-size:13px;
    text-align:center;
    vertical-align:middle;
}

.kaya-cert-table tbody tr:nth-child(even){
    background:#f8fafc;
}

.kaya-row-inactive{
    opacity:.68;
    background:#f8fafc !important;
}

.kaya-title-text{
    font-weight:700;
    color:#0f172a;
}

.kaya-customer-text{
    font-weight:600;
    color:#334155;
}

.kaya-category{
    background:none;
    color:#334155;
    padding:0;
    border-radius:0;
    font-size:13px;
    font-weight:600;
}

.kaya-action-wrap{
    display:flex;
    align-items:center;
    justify-content:center;
    gap:8px;
    flex-wrap:wrap;
}

.kaya-action-btn{
    display:inline-flex;
    align-items:center;
    justify-content:center;
    height:30px;
    min-width:88px;
    padding:0 12px;
    border-radius:8px;
    text-decoration:none;
    font-size:12px;
    font-weight:700;
    border:1px solid transparent;
}

.kaya-action-btn:hover{
    opacity:.92;
}

.kaya-btn-deactivate{
    background:#fee2e2;
    color:#dc2626;
    border-color:#fecaca;
}

.kaya-btn-deactivate:hover{
    background:#fecaca;
}

.kaya-btn-activate{
    background:#dcfce7;
    color:#15803d;
    border-color:#bbf7d0;
}

.kaya-btn-activate:hover{
    background:#bbf7d0;
}

.kaya-btn-edit{
    background:#fef3c7;
    color:#b45309;
    border-color:#fcd34d;
}

.kaya-btn-edit:hover{
    background:#fde68a;
}

.kaya-pdf-btn{
    display:inline-flex;
    align-items:center;
    justify-content:center;
    height:30px;
    min-width:78px;
    padding:0 12px;
    border-radius:8px;
    text-decoration:none;
    font-size:12px;
    font-weight:700;
    background:#eff6ff;
    color:#1d4ed8;
    border:1px solid #bfdbfe;
}

.kaya-pdf-btn:hover{
    background:#dbeafe;
}

.kaya-empty{
    padding:25px;
    text-align:center;
    color:#64748b;
}

</style>
';

if ($kaya_debug_columns && $is_user01) {
    $available_columns = [];
    $sample_row = [];

    if (!empty($rows) && is_array($rows[0])) {
        $available_columns = array_keys($rows[0]);
        $sample_row = $rows[0];
    }

    echo '<div class="kaya-debug-box">';
    echo '<h3>Debug: Available Table Columns</h3>';
    echo '<div class="kaya-debug-grid">';

    echo '<div>';
    echo '<span class="kaya-debug-label">Table Name:</span>';
    echo '<span class="kaya-debug-code">' . esc_html($table) . '</span>';
    echo '</div>';

    echo '<div>';
    echo '<span class="kaya-debug-label">Total Rows:</span>';
    echo '<span class="kaya-debug-code">' . esc_html(count($rows)) . '</span>';
    echo '</div>';

    echo '<div>';
    echo '<span class="kaya-debug-label">Current View:</span>';
    echo '<span class="kaya-debug-code">' . ($is_user01 ? 'User01 sees all rows' : 'Normal users see only is_active = T') . '</span>';
    echo '</div>';

    echo '<div>';
    echo '<span class="kaya-debug-label">Available Columns:</span>';
    echo '<span class="kaya-debug-code">';
    echo !empty($available_columns)
        ? esc_html(implode(', ', $available_columns))
        : 'No rows found, so columns could not be detected from query result.';
    echo '</span>';
    echo '</div>';

    echo '<div>';
    echo '<span class="kaya-debug-label">Sample First Row:</span>';
    echo '<span class="kaya-debug-code">';
    if (!empty($sample_row)) {
        foreach ($sample_row as $col => $val) {
            echo esc_html($col . ' = ' . (is_scalar($val) || $val === null ? (string)$val : wp_json_encode($val))) . "\n";
        }
    } else {
        echo 'No sample row available.';
    }
    echo '</span>';
    echo '</div>';

    echo '</div>';
    echo '</div>';
}

echo '<div class="kaya-cert-wrap">';
echo '<div class="kaya-cert-card">';

echo '<div class="kaya-cert-head">';
echo '<h2 class="kaya-cert-title">LIST</h2>';
echo '</div>';

echo '<div class="kaya-cert-toolbar">';
echo '<div class="kaya-cert-search-wrap">';
echo '<input type="text" id="kayaCertSearch" class="kaya-cert-search" placeholder="Search title, customer name, category...">';
echo '</div>';
echo '<div class="kaya-cert-note">Click column headers to sort</div>';
echo '</div>';

echo '<div class="kaya-cert-tablewrap">';
echo '<table class="kaya-cert-table" id="kayaCertTable">';

echo '<thead>';
echo '<tr>';
echo '<th data-sort="number">No.</th>';
echo '<th data-sort="text" class="kaya-col-title-head">Title</th>';
echo '<th data-sort="text">Customer Name</th>';
echo '<th data-sort="text">Category</th>';

if ($is_user01) {
    echo '<th data-sort="text">Active Flag</th>';
}

if ($can_edit_records || $is_user01) {
    echo '<th>Action</th>';
}

echo '<th data-sort="text">Invoice</th>';
echo '</tr>';
echo '</thead>';

echo '<tbody>';

if (!$rows) {

    $colspan = 5;
    if ($is_user01) $colspan++;
    if ($can_edit_records || $is_user01) $colspan++;

    echo '<tr><td colspan="'.$colspan.'" class="kaya-empty">No records found.</td></tr>';

} else {

    $no = 1;

    foreach ($rows as $row) {

        $is_active = isset($row['is_active']) && strtoupper((string)$row['is_active']) === 'F' ? 'F' : 'T';
        $pdf_url = isset($row['pdf_url']) ? trim((string) $row['pdf_url']) : '';
        $row_class = ($is_active === 'F') ? ' class="kaya-row-inactive"' : '';

        $title = isset($row['title']) ? (string)$row['title'] : '';
        $customer_name = isset($row['cust_name']) ? (string)$row['cust_name'] : '';
        $category_slug = !empty($row['category_slug']) ? (string)$row['category_slug'] : 'uncategorized';
        $row_id = isset($row['id']) ? intval($row['id']) : 0;

        echo '<tr' . $row_class . '>';

        echo '<td data-value="'.esc_attr($no).'">'.$no.'</td>';

        echo '<td data-value="'.esc_attr($title).'" class="kaya-col-title">
            <span class="kaya-title-text">'.esc_html($title !== '' ? $title : '-').'</span>
        </td>';

        echo '<td data-value="'.esc_attr($customer_name).'" class="kaya-col-customer">
            <span class="kaya-customer-text">'.esc_html($customer_name !== '' ? $customer_name : '-').'</span>
        </td>';

        echo '<td data-value="'.esc_attr($category_slug).'">
            <span class="kaya-category">'.esc_html($category_slug).'</span>
        </td>';

        if ($is_user01) {
            echo '<td data-value="'.esc_attr($is_active).'">' . esc_html($is_active) . '</td>';
        }

        if ($can_edit_records || $is_user01) {
            $edit_url = add_query_arg([
                'edit_id' => $row_id,
            ], $invoice_edit_base_url);

            echo '<td>';
            echo '<div class="kaya-action-wrap">';

            if ($can_edit_records) {
                echo '<a class="kaya-action-btn kaya-btn-edit" href="'.esc_url($edit_url).'">Edit</a>';
            }

            if ($is_user01) {
                if ($is_active === 'T') {
                    $toggle_url = wp_nonce_url(
                        add_query_arg([
                            'kaya_toggle_id' => $row_id,
                            'kaya_toggle_to' => 'F',
                        ]),
                        'kaya_toggle_row'
                    );

                    echo '<a class="kaya-action-btn kaya-btn-deactivate" href="'.esc_url($toggle_url).'" onclick="return confirm(\'Are you sure you want to deactivate this row?\');">Deactivate</a>';
                } else {
                    $toggle_url = wp_nonce_url(
                        add_query_arg([
                            'kaya_toggle_id' => $row_id,
                            'kaya_toggle_to' => 'T',
                        ]),
                        'kaya_toggle_row'
                    );

                    echo '<a class="kaya-action-btn kaya-btn-activate" href="'.esc_url($toggle_url).'" onclick="return confirm(\'Are you sure you want to activate this row?\');">Activate</a>';
                }
            }

            echo '</div>';
            echo '</td>';
        }

        $post_id = isset($row['post_id']) ? intval($row['post_id']) : 0;

        echo '<td data-value="'.esc_attr($pdf_url).'">';

        if ($post_id > 0 && get_post($post_id)) {
            $page_url = get_permalink($post_id);
            echo '<a class="kaya-pdf-btn" href="'.esc_url($page_url).'">View Invoice</a>';
        } elseif ($pdf_url !== '') {
            echo '<a class="kaya-pdf-btn" href="'.esc_url($pdf_url).'" target="_blank" rel="noopener noreferrer">Open PDF</a>';
        } else {
            echo '-';
        }

        echo '</td>';

        echo '</tr>';

        $no++;
    }
}

echo '</tbody>';
echo '</table>';   // FIXED: was 'carbohydratetable' - now properly closes the table
echo '</div>';
echo '</div>';
echo '</div>';
?>

<script>
document.addEventListener('DOMContentLoaded', function () {
    const searchInput = document.getElementById('kayaCertSearch');
    const table = document.getElementById('kayaCertTable');
    const tbody = table.querySelector('tbody');
    const headers = table.querySelectorAll('thead th[data-sort]');

    if (searchInput) {
        searchInput.addEventListener('input', function () {
            const keyword = this.value.toLowerCase();
            const rows = tbody.querySelectorAll('tr');

            rows.forEach(row => {
                row.style.display = row.innerText.toLowerCase().includes(keyword) ? '' : 'none';
            });
        });
    }

    headers.forEach((header, index) => {
        let asc = true;

        header.addEventListener('click', function () {
            const type = header.getAttribute('data-sort');
      ������	������������
ZO����      const rows = Array.from(tbody.querySelectorAll('tr'));

            rows.sort((a, b) => {
                const aCell = a.children[index];
                const bCell = b.children[index];

                const aVal = aCell ? (aCell.dataset.value || aCell.innerText) : '';
                const bVal = bCell ? (bCell.dataset.value || bCell.innerText) : '';

                let result;

                if (type === 'number' || type === 'date') {
                    result = parseFloat(aVal || 0) - parseFloat(bVal || 0);
                } else {
                    result = String(aVal).localeCompare(String(bVal));
                }

                return asc ? result : -result;
            });

            rows.forEach(row => tbody.appendChild(row));
            asc = !asc;
        });
    });
});
</script>����e�H
��������m�e�
Z?�<?php

if (!defined('ABSPATH')) exit;

global $wpdb;

$table = $wpdb->prefix . 'kaya_certificates';
$current_user = wp_get_current_user();

/*
|--------------------------------------------------------------------------
| DEBUG TOGGLE
|--------------------------------------------------------------------------
*/
$kaya_debug_columns = false;

$is_user01 = ($current_user && $current_user->user_login === 'User01');
$can_edit_records = ($current_user && array_intersect(['administrator', 'editor'], (array) $current_user->roles));
$invoice_edit_base_url = 'https://invoice.webstation.my/index.php/invoice-edit/';

/*
|--------------------------------------------------------------------------
| Activate / Deactivate row (only User01)
|--------------------------------------------------------------------------
*/
if (
    isset($_GET['kaya_toggle_id'], $_GET['kaya_toggle_to'], $_GET['_wpnonce']) &&
    $is_user01 &&
    wp_verify_nonce($_GET['_wpnonce'], 'kaya_toggle_row')
) {
    $toggle_id = intval($_GET['kaya_toggle_id']);
    $toggle_to = sanitize_text_field(wp_unslash($_GET['kaya_toggle_to']));
    $toggle_to = ($toggle_to === 'F') ? 'F' : 'T';

    if ($toggle_id > 0) {

        $target_row = $wpdb->get_row(
            $wpdb->prepare("SELECT * FROM {$table} WHERE id = %d", $toggle_id),
            ARRAY_A
        );

        if ($target_row) {

            $updated = $wpdb->update(
                $table,
                ['is_active' => $toggle_to],
                ['id' => $toggle_id],
                ['%s'],
                ['%d']
            );

            $post_ok = true;
            $post_id = isset($target_row['post_id']) ? intval($target_row['post_id']) : 0;

            if ($post_id > 0 && get_post($post_id)) {
                $new_post_status = ($toggle_to === 'F') ? 'draft' : 'publish';

                $post_result = wp_update_post([
                    'ID'          => $post_id,
                    'post_status' => $new_post_status,
                ], true);

                if (is_wp_error($post_result)) {
                    $post_ok = false;
                }
            }

            if ($updated !== false && $post_ok) {
                echo '<div style="max-width:1200px;margin:15px auto 0;padding:12px;background:#ecfdf5;border:1px solid #bbf7d0;color:#166534;border-radius:10px;font-weight:600;">Row and related post status updated successfully.</div>';
            } elseif ($updated !== false && !$post_ok) {
                echo '<div style="max-width:1200px;margin:15px auto 0;padding:12px;background:#fff7ed;border:1px solid #fdba74;color:#9a3412;border-radius:10px;font-weight:600;">Row status updated, but related post status could not be changed.</div>';
            } else {
                echo '<div style="max-width:1200px;margin:15px auto 0;padding:12px;background:#fff1f2;border:1px solid #fecaca;color:#991b1b;border-radius:10px;font-weight:600;">Failed to update row status.</div>';
            }

        } else {
            echo '<div style="max-width:1200px;margin:15px auto 0;padding:12px;background:#fff1f2;border:1px solid #fecaca;color:#991b1b;border-radius:10px;font-weight:600;">Record not found.</div>';
        }
    }
}

/*
|--------------------------------------------------------------------------
| Query rows
|--------------------------------------------------------------------------
*/
if ($is_user01) {
    $rows = $wpdb->get_results("SELECT * FROM {$table} ORDER BY id DESC", ARRAY_A);
} else {
    $rows = $wpdb->get_results("SELECT * FROM {$table} WHERE is_active = 'T' ORDER BY id DESC", ARRAY_A);
}

echo '
<style>

.kaya-cert-wrap{
    max-width:1200px;
    margin:0;
    height:63vh;
    display:flex;
    flex-direction:column;
}

.kaya-cert-card{
    display:flex;
    flex-direction:column;
    height:100%;
    border-radius:18px;
    overflow:hidden;
    border:1px solid #e2e8f0;
    background:#ffffff;
}

/* DEBUG PANEL */
.kaya-debug-box{
    max-width:1200px;
    margin:0 auto 14px;
    border:1px solid #fcd34d;
    background:#fffbeb;
    border-radius:14px;
    padding:14px 16px;
    color:#78350f;
    font-size:13px;
    line-height:1.6;
}
.kaya-debug-box h3{
    margin:0 0 10px;
    font-size:15px;
    font-weight:800;
    color:#92400e;
}
.kaya-debug-grid{
    display:grid;
    gap:10px;
}
.kaya-debug-label{
    font-weight:700;
    color:#92400e;
}
.kaya-debug-code{
    display:block;
    margin-top:4px;
    padding:10px 12px;
    background:#fff;
    border:1px solid #fde68a;
    border-radius:10px;
    font-family:Consolas, Monaco, monospace;
    font-size:12px;
    color:#7c2d12;
    white-space:pre-wrap;
    word-break:break-word;
}

/* HEADER */
.kaya-cert-head{
    padding:20px 24px;
    background:linear-gradient(135deg,#1e293b,#0f172a);
}

.kaya-cert-title{
    margin:0;
    font-size:22px;
    font-weight:800;
    color:#ffffff;
}

/* TOOLBAR */
.kaya-cert-toolbar{
    padding:12px 20px;
    background:#f8fafc;
    border-bottom:1px solid #e2e8f0;
    display:flex;
    align-items:center;
    justify-content:space-between;
    gap:16px;
    flex-wrap:wrap;
}

.kaya-cert-search-wrap{
    flex:1 1 420px;
    max-width:560px;
    position:relative;
}

.kaya-cert-search{
    width:100%;
    height:40px;
    border-radius:12px;
    border:1px solid #cbd5e1;
    padding:0 14px;
    background:#ffffff;
    font-size:13px;
    color:#0f172a;
    outline:none;
    transition:border-color .18s ease, box-shadow .18s ease, background .18s ease;
}

.kaya-cert-search::placeholder{
    color:#64748b;
}

.kaya-cert-search:focus{
    border-color:#3b82f6;
    box-shadow:0 0 0 4px rgba(59,130,246,.12);
    background:#fff;
}

.kaya-cert-note{
    display:inline-flex;
    align-items:center;
    gap:6px;
    font-size:12px;
    color:#64748b;
    font-weight:600;
    white-space:nowrap;
    margin-left:auto;
}

.kaya-cert-note::before{
    content:"↕";
    font-size:12px;
    color:#94a3b8;
}

/* TABLE */
.kaya-cert-tablewrap{
    flex:1;
    overflow:auto;
}

.kaya-cert-table{
    width:100%;
    border-collapse:collapse;
}

.kaya-cert-table thead th{
    position:sticky;
    top:0;
    z-index:2;
    background:#f1f5f9;
    font-weight:700;
    font-size:13px;
    padding:12px;
    border-bottom:1px solid #e2e8f0;
    text-align:center;
    cursor:pointer;
    user-select:none;
}

.kaya-col-title{
    text-align:left !important;
}

.kaya-cert-table tbody td{
    padding:10px;
    border-bottom:1px solid #e2e8f0;
    font-size:13px;
    text-align:center;
    vertical-align:middle;
}

.kaya-cert-table tbody tr:nth-child(even){
    background:#f8fafc;
}

.kaya-row-inactive{
    opacity:.68;
    background:#f8fafc !important;
}

.kaya-title-text{
    font-weight:700;
    color:#0f172a;
}

.kaya-customer-text{
    font-weight:600;
    color:#334155;
}

.kaya-category{
    background:#eef2ff;
    color:#3730a3;
    padding:5px 10px;
    border-radius:999px;
    font-size:12px;
    font-weight:600;
}

.kaya-action-wrap{
    display:flex;
    align-items:center;
    justify-content:center;
    gap:8px;
    flex-wrap:wrap;
}

.kaya-action-btn{
    display:inline-flex;
    align-items:center;
    justify-content:center;
    height:30px;
    min-width:88px;
    padding:0 12px;
    border-radius:8px;
    text-decoration:none;
    font-size:12px;
    font-weight:700;
    border:1px solid transparent;
}

.kaya-action-btn:hover{
    opacity:.92;
}

.kaya-btn-deactivate{
    background:#fee2e2;
    color:#dc2626;
    border-color:#fecaca;
}

.kaya-btn-deactivate:hover{
    background:#fecaca;
}

.kaya-btn-activate{
    background:#dcfce7;
    color:#15803d;
    border-color:#bbf7d0;
}

.kaya-btn-activate:hover{
    background:#bbf7d0;
}

.kaya-btn-edit{
    background:#fef3c7;
    color:#b45309;
    border-color:#fcd34d;
}

.kaya-btn-edit:hover{
    background:#fde68a;
}

.kaya-pdf-btn{
    display:inline-flex;
    align-items:center;
    justify-content:center;
    height:30px;
    min-width:78px;
    padding:0 12px;
    border-radius:8px;
    text-decoration:none;
    font-size:12px;
    font-weight:700;
    background:#eff6ff;
    color:#1d4ed8;
    border:1px solid #bfdbfe;
}

.kaya-pdf-btn:hover{
    background:#dbeafe;
}

.kaya-empty{
    padding:25px;
    text-align:center;
    color:#64748b;
}

</style>
';

if ($kaya_debug_columns && $is_user01) {
    $available_columns = [];
    $sample_row = [];

    if (!empty($rows) && is_array($rows[0])) {
        $available_columns = array_keys($rows[0]);
        $sample_row = $rows[0];
    }

    echo '<div class="kaya-debug-box">';
    echo '<h3>Debug: Available Table Columns</h3>';
    echo '<div class="kaya-debug-grid">';

    echo '<div>';
    echo '<span class="kaya-debug-label">Table Name:</span>';
    echo '<span class="kaya-debug-code">' . esc_html($table) . '</span>';
    echo '</div>';

    echo '<div>';
    echo '<span class="kaya-debug-label">Total Rows:</span>';
    echo '<span class="kaya-debug-code">' . esc_html(count($rows)) . '</span>';
    echo '</div>';

    echo '<div>';
    echo '<span class="kaya-debug-label">Current View:</span>';
    echo '<span class="kaya-debug-code">' . ($is_user01 ? 'User01 sees all rows' : 'Normal users see only is_active = T') . '</span>';
    echo '</div>';

    echo '<div>';
    echo '<span class="kaya-debug-label">Available Columns:</span>';
    echo '<span class="kaya-debug-code">';
    echo !empty($available_columns)
        ? esc_html(implode(', ', $available_columns))
        : 'No rows found, so columns could not be detected from query result.';
    echo '</span>';
    echo '</div>';

    echo '<div>';
    echo '<span class="kaya-debug-label">Sample First Row:</span>';
    echo '<span class="kaya-debug-code">';
    if (!empty($sample_row)) {
        foreach ($sample_row as $col => $val) {
            echo esc_html($col . ' = ' . (is_scalar($val) || $val === null ? (string)$val : wp_json_encode($val))) . "\n";
        }
    } else {
        echo 'No sample row available.';
    }
    echo '</span>';
    echo '</div>';

    echo '</div>';
    echo '</div>';
}

echo '<div class="kaya-cert-wrap">';
echo '<div class="kaya-cert-card">';

echo '<div class="kaya-cert-head">';
echo '<h2 class="kaya-cert-title">LIST</h2>';
echo '</div>';

echo '<div class="kaya-cert-toolbar">';
echo '<div class="kaya-cert-search-wrap">';
echo '<input type="text" id="kayaCertSearch" class="kaya-cert-search" placeholder="Search title, customer name, category...">';
echo '</div>';
echo '<div class="kaya-cert-note">Click column headers to sort</div>';
echo '</div>';

echo '<div class="kaya-cert-tablewrap">';
echo '<table class="kaya-cert-table" id="kayaCertTable">';

echo '<thead>';
echo '<tr>';
echo '<th data-sort="number">No.</th>';
echo '<th data-sort="text" class="kaya-col-title-head">Title</th>';
echo '<th data-sort="text">Customer Name</th>';
echo '<th data-sort="text">Category</th>';

if ($is_user01) {
    echo '<th data-sort="text">Active Flag</th>';
}

if ($can_edit_records || $is_user01) {
    echo '<th>Action</th>';
}

echo '<th data-sort="text">Invoice</th>';
echo '</tr>';
echo '</thead>';

echo '<tbody>';

if (!$rows) {

    $colspan = 5;
    if ($is_user01) $colspan++;
    if ($can_edit_records || $is_user01) $colspan++;

    echo '<tr><td colspan="'.$colspan.'" class="kaya-empty">No records found.</td></tr>';

} else {

    $no = 1;

    foreach ($rows as $row) {

        $is_active = isset($row['is_active']) && strtoupper((string)$row['is_active']) === 'F' ? 'F' : 'T';
        $pdf_url = isset($row['pdf_url']) ? trim((string) $row['pdf_url']) : '';
        $row_class = ($is_active === 'F') ? ' class="kaya-row-inactive"' : '';

        $title = isset($row['title']) ? (string)$row['title'] : '';
        $customer_name = isset($row['cust_name']) ? (string)$row['cust_name'] : '';
        $category_slug = !empty($row['category_slug']) ? (string)$row['category_slug'] : 'uncategorized';
        $row_id = isset($row['id']) ? intval($row['id']) : 0;

        echo '<tr' . $row_class . '>';

        echo '<td data-value="'.esc_attr($no).'">'.$no.'</td>';

        echo '<td data-value="'.esc_attr($title).'" class="kaya-col-title">
            <span class="kaya-title-text">'.esc_html($title !== '' ? $title : '-').'</span>
        </td>';

        echo '<td data-value="'.esc_attr($customer_name).'">
            <span class="kaya-customer-text">'.esc_html($customer_name !== '' ? $customer_name : '-').'</span>
        </td>';

        echo '<td data-value="'.esc_attr($category_slug).'">
            <span class="kaya-category">'.esc_html($category_slug).'</span>
        </td>';

        if ($is_user01) {
            echo '<td data-value="'.esc_attr($is_active).'">' . esc_html($is_active) . '</td>';
        }

        if ($can_edit_records || $is_user01) {
            $edit_url = add_query_arg([
                'edit_id' => $row_id,
            ], $invoice_edit_base_url);

            echo '<td>';
            echo '<div class="kaya-action-wrap">';

            if ($can_edit_records) {
                echo '<a class="kaya-action-btn kaya-btn-edit" href="'.esc_url($edit_url).'">Edit</a>';
            }

            if ($is_user01) {
                if ($is_active === 'T') {
                    $toggle_url = wp_nonce_url(
                        add_query_arg([
                            'kaya_toggle_id' => $row_id,
                            'kaya_toggle_to' => 'F',
                        ]),
                        'kaya_toggle_row'
                    );

                    echo '<a class="kaya-action-btn kaya-btn-deactivate" href="'.esc_url($toggle_url).'" onclick="return confirm(\'Are you sure you want to deactivate this row?\');">Deactivate</a>';
                } else {
                    $toggle_url = wp_nonce_url(
                        add_query_arg([
                            'kaya_toggle_id' => $row_id,
                            'kaya_toggle_to' => 'T',
                        ]),
                        'kaya_toggle_row'
                    );

                    echo '<a class="kaya-action-btn kaya-btn-activate" href="'.esc_url($toggle_url).'" onclick="return confirm(\'Are you sure you want to activate this row?\');">Activate</a>';
                }
            }

            echo '</div>';
            echo '</td>';
        }

        $post_id = isset($row['post_id']) ? intval($row['post_id']) : 0;

        echo '<td data-value="'.esc_attr($pdf_url).'">';

        if ($post_id > 0 && get_post($post_id)) {
            $page_url = get_permalink($post_id);
            echo '<a class="kaya-pdf-btn" href="'.esc_url($page_url).'">View Invoice</a>';
        } elseif ($pdf_url !== '') {
            echo '<a class="kaya-pdf-btn" href="'.esc_url($pdf_url).'" target="_blank" rel="noopener noreferrer">Open PDF</a>';
        } else {
            echo '-';
        }

        echo '</td>';

        echo '</tr>';

        $no++;
    }
}

echo '</tbody>';
echo '</table>';
echo '</div>';
echo '</div>';
echo '</div>';
?>

<script>
document.addEventListener('DOMContentLoaded', function () {
    const searchInput = document.getElementById('kayaCertSearch');
    const table = document.getElementById('kayaCertTable');
    const tbody = table.querySelector('tbody');
    const headers = table.querySelectorAll('thead th[data-sort]');

    if (searchInput) {
        searchInput.addEventListener('input', function () {
            const keyword = this.value.toLowerCase();
            const rows = tbody.querySelectorAll('tr');

            rows.forEach(row => {
                row.style.display = row.innerText.toLowerCase().includes(keyword) ? '' : 'none';
            });
        });
    }

    headers.forEach((header, index) => {
        let asc = tm�e�Ȼ���������m�e�
Z����rue;

        header.addEventListener('click', function () {
            const type = header.getAttribute('data-sort');
            const rows = Array.from(tbody.querySelectorAll('tr'));

            rows.sort((a, b) => {
                const aCell = a.children[index];
                const bCell = b.children[index];

                const aVal = aCell ? (aCell.dataset.value || aCell.innerText) : '';
                const bVal = bCell ? (bCell.dataset.value || bCell.innerText) : '';

                let result;

                if (type === 'number' || type === 'date') {
                    result = parseFloat(aVal || 0) - parseFloat(bVal || 0);
                } else {
                    result = String(aVal).localeCompare(String(bVal));
                }

                return asc ? result : -result;
            });

            rows.forEach(row => tbody.appendChild(row));
            asc = !asc;
        });
    });
});
</script>m�e��&t4

Youez - 2016 - github.com/yon3zu
LinuXploit