<?php
/**
 * Project Form View (Add/Edit)
 *
 * @package Freelance_Studio
 */

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

$page_title = $is_new ? __('Add New Project', 'freelance-studio') : __('Edit Project', 'freelance-studio');

// Initialize prefill array if not set
if (!isset($prefill)) $prefill = array();

// Get existing services if editing
$services = array();
if (!$is_new && $project && !empty($project->services)) {
    $services = json_decode($project->services, true) ?: array();
} elseif ($is_new && !empty($prefill['services'])) {
    $services = $prefill['services'];
}

// Calculate project total from services (v3.9.0: include retainer amounts)
$project_total = 0;
foreach ($services as $svc) {
    $pricing_type = $svc['pricing_type'] ?? 'fixed';
    if ($pricing_type === 'retainer') {
        $project_total += floatval($svc['retainer_amount'] ?? 0);
    } else {
        $project_total += floatval($svc['amount'] ?? 0);
    }
}

// Deposit settings
$deposit_required = !$is_new && $project ? ($project->deposit_percentage > 0 || $project->deposit_amount > 0) : false;
$deposit_type = !$is_new && $project && isset($project->deposit_type) ? $project->deposit_type : 'percentage';
$deposit_percentage = !$is_new && $project ? $project->deposit_percentage : $default_deposit;
$deposit_amount = !$is_new && $project ? $project->deposit_amount : 0;

// Billing frequencies
$billing_frequencies = array(
    'once' => __('Once', 'freelance-studio'),
    'weekly' => __('Weekly', 'freelance-studio'),
    'biweekly' => __('Bi-weekly', 'freelance-studio'),
    'monthly' => __('Monthly', 'freelance-studio'),
    'quarterly' => __('Quarterly', 'freelance-studio'),
    'milestone' => __('Per Milestone', 'freelance-studio'),
);

// Activity types for tracked time services
$activity_types = FS_Time_Entry::get_activity_types();
?>

