<?php
/**
 * Client Portal Dashboard
 *
 * @package Freelance_Studio
 */

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

$primary_color = !empty($settings['invoice_primary_color']) ? $settings['invoice_primary_color'] : (!empty($settings['primary_color']) ? $settings['primary_color'] : '#4173B0');
$color_mode = !empty($settings['invoice_primary_color_mode']) ? $settings['invoice_primary_color_mode'] : 'solid';
$color_2 = !empty($settings['invoice_primary_color_2']) ? $settings['invoice_primary_color_2'] : $primary_color;
$grad_dir = !empty($settings['invoice_gradient_direction']) ? intval($settings['invoice_gradient_direction']) : 135;
$primary_bg = ($color_mode === 'gradient' && !empty($color_2))
    ? 'linear-gradient(' . $grad_dir . 'deg, ' . esc_attr($primary_color) . ' 0%, ' . esc_attr($color_2) . ' 100%)'
    : esc_attr($primary_color);
$business_name = !empty($settings['business_name']) ? $settings['business_name'] : get_bloginfo('name');
$client_name = $client->company_name ?: $client->name;

// Determine booking URL based on booking type setting
$booking_type = !empty($settings['booking_type']) ? $settings['booking_type'] : 'external';
$booking_url = '';
if ($booking_type === 'internal') {
    $booking_url = home_url('/book/');
} elseif ($booking_type === 'external' && !empty($settings['booking_url'])) {
    $booking_url = $settings['booking_url'];
}
// If booking_type is 'none', booking_url stays empty

// Ensure variables are set
$pending_contracts = isset($pending_contracts) ? $pending_contracts : array();
$all_contracts = isset($all_contracts) ? $all_contracts : array();
$all_invoices = isset($all_invoices) ? $all_invoices : array();
$projects_lookup = isset($projects_lookup) ? $projects_lookup : array();

// Count outstanding items
$outstanding_count = count($outstanding_invoices) + count($pending_proposals) + count($pending_contracts);
?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo('charset'); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="robots" content="noindex, nofollow">
    <title><?php echo esc_html($client_name); ?> - <?php esc_html_e('Client Portal', 'freelance-studio'); ?></title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <?php // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet -- Public-facing portal page; Google Fonts loaded directly. ?>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <style>
        <?php include JEEB_FS_PLUGIN_DIR . 'includes/views/public/portal/portal-styles.php'; ?>
    </style>
