<?php
/**
 * Manual Income Form View
 *
 * @package Freelance_Studio
 */

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

$page_title = $is_new ? __('Add Other Income', 'freelance-studio') : __('Edit Other Income', 'freelance-studio');
?>

<?php if (empty($GLOBALS['fs_in_finances'])): ?>
<div class="wrap fs-wrap">
<?php endif; ?>
    <div class="fs-page-header">
        <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-finances&tab=income') ); ?>" class="fs-btn fs-btn-secondary fs-btn-small">
            <span class="dashicons dashicons-arrow-left-alt2"></span> <?php esc_html_e('Back', 'freelance-studio'); ?>
        </a>
        <h1><?php echo esc_html($page_title); ?></h1>
    </div>

    <?php if ($message === 'created'): ?>
        <div class="notice notice-success is-dismissible">
            <p><?php esc_html_e('Income entry created successfully.', 'freelance-studio'); ?></p>
        </div>
    <?php elseif ($message === 'duplicated'): ?>
        <div class="notice notice-success is-dismissible">
            <p><?php esc_html_e('Income entry duplicated. Update the details below and save.', 'freelance-studio'); ?></p>
        </div>
    <?php elseif ($message === 'updated'): ?>
        <div class="notice notice-success is-dismissible">
            <p><?php esc_html_e('Income entry updated successfully.', 'freelance-studio'); ?></p>
        </div>
    <?php elseif ($message === 'error'): ?>
        <div class="notice notice-error is-dismissible">
            <p><?php esc_html_e('An error occurred. Please try again.', 'freelance-studio'); ?></p>
        </div>
    <?php endif; ?>

    <form method="post" class="fs-form">
        <?php wp_nonce_field('fs_save_income', 'fs_income_nonce'); ?>
        <input type="hidden" name="action" value="<?php echo $is_new ? 'create' : 'update'; ?>">
        <?php if (!$is_new): ?>
            <input type="hidden" name="income_id" value="<?php echo esc_attr($income->id); ?>">
        <?php endif; ?>
        
        <div class="fs-form-grid">
            <div class="fs-form-main">
                <div class="fs-form-section">
                    <h3><?php esc_html_e('Income Details', 'freelance-studio'); ?></h3>
                    
                    <div class="fs-form-row">
                        <div class="fs-form-field">
                            <label for="source"><?php esc_html_e('Source', 'freelance-studio'); ?> <span class="required">*</span></label>
                            <input type="text" name="source" id="source" required
                                   value="<?php echo $is_new ? '' : esc_attr($income->source); ?>"
                                   placeholder="<?php esc_attr_e('e.g., Etsy Shop, Art Fair, Client Name', 'freelance-studio'); ?>">
                            <p class="description"><?php esc_html_e('Where did this income come from?', 'freelance-studio'); ?></p>
                        </div>
                    </div>
                    
                    <div class="fs-form-row fs-form-row-2">
                        <div class="fs-form-field">
                            <label for="amount"><?php esc_html_e('Amount', 'freelance-studio'); ?> <span class="required">*</span></label>
                            <div class="fs-input-group">
                                <span class="fs-input-prefix">$</span>
                                <input type="number" name="amount" id="amount" step="0.01" min="0" required
                                       value="<?php echo $is_new ? '' : esc_attr($income->amount); ?>">
                            </div>
                        </div>
                        
                        <div class="fs-form-field">
                            <label for="income_date"><?php esc_html_e('Date', 'freelance-studio'); ?> <span class="required">*</span></label>
                            <input type="date" name="income_date" id="income_date" required
                                   value="<?php echo  esc_attr( $is_new ? gmdate('Y-m-d') : esc_attr($income->income_date) ); ?>">
                        </div>
                    </div>
                    
                    <div class="fs-form-row fs-form-row-2">
                        <div class="fs-form-field">
                            <label for="category"><?php esc_html_e('Category', 'freelance-studio'); ?></label>
                            <select name="category" id="category">
                                <?php foreach ($categories as $key => $label): ?>
                                    <option value="<?php echo esc_attr($key); ?>" 
                                            <?php selected(!$is_new && $income->category === $key); ?>>
                                        <?php echo esc_html($label); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        
                        <div class="fs-form-field">
                            <label for="reference"><?php esc_html_e('Reference/Order #', 'freelance-studio'); ?></label>
                            <input type="text" name="reference" id="reference"
                                   value="<?php echo $is_new ? '' : esc_attr($income->reference); ?>"
                                   placeholder="<?php esc_attr_e('e.g., Order #12345, Check #456', 'freelance-studio'); ?>">
                        </div>
                    </div>
                    
                    <div class="fs-form-row">
                        <div class="fs-form-field">
                            <label for="description"><?php esc_html_e('Description', 'freelance-studio'); ?></label>
                            <input type="text" name="description" id="description"
                                   value="<?php echo $is_new ? '' : esc_attr($income->description); ?>"
                                   placeholder="<?php esc_attr_e('Brief description of the income', 'freelance-studio'); ?>">
                        </div>
                    </div>
                    
                    <div class="fs-form-row">
                        <div class="fs-form-field">
                            <label for="notes"><?php esc_html_e('Notes', 'freelance-studio'); ?></label>
                            <textarea name="notes" id="notes" rows="3"
                                      placeholder="<?php esc_attr_e('Any additional notes...', 'freelance-studio'); ?>"><?php echo $is_new ? '' : esc_textarea($income->notes); ?></textarea>
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="fs-form-sidebar">
                <div class="fs-form-section">
                    <h3><?php esc_html_e('Actions', 'freelance-studio'); ?></h3>
                    <button type="submit" class="fs-btn fs-btn-primary fs-full-width">
                        <?php echo  esc_html( $is_new ? __('Add Income', 'freelance-studio') : __('Update Income', 'freelance-studio') ); ?>
                    </button>
                    
                    <?php if (!$is_new): ?>
                        <hr style="margin: 15px 0;">
                        <a href="<?php echo esc_url( wp_nonce_url( admin_url('admin.php?page=fs-finances&tab=income&delete=' . $income->id), 'delete_income_' . $income->id) ); ?>" 
                           class="fs-btn fs-btn-danger fs-full-width"
                           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>
                    <?php endif; ?>
                </div>
                
                <div class="fs-form-section">
                    <h3><?php esc_html_e('Quick Categories', 'freelance-studio'); ?></h3>
                    <div class="fs-quick-categories" style="display: flex; flex-wrap: wrap; gap: 5px;">
                        <?php foreach (array_slice($categories, 0, 6) as $key => $label): ?>
                            <button type="button" class="fs-btn fs-btn-small fs-btn-secondary fs-category-quick" 
                                    data-category="<?php echo esc_attr($key); ?>">
                                <?php echo esc_html($label); ?>
                            </button>
                        <?php endforeach; ?>
                    </div>
                </div>
            </div>
        </div>
    </form>
<?php if (empty($GLOBALS['fs_in_finances'])): ?>
</div>
<?php endif; ?>

<script>
jQuery(document).ready(function($) {
    // Quick category buttons
    $('.fs-category-quick').on('click', function() {
        $('#category').val($(this).data('category'));
    });
});
</script>