<div class="wrap fs-wrap">
    <h1><?php echo esc_html($page_title); ?></h1>
    
    <?php if ($message): ?>
        <div class="notice notice-<?php echo $message === 'error' ? 'error' : 'success'; ?> is-dismissible">
            <p>
                <?php echo  esc_html( $message === 'created' ? __('Project created successfully.', 'freelance-studio') : __('Project updated successfully.', 'freelance-studio') ); ?>
                <?php if (!$is_new && $project): ?>
                    <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-projects&action=view&id=' . $project->id) ); ?>" class="button button-small" style="margin-left: 10px;">
                        <?php esc_html_e('View Project', 'freelance-studio'); ?>
                    </a>
                    <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-projects') ); ?>" class="button button-small">
                        <?php esc_html_e('Back to All Projects', 'freelance-studio'); ?>
                    </a>
                <?php endif; ?>
            </p>
        </div>
    <?php endif; ?>
    
    <form method="post" action="" id="fs-project-form">
        <?php wp_nonce_field('fs_save_project', 'fs_project_nonce'); ?>
        <input type="hidden" name="action" value="<?php echo $is_new ? 'create' : 'update'; ?>">
        <?php if (!$is_new): ?>
            <input type="hidden" name="project_id" value="<?php echo  intval( $project->id ); ?>">
        <?php endif; ?>
        
        <!-- Basic Information -->
        <div class="fs-form-section">
            <h2><?php esc_html_e('Project Details', 'freelance-studio'); ?></h2>
            
            <div class="fs-form-row">
                <div class="fs-form-group required">
                    <label for="client_id"><?php esc_html_e('Client', 'freelance-studio'); ?></label>
                    <select name="client_id" id="client_id" required>
                        <option value=""><?php esc_html_e('— Select Client —', 'freelance-studio'); ?></option>
                        <?php foreach ($clients as $c): ?>
                            <option value="<?php echo  intval( $c->id ); ?>" <?php selected($project ? $project->client_id : $selected_client_id, $c->id); ?>>
                                <?php echo esc_html($c->company_name ?: $c->name); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                
                <div class="fs-form-group required">
                    <label for="name"><?php esc_html_e('Project Name', 'freelance-studio'); ?></label>
                    <input type="text" name="name" id="name" required
                           value="<?php echo $project ? esc_attr($project->name) : esc_attr($prefill['name'] ?? ''); ?>">
                </div>
                
                <div class="fs-form-group">
                    <label for="status"><?php esc_html_e('Status', 'freelance-studio'); ?></label>
                    <select name="status" id="status">
                        <?php foreach ($statuses as $key => $label): ?>
                            <option value="<?php echo esc_attr($key); ?>" <?php selected($project ? $project->status : 'pending', $key); ?>>
                                <?php echo esc_html($label); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
            
            <div class="fs-form-group">
                <label for="description"><?php esc_html_e('Description', 'freelance-studio'); ?></label>
                <textarea name="description" id="description" rows="3"><?php echo $project ? esc_textarea($project->description) : esc_textarea($prefill['description'] ?? ''); ?></textarea>
            </div>
        </div>
        
        <!-- Timeline -->
        <div class="fs-form-section">
            <h2><?php esc_html_e('Timeline', 'freelance-studio'); ?></h2>
            
            <div class="fs-form-row">
                <div class="fs-form-group">
                    <label for="start_date"><?php esc_html_e('Start Date', 'freelance-studio'); ?></label>
                    <input type="date" name="start_date" id="start_date"
                           value="<?php echo $project && $project->start_date ? esc_attr($project->start_date) : esc_attr($prefill['start_date'] ?? ''); ?>">
                </div>
                
                <div class="fs-form-group">
                    <label for="due_date"><?php esc_html_e('Due Date', 'freelance-studio'); ?></label>
                    <input type="date" name="due_date" id="due_date"
                           value="<?php echo $project && $project->due_date ? esc_attr($project->due_date) : esc_attr($prefill['due_date'] ?? ''); ?>">
                </div>
            </div>
        </div>
        
        <!-- Project Services -->
        <div class="fs-form-section">
            <h2><?php esc_html_e('Project Services', 'freelance-studio'); ?></h2>
            <p class="description"><?php esc_html_e('Define the services and pricing for this project.', 'freelance-studio'); ?></p>
            
            <div id="fs-services-container">
                <?php if (!empty($services)): ?>
                    <?php foreach ($services as $index => $svc): 
                        $pricing_type = $svc['pricing_type'] ?? 'fixed';
                        $hourly_source = $svc['hourly_source'] ?? 'estimated';
                        $service_activity = $svc['activity'] ?? '';
                        $retainer_amount = $svc['retainer_amount'] ?? '';
                        $retainer_hours = $svc['retainer_hours'] ?? '';
                        $retainer_rollover = isset($svc['retainer_rollover']) ? (bool)$svc['retainer_rollover'] : true;
                        $retainer_overage_rate = $svc['retainer_overage_rate'] ?? '';
                        $hour_cap = $svc['hour_cap'] ?? '';
                        $hour_cap_overage_rate = $svc['hour_cap_overage_rate'] ?? '';
                        $hour_cap_notify_client = !empty($svc['hour_cap_notify_client']);
                        $hour_cap_notify_freelancer = !empty($svc['hour_cap_notify_freelancer']);
                        $hour_cap_warn_threshold = $svc['hour_cap_warn_threshold'] ?? '';
                        $has_hour_cap = !empty($hour_cap) && floatval($hour_cap) > 0;
                    ?>
                        <div class="fs-service-item" data-index="<?php echo  esc_attr( $index ); ?>">
                            <div class="fs-service-header">
                                <span class="fs-service-drag">☰</span>
                                <input type="text" name="services[<?php echo  esc_attr( $index ); ?>][name]" 
                                       class="fs-service-name-input" placeholder="<?php esc_attr_e('Service name', 'freelance-studio'); ?>"
                                       value="<?php echo esc_attr($svc['name'] ?? ''); ?>">
                                <button type="button" class="fs-remove-service" title="<?php esc_attr_e('Remove', 'freelance-studio'); ?>">×</button>
                            </div>
                            <div class="fs-service-body">
                                <textarea name="services[<?php echo  esc_attr( $index ); ?>][description]" 
                                          class="fs-service-description" rows="2"
                                          placeholder="<?php esc_attr_e('Description (optional)', 'freelance-studio'); ?>"><?php echo esc_textarea($svc['description'] ?? ''); ?></textarea>
                                <div class="fs-service-pricing">
                                    <select name="services[<?php echo  esc_attr( $index ); ?>][pricing_type]" class="fs-service-pricing-type">
                                        <option value="fixed" <?php selected($pricing_type, 'fixed'); ?>><?php esc_html_e('Flat fee', 'freelance-studio'); ?></option>
                                        <option value="hourly" <?php selected($pricing_type, 'hourly'); ?>><?php esc_html_e('Hourly', 'freelance-studio'); ?></option>
                                        <option value="retainer" <?php selected($pricing_type, 'retainer'); ?>><?php esc_html_e('Monthly Retainer', 'freelance-studio'); ?></option>
                                    </select>
                                    <div class="fs-hourly-options" style="<?php echo $pricing_type !== 'hourly' ? 'display:none;' : ''; ?>">
                                        <select name="services[<?php echo  esc_attr( $index ); ?>][hourly_source]" class="fs-hourly-source">
                                            <option value="estimated" <?php selected($hourly_source, 'estimated'); ?>><?php esc_html_e('Estimated hours', 'freelance-studio'); ?></option>
                                            <option value="tracked" <?php selected($hourly_source, 'tracked'); ?>><?php esc_html_e('From tracked time', 'freelance-studio'); ?></option>
                                        </select>
                                        <select name="services[<?php echo  esc_attr( $index ); ?>][activity]" class="fs-service-activity" style="<?php echo  esc_attr( $hourly_source !== 'tracked' ? 'display:none;' : '' ); ?>">
                                            <option value=""><?php esc_html_e('All activities', 'freelance-studio'); ?></option>
                                            <?php foreach ($activity_types as $act_key => $act_label): ?>
                                                <option value="<?php echo esc_attr($act_key); ?>" <?php selected($service_activity, $act_key); ?>>
                                                    <?php echo esc_html($act_label); ?>
                                                </option>
                                            <?php endforeach; ?>
                                        </select>
                                    </div>
                                    <!-- Standard pricing fields (hidden for retainer) -->
                                    <div class="fs-pricing-fields" style="<?php echo $pricing_type === 'retainer' ? 'display:none;' : ''; ?>">
                                        <input type="number" name="services[<?php echo  esc_attr( $index ); ?>][rate]" 
                                               class="fs-service-rate" step="0.01" min="0"
                                               placeholder="<?php esc_attr_e('Rate', 'freelance-studio'); ?>"
                                               value="<?php echo esc_attr($svc['rate'] ?? ''); ?>">
                                        <span class="fs-pricing-separator fs-qty-separator" style="<?php echo ($pricing_type === 'hourly' && $hourly_source === 'tracked') ? 'display:none;' : ''; ?>">×</span>
                                        <input type="number" name="services[<?php echo  esc_attr( $index ); ?>][quantity]" 
                                               class="fs-service-quantity" step="0.01" min="0"
                                               placeholder="<?php esc_attr_e('Qty', 'freelance-studio'); ?>"
                                               value="<?php echo esc_attr($svc['quantity'] ?? 1); ?>"
                                               style="<?php echo ($pricing_type === 'hourly' && $hourly_source === 'tracked') ? 'display:none;' : ''; ?>">
                                        <span class="fs-tracked-hours-note" style="<?php echo ($pricing_type === 'hourly' && $hourly_source === 'tracked') ? '' : 'display:none;'; ?>">
                                            <?php esc_html_e('× tracked time', 'freelance-studio'); ?>
                                        </span>
                                        <span class="fs-pricing-separator">=</span>
                                        <input type="number" name="services[<?php echo  esc_attr( $index ); ?>][amount]" 
                                               class="fs-service-amount" step="0.01" min="0" readonly
                                               value="<?php echo esc_attr($svc['amount'] ?? ''); ?>">
                                    </div>
                                    
                                    <!-- Hour Cap fields (for hourly tracked services) -->
                                    <div class="fs-hour-cap-fields" style="<?php echo ($pricing_type === 'hourly' && $hourly_source === 'tracked') ? '' : 'display:none;'; ?>">
                                        <label class="fs-checkbox-label fs-hour-cap-toggle">
                                            <input type="checkbox" name="services[<?php echo  esc_attr( $index ); ?>][hour_cap_enabled]" 
                                                   class="fs-hour-cap-checkbox" value="1" <?php checked($has_hour_cap); ?>>
                                            <?php esc_html_e('Cap my hours', 'freelance-studio'); ?>
                                        </label>
                                        <div class="fs-hour-cap-options" style="<?php echo $has_hour_cap ? '' : 'display:none;'; ?>">
                                            <div class="fs-hour-cap-row">
                                                <div class="fs-hour-cap-field">
                                                    <label><?php esc_html_e('Hour Limit', 'freelance-studio'); ?></label>
                                                    <div class="fs-hours-input">
                                                        <input type="number" name="services[<?php echo  esc_attr( $index ); ?>][hour_cap]" 
                                                               class="fs-hour-cap-input" step="0.5" min="0"
                                                               placeholder="10"
                                                               value="<?php echo esc_attr($hour_cap); ?>">
                                                        <span class="fs-hours-suffix"><?php esc_html_e('hrs', 'freelance-studio'); ?></span>
                                                    </div>
                                                </div>
                                                <div class="fs-hour-cap-field">
                                                    <label><?php esc_html_e('Overage Rate', 'freelance-studio'); ?></label>
                                                    <div class="fs-currency-input">
                                                        <span class="fs-currency-symbol">$</span>
                                                        <input type="number" name="services[<?php echo  esc_attr( $index ); ?>][hour_cap_overage_rate]" 
                                                               class="fs-hour-cap-overage-rate" step="0.01" min="0"
                                                               placeholder="<?php echo esc_attr($svc['rate'] ?? ''); ?>"
                                                               value="<?php echo esc_attr($hour_cap_overage_rate); ?>">
                                                        <span class="fs-rate-suffix">/hr</span>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="fs-hour-cap-notify-row">
                                                <label class="fs-checkbox-label">
                                                    <input type="checkbox" name="services[<?php echo  esc_attr( $index ); ?>][hour_cap_notify_freelancer]" 
                                                           class="fs-hour-cap-notify-freelancer-checkbox" value="1" <?php checked($hour_cap_notify_freelancer); ?>>
                                                    <?php esc_html_e('Notify me when cap is reached', 'freelance-studio'); ?>
                                                </label>
                                            </div>
                                            <div class="fs-hour-cap-notify-row">
                                                <label class="fs-checkbox-label">
                                                    <input type="checkbox" name="services[<?php echo  esc_attr( $index ); ?>][hour_cap_notify_client]" 
                                                           class="fs-hour-cap-notify-checkbox" value="1" <?php checked($hour_cap_notify_client); ?>>
                                                    <?php esc_html_e('Notify client when cap is reached', 'freelance-studio'); ?>
                                                </label>
                                                <?php 
                                                $notify_contact_id = $svc['hour_cap_notify_contact_id'] ?? 'primary';
                                                if (!$is_new && $project && $project->client_id):
                                                    $cap_contacts = FS_Client_Contact::get_by_client($project->client_id);
                                                    $cap_client = FS_Client::get($project->client_id);
                                                ?>
                                                <select name="services[<?php echo  esc_attr( $index ); ?>][hour_cap_notify_contact_id]" 
                                                        class="fs-hour-cap-contact-select"
                                                        style="<?php echo $hour_cap_notify_client ? '' : 'display:none;'; ?>">
                                                    <?php if ($cap_client && $cap_client->email): ?>
                                                        <option value="primary" <?php selected($notify_contact_id, 'primary'); ?>>
                                                            <?php echo esc_html(($cap_client->name ?: $cap_client->company_name) . ' — ' . $cap_client->email); ?>
                                                        </option>
                                                    <?php endif; ?>
                                                    <?php foreach ($cap_contacts as $cc): if (!empty($cc->email)): ?>
                                                        <option value="<?php echo esc_attr($cc->id); ?>" <?php selected($notify_contact_id, $cc->id); ?>>
                                                            <?php echo esc_html($cc->name . ' — ' . $cc->email); ?>
                                                        </option>
                                                    <?php endif; endforeach; ?>
                                                </select>
                                                <?php endif; ?>
                                            </div>
                                            <div class="fs-hour-cap-warn-row">
                                                <label class="fs-checkbox-label">
                                                    <input type="checkbox" class="fs-hour-cap-warn-checkbox" value="1" <?php checked(!empty($hour_cap_warn_threshold)); ?>>
                                                    <?php esc_html_e('Send early warning at', 'freelance-studio'); ?>
                                                </label>
                                                <div class="fs-hour-cap-warn-fields" style="<?php echo !empty($hour_cap_warn_threshold) ? '' : 'display:none;'; ?>">
                                                    <div class="fs-hours-input fs-warn-threshold-input">
                                                        <input type="number" name="services[<?php echo  esc_attr( $index ); ?>][hour_cap_warn_threshold]"
                                                               class="fs-hour-cap-warn-threshold" step="1" min="1" max="99"
                                                               placeholder="80"
                                                               value="<?php echo esc_attr($hour_cap_warn_threshold); ?>">
                                                        <span class="fs-hours-suffix">%</span>
                                                    </div>
                                                    <span class="fs-warn-label"><?php esc_html_e('of hours used', 'freelance-studio'); ?></span>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <!-- Retainer-specific fields (v3.9.0) -->
                                    <div class="fs-retainer-service-fields" style="<?php echo $pricing_type !== 'retainer' ? 'display:none;' : ''; ?>">
                                        <div class="fs-retainer-service-row">
                                            <div class="fs-retainer-field">
                                                <label><?php esc_html_e('Monthly Amount', 'freelance-studio'); ?></label>
                                                <div class="fs-currency-input">
                                                    <span class="fs-currency-symbol">$</span>
                                                    <input type="number" name="services[<?php echo  esc_attr( $index ); ?>][retainer_amount]" 
                                                           class="fs-retainer-amount" step="0.01" min="0"
                                                           placeholder="1500.00"
                                                           value="<?php echo esc_attr($retainer_amount); ?>">
                                                </div>
                                            </div>
                                            <div class="fs-retainer-field">
                                                <label><?php esc_html_e('Hours Included', 'freelance-studio'); ?></label>
                                                <div class="fs-hours-input">
                                                    <input type="number" name="services[<?php echo  esc_attr( $index ); ?>][retainer_hours]" 
                                                           class="fs-retainer-hours" step="0.5" min="0"
                                                           placeholder="10"
                                                           value="<?php echo esc_attr($retainer_hours); ?>">
                                                    <span class="fs-hours-suffix"><?php esc_html_e('hrs/mo', 'freelance-studio'); ?></span>
                                                </div>
                                            </div>
                                            <div class="fs-retainer-field">
                                                <label><?php esc_html_e('Overage Rate', 'freelance-studio'); ?></label>
                                                <div class="fs-currency-input">
                                                    <span class="fs-currency-symbol">$</span>
                                                    <input type="number" name="services[<?php echo  esc_attr( $index ); ?>][retainer_overage_rate]" 
                                                           class="fs-retainer-overage" step="0.01" min="0"
                                                           placeholder="150.00"
                                                           value="<?php echo esc_attr($retainer_overage_rate); ?>">
                                                    <span class="fs-rate-suffix">/hr</span>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="fs-retainer-options">
                                            <label class="fs-checkbox-label">
                                                <input type="checkbox" name="services[<?php echo  esc_attr( $index ); ?>][retainer_rollover]" 
                                                       class="fs-retainer-rollover" value="1" <?php checked($retainer_rollover); ?>>
                                                <?php esc_html_e('Allow unused hours to roll over to next month', 'freelance-studio'); ?>
                                            </label>
                                            <div class="fs-retainer-reset-day-row">
                                                <label><?php esc_html_e('Hours reset on the', 'freelance-studio'); ?></label>
                                                <select name="retainer_reset_day" class="fs-retainer-reset-day-select">
                                                    <option value=""><?php esc_html_e('— same day as project start —', 'freelance-studio'); ?></option>
                                                    <?php
                                                    $saved_reset_day = !$is_new && $project ? intval($project->retainer_reset_day ?? 0) : 0;
                                                    for ($d = 1; $d <= 28; $d++):
                                                    ?>
                                                    <option value="<?php echo  esc_attr( $d ); ?>" <?php selected($saved_reset_day, $d); ?>><?php echo  esc_attr( $d . __('th of each month', 'freelance-studio') ); ?></option>
                                                    <?php endfor; ?>
                                                </select>
                                                <span class="fs-reset-day-hint"><?php esc_html_e('(max 28 to handle all months)', 'freelance-studio'); ?></span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
            
            <div class="fs-services-actions">
                <button type="button" class="button fs-add-service-btn">➕ <?php esc_html_e('Add Service', 'freelance-studio'); ?></button>
                <span class="fs-services-actions-divider">|</span>
                <select id="fs-load-preset-service" class="fs-preset-service-select">
                    <option value=""><?php esc_html_e('— Load Preset Service —', 'freelance-studio'); ?></option>
                </select>
            </div>
            
            <div class="fs-services-total">
                <span><?php esc_html_e('Project Total:', 'freelance-studio'); ?></span>
                <span id="fs-services-total"><?php echo  esc_html( FS_View_Context::format_currency($project_total) ); ?></span>
            </div>
        </div>
        
        <!-- Billing Settings -->
        <div class="fs-form-section">
            <h2><?php esc_html_e('Billing', 'freelance-studio'); ?></h2>
            
            <div class="fs-form-row">
                <div class="fs-form-group">
                    <label for="billing_frequency"><?php esc_html_e('Billing Frequency', 'freelance-studio'); ?></label>
                    <select name="billing_frequency" id="billing_frequency">
                        <?php foreach ($billing_frequencies as $key => $label): ?>
                            <option value="<?php echo esc_attr($key); ?>" <?php selected($project ? ($project->billing_frequency ?? 'once') : 'once', $key); ?>>
                                <?php echo esc_html($label); ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                
                <div class="fs-form-group">
                    <label for="first_invoice_date"><?php esc_html_e('Date of 1st Invoice', 'freelance-studio'); ?></label>
                    <input type="date" name="first_invoice_date" id="first_invoice_date"
                           value="<?php echo $project && !empty($project->first_invoice_date) ? esc_attr($project->first_invoice_date) : ''; ?>">
                </div>
            </div>
            
            <!-- Deposit Section -->
            <div class="fs-deposit-section">
                <div class="fs-form-group">
                    <label class="fs-checkbox-label">
                        <input type="checkbox" name="deposit_required" id="deposit_required" value="1" <?php checked($deposit_required); ?>>
                        <?php esc_html_e('Deposit Required', 'freelance-studio'); ?>
                    </label>
                </div>
                
                <div class="fs-deposit-fields" style="<?php echo !$deposit_required ? 'display:none;' : ''; ?>">
                    <div class="fs-form-row">
                        <div class="fs-form-group">
                            <label><?php esc_html_e('Deposit Type', 'freelance-studio'); ?></label>
                            <select name="deposit_type" id="deposit_type">
                                <option value="percentage" <?php selected($deposit_type, 'percentage'); ?>><?php esc_html_e('Percentage of total', 'freelance-studio'); ?></option>
                                <option value="fixed" <?php selected($deposit_type, 'fixed'); ?>><?php esc_html_e('Fixed amount', 'freelance-studio'); ?></option>
                            </select>
                        </div>
                        
                        <div class="fs-form-group fs-deposit-percentage-field" style="<?php echo $deposit_type !== 'percentage' ? 'display:none;' : ''; ?>">
                            <label><?php esc_html_e('Percentage', 'freelance-studio'); ?></label>
                            <div class="fs-input-with-suffix">
                                <input type="number" name="deposit_percentage" id="deposit_percentage" step="1" min="0" max="100"
                                       value="<?php echo esc_attr($deposit_percentage); ?>">
                                <span class="fs-input-suffix">%</span>
                            </div>
                        </div>
                        
                        <div class="fs-form-group fs-deposit-amount-field" style="<?php echo $deposit_type !== 'fixed' ? 'display:none;' : ''; ?>">
                            <label><?php esc_html_e('Amount', 'freelance-studio'); ?></label>
                            <input type="number" name="deposit_fixed_amount" id="deposit_fixed_amount" step="0.01" min="0"
                                   value="<?php echo esc_attr($deposit_amount); ?>">
                        </div>
                    </div>
                    
                    <div class="fs-deposit-summary">
                        <span class="fs-deposit-label"><?php esc_html_e('Deposit Amount:', 'freelance-studio'); ?></span>
                        <span class="fs-deposit-value" id="deposit-amount-display"><?php echo  esc_html( FS_View_Context::format_currency($deposit_amount) ); ?></span>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- Notes -->
        <div class="fs-form-section">
            <h2><?php esc_html_e('Notes', 'freelance-studio'); ?></h2>
            <div class="fs-form-group">
                <textarea name="notes" id="notes" rows="4"><?php echo $project ? esc_textarea($project->notes) : ''; ?></textarea>
                <p class="description"><?php esc_html_e('Internal notes (not visible to client).', 'freelance-studio'); ?></p>
            </div>
        </div>
        
        <!-- Hidden fields for legacy support -->
        <input type="hidden" name="billing_type" value="fixed">
        <input type="hidden" name="fixed_price" id="hidden_fixed_price" value="<?php echo esc_attr($project_total); ?>">
        
        <p class="submit">
            <input type="submit" name="submit" class="fs-btn fs-btn-primary" value="<?php echo  esc_attr( $is_new ? __('Create Project', 'freelance-studio') : __('Update Project', 'freelance-studio') ); ?>">
            <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-projects') ); ?>" class="fs-btn fs-btn-secondary"><?php esc_html_e('Cancel', 'freelance-studio'); ?></a>
        </p>
    </form>