</head>
<body class="fs-portal">
    <div class="fs-portal-container">
        <!-- Header -->
        <header class="fs-portal-header">
            <div class="fs-portal-header-inner">
                <div class="fs-portal-branding">
                    <?php if (!empty($settings['business_logo'])): ?>
                        <img src="<?php echo esc_url($settings['business_logo']); ?>" alt="<?php echo esc_attr($business_name); ?>" class="fs-portal-logo">
                    <?php else: ?>
                        <span class="fs-portal-business-name"><?php echo esc_html($business_name); ?></span>
                    <?php endif; ?>
                </div>
                <div class="fs-portal-user">
                    <?php 
                    $client_logo_url = FS_Client::get_logo_url($client->id, 'thumbnail');
                    if ($client_logo_url): 
                    ?>
                        <img src="<?php echo esc_url($client_logo_url); ?>" alt="" class="fs-portal-client-logo">
                    <?php endif; ?>
                    <span class="fs-portal-welcome"><?php echo esc_html($client_name); ?></span>
                </div>
            </div>
        </header>
        
        <!-- Main Content -->
        <main class="fs-portal-main">
            <div class="fs-portal-content">
                <!-- Page Title -->
                <div class="fs-portal-page-header">
                    <h1><?php esc_html_e('Dashboard', 'freelance-studio'); ?></h1>
                </div>
                
                <!-- Two Column Layout -->
                <div class="fs-portal-grid">
                    <!-- Left Column - Main Content -->
                    <div class="fs-portal-main-col">
                        <!-- Outstanding Items -->
                        <?php if ($outstanding_count > 0): ?>
                        <div class="fs-portal-card">
                            <div class="fs-portal-card-header">
                                <h2><?php esc_html_e('Outstanding Items', 'freelance-studio'); ?> <span class="fs-count-badge"><?php echo  intval( $outstanding_count ); ?></span></h2>
                            </div>
                            <div class="fs-portal-card-body">
                                <ul class="fs-portal-items-list">
                                    <?php foreach ($outstanding_invoices as $invoice): ?>
                                    <li class="fs-portal-item">
                                        <div class="fs-portal-item-icon fs-icon-invoice">
                                            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                                <rect x="3" y="3" width="18" height="18" rx="2"/>
                                                <path d="M9 7h6M9 11h6M9 15h4"/>
                                            </svg>
                                        </div>
                                        <div class="fs-portal-item-content">
                                            <div class="fs-portal-item-title">
                                                <?php /* translators: 1. number/ID */ ?>
                                                <?php echo esc_html( sprintf( __( 'Pay Invoice #%s', 'freelance-studio'), esc_html($invoice->invoice_number)) ); ?>
                                                <?php if (!empty($invoice->project_id)): ?>
                                                    <span class="fs-portal-item-subtitle">: <?php echo esc_html($invoice->project_name ?? ''); ?></span>
                                                <?php endif; ?>
                                            </div>
                                            <div class="fs-portal-item-meta">
                                                <?php if ($invoice->sent_at): ?>
                                                    <?php /* translators: 1. sent value */ ?>
                                                    <?php echo esc_html( sprintf( __( 'Sent %s', 'freelance-studio'), date_i18n(get_option('date_format'), strtotime($invoice->sent_at))) ); ?>
                                                <?php endif; ?>
                                                <?php if (strtotime($invoice->due_date) < time()): ?>
                                                    <span class="fs-overdue-badge"><?php esc_html_e('Overdue', 'freelance-studio'); ?></span>
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                        <div class="fs-portal-item-amount">
                                            <?php echo  esc_html( FS_View_Context::format_currency($invoice->balance_due) ); ?>
                                        </div>
                                        <a href="<?php echo esc_url(home_url('?fs_invoice=' . $invoice->view_token)); ?>" class="fs-portal-btn fs-portal-btn-primary">
                                            <?php esc_html_e('View Invoice', 'freelance-studio'); ?>
                                        </a>
                                    </li>
                                    <?php endforeach; ?>
                                    
                                    <?php foreach ($pending_proposals as $proposal): ?>
                                    <li class="fs-portal-item">
                                        <div class="fs-portal-item-icon fs-icon-proposal">
                                            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                                <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/>
                                                <polyline points="14 2 14 8 20 8"/>
                                                <line x1="16" y1="13" x2="8" y2="13"/>
                                                <line x1="16" y1="17" x2="8" y2="17"/>
                                            </svg>
                                        </div>
                                        <div class="fs-portal-item-content">
                                            <div class="fs-portal-item-title">
                                                <?php esc_html_e('Review Proposal', 'freelance-studio'); ?>: <?php echo esc_html($proposal->title); ?>
                                            </div>
                                            <div class="fs-portal-item-meta">
                                                <?php if ($proposal->sent_at): ?>
                                                    <?php /* translators: 1. sent value */ ?>
                                                    <?php echo esc_html( sprintf( __( 'Sent %s', 'freelance-studio'), date_i18n(get_option('date_format'), strtotime($proposal->sent_at))) ); ?>
                                                <?php endif; ?>
                                                <?php if ($proposal->valid_until && strtotime($proposal->valid_until) < time()): ?>
                                                    <span class="fs-overdue-badge"><?php esc_html_e('Expired', 'freelance-studio'); ?></span>
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                        <div class="fs-portal-item-amount">
                                            <?php echo  esc_html( FS_View_Context::format_currency($proposal->total_amount) ); ?>
                                        </div>
                                        <a href="<?php echo esc_url(home_url('?fs_proposal=' . $proposal->view_token)); ?>" class="fs-portal-btn fs-portal-btn-primary">
                                            <?php esc_html_e('View Proposal', 'freelance-studio'); ?>
                                        </a>
                                    </li>
                                    <?php endforeach; ?>
                                    
                                    <?php foreach ($pending_contracts as $contract): ?>
                                    <li class="fs-portal-item">
                                        <div class="fs-portal-item-icon fs-icon-contract">
                                            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                                <path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/>
                                                <polyline points="14 2 14 8 20 8"/>
                                                <path d="M9 15l2 2 4-4"/>
                                            </svg>
                                        </div>
                                        <div class="fs-portal-item-content">
                                            <div class="fs-portal-item-title">
                                                <?php esc_html_e('Sign Contract', 'freelance-studio'); ?>: <?php echo esc_html($contract->title); ?>
                                            </div>
                                            <div class="fs-portal-item-meta">
                                                <?php if ($contract->sent_at): ?>
                                                    <?php /* translators: 1. sent value */ ?>
                                                    <?php echo esc_html( sprintf( __( 'Sent %s', 'freelance-studio'), date_i18n(get_option('date_format'), strtotime($contract->sent_at))) ); ?>
                                                <?php endif; ?>
                                                <?php if ($contract->valid_until && strtotime($contract->valid_until) < time()): ?>
                                                    <span class="fs-overdue-badge"><?php esc_html_e('Expired', 'freelance-studio'); ?></span>
                                                <?php endif; ?>
                                            </div>
                                        </div>
                                        <div class="fs-portal-item-amount">
                                            <?php echo  esc_html( FS_View_Context::format_currency($contract->total_amount) ); ?>
                                        </div>
                                        <a href="<?php echo esc_url(home_url('?fs_contract=' . $contract->view_token)); ?>" class="fs-portal-btn fs-portal-btn-primary">
                                            <?php esc_html_e('View Contract', 'freelance-studio'); ?>
                                        </a>
                                    </li>
                                    <?php endforeach; ?>
                                </ul>
                            </div>
                        </div>
                        <?php endif; ?>
                        
                        <!-- Projects -->
                        <div class="fs-portal-card">
                            <div class="fs-portal-card-header">
                                <h2><?php esc_html_e('Projects', 'freelance-studio'); ?> <span class="fs-count-badge"><?php echo count($projects); ?></span></h2>
                            </div>
                            <div class="fs-portal-card-body">
                                <?php if (!empty($projects)): ?>
                                <ul class="fs-portal-projects-list">
                                    <?php foreach ($projects as $project): 
                                        $project_url = home_url('?fs_project_portal=' . $portal_token . ':' . $project->id);
                                    ?>
                                    <li class="fs-portal-project">
                                        <a href="<?php echo esc_url($project_url); ?>" class="fs-portal-project-link">
                                            <div class="fs-portal-project-info">
                                                <div class="fs-portal-project-name"><?php echo esc_html($project->name); ?></div>
                                                <?php if ($project->start_date || $project->due_date): ?>
                                                <div class="fs-portal-project-dates">
                                                    <?php 
                                                    if ($project->start_date) {
                                                        echo  esc_html( date_i18n('M j, Y', strtotime($project->start_date)) );                                                    }
                                                    if ($project->start_date && $project->due_date) {
                                                        echo ' - ';
                                                    }
                                                    if ($project->due_date) {
                                                        echo  esc_html( date_i18n('M j, Y', strtotime($project->due_date)) );                                                    }
                                                    ?>
                                                </div>
                                                <?php endif; ?>
                                            </div>
                                            <div class="fs-portal-project-status">
                                                <span class="fs-status-badge fs-status-<?php echo esc_attr($project->status); ?>">
                                                    <?php echo esc_html(ucfirst($project->status)); ?>
                                                </span>
                                            </div>
                                            <div class="fs-portal-project-arrow">
                                                <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                                    <polyline points="9 18 15 12 9 6"/>
                                                </svg>
                                            </div>
                                        </a>
                                    </li>
                                    <?php endforeach; ?>
                                </ul>
                                <?php else: ?>
                                <div class="fs-portal-empty">
                                    <p><?php esc_html_e('No projects yet.', 'freelance-studio'); ?></p>
                                </div>
                                <?php endif; ?>
                            </div>
                        </div>
                        
                        <!-- Tasks Section -->
                        <?php 
                        $all_tasks = isset($all_tasks) ? $all_tasks : array();
                        $active_tasks = array_filter($all_tasks, function($t) { return !in_array($t->status, array('completed', 'archived', 'cancelled')); });
                        $inactive_tasks = array_filter($all_tasks, function($t) { return in_array($t->status, array('completed', 'archived', 'cancelled')); });
                        
                        // Get active projects for task request modal
                        $active_projects = array_filter($projects, function($p) {
                            return in_array($p->status, array('active', 'running', 'pending'));
                        });
                        
                        // Task status labels and colors for display
                        $task_status_display = array(
                            'requested' => array('label' => __('Requested', 'freelance-studio'), 'class' => 'requested'),
                            'pending' => array('label' => __('Pending', 'freelance-studio'), 'class' => 'pending'),
                            'in_progress' => array('label' => FS_View_Context::get_task_status_labels('in_progress'), 'class' => 'active'),
                            'needs_info' => array('label' => FS_View_Context::get_task_status_labels('needs_info'), 'class' => 'pending'),
                            'with_client' => array('label' => FS_View_Context::get_task_status_labels('with_client'), 'class' => 'sent'),
                            'completed' => array('label' => __('Completed', 'freelance-studio'), 'class' => 'completed'),
                        );
                        
                        $task_priority_display = array(
                            'low' => array('label' => __('Low', 'freelance-studio'), 'color' => '#6b7280'),
                            'medium' => array('label' => __('Medium', 'freelance-studio'), 'color' => '#f59e0b'),
                            'high' => array('label' => __('High', 'freelance-studio'), 'color' => '#f97316'),
                            'urgent' => array('label' => __('Urgent', 'freelance-studio'), 'color' => '#ef4444'),
                        );
                        ?>
                        <div class="fs-portal-card">
                            <div class="fs-portal-card-header fs-portal-tabs-header" style="display:flex; align-items:center; justify-content:space-between;">
                                <div class="fs-portal-tabs">
                                    <button type="button" class="fs-portal-tab active" data-tab="tasks-active">
                                        <?php esc_html_e('Active', 'freelance-studio'); ?> 
                                        <?php if (!empty($active_tasks)): ?>
                                        <span class="fs-count-badge"><?php echo count($active_tasks); ?></span>
                                        <?php endif; ?>
                                    </button>
                                    <button type="button" class="fs-portal-tab" data-tab="tasks-inactive">
                                        <?php esc_html_e('Inactive', 'freelance-studio'); ?> 
                                        <?php if (!empty($inactive_tasks)): ?>
                                        <span class="fs-count-badge"><?php echo count($inactive_tasks); ?></span>
                                        <?php endif; ?>
                                    </button>
                                </div>
                                <button type="button" class="fs-portal-btn fs-portal-btn-primary fs-portal-btn-sm" id="fs-dashboard-request-task-btn">
                                    <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><line x1="12" y1="5" x2="12" y2="19"/><line x1="5" y1="12" x2="19" y2="12"/></svg>
                                    <?php esc_html_e('Request Task', 'freelance-studio'); ?>
                                </button>
                            </div>
                            
                            <!-- Active Tasks Tab -->
                            <div class="fs-portal-card-body fs-portal-tab-content active" id="tab-tasks-active">
                                <?php if (!empty($active_tasks)): ?>
                                <div class="fs-portal-tasks-dashboard-list">
                                    <?php foreach ($active_tasks as $task): 
                                        $status_info = isset($task_status_display[$task->status]) ? $task_status_display[$task->status] : $task_status_display['pending'];
                                        $priority_info = isset($task_priority_display[$task->priority]) ? $task_priority_display[$task->priority] : $task_priority_display['medium'];
                                        $is_overdue = $task->due_date && strtotime($task->due_date) < time();
                                    ?>
                                    <div class="fs-portal-task-row">
                                        <div class="fs-portal-task-row-priority" style="background-color: <?php echo esc_attr($priority_info['color']); ?>;" title="<?php echo esc_attr($priority_info['label']); ?>"></div>
                                        <div class="fs-portal-task-row-main">
                                            <div class="fs-portal-task-row-title"><?php echo esc_html($task->title); ?></div>
                                            <?php if (!empty($task->project_name)): ?>
                                            <div class="fs-portal-task-row-project"><?php echo esc_html($task->project_name); ?></div>
                                            <?php endif; ?>
                                        </div>
                                        <div class="fs-portal-task-row-meta">
                                            <span class="fs-portal-task-status-badge <?php echo esc_attr($status_info['class']); ?>"><?php echo esc_html($status_info['label']); ?></span>
                                            <?php if ($task->due_date): ?>
                                            <span class="fs-portal-task-row-due <?php echo $is_overdue ? 'overdue' : ''; ?>">
                                                <?php echo  esc_html( date_i18n('M j', strtotime($task->due_date)) ); ?>
                                            </span>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                    <?php endforeach; ?>
                                </div>
                                <?php else: ?>
                                <div class="fs-portal-empty">
                                    <p><?php esc_html_e('No active tasks.', 'freelance-studio'); ?></p>
                                </div>
                                <?php endif; ?>
                            </div>
                            
                            <!-- Inactive Tasks Tab -->
                            <div class="fs-portal-card-body fs-portal-tab-content" id="tab-tasks-inactive">
                                <?php if (!empty($inactive_tasks)): ?>
                                <div class="fs-portal-tasks-dashboard-list">
                                    <?php foreach ($inactive_tasks as $task): 
                                        $inactive_status_labels = array(
                                            'completed' => __('Completed', 'freelance-studio'),
                                            'archived' => __('Archived', 'freelance-studio'),
                                            'cancelled' => __('Cancelled', 'freelance-studio'),
                                        );
                                        $inactive_label = $inactive_status_labels[$task->status] ?? ucfirst($task->status);
                                        $is_completed = $task->status === 'completed';
                                    ?>
                                    <div class="fs-portal-task-row completed">
                                        <div class="fs-portal-task-row-check">
                                            <?php if ($is_completed): ?>
                                            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5"><polyline points="20 6 9 17 4 12"/></svg>
                                            <?php else: ?>
                                            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" style="opacity: 0.4;"><line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/></svg>
                                            <?php endif; ?>
                                        </div>
                                        <div class="fs-portal-task-row-main">
                                            <div class="fs-portal-task-row-title"><?php echo esc_html($task->title); ?></div>
                                            <?php if (!empty($task->project_name)): ?>
                                            <div class="fs-portal-task-row-project"><?php echo esc_html($task->project_name); ?></div>
                                            <?php endif; ?>
                                        </div>
                                        <div class="fs-portal-task-row-meta">
                                            <span class="fs-portal-task-status-badge <?php echo $is_completed ? 'completed' : 'cancelled'; ?>"><?php echo esc_html($inactive_label); ?></span>
                                        </div>
                                    </div>
                                    <?php endforeach; ?>
                                </div>
                                <?php else: ?>
                                <div class="fs-portal-empty">
                                    <p><?php esc_html_e('No inactive tasks.', 'freelance-studio'); ?></p>
                                </div>
                                <?php endif; ?>
                            </div>
                        </div>
                        
                        <!-- Proposals, Contracts & Invoices Section (Tabbed) -->
                        <?php 
                        $has_proposals = !empty($all_proposals);
                        $has_contracts = !empty($all_contracts);
                        $has_invoices = !empty($all_invoices);
                        
                        // Determine which tab should be active by default
                        $first_tab = $has_invoices ? 'invoices' : ($has_proposals ? 'proposals' : ($has_contracts ? 'contracts' : ''));
                        
                        if ($has_proposals || $has_contracts || $has_invoices): 
                        ?>
                        <div class="fs-portal-card">
                            <div class="fs-portal-card-header fs-portal-tabs-header">
                                <div class="fs-portal-tabs">
                                    <?php if ($has_invoices): ?>
                                    <button type="button" class="fs-portal-tab <?php echo $first_tab === 'invoices' ? 'active' : ''; ?>" data-tab="invoices">
                                        <?php esc_html_e('Invoices', 'freelance-studio'); ?> 
                                        <span class="fs-count-badge"><?php echo count($all_invoices); ?></span>
                                    </button>
                                    <?php endif; ?>
                                    <?php if ($has_proposals): ?>
                                    <button type="button" class="fs-portal-tab <?php echo $first_tab === 'proposals' ? 'active' : ''; ?>" data-tab="proposals">
                                        <?php esc_html_e('Proposals', 'freelance-studio'); ?> 
                                        <span class="fs-count-badge"><?php echo count($all_proposals); ?></span>
                                    </button>
                                    <?php endif; ?>
                                    <?php if ($has_contracts): ?>
                                    <button type="button" class="fs-portal-tab <?php echo $first_tab === 'contracts' ? 'active' : ''; ?>" data-tab="contracts">
                                        <?php esc_html_e('Contracts', 'freelance-studio'); ?> 
                                        <span class="fs-count-badge"><?php echo count($all_contracts); ?></span>
                                    </button>
                                    <?php endif; ?>
                                </div>
                            </div>
                            
                            <!-- Invoices Tab Content -->
                            <?php if ($has_invoices): ?>
                            <div class="fs-portal-card-body fs-portal-tab-content <?php echo $first_tab === 'invoices' ? 'active' : ''; ?>" id="tab-invoices">
                                <table class="fs-portal-table">
                                    <thead>
                                        <tr>
                                            <th><?php esc_html_e('Invoice', 'freelance-studio'); ?></th>
                                            <th><?php esc_html_e('Date', 'freelance-studio'); ?></th>
                                            <th><?php esc_html_e('Amount', 'freelance-studio'); ?></th>
                                            <th><?php esc_html_e('Status', 'freelance-studio'); ?></th>
                                            <th></th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($all_invoices as $invoice): 
                                            $project_name = '';
                                            if (!empty($invoice->project_id) && isset($projects_lookup[$invoice->project_id])) {
                                                $project_name = $projects_lookup[$invoice->project_id]->name;
                                            }
                                        ?>
                                        <tr>
                                            <td>
                                                <?php if ($project_name): ?>
                                                    <strong><?php echo esc_html($project_name); ?></strong>
                                                    <div class="fs-invoice-number-small">#<?php echo esc_html($invoice->invoice_number); ?></div>
                                                <?php else: ?>
                                                    <strong>#<?php echo esc_html($invoice->invoice_number); ?></strong>
                                                <?php endif; ?>
                                            </td>
                                            <td><?php echo  esc_html( date_i18n('M j, Y', strtotime($invoice->invoice_date)) ); ?></td>
                                            <td><?php echo  esc_html( FS_View_Context::format_currency($invoice->total) ); ?></td>
                                            <td>
                                                <span class="fs-portal-invoice-status fs-status-<?php echo esc_attr($invoice->status); ?>">
                                                    <?php echo esc_html(ucfirst($invoice->status)); ?>
                                                </span>
                                            </td>
                                            <td>
                                                <?php if (!empty($invoice->view_token)): ?>
                                                <a href="<?php echo esc_url(home_url('?fs_invoice=' . $invoice->view_token)); ?>" class="fs-portal-btn fs-portal-btn-small">
                                                    <?php esc_html_e('View', 'freelance-studio'); ?>
                                                </a>
                                                <?php endif; ?>
                                            </td>
                                        </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                            <?php endif; ?>
                            
                            <!-- Proposals Tab Content -->
                            <?php if ($has_proposals): ?>
                            <div class="fs-portal-card-body fs-portal-tab-content <?php echo $first_tab === 'proposals' ? 'active' : ''; ?>" id="tab-proposals">
                                <table class="fs-portal-table">
                                    <thead>
                                        <tr>
                                            <th><?php esc_html_e('Proposal', 'freelance-studio'); ?></th>
                                            <th><?php esc_html_e('Date', 'freelance-studio'); ?></th>
                                            <th><?php esc_html_e('Amount', 'freelance-studio'); ?></th>
                                            <th><?php esc_html_e('Status', 'freelance-studio'); ?></th>
                                            <th></th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($all_proposals as $proposal): ?>
                                        <tr>
                                            <td><strong><?php echo esc_html($proposal->title); ?></strong></td>
                                            <td><?php echo  esc_html( date_i18n('M j, Y', strtotime($proposal->created_at)) ); ?></td>
                                            <td><?php echo  esc_html( FS_View_Context::format_currency($proposal->total_amount) ); ?></td>
                                            <td>
                                                <span class="fs-portal-invoice-status fs-status-<?php echo esc_attr($proposal->status); ?>">
                                                    <?php echo esc_html(ucfirst($proposal->status)); ?>
                                                </span>
                                            </td>
                                            <td>
                                                <?php if (!empty($proposal->view_token)): ?>
                                                <a href="<?php echo esc_url(home_url('?fs_proposal=' . $proposal->view_token)); ?>" class="fs-portal-btn fs-portal-btn-small">
                                                    <?php esc_html_e('View', 'freelance-studio'); ?>
                                                </a>
                                                <?php endif; ?>
                                            </td>
                                        </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                            <?php endif; ?>
                            
                            <!-- Contracts Tab Content -->
                            <?php if ($has_contracts): ?>
                            <div class="fs-portal-card-body fs-portal-tab-content <?php echo $first_tab === 'contracts' ? 'active' : ''; ?>" id="tab-contracts">
                                <table class="fs-portal-table">
                                    <thead>
                                        <tr>
                                            <th><?php esc_html_e('Contract', 'freelance-studio'); ?></th>
                                            <th><?php esc_html_e('Date', 'freelance-studio'); ?></th>
                                            <th><?php esc_html_e('Amount', 'freelance-studio'); ?></th>
                                            <th><?php esc_html_e('Status', 'freelance-studio'); ?></th>
                                            <th></th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($all_contracts as $contract): ?>
                                        <tr>
                                            <td><strong><?php echo esc_html($contract->title); ?></strong></td>
                                            <td><?php echo  esc_html( date_i18n('M j, Y', strtotime($contract->created_at)) ); ?></td>
                                            <td><?php echo  esc_html( FS_View_Context::format_currency($contract->total_amount) ); ?></td>
                                            <td>
                                                <span class="fs-portal-invoice-status fs-status-<?php echo esc_attr($contract->status); ?>">
                                                    <?php echo esc_html(ucfirst($contract->status)); ?>
                                                </span>
                                            </td>
                                            <td>
                                                <?php if (!empty($contract->view_token)): ?>
                                                <a href="<?php echo esc_url(home_url('?fs_contract=' . $contract->view_token)); ?>" class="fs-portal-btn fs-portal-btn-small">
                                                    <?php esc_html_e('View', 'freelance-studio'); ?>
                                                </a>
                                                <?php endif; ?>
                                            </td>
                                        </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                </table>
                            </div>
                            <?php endif; ?>
                        </div>
                        <?php endif; ?>
                    </div>
                    
                    <!-- Right Column - Sidebar -->
                    <div class="fs-portal-sidebar">
                        <!-- Book a Meeting -->
                        <?php if ($booking_url): ?>
                        <div class="fs-portal-card fs-portal-booking-card">
                            <div class="fs-portal-card-body">
                                <div class="fs-portal-booking">
                                    <svg class="fs-portal-booking-icon" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                                        <rect x="3" y="4" width="18" height="18" rx="2" ry="2"/>
                                        <line x1="16" y1="2" x2="16" y2="6"/>
                                        <line x1="8" y1="2" x2="8" y2="6"/>
                                        <line x1="3" y1="10" x2="21" y2="10"/>
                                    </svg>
                                    <div class="fs-portal-booking-text">
                                        <span class="fs-portal-booking-title"><?php esc_html_e('Need to discuss something?', 'freelance-studio'); ?></span>
                                        <span class="fs-portal-booking-desc"><?php esc_html_e('Schedule a time that works for you.', 'freelance-studio'); ?></span>
                                    </div>
                                    <a href="<?php echo esc_url($booking_url); ?>" target="_blank" rel="noopener" class="fs-portal-btn fs-portal-btn-primary fs-portal-btn-booking">
                                        <?php esc_html_e('Book a Meeting', 'freelance-studio'); ?>
                                    </a>
                                </div>
                            </div>
                        </div>
                        <?php endif; ?>
                        
                        <!-- Billing Summary -->
                        <div class="fs-portal-card">
                            <div class="fs-portal-card-header">
                                <h2><?php esc_html_e('Billing', 'freelance-studio'); ?></h2>
                            </div>
                            <div class="fs-portal-card-body">
                                <div class="fs-portal-billing-stats">
                                    <div class="fs-portal-stat">
                                        <div class="fs-portal-stat-label"><?php esc_html_e('Outstanding', 'freelance-studio'); ?></div>
                                        <div class="fs-portal-stat-value"><?php echo  esc_html( FS_View_Context::format_currency($billing_stats['outstanding']) ); ?></div>
                                    </div>
                                    <div class="fs-portal-stat">
                                        <div class="fs-portal-stat-label"><?php esc_html_e('Overdue', 'freelance-studio'); ?></div>
                                        <div class="fs-portal-stat-value <?php echo $billing_stats['overdue'] > 0 ? 'fs-text-danger' : ''; ?>">
                                            <?php echo  esc_html( FS_View_Context::format_currency($billing_stats['overdue']) ); ?>
                                        </div>
                                    </div>
                                    <div class="fs-portal-stat">
                                        <div class="fs-portal-stat-label"><?php esc_html_e('Paid This Year', 'freelance-studio'); ?></div>
                                        <div class="fs-portal-stat-value fs-text-success"><?php echo  esc_html( FS_View_Context::format_currency($billing_stats['paid']) ); ?></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <!-- Contact Info -->
                        <div class="fs-portal-card">
                            <div class="fs-portal-card-header">
                                <h2><?php esc_html_e('Contact', 'freelance-studio'); ?></h2>
                            </div>
                            <div class="fs-portal-card-body">
                                <div class="fs-portal-contact">
                                    <?php if (!empty($settings['business_name'])): ?>
                                    <p class="fs-portal-contact-name"><?php echo esc_html($settings['business_name']); ?></p>
                                    <?php endif; ?>
                                    <?php if (!empty($settings['business_email'])): ?>
                                    <p><a href="mailto:<?php echo esc_attr($settings['business_email']); ?>"><?php echo esc_html($settings['business_email']); ?></a></p>
                                    <?php endif; ?>
                                    <?php if (!empty($settings['business_phone'])): ?>
                                    <p><a href="tel:<?php echo esc_attr($settings['business_phone']); ?>"><?php echo esc_html($settings['business_phone']); ?></a></p>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
        
        <!-- Footer -->
        <footer class="fs-portal-footer">
            <?php /* translators: 1. powered by value */ ?>
            <p><?php echo wp_kses_post( sprintf( __('Powered by %s', 'freelance-studio'), esc_html($business_name))); ?></p>
        </footer>
    </div>
    
    <!-- Task Request Modal -->
    <div class="fs-portal-modal-overlay" id="fs-task-request-modal" style="display:none;">
        <div class="fs-portal-modal">
            <div class="fs-portal-modal-header">
                <h3><?php esc_html_e('Request a Task', 'freelance-studio'); ?></h3>
                <button type="button" class="fs-portal-modal-close">&times;</button>
            </div>
            <form id="fs-task-request-form">
                <div class="fs-portal-modal-body">
                    <div class="fs-portal-form-group">
                        <label for="fs-task-title"><?php esc_html_e('What do you need?', 'freelance-studio'); ?> <span class="required">*</span></label>
                        <input type="text" id="fs-task-title" name="title" required placeholder="<?php esc_attr_e('e.g., Update homepage banner image', 'freelance-studio'); ?>">
                    </div>
                    <div class="fs-portal-form-group">
                        <label for="fs-task-description"><?php esc_html_e('Details', 'freelance-studio'); ?></label>
                        <textarea id="fs-task-description" name="description" rows="3" placeholder="<?php esc_attr_e('Describe what you need in more detail...', 'freelance-studio'); ?>"></textarea>
                    </div>
                    <div class="fs-portal-form-row">
                        <div class="fs-portal-form-group">
                            <label for="fs-task-project"><?php esc_html_e('Project', 'freelance-studio'); ?></label>
                            <select id="fs-task-project" name="project_id">
                                <option value=""><?php esc_html_e('Select project...', 'freelance-studio'); ?></option>
                                <?php foreach ($active_projects as $p): ?>
                                <option value="<?php echo esc_attr($p->id); ?>"><?php echo esc_html($p->name); ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="fs-portal-form-group">
                            <label for="fs-task-priority"><?php esc_html_e('Priority', 'freelance-studio'); ?></label>
                            <select id="fs-task-priority" name="priority">
                                <option value="low"><?php esc_html_e('Low', 'freelance-studio'); ?></option>
                                <option value="medium" selected><?php esc_html_e('Medium', 'freelance-studio'); ?></option>
                                <option value="high"><?php esc_html_e('High', 'freelance-studio'); ?></option>
                                <option value="urgent"><?php esc_html_e('Urgent', 'freelance-studio'); ?></option>
                            </select>
                        </div>
                    </div>
                    <div class="fs-portal-form-group">
                        <label for="fs-task-due-date"><?php esc_html_e('Requested Due Date', 'freelance-studio'); ?></label>
                        <input type="date" id="fs-task-due-date" name="due_date">
                    </div>
                </div>
                <div class="fs-portal-modal-footer">
                    <button type="button" class="fs-portal-btn fs-portal-btn-secondary fs-portal-modal-cancel"><?php esc_html_e('Cancel', 'freelance-studio'); ?></button>
                    <button type="submit" class="fs-portal-btn fs-portal-btn-primary" id="fs-task-submit-btn"><?php esc_html_e('Submit Request', 'freelance-studio'); ?></button>
                </div>
            </form>
        </div>
    </div>
    
    <script>
    // Tab switching
    document.addEventListener('DOMContentLoaded', function() {
        var tabs = document.querySelectorAll('.fs-portal-tab');
        
        tabs.forEach(function(tab) {
            tab.addEventListener('click', function() {
                var tabName = this.getAttribute('data-tab');
                var parent = this.closest('.fs-portal-card');
                
                // Deactivate all tabs in this card
                parent.querySelectorAll('.fs-portal-tab').forEach(function(t) {
                    t.classList.remove('active');
                });
                
                // Hide all tab content in this card
                parent.querySelectorAll('.fs-portal-tab-content').forEach(function(c) {
                    c.classList.remove('active');
                });
                
                // Activate clicked tab
                this.classList.add('active');
                
                // Show corresponding content
                var content = document.getElementById('tab-' + tabName);
                if (content) {
                    content.classList.add('active');
                }
            });
        });
        
        // Task request modal
        var modal = document.getElementById('fs-task-request-modal');
        var openBtn = document.getElementById('fs-dashboard-request-task-btn');
        var closeEls = modal ? modal.querySelectorAll('.fs-portal-modal-close, .fs-portal-modal-cancel') : [];
        
        function openModal(projectId) {
            if (modal) {
                modal.style.display = 'flex';
                document.body.style.overflow = 'hidden';
                if (projectId) {
                    document.getElementById('fs-task-project').value = projectId;
                }
            }
        }
        
        function closeModal() {
            if (modal) {
                modal.style.display = 'none';
                document.body.style.overflow = '';
                document.getElementById('fs-task-request-form').reset();
            }
        }
        
        if (openBtn) {
            openBtn.addEventListener('click', function() { openModal(''); });
        }
        
        closeEls.forEach(function(el) {
            el.addEventListener('click', closeModal);
        });
        
        if (modal) {
            modal.addEventListener('click', function(e) {
                if (e.target === modal) closeModal();
            });
        }
        
        // Submit task request
        var form = document.getElementById('fs-task-request-form');
        if (form) {
            form.addEventListener('submit', function(e) {
                e.preventDefault();
                
                var submitBtn = document.getElementById('fs-task-submit-btn');
                var origText = submitBtn.textContent;
                submitBtn.disabled = true;
                submitBtn.textContent = '<?php echo esc_js(__('Submitting...', 'freelance-studio')); ?>';
                
                var formData = new FormData(form);
                formData.append('action', 'fs_client_add_task');
                formData.append('portal_token', '<?php echo esc_js($portal_token); ?>');
                
                fetch('<?php echo  esc_url( admin_url('admin-ajax.php') ); ?>', {
                    method: 'POST',
                    body: formData,
                    credentials: 'same-origin'
                })
                .then(function(response) { return response.json(); })
                .then(function(data) {
                    if (data.success) {
                        closeModal();
                        // Show success and reload to show new task
                        var successMsg = document.createElement('div');
                        successMsg.className = 'fs-portal-success-toast';
                        successMsg.textContent = data.data.message || '<?php echo esc_js(__('Request submitted!', 'freelance-studio')); ?>';
                        document.body.appendChild(successMsg);
                        setTimeout(function() { 
                            successMsg.remove();
                            location.reload();
                        }, 2000);
                    } else {
                        alert(data.data || '<?php echo esc_js(__('Error submitting request', 'freelance-studio')); ?>');
                        submitBtn.disabled = false;
                        submitBtn.textContent = origText;
                    }
                })
                .catch(function() {
                    alert('<?php echo esc_js(__('Error submitting request', 'freelance-studio')); ?>');
                    submitBtn.disabled = false;
                    submitBtn.textContent = origText;
                });
            });
        }
    });
    </script>
</body>
</html>
