<?php
/**
 * Render table rows for the Work Log.
 * Expected variable: $logs array
 */
if (!defined('ABSPATH'))
    exit;

if (empty($mediat_logs)): ?>
    <tr>
        <td colspan="5">
            <?php esc_html_e('No events logged yet.', 'mediatasker'); ?>
        </td>
    </tr>
<?php else: ?>
    <?php foreach ($mediat_logs as $mediat_log): ?>
        <?php
        $mediat_user_name = 'Guest/System';
        if ($mediat_log->user_id) {
            $mediat_user = get_userdata($mediat_log->user_id);
            if ($mediat_user)
                $mediat_user_name = $mediat_user->display_name;
        }

        $mediat_task_name = 'Deleted/Unknown';
        if ($mediat_log->task_id && get_post_type($mediat_log->task_id) === 'mediat_task') {
            $mediat_task_name = get_the_title($mediat_log->task_id);
        } else {
            $mediat_details = json_decode($mediat_log->details, true);
            if (isset($mediat_details['file_name'])) {
                $mediat_task_name = $mediat_details['file_name'];
            }
        }

        $mediat_details_str = '';
        if ($mediat_log->details) {
            $mediat_details = json_decode($mediat_log->details, true);
            if (isset($mediat_details['new_status'])) {
                $mediat_details_str = 'Status: ' . $mediat_details['new_status'];
            } elseif (isset($mediat_details['comment_id'])) {
                $mediat_details_str = 'Comment ID: ' . $mediat_details['comment_id'];
            }
        }

        $mediat_type_html = esc_html(ucwords(str_replace('_', ' ', $mediat_log->action_type)));
        switch ($mediat_log->action_type) {
            case 'upload':
                $mediat_type_html = '<span class="dashicons dashicons-upload" style="color:#2271b1;" title="Upload"></span> Upload';
                break;
            case 'status_change':
                $mediat_type_html = '<span class="dashicons dashicons-update" style="color:#f56e28;" title="Status Change"></span> Status Change';
                break;
            case 'comment_added':
                $mediat_type_html = '<span class="dashicons dashicons-admin-comments" style="color:#00a0d2;" title="Comment"></span> Comment Added';
                break;
            case 'comment_edited':
                $mediat_type_html = '<span class="dashicons dashicons-admin-comments" style="color:#00a0d2;" title="Comment Edited"></span> Comment Edited';
                break;
            case 'comment_deleted':
                $mediat_type_html = '<span class="dashicons dashicons-trash" style="color:#d63638;" title="Comment Deleted"></span> Comment Deleted';
                break;
            case 'media_deleted':
                $mediat_type_html = '<span class="dashicons dashicons-trash" style="color:#d63638;" title="Media Deleted"></span> Media Deleted';
                break;
        }
        ?>
        <tr>
            <td>
                <?php echo esc_html(wp_date(get_option('date_format') . ' ' . get_option('time_format'), strtotime($mediat_log->created_at))); ?>
            </td>
            <td>
                <?php echo esc_html($mediat_user_name); ?>
            </td>
            <td>
                <?php echo wp_kses($mediat_type_html, array(
                    'span' => array(
                        'class' => array(),
                        'style' => array(),
                        'title' => array(),
                    )
                )); ?>
            </td>
            <td>
                <?php if (get_post_type($mediat_log->task_id) === 'mediat_task'): ?>
                    <a href="<?php echo esc_url(get_edit_post_link($mediat_log->task_id)); ?>">
                        <?php echo esc_html($mediat_task_name); ?>
                    </a>
                <?php else: ?>
                    <?php echo esc_html($mediat_task_name); ?>
                <?php endif; ?>
            </td>
            <td>
                <?php echo esc_html($mediat_details_str); ?>
            </td>
        </tr>
    <?php endforeach; ?>
<?php endif; ?>

