<?php
/**
 * Other Income List View
 *
 * @package Freelance_Studio
 */

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

<?php if (empty($GLOBALS['fs_in_finances'])): ?>
<div class="wrap fs-wrap">
<?php endif; ?>
    <div class="fs-list-header">
        <h1><?php esc_html_e('Invoices', 'freelance-studio'); ?></h1>
        <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-finances&tab=income&action=add') ); ?>" class="fs-btn fs-btn-primary">
            <?php esc_html_e('Add Other Income', 'freelance-studio'); ?>
        </a>
    </div>
    <?php if ($message === 'deleted'): ?>
        <div class="notice notice-success is-dismissible">
            <p><?php esc_html_e('Income entry deleted successfully.', 'freelance-studio'); ?></p>
        </div>
    <?php elseif ($message === 'duplicated'): ?>
        <div class="notice notice-success is-dismissible">
            <p><?php esc_html_e('Income entry duplicated. You are now editing the copy.', 'freelance-studio'); ?></p>
        </div>
    <?php elseif ($message === 'not_found'): ?>
        <div class="notice notice-error is-dismissible">
            <p><?php esc_html_e('Income entry not found.', 'freelance-studio'); ?></p>
        </div>
    <?php endif; ?>
    
    <?php if ($total > 0): ?>
        <div class="fs-notice info">
            <strong><?php esc_html_e('Total:', 'freelance-studio'); ?></strong> <?php echo  esc_html( FS_View_Context::format_currency($total) ); ?>
            <span style="margin-left: 10px; color: var(--fs-gray-600);">(<?php echo  esc_html( count($income_entries) ); ?> <?php echo  esc_html( _n('entry', 'entries', count($income_entries), 'freelance-studio') ); ?>)</span>
        </div>
    <?php endif; ?>
    
    <!-- Filters -->
    <div class="fs-filters">
        <form method="get">
            <input type="hidden" name="page" value="fs-finances">
            <input type="hidden" name="tab" value="invoices">
            <input type="hidden" name="action" value="income">
            <select name="category">
                <option value=""><?php esc_html_e('All Categories', 'freelance-studio'); ?></option>
                <?php foreach ($categories as $key => $label): ?>
                    <option value="<?php echo esc_attr($key); ?>" <?php selected($filters['category'], $key); ?>>
                        <?php echo esc_html($label); ?>
                    </option>
                <?php endforeach; ?>
            </select>
            <input type="date" name="start_date" value="<?php echo esc_attr($filters['start_date']); ?>" placeholder="<?php esc_attr_e('From', 'freelance-studio'); ?>">
            <input type="date" name="end_date" value="<?php echo esc_attr($filters['end_date']); ?>" placeholder="<?php esc_attr_e('To', 'freelance-studio'); ?>">
            <button type="submit" class="button"><?php esc_html_e('Filter', 'freelance-studio'); ?></button>
        </form>
    </div>

    <?php if (empty($income_entries)): ?>
        <div class="fs-empty-state">
            <div class="fs-empty-state-icon">💰</div>
            <h3><?php esc_html_e('No other income entries yet', 'freelance-studio'); ?></h3>
            <p><?php esc_html_e('Track income from Etsy, art fairs, commissions, and other non-invoice sources.', 'freelance-studio'); ?></p>
            <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-finances&tab=income&action=add') ); ?>" class="button button-primary"><?php esc_html_e('Add Income Entry', 'freelance-studio'); ?></a>
        </div>
    <?php else: ?>
        <div class="fs-form-section">
            <div class="fs-table-responsive">
                <table class="fs-table widefat">
                    <thead>
                        <tr>
                            <th><?php esc_html_e('Date', 'freelance-studio'); ?></th>
                            <th><?php esc_html_e('Source', 'freelance-studio'); ?></th>
                            <th><?php esc_html_e('Category', 'freelance-studio'); ?></th>
                            <th><?php esc_html_e('Reference', 'freelance-studio'); ?></th>
                            <th class="text-right"><?php esc_html_e('Amount', 'freelance-studio'); ?></th>
                            <th class="column-actions"></th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($income_entries as $entry): ?>
                            <tr>
                                <td>
                                    <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-finances&tab=income&action=edit&id=' . $entry->id) ); ?>">
                                        <?php echo esc_html(gmdate('M j, Y', strtotime($entry->income_date))); ?>
                                    </a>
                                </td>
                                <td>
                                    <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-finances&tab=income&action=edit&id=' . $entry->id) ); ?>">
                                        <strong><?php echo esc_html($entry->source); ?></strong>
                                    </a>
                                    <?php if ($entry->description): ?>
                                        <br><small style="color: var(--fs-gray-500);"><?php echo esc_html($entry->description); ?></small>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <span class="fs-status-badge status-income-<?php echo esc_attr($entry->category); ?>">
                                        <?php echo esc_html($categories[$entry->category] ?? ucfirst($entry->category)); ?>
                                    </span>
                                </td>
                                <td>
                                    <?php if ($entry->reference): ?>
                                        <code style="background: var(--fs-gray-100); padding: 2px 6px; border-radius: 3px; font-size: 12px;"><?php echo esc_html($entry->reference); ?></code>
                                    <?php else: ?>
                                        <span style="color: var(--fs-gray-400);">—</span>
                                    <?php endif; ?>
                                </td>
                                <td class="text-right">
                                    <strong style="color: var(--fs-primary-dark);"><?php echo  esc_html( FS_View_Context::format_currency($entry->amount) ); ?></strong>
                                </td>
                                <td class="column-actions">
                                    <div class="fs-row-menu">
                                        <button type="button" class="fs-row-menu-trigger">⋯</button>
                                        <div class="fs-row-menu-dropdown">
                                            <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-finances&tab=income&action=edit&id=' . $entry->id) ); ?>">
                                                <?php esc_html_e('Edit', 'freelance-studio'); ?>
                                            </a>
                                            <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-finances&tab=income&action=duplicate&id=' . $entry->id) ); ?>">
                                                <?php esc_html_e('Duplicate', 'freelance-studio'); ?>
                                            </a>
                                            <hr>
                                            <a href="<?php echo esc_url( wp_nonce_url( admin_url('admin.php?page=fs-finances&tab=income&delete=' . $entry->id), 'delete_income_' . $entry->id) ); ?>" 
                                               class="text-danger" 
                                               onclick="return confirm('<?php esc_html_e('Are you sure you want to delete this income entry?', 'freelance-studio'); ?>')">
                                                <?php esc_html_e('Delete', 'freelance-studio'); ?>
                                            </a>
                                        </div>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    <?php endif; ?>
<?php if (empty($GLOBALS['fs_in_finances'])): ?>
</div>
<?php endif; ?>
