<?php
/**
 * Dashboard Widget Renderer
 *
 * @package Freelance_Studio
 */

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

function fs_render_widget($widget_id, $data) {
    extract($data);
    
    switch ($widget_id) {
        case 'action-needed':
            ?>
            <div class="fs-card-header">
                <h3>
                    <?php esc_html_e('Action Needed', 'freelance-studio'); ?>
                    <?php if (!empty($action_items)): ?>
                        <span class="fs-action-needed-count"><?php echo count($action_items); ?></span>
                    <?php endif; ?>
                </h3>
            </div>
            <?php if (!empty($action_items)): ?>
            <div class="fs-action-needed-list">
                <?php foreach ($action_items as $item): ?>
                <a href="<?php echo esc_url($item->url); ?>" class="fs-action-needed-item fs-action-type-<?php echo esc_attr($item->type); ?>">
                    <div class="fs-action-needed-icon">
                        <span class="dashicons <?php echo esc_attr($item->icon); ?>"></span>
                    </div>
                    <div class="fs-action-needed-content">
                        <span class="fs-action-needed-title"><?php echo esc_html($item->title); ?></span>
                        <?php if (!empty($item->subtitle)): ?>
                            <span class="fs-action-needed-subtitle"><?php echo esc_html($item->subtitle); ?></span>
                        <?php endif; ?>
                    </div>
                    <div class="fs-action-needed-meta">
                        <span class="fs-status-badge <?php echo esc_attr($item->badge_class); ?>"><?php echo esc_html($item->badge_text); ?></span>
                    </div>
                </a>
                <?php endforeach; ?>
            </div>
            <?php else: ?>
            <div class="fs-action-needed-empty">
                <span class="dashicons dashicons-yes-alt"></span>
                <p><?php esc_html_e('You\'re all caught up!', 'freelance-studio'); ?></p>
            </div>
            <?php endif; ?>
            <?php
            break;
            
        case 'financial-overview':
            ?>
            <div class="fs-financial-overview">
                <div class="fs-financial-header">
                    <h2><?php esc_html_e('Financial Overview', 'freelance-studio'); ?> <span class="fs-year-badge"><?php echo  esc_html( gmdate('Y') ); ?></span></h2>
                    <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-finances&tab=reporting') ); ?>" class="fs-link-subtle"><?php esc_html_e('View Reports', 'freelance-studio'); ?> →</a>
                </div>
                <div class="fs-financial-grid">
                    <div class="fs-financial-item fs-financial-gross">
                        <div class="fs-financial-details">
                            <span class="fs-financial-label"><?php esc_html_e('Gross Income', 'freelance-studio'); ?></span>
                            <span class="fs-financial-value"><?php echo '$' . number_format($financial_summary['gross_income'], 0); ?></span>
                        </div>
                    </div>
                    <div class="fs-financial-item fs-financial-pending">
                        <div class="fs-financial-details">
                            <span class="fs-financial-label"><?php esc_html_e('Pending Income', 'freelance-studio'); ?></span>
                            <span class="fs-financial-value"><?php echo '$' . number_format($financial_summary['pending_income'], 0); ?></span>
                        </div>
                    </div>
                    <div class="fs-financial-item fs-financial-expenses">
                        <div class="fs-financial-details">
                            <span class="fs-financial-label"><?php esc_html_e('Expenses YTD', 'freelance-studio'); ?></span>
                            <span class="fs-financial-value"><?php echo '$' . number_format($financial_summary['expenses'], 0); ?></span>
                        </div>
                    </div>
                    <div class="fs-financial-item fs-financial-tax">
                        <div class="fs-financial-details">
                            <span class="fs-financial-label"><?php esc_html_e('Estimated Tax Owed', 'freelance-studio'); ?></span>
                            <span class="fs-financial-value"><?php echo '$' . number_format($financial_summary['estimated_tax'], 0); ?></span>
                        </div>
                    </div>
                </div>
            </div>
            <?php
            break;
            
        case 'recent-projects':
            ?>
            <div class="fs-card-header">
                <h3><?php esc_html_e('Recent Projects', 'freelance-studio'); ?></h3>
                <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-projects&action=add') ); ?>" class="fs-btn-icon" title="<?php esc_attr_e('New Project', 'freelance-studio'); ?>">+</a>
            </div>
            <?php if (!empty($recent_projects)): ?>
            <ul class="fs-item-list">
                <?php foreach ($recent_projects as $project): ?>
                <li class="fs-item-row">
                    <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-projects&action=view&id=' . $project->id) ); ?>" class="fs-item-title"><?php echo esc_html($project->name); ?></a>
                    <?php echo  wp_kses_post( FS_View_Context::status_badge($project->status) ); ?>
                </li>
                <?php endforeach; ?>
            </ul>
            <?php else: ?>
            <p class="fs-empty-message"><?php esc_html_e('No projects yet.', 'freelance-studio'); ?></p>
            <?php endif; ?>
            <div class="fs-card-footer"><a href="<?php echo  esc_url( admin_url('admin.php?page=fs-projects') ); ?>"><?php esc_html_e('View All', 'freelance-studio'); ?> →</a></div>
            <?php
            break;
            
        case 'invoices':
            ?>
            <div class="fs-card-header">
                <h3><?php esc_html_e('Invoices', 'freelance-studio'); ?></h3>
                <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-finances&tab=invoices&action=add') ); ?>" class="fs-btn-icon" title="<?php esc_attr_e('New Invoice', 'freelance-studio'); ?>">+</a>
            </div>
            <?php if (!empty($dashboard_invoices)): ?>
            <div class="fs-invoice-mobile-list">
                <?php foreach ($dashboard_invoices as $invoice): 
                    $status_class = '';
                    $status_label = '';
                    if ($invoice->display_status === 'paid') {
                        $status_class = 'status-paid';
                        $status_label = __('Paid', 'freelance-studio');
                    } elseif ($invoice->display_status === 'overdue') {
                        $status_class = 'status-overdue';
                        $status_label = __('Overdue', 'freelance-studio');
                    } else {
                        $status_class = 'status-sent';
                        $status_label = __('Sent', 'freelance-studio');
                    }
                ?>
                <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-finances&tab=invoices&action=view&id=' . $invoice->id) ); ?>" class="fs-invoice-mobile-row">
                    <div class="fs-invoice-mobile-left">
                        <span class="fs-invoice-mobile-client"><?php echo esc_html($invoice->project_name ?: $invoice->invoice_number); ?></span>
                        <span class="fs-invoice-mobile-number"><?php echo esc_html($invoice->invoice_number); ?><?php if (!empty($invoice->client_name)): ?> · <?php echo esc_html($invoice->client_name); ?><?php endif; ?></span>
                        <span class="fs-status-badge <?php echo  esc_attr( $status_class ); ?>"><?php echo  esc_attr( $status_label ); ?></span>
                    </div>
                    <div class="fs-invoice-mobile-right">
                        <span class="fs-invoice-mobile-amount"><?php echo  esc_html( FS_View_Context::format_currency($invoice->total) ); ?></span>
                        <?php if (!empty($invoice->due_date)): ?>
                        <span class="fs-invoice-mobile-date<?php echo $invoice->display_status === 'overdue' ? ' fs-text-danger' : ''; ?>"><?php echo esc_html(gmdate('m/d/y', strtotime($invoice->due_date))); ?></span>
                        <?php endif; ?>
                    </div>
                </a>
                <?php endforeach; ?>
            </div>
            <?php else: ?>
            <p class="fs-empty-message"><?php esc_html_e('No invoices yet.', 'freelance-studio'); ?></p>
            <?php endif; ?>
            <div class="fs-card-footer"><a href="<?php echo  esc_url( admin_url('admin.php?page=fs-finances&tab=invoices') ); ?>"><?php esc_html_e('View All', 'freelance-studio'); ?> →</a></div>
            <?php
            break;
            
        case 'tasks':
            $task_widget_statuses = get_option('fs_dashboard_task_statuses', array('pending', 'in_progress'));
            if (empty($task_widget_statuses)) {
                $task_widget_statuses = array('pending', 'in_progress');
            }
            $task_display_limit = get_option('fs_dashboard_task_limit', 5);
            $all_statuses = array(
                'pending'     => FS_View_Context::get_task_status_labels('pending'),
                'in_progress' => FS_View_Context::get_task_status_labels('in_progress'),
                'needs_info'  => FS_View_Context::get_task_status_labels('needs_info'),
                'with_client' => FS_View_Context::get_task_status_labels('with_client'),
            );
            ?>
            <div class="fs-card-header fs-task-widget-header">
                <div>
                    <h3><?php esc_html_e('Tasks', 'freelance-studio'); ?></h3>
                    <?php 
                    $sort_labels = array(
                        'due_date' => __('due date', 'freelance-studio'),
                        'title' => __('title', 'freelance-studio'),
                        'priority' => __('priority', 'freelance-studio'),
                        'status' => __('status', 'freelance-studio'),
                        'client_name' => __('client', 'freelance-studio'),
                        'position' => __('position', 'freelance-studio'),
                    );
                    $current_task_sort = get_user_meta(get_current_user_id(), 'fs_task_sort_orderby', true) ?: 'due_date';
                    $sort_display = isset($sort_labels[$current_task_sort]) ? $sort_labels[$current_task_sort] : $current_task_sort;
                    ?>
                    <?php /* translators: 1. sorted by value */ ?>
                    <span class="fs-widget-subhead"><?php echo wp_kses_post( sprintf( __('Sorted by %s', 'freelance-studio'), $sort_display)); ?></span>
                </div>
                <div class="fs-task-widget-controls">
                    <button type="button" class="fs-btn fs-btn-primary fs-btn-small" id="fs-dashboard-add-task-btn"><?php esc_html_e('Add Task', 'freelance-studio'); ?></button>
                    <select class="fs-task-limit-select" id="fs-task-limit-select" title="<?php esc_attr_e('Tasks to show', 'freelance-studio'); ?>">
                        <option value="5" <?php selected($task_display_limit, 5); ?>>5</option>
                        <option value="10" <?php selected($task_display_limit, 10); ?>>10</option>
                        <option value="20" <?php selected($task_display_limit, 20); ?>>20</option>
                        <option value="all" <?php selected($task_display_limit, 'all'); ?>><?php esc_html_e('All', 'freelance-studio'); ?></option>
                    </select>
                    <div class="fs-task-filter-dropdown">
                        <button type="button" class="fs-icon-btn fs-task-filter-toggle" title="<?php esc_attr_e('Filter task statuses', 'freelance-studio'); ?>">
                            <span class="dashicons dashicons-filter"></span>
                        </button>
                        <div class="fs-task-filter-menu" style="display: none;">
                            <div class="fs-task-filter-title"><?php esc_html_e('Show tasks:', 'freelance-studio'); ?></div>
                            <?php foreach ($all_statuses as $status_key => $status_label): ?>
                            <label class="fs-task-filter-option">
                                <input type="checkbox" name="task_status[]" value="<?php echo  esc_attr( $status_key ); ?>" <?php checked(in_array($status_key, $task_widget_statuses)); ?>>
                                <?php echo  esc_html( $status_label ); ?>
                            </label>
                            <?php endforeach; ?>
                            <button type="button" class="fs-btn fs-btn-primary fs-btn-small fs-save-task-filter"><?php esc_html_e('Apply', 'freelance-studio'); ?></button>
                        </div>
                    </div>
                </div>
            </div>
            <ul class="fs-task-list" id="fs-dashboard-tasks">
                <?php if (!empty($incomplete_tasks)): ?>
                    <?php 
                    // Use same color mapping as task list page
                    $dashboard_status_colors = FS_View_Context::get_task_status_colors();
                    $dashboard_status_labels = FS_View_Context::get_task_status_labels();
                    ?>
                    <?php 
                    $display_tasks = ($task_display_limit === 'all') 
                        ? $incomplete_tasks 
                        : array_slice($incomplete_tasks, 0, intval($task_display_limit));
                    ?>
                    <?php foreach ($display_tasks as $task): 
                        $is_overdue = $task->due_date && strtotime($task->due_date) < strtotime('today');
                        $status_color = $dashboard_status_colors[$task->status] ?? '#6b7280';
                        $status_label = $dashboard_status_labels[$task->status] ?? ucfirst(str_replace('_', ' ', $task->status));
                    ?>
                    <li class="fs-task-item" data-task-id="<?php echo  intval( $task->id ); ?>">
                        <label class="fs-task-checkbox">
                            <input type="checkbox" class="fs-toggle-task" data-id="<?php echo  intval( $task->id ); ?>">
                            <span class="fs-checkmark"></span>
                        </label>
                        <div class="fs-task-info">
                            <span class="fs-task-title"><?php echo esc_html($task->title); ?></span>
                            <select class="fs-inline-status-select fs-dashboard-task-status task-status-<?php echo esc_attr($task->status); ?>" 
                                    data-task-id="<?php echo esc_attr($task->id); ?>" 
                                    data-original="<?php echo esc_attr($task->status); ?>">
                                <?php 
                                $inline_dash_statuses = array_diff_key($dashboard_status_labels, array('archived' => 1, 'cancelled' => 1));
                                foreach ($inline_dash_statuses as $skey => $slabel): ?>
                                    <option value="<?php echo esc_attr($skey); ?>" <?php selected($task->status, $skey); ?>>
                                        <?php echo esc_html($slabel); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="fs-task-meta">
                            <?php if ($task->due_date): ?><span class="fs-task-due <?php echo  esc_attr( $is_overdue ? 'overdue' : '' ); ?>"><?php echo  esc_attr( gmdate('M j', strtotime($task->due_date)) ); ?></span><?php endif; ?>
                            <?php if ($is_overdue): ?><span class="fs-status-badge status-overdue"><?php esc_html_e('Overdue', 'freelance-studio'); ?></span><?php endif; ?>
                            <?php if (!empty($task->project_name)): ?><span class="fs-task-project"><?php echo esc_html($task->project_name); ?></span><?php endif; ?>
                        </div>
                        <?php if (!empty($task->project_id)): ?>
                        <?php if ( ! function_exists( 'fs_studio_is_free_plan' ) || ! fs_studio_is_free_plan() ): ?>
                        <button type="button" class="fs-task-start-timer fs-dashboard-task-timer" 
                                data-task-id="<?php echo esc_attr($task->id); ?>"
                                data-project-id="<?php echo esc_attr($task->project_id); ?>"
                                data-task-name="<?php echo esc_attr($task->title); ?>"
                                title="<?php esc_attr_e('Start Timer', 'freelance-studio'); ?>">
                            <span class="dashicons dashicons-clock"></span>
                        </button>
                        <?php endif; ?>
                        <?php endif; ?>
                    </li>
                    <?php endforeach; ?>
                <?php else: ?>
                    <li class="fs-empty-message"><?php esc_html_e('No tasks yet!', 'freelance-studio'); ?></li>
                <?php endif; ?>
            </ul>
            <div class="fs-card-footer"><a href="<?php echo  esc_url( admin_url('admin.php?page=fs-tasks') ); ?>"><?php esc_html_e('View All', 'freelance-studio'); ?> →</a></div>
            <?php
            break;
            
        case 'timer':
            // Use the shared timer widget partial for consistency
            $projects = isset($projects_dropdown) ? $projects_dropdown : array();
            $timer_context = 'dashboard';
            $selected_project_id = 0;
            include JEEB_FS_PLUGIN_DIR . 'includes/views/partials/timer-widget.php';
            break;
            
        case 'time-goals':
            if ($time_goals['weekly_goal'] > 0 || $time_goals['daily_goal'] > 0):
            ?>
            <div class="fs-card-header"><h3><?php esc_html_e('Time Goals', 'freelance-studio'); ?></h3></div>
            <div class="fs-time-goals">
                <?php if ($time_goals['daily_goal'] > 0): ?>
                <div class="fs-goal-item">
                    <div class="fs-goal-header">
                        <span class="fs-goal-label"><?php esc_html_e('Today', 'freelance-studio'); ?></span>
                        <span class="fs-goal-value"><?php echo  esc_html( $time_goals['daily_hours'] ); ?>h / <?php echo  esc_html( $time_goals['daily_goal'] ); ?>h</span>
                    </div>
                    <div class="fs-goal-progress-bar"><div class="fs-goal-progress-fill <?php echo  esc_attr( $time_goals['daily_progress'] >= 100 ? 'complete' : '' ); ?>" style="width: <?php echo  esc_attr( min(100, $time_goals['daily_progress']) ); ?>%"></div></div>
                </div>
                <?php endif; ?>
                <?php if ($time_goals['weekly_goal'] > 0): ?>
                <div class="fs-goal-item">
                    <div class="fs-goal-header">
                        <span class="fs-goal-label"><?php esc_html_e('This Week', 'freelance-studio'); ?></span>
                        <span class="fs-goal-value"><?php echo  esc_html( $time_goals['weekly_hours'] ); ?>h / <?php echo  esc_html( $time_goals['weekly_goal'] ); ?>h</span>
                    </div>
                    <div class="fs-goal-progress-bar"><div class="fs-goal-progress-fill <?php echo  esc_attr( $time_goals['weekly_progress'] >= 100 ? 'complete' : '' ); ?>" style="width: <?php echo  esc_attr( min(100, $time_goals['weekly_progress']) ); ?>%"></div></div>
                </div>
                <?php endif; ?>
            </div>
            <div class="fs-card-footer"><a href="<?php echo  esc_url( admin_url('admin.php?page=fs-time-tracking') ); ?>"><?php esc_html_e('View Time Tracking', 'freelance-studio'); ?> →</a></div>
            <?php
            endif;
            break;
            
        case 'recent-sessions':
            if (!empty($recent_sessions)):
            ?>
            <div class="fs-card-header"><h3><?php esc_html_e('Recent Sessions', 'freelance-studio'); ?></h3></div>
            <div class="fs-timer-history">
                <?php foreach ($recent_sessions as $date => $day_data): ?>
                <div class="fs-history-day">
                    <div class="fs-history-date">
                        <span class="fs-date-label"><?php echo esc_html($day_data['label']); ?></span>
                        <span class="fs-date-total"><?php echo esc_html(FS_Time_Entry::format_duration($day_data['total_seconds'])); ?></span>
                    </div>
                    <div class="fs-history-entries">
                        <?php foreach ($day_data['entries'] as $entry): ?>
                        <div class="fs-history-entry">
                            <div class="fs-entry-time">
                                <span class="fs-entry-duration"><?php echo esc_html(FS_Time_Entry::format_duration($entry->duration)); ?></span>
                                <span class="fs-entry-range"><?php echo esc_html(FS_Time_Entry::utc_to_local($entry->start_time, 'g:ia')); ?></span>
                            </div>
                            <div class="fs-entry-details">
                                <?php if ($entry->project_name): ?><span class="fs-entry-project"><?php echo esc_html($entry->project_name); ?></span><?php endif; ?>
                                <?php if ($entry->description): ?><span class="fs-entry-desc"><?php echo esc_html(wp_trim_words($entry->description, 6)); ?></span><?php endif; ?>
                            </div>
                        </div>
                        <?php endforeach; ?>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
            <div class="fs-card-footer"><a href="<?php echo  esc_url( admin_url('admin.php?page=fs-time-tracking') ); ?>"><?php esc_html_e('View All', 'freelance-studio'); ?> →</a></div>
            <?php
            endif;
            break;
            
        case 'pending-proposals':
            ?>
            <div class="fs-card-header"><h3><?php esc_html_e('Pending Proposals', 'freelance-studio'); ?></h3></div>
            <?php if (!empty($pending_proposals)): ?>
            <div class="fs-invoice-mobile-list">
                <?php foreach ($pending_proposals as $proposal): 
                    $p_status_class = 'status-sent';
                    $p_status_label = __('Sent', 'freelance-studio');
                    if (isset($proposal->status)) {
                        switch ($proposal->status) {
                            case 'viewed':
                                $p_status_class = 'status-viewed';
                                $p_status_label = __('Viewed', 'freelance-studio');
                                break;
                            case 'revision_requested':
                                $p_status_class = 'status-revision';
                                $p_status_label = __('Revision Requested', 'freelance-studio');
                                break;
                            case 'revised':
                                $p_status_class = 'status-sent';
                                $p_status_label = __('Revised', 'freelance-studio');
                                break;
                        }
                    }
                ?>
                <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-proposals&action=view&id=' . $proposal->id) ); ?>" class="fs-invoice-mobile-row">
                    <div class="fs-invoice-mobile-left">
                        <span class="fs-invoice-mobile-client"><?php echo esc_html($proposal->title); ?></span>
                        <span class="fs-invoice-mobile-number"><?php echo esc_html($proposal->client_company ?: $proposal->client_name); ?></span>
                        <span class="fs-status-badge <?php echo  esc_attr( $p_status_class ); ?>"><?php echo  esc_attr( $p_status_label ); ?></span>
                    </div>
                    <div class="fs-invoice-mobile-right">
                        <?php $p_total = !empty($proposal->total_amount) ? $proposal->total_amount : 0; ?>
                        <span class="fs-invoice-mobile-amount"><?php echo  esc_html( FS_View_Context::format_currency($p_total) ); ?></span>
                        <?php if (!empty($proposal->valid_until)): ?>
                        <span class="fs-invoice-mobile-date<?php echo strtotime($proposal->valid_until) < time() ? ' fs-text-danger' : ''; ?>"><?php echo esc_html(gmdate('m/d/y', strtotime($proposal->valid_until))); ?></span>
                        <?php endif; ?>
                    </div>
                </a>
                <?php endforeach; ?>
            </div>
            <?php else: ?>
            <p class="fs-empty-message"><?php esc_html_e('No pending proposals.', 'freelance-studio'); ?></p>
            <?php endif; ?>
            <div class="fs-card-footer"><a href="<?php echo  esc_url( admin_url('admin.php?page=fs-proposals') ); ?>"><?php esc_html_e('View All', 'freelance-studio'); ?> →</a></div>
            <?php
            break;
            
        case 'awaiting-signature':
            ?>
            <div class="fs-card-header"><h3><?php esc_html_e('Awaiting Signature', 'freelance-studio'); ?></h3></div>
            <?php if (!empty($unsigned_contracts)): ?>
            <div class="fs-invoice-mobile-list">
                <?php foreach ($unsigned_contracts as $contract): 
                    $c_status_class = 'status-sent';
                    $c_status_label = __('Sent', 'freelance-studio');
                    if (isset($contract->status)) {
                        if ($contract->status === 'viewed') {
                            $c_status_class = 'status-viewed';
                            $c_status_label = __('Viewed', 'freelance-studio');
                        }
                    }
                ?>
                <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-contracts&action=view&id=' . $contract->id) ); ?>" class="fs-invoice-mobile-row">
                    <div class="fs-invoice-mobile-left">
                        <span class="fs-invoice-mobile-client"><?php echo esc_html($contract->title); ?></span>
                        <span class="fs-invoice-mobile-number"><?php echo esc_html($contract->client_name); ?></span>
                        <span class="fs-status-badge <?php echo  esc_attr( $c_status_class ); ?>"><?php echo  esc_attr( $c_status_label ); ?></span>
                    </div>
                    <div class="fs-invoice-mobile-right">
                        <?php if (!empty($contract->valid_until)): ?>
                        <span class="fs-invoice-mobile-date<?php echo strtotime($contract->valid_until) < time() ? ' fs-text-danger' : ''; ?>"><?php echo esc_html(gmdate('m/d/y', strtotime($contract->valid_until))); ?></span>
                        <?php endif; ?>
                    </div>
                </a>
                <?php endforeach; ?>
            </div>
            <?php else: ?>
            <p class="fs-empty-message"><?php esc_html_e('No contracts awaiting signature.', 'freelance-studio'); ?></p>
            <?php endif; ?>
            <div class="fs-card-footer"><a href="<?php echo  esc_url( admin_url('admin.php?page=fs-contracts') ); ?>"><?php esc_html_e('View All', 'freelance-studio'); ?> →</a></div>
            <?php
            break;
            
        case 'quick-actions':
            ?>
            <div class="fs-quick-actions">
                <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-clients&action=add') ); ?>" class="fs-quick-action">
                    <span class="dashicons dashicons-businessperson"></span>
                    <span><?php esc_html_e('New Client', 'freelance-studio'); ?></span>
                </a>
                <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-proposals&action=add') ); ?>" class="fs-quick-action">
                    <span class="dashicons dashicons-media-document"></span>
                    <span><?php esc_html_e('New Proposal', 'freelance-studio'); ?></span>
                </a>
                <a href="#" class="fs-quick-action fs-open-invoice-wizard">
                    <span class="dashicons dashicons-clipboard"></span>
                    <span><?php esc_html_e('Invoice Wizard', 'freelance-studio'); ?></span>
                </a>
                <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-finances&tab=expenses&action=add') ); ?>" class="fs-quick-action">
                    <span class="dashicons dashicons-money-alt"></span>
                    <span><?php esc_html_e('Log Expense', 'freelance-studio'); ?></span>
                </a>
            </div>
            <?php
            break;
            
        case 'upcoming-events':
            $upcoming_events = isset($upcoming_events) ? $upcoming_events : array();
            $today = gmdate('Y-m-d');
            ?>
            <?php if (!empty($upcoming_events)): ?>
            <div class="fs-card-header"><h3><?php esc_html_e('Upcoming Events', 'freelance-studio'); ?></h3></div>
            <div class="fs-widget-calendar-list">
                <?php foreach ($upcoming_events as $event): 
                    $is_today = $event['date'] === $today;
                    $is_overdue = $event['date'] < $today;
                    $item_class = 'fs-widget-calendar-item';
                    if ($is_overdue) $item_class .= ' fs-cal-overdue';
                    elseif ($is_today) $item_class .= ' fs-cal-today';
                ?>
                <a href="<?php echo esc_url($event['url']); ?>" class="<?php echo esc_attr( $item_class ); ?>" style="border-left-color: <?php echo esc_attr($event['color']); ?>;">
                    <div class="fs-cal-date">
                        <span class="fs-cal-month"><?php echo  esc_html( gmdate('M', strtotime($event['date'])) ); ?></span>
                        <span class="fs-cal-day"><?php echo  esc_html( gmdate('j', strtotime($event['date'])) ); ?></span>
                    </div>
                    <div class="fs-cal-content">
                        <div class="fs-cal-title">
                            <span><?php echo  esc_html( $event['icon'] ); ?></span>
                            <?php echo esc_html($event['title']); ?>
                        </div>
                        <div class="fs-cal-subtitle"><?php echo esc_html($event['subtitle']); ?></div>
                    </div>
                </a>
                <?php endforeach; ?>
            </div>
            <?php else: ?>
            <div class="fs-widget-no-events">
                <span class="dashicons dashicons-calendar-alt"></span>
                <p><?php esc_html_e('No upcoming events in the next 7 days.', 'freelance-studio'); ?></p>
            </div>
            <?php endif; ?>
            <div class="fs-card-footer">
                <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-calendar') ); ?>"><?php esc_html_e('View Calendar', 'freelance-studio'); ?> →</a>
            </div>
            <?php
            break;
            
        case 'mini-calendar':
            $calendar_data = isset($calendar_data) ? $calendar_data : array();
            $cal_year = isset($calendar_data['year']) ? $calendar_data['year'] : gmdate('Y');
            $cal_month = isset($calendar_data['month']) ? $calendar_data['month'] : gmdate('n');
            $cal_events = isset($calendar_data['events']) ? $calendar_data['events'] : array();
            $today = gmdate('Y-m-d');
            
            // Group events by date
            $events_by_date = array();
            foreach ($cal_events as $event) {
                $date = $event['date'];
                if (!isset($events_by_date[$date])) {
                    $events_by_date[$date] = array();
                }
                $events_by_date[$date][] = $event;
            }
            
            // Calculate calendar grid
            $first_day_of_month = mktime(0, 0, 0, $cal_month, 1, $cal_year);
            $days_in_month = gmdate('t', $first_day_of_month);
            $day_of_week = gmdate('N', $first_day_of_month);
            $month_name = gmdate('F', $first_day_of_month);
            
            // Navigation
            $prev_month = $cal_month - 1;
            $prev_year = $cal_year;
            if ($prev_month < 1) { $prev_month = 12; $prev_year--; }
            $next_month = $cal_month + 1;
            $next_year = $cal_year;
            if ($next_month > 12) { $next_month = 1; $next_year++; }
            ?>
            <div class="fs-sidebar-mini-cal fs-dashboard-mini-cal" id="fs-dashboard-mini-calendar" 
                 data-year="<?php echo esc_attr($cal_year); ?>" 
                 data-month="<?php echo esc_attr($cal_month); ?>">
                <div class="fs-sidebar-mini-cal-header">
                    <span class="fs-sidebar-mini-cal-title"><?php echo esc_html($month_name . ' ' . $cal_year); ?></span>
                    <div class="fs-sidebar-mini-cal-nav">
                        <button type="button" class="fs-mini-nav-btn fs-dashboard-cal-nav" data-direction="prev" title="<?php esc_attr_e('Previous month', 'freelance-studio'); ?>">
                            <span class="dashicons dashicons-arrow-left-alt2"></span>
                        </button>
                        <button type="button" class="fs-mini-nav-btn fs-dashboard-cal-nav" data-direction="next" title="<?php esc_attr_e('Next month', 'freelance-studio'); ?>">
                            <span class="dashicons dashicons-arrow-right-alt2"></span>
                        </button>
                    </div>
                </div>
                <div class="fs-sidebar-mini-cal-grid">
                    <span class="fs-sidebar-mini-cal-weekday"><?php esc_html_e('M', 'freelance-studio'); ?></span>
                    <span class="fs-sidebar-mini-cal-weekday"><?php esc_html_e('T', 'freelance-studio'); ?></span>
                    <span class="fs-sidebar-mini-cal-weekday"><?php esc_html_e('W', 'freelance-studio'); ?></span>
                    <span class="fs-sidebar-mini-cal-weekday"><?php esc_html_e('T', 'freelance-studio'); ?></span>
                    <span class="fs-sidebar-mini-cal-weekday"><?php esc_html_e('F', 'freelance-studio'); ?></span>
                    <span class="fs-sidebar-mini-cal-weekday"><?php esc_html_e('S', 'freelance-studio'); ?></span>
                    <span class="fs-sidebar-mini-cal-weekday"><?php esc_html_e('S', 'freelance-studio'); ?></span>
                    <?php
                    // Empty cells before first day
                    $start_offset = $day_of_week - 1;
                    for ($i = 0; $i < $start_offset; $i++): ?>
                        <span class="fs-sidebar-mini-cal-day fs-other-month"></span>
                    <?php endfor;
                    
                    // Days of month
                    for ($day = 1; $day <= $days_in_month; $day++):
                        $date_str = sprintf('%04d-%02d-%02d', $cal_year, $cal_month, $day);
                        $is_today = $date_str === $today;
                        $has_events = isset($events_by_date[$date_str]);
                        $event_count = $has_events ? count($events_by_date[$date_str]) : 0;
                        
                        $classes = array('fs-sidebar-mini-cal-day');
                        if ($is_today) $classes[] = 'fs-today';
                        if ($has_events) $classes[] = 'fs-has-events';
                    ?>
                        <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-calendar&year=' . $cal_year . '&month=' . $cal_month) ); ?>" 
                           class="<?php echo  esc_attr( implode(' ', $classes) ); ?>"
                           title="<?php echo  esc_attr( $has_events ? $event_count . ' event(s)' : '' ); ?>">
                            <?php echo  esc_html( $day ); ?>
                            <?php if ($has_events): ?>
                                <span class="fs-mini-cal-dot"></span>
                            <?php endif; ?>
                        </a>
                    <?php endfor;
                    
                    // Empty cells after last day
                    $remaining = (7 - (($start_offset + $days_in_month) % 7)) % 7;
                    for ($i = 0; $i < $remaining; $i++): ?>
                        <span class="fs-sidebar-mini-cal-day fs-other-month"></span>
                    <?php endfor; ?>
                </div>
            </div>
            <div class="fs-card-footer">
                <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-calendar') ); ?>"><?php esc_html_e('Open Calendar', 'freelance-studio'); ?> →</a>
            </div>
            <?php
            break;
            
        case 'week-view':
            $week_data = isset($week_data) ? $week_data : array();
            $week_start = isset($week_data['week_start']) ? $week_data['week_start'] : gmdate('Y-m-d', strtotime('monday this week'));
            $week_events = isset($week_data['events']) ? $week_data['events'] : array();
            $today = gmdate('Y-m-d');
            
            // Check user preference for showing external events
            $show_external = get_user_meta(get_current_user_id(), 'fs_dashboard_show_external_events', true);
            if ($show_external === '') $show_external = 1; // Default: show
            $show_external = intval($show_external);
            
            // Filter out external events if hidden
            if (!$show_external) {
                $week_events = array_filter($week_events, function($e) {
                    return $e['type'] !== 'external';
                });
            }
            
            // Group events by date
            $events_by_date = array();
            foreach ($week_events as $event) {
                $date = $event['date'];
                if (!isset($events_by_date[$date])) {
                    $events_by_date[$date] = array();
                }
                $events_by_date[$date][] = $event;
            }
            
            $week_end = gmdate('Y-m-d', strtotime($week_start . ' +6 days'));
            ?>
            <div class="fs-week-glance-widget">
                <div class="fs-week-glance-header">
                    <span class="fs-week-glance-title"><?php esc_html_e('Your Week at a Glance', 'freelance-studio'); ?></span>
                    <div class="fs-week-glance-header-actions">
                        <label class="fs-week-external-toggle" title="<?php esc_attr_e('Show imported calendar events', 'freelance-studio'); ?>">
                            <input type="checkbox" id="fs-toggle-external-events" <?php checked($show_external, 1); ?>>
                            <span class="dashicons dashicons-calendar"></span>
                            <span class="fs-week-external-label"><?php esc_html_e('Imported', 'freelance-studio'); ?></span>
                        </label>
                        <span class="fs-week-glance-range">
                            <?php echo  esc_html( gmdate('M j', strtotime($week_start)) . ' - ' . gmdate('M j', strtotime($week_end)) ); ?>
                        </span>
                    </div>
                </div>
                <div class="fs-week-glance-grid">
                    <?php
                    $current_date = strtotime($week_start);
                    for ($i = 0; $i < 7; $i++):
                        $date_str = gmdate('Y-m-d', $current_date);
                        $day_name = gmdate('D', $current_date);
                        $day_num = gmdate('j', $current_date);
                        $is_today = $date_str === $today;
                        $is_weekend = gmdate('N', $current_date) >= 6;
                        $day_events = isset($events_by_date[$date_str]) ? $events_by_date[$date_str] : array();
                        
                        $classes = array('fs-week-glance-day');
                        if ($is_today) $classes[] = 'fs-today';
                        if ($is_weekend) $classes[] = 'fs-weekend';
                    ?>
                        <div class="<?php echo  esc_attr( implode(' ', $classes) ); ?>">
                            <div class="fs-week-glance-day-header">
                                <span class="fs-week-glance-day-name"><?php echo  esc_html( strtoupper($day_name) ); ?></span>
                                <span class="fs-week-glance-day-num"><?php echo  intval( $day_num ); ?></span>
                            </div>
                            <div class="fs-week-glance-events">
                                <?php if (empty($day_events)): ?>
                                    <span class="fs-week-glance-empty">—</span>
                                <?php else: 
                                    $shown = 0;
                                    foreach ($day_events as $event):
                                        if ($shown >= 3) {
                                            $remaining = count($day_events) - 3;
                                            echo wp_kses_post( '<span class="fs-week-glance-more">+' . $remaining . '</span>' );                                            break;
                                        }
                                        $shown++;
                                ?>
                                    <a href="<?php echo esc_url($event['url']); ?>" 
                                       class="fs-week-glance-event fs-week-event-<?php echo esc_attr($event['type']); ?>"
                                       style="background: <?php echo esc_attr($event['color']); ?>; --fs-task-accent: <?php echo esc_attr($event['color']); ?>;"
                                       title="<?php echo esc_attr($event['title']); ?>">
                                        <span class="fs-week-event-title"><?php echo esc_html(wp_trim_words($event['title'], 3, '…')); ?></span>
                                        <span class="fs-week-event-title-full"><?php echo esc_html($event['title']); ?></span>
                                        <?php if (!empty($event['time'])): ?>
                                            <span class="fs-week-event-time"><?php echo esc_html($event['time']); ?></span>
                                        <?php endif; ?>
                                    </a>
                                <?php 
                                    endforeach;
                                endif; ?>
                            </div>
                        </div>
                    <?php 
                        $current_date = strtotime('+1 day', $current_date);
                    endfor; ?>
                </div>
            </div>
            <div class="fs-card-footer">
                <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-calendar&view=week') ); ?>"><?php esc_html_e('View Full Week', 'freelance-studio'); ?> →</a>
            </div>
            <?php
            break;
            
        case 'internal-tasks':
            ?>
            <div class="fs-card-header">
                <h3><?php esc_html_e('Internal Tasks', 'freelance-studio'); ?></h3>
                <?php 
                $internal_client = FS_Client::get_internal_client();
                if ($internal_client): 
                ?>
                    <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-clients&action=view&id=' . $internal_client->id . '&tab=tasks') ); ?>" class="fs-btn-icon" title="<?php esc_attr_e('Manage Tasks', 'freelance-studio'); ?>">+</a>
                <?php endif; ?>
            </div>
            <?php if (!empty($internal_tasks)): ?>
            <div class="fs-internal-tasks-widget">
                <?php foreach ($internal_tasks as $task): 
                    $is_overdue = $task->due_date && strtotime($task->due_date) < time();
                ?>
                <div class="fs-task-item">
                    <label class="fs-task-checkbox-wrapper">
                        <input type="checkbox" 
                               class="fs-task-toggle" 
                               data-id="<?php echo esc_attr($task->id); ?>">
                        <span class="fs-task-checkbox-custom"></span>
                    </label>
                    <div class="fs-task-content">
                        <div class="fs-task-title"><?php echo esc_html($task->title); ?></div>
                        <div class="fs-task-meta">
                            <?php if ($task->due_date): ?>
                                <span class="<?php echo $is_overdue ? 'overdue' : ''; ?>">
                                    <span class="dashicons dashicons-calendar-alt"></span>
                                    <?php echo  esc_html( FS_View_Context::format_date($task->due_date) ); ?>
                                </span>
                            <?php endif; ?>
                            <span><?php echo  esc_html( ucfirst($task->priority) ); ?> priority</span>
                        </div>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
            <?php else: ?>
            <p class="fs-empty-message"><?php esc_html_e('No internal tasks.', 'freelance-studio'); ?></p>
            <?php endif; ?>
            <?php if ($internal_client): ?>
            <div class="fs-card-footer">
                <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-clients&action=view&id=' . $internal_client->id . '&tab=tasks') ); ?>"><?php esc_html_e('View All', 'freelance-studio'); ?> →</a>
            </div>
            <?php endif; ?>
            <?php
            break;
    }
}