</div>

<!-- Service Item Template -->
<script type="text/template" id="fs-service-template">
    <div class="fs-service-item" data-index="{{index}}">
        <div class="fs-service-header">
            <span class="fs-service-drag">☰</span>
            <input type="text" name="services[{{index}}][name]" 
                   class="fs-service-name-input" placeholder="<?php esc_attr_e('Service name', 'freelance-studio'); ?>">
            <button type="button" class="fs-remove-service" title="<?php esc_attr_e('Remove', 'freelance-studio'); ?>">×</button>
        </div>
        <div class="fs-service-body">
            <textarea name="services[{{index}}][description]" 
                      class="fs-service-description" rows="2"
                      placeholder="<?php esc_attr_e('Description (optional)', 'freelance-studio'); ?>"></textarea>
            <div class="fs-service-pricing">
                <select name="services[{{index}}][pricing_type]" class="fs-service-pricing-type">
                    <option value="fixed"><?php esc_html_e('Flat fee', 'freelance-studio'); ?></option>
                    <option value="hourly"><?php esc_html_e('Hourly', 'freelance-studio'); ?></option>
                    <option value="retainer"><?php esc_html_e('Monthly Retainer', 'freelance-studio'); ?></option>
                </select>
                <div class="fs-hourly-options" style="display:none;">
                    <select name="services[{{index}}][hourly_source]" class="fs-hourly-source">
                        <option value="estimated"><?php esc_html_e('Estimated hours', 'freelance-studio'); ?></option>
                        <option value="tracked"><?php esc_html_e('From tracked time', 'freelance-studio'); ?></option>
                    </select>
                    <select name="services[{{index}}][activity]" class="fs-service-activity" style="display:none;">
                        <option value=""><?php esc_html_e('All activities', 'freelance-studio'); ?></option>
                        <?php foreach ($activity_types as $act_key => $act_label): ?>
                            <option value="<?php echo esc_attr($act_key); ?>"><?php echo esc_html($act_label); ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="fs-pricing-fields">
                    <input type="number" name="services[{{index}}][rate]" 
                           class="fs-service-rate" step="0.01" min="0"
                           placeholder="<?php esc_attr_e('Rate', 'freelance-studio'); ?>"
                           value="<?php echo esc_attr($default_hourly_rate); ?>">
                    <span class="fs-pricing-separator fs-qty-separator">×</span>
                    <input type="number" name="services[{{index}}][quantity]" 
                           class="fs-service-quantity" step="0.01" min="0"
                           placeholder="<?php esc_attr_e('Qty', 'freelance-studio'); ?>"
                           value="1">
                    <span class="fs-tracked-hours-note" style="display:none;">
                        <?php esc_html_e('× tracked time', 'freelance-studio'); ?>
                    </span>
                    <span class="fs-pricing-separator">=</span>
                    <input type="number" name="services[{{index}}][amount]" 
                           class="fs-service-amount" step="0.01" min="0" readonly
                           value="<?php echo esc_attr($default_hourly_rate); ?>">
                </div>
                
                <!-- Hour Cap fields (for hourly tracked services) -->
                <div class="fs-hour-cap-fields" style="display:none;">
                    <label class="fs-checkbox-label fs-hour-cap-toggle">
                        <input type="checkbox" name="services[{{index}}][hour_cap_enabled]" 
                               class="fs-hour-cap-checkbox" value="1">
                        <?php esc_html_e('Cap my hours', 'freelance-studio'); ?>
                    </label>
                    <div class="fs-hour-cap-options" style="display:none;">
                        <div class="fs-hour-cap-row">
                            <div class="fs-hour-cap-field">
                                <label><?php esc_html_e('Hour Limit', 'freelance-studio'); ?></label>
                                <div class="fs-hours-input">
                                    <input type="number" name="services[{{index}}][hour_cap]" 
                                           class="fs-hour-cap-input" step="0.5" min="0"
                                           placeholder="10">
                                    <span class="fs-hours-suffix"><?php esc_html_e('hrs', 'freelance-studio'); ?></span>
                                </div>
                            </div>
                            <div class="fs-hour-cap-field">
                                <label><?php esc_html_e('Overage Rate', 'freelance-studio'); ?></label>
                                <div class="fs-currency-input">
                                    <span class="fs-currency-symbol">$</span>
                                    <input type="number" name="services[{{index}}][hour_cap_overage_rate]" 
                                           class="fs-hour-cap-overage-rate" step="0.01" min="0"
                                           placeholder="">
                                    <span class="fs-rate-suffix">/hr</span>
                                </div>
                            </div>
                        </div>
                        <div class="fs-hour-cap-notify-row">
                            <label class="fs-checkbox-label">
                                <input type="checkbox" name="services[{{index}}][hour_cap_notify_freelancer]" 
                                       class="fs-hour-cap-notify-freelancer-checkbox" value="1">
                                <?php esc_html_e('Notify me when cap is reached', 'freelance-studio'); ?>
                            </label>
                        </div>
                        <div class="fs-hour-cap-notify-row">
                            <label class="fs-checkbox-label">
                                <input type="checkbox" name="services[{{index}}][hour_cap_notify_client]" 
                                       class="fs-hour-cap-notify-checkbox" value="1">
                                <?php esc_html_e('Notify client when cap is reached', 'freelance-studio'); ?>
                            </label>
                            <select name="services[{{index}}][hour_cap_notify_contact_id]" 
                                    class="fs-hour-cap-contact-select"
                                    style="display:none;">
                                <option value="primary"><?php esc_html_e('— Select contact —', 'freelance-studio'); ?></option>
                            </select>
                        </div>
                        <div class="fs-hour-cap-warn-row">
                            <label class="fs-checkbox-label">
                                <input type="checkbox" class="fs-hour-cap-warn-checkbox" value="1">
                                <?php esc_html_e('Send early warning at', 'freelance-studio'); ?>
                            </label>
                            <div class="fs-hour-cap-warn-fields" style="display:none;">
                                <div class="fs-hours-input fs-warn-threshold-input">
                                    <input type="number" name="services[{{index}}][hour_cap_warn_threshold]"
                                           class="fs-hour-cap-warn-threshold" step="1" min="1" max="99"
                                           placeholder="80">
                                    <span class="fs-hours-suffix">%</span>
                                </div>
                                <span class="fs-warn-label"><?php esc_html_e('of hours used', 'freelance-studio'); ?></span>
                            </div>
                        </div>
                    </div>
                </div>
                
                <!-- Retainer-specific fields (v3.9.0) -->
                <div class="fs-retainer-service-fields" style="display:none;">
                    <div class="fs-retainer-service-row">
                        <div class="fs-retainer-field">
                            <label><?php esc_html_e('Monthly Amount', 'freelance-studio'); ?></label>
                            <div class="fs-currency-input">
                                <span class="fs-currency-symbol">$</span>
                                <input type="number" name="services[{{index}}][retainer_amount]" 
                                       class="fs-retainer-amount" step="0.01" min="0"
                                       placeholder="1500.00">
                            </div>
                        </div>
                        <div class="fs-retainer-field">
                            <label><?php esc_html_e('Hours Included', 'freelance-studio'); ?></label>
                            <div class="fs-hours-input">
                                <input type="number" name="services[{{index}}][retainer_hours]" 
                                       class="fs-retainer-hours" step="0.5" min="0"
                                       placeholder="10">
                                <span class="fs-hours-suffix"><?php esc_html_e('hrs/mo', 'freelance-studio'); ?></span>
                            </div>
                        </div>
                        <div class="fs-retainer-field">
                            <label><?php esc_html_e('Overage Rate', 'freelance-studio'); ?></label>
                            <div class="fs-currency-input">
                                <span class="fs-currency-symbol">$</span>
                                <input type="number" name="services[{{index}}][retainer_overage_rate]" 
                                       class="fs-retainer-overage" step="0.01" min="0"
                                       placeholder="150.00">
                                <span class="fs-rate-suffix">/hr</span>
                            </div>
                        </div>
                    </div>
                    <div class="fs-retainer-options">
                        <label class="fs-checkbox-label">
                            <input type="checkbox" name="services[{{index}}][retainer_rollover]" 
                                   class="fs-retainer-rollover" value="1" checked>
                            <?php esc_html_e('Allow unused hours to roll over to next month', 'freelance-studio'); ?>
                        </label>
                        <div class="fs-retainer-reset-day-row">
                            <label><?php esc_html_e('Hours reset on the', 'freelance-studio'); ?></label>
                            <select name="retainer_reset_day" class="fs-retainer-reset-day-select">
                                <option value=""><?php esc_html_e('— same day as project start —', 'freelance-studio'); ?></option>
                                <?php for ($d = 1; $d <= 28; $d++): ?>
                                <option value="<?php echo  esc_attr( $d ); ?>"><?php echo  esc_attr( $d . __('th of each month', 'freelance-studio') ); ?></option>
                                <?php endfor; ?>
                            </select>
                            <span class="fs-reset-day-hint"><?php esc_html_e('(max 28 to handle all months)', 'freelance-studio'); ?></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</script>
