<?php
/**
 * Expense Form View
 */
if (!defined('ABSPATH')) exit;
$page_title = $is_new ? __('Add Expense', 'freelance-studio') : __('Edit Expense', 'freelance-studio');
$receipt_info = !$is_new ? FS_Expense::get_receipt_info($expense->id) : false;
$recurring_frequencies = FS_Expense::get_recurring_frequencies();
?>
<?php if (empty($GLOBALS['fs_in_finances'])): ?>
<div class="wrap fs-wrap">
<?php endif; ?>
    <h1><?php echo esc_html($page_title); ?></h1>
    
    <form method="post" enctype="multipart/form-data">
        <?php wp_nonce_field('fs_save_expense', 'fs_expense_nonce'); ?>
        <input type="hidden" name="action" value="<?php echo $is_new ? 'create' : 'update'; ?>">
        <?php if (!$is_new): ?><input type="hidden" name="expense_id" value="<?php echo  intval( $expense->id ); ?>"><?php endif; ?>
        <?php if (!empty($selected_project_id)): ?><input type="hidden" name="from_project" value="<?php echo intval($selected_project_id); ?>"><?php endif; ?>
        
        <div class="fs-form-section">
            <h2><?php esc_html_e('Expense Details', 'freelance-studio'); ?></h2>
            
            <div class="fs-form-row">
                <div class="fs-form-group required">
                    <label for="expense_date"><?php esc_html_e('Date', 'freelance-studio'); ?></label>
                    <input type="date" name="expense_date" id="expense_date" required value="<?php echo  esc_attr( $expense ? esc_attr($expense->expense_date) : gmdate('Y-m-d') ); ?>">
                </div>
                
                <div class="fs-form-group required">
                    <label for="amount"><?php esc_html_e('Amount', 'freelance-studio'); ?></label>
                    <input type="number" name="amount" id="amount" step="0.01" min="0" required value="<?php echo $expense ? esc_attr($expense->amount) : ''; ?>">
                </div>
                
                <div class="fs-form-group required">
                    <label for="category"><?php esc_html_e('Category', 'freelance-studio'); ?></label>
                    <select name="category" id="category" required>
                        <option value=""><?php esc_html_e('Select...', 'freelance-studio'); ?></option>
                        <?php foreach ($categories as $k => $l): ?>
                            <option value="<?php echo esc_attr($k); ?>" <?php selected($expense ? $expense->category : '', $k); ?>><?php echo esc_html($l); ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
            </div>
            
            <div class="fs-form-group required">
                <label for="expense_name"><?php esc_html_e('Description', 'freelance-studio'); ?></label>
                <input type="text" name="expense_name" id="expense_name" required value="<?php echo $expense ? esc_attr($expense->expense_name) : ''; ?>" placeholder="<?php esc_attr_e('e.g., Adobe Creative Cloud, Dropbox Plus', 'freelance-studio'); ?>">
            </div>
            
            <div class="fs-form-row">
                <div class="fs-form-group">
                    <label for="project_id"><?php esc_html_e('Project (optional)', 'freelance-studio'); ?></label>
                    <select name="project_id" id="project_id">
                        <option value=""><?php esc_html_e('No project', 'freelance-studio'); ?></option>
                        <?php foreach ($projects as $p): ?>
                            <option value="<?php echo  intval( $p->id ); ?>" <?php selected($expense ? $expense->project_id : ($selected_project_id ?? 0), $p->id); ?>><?php echo esc_html($p->name); ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                
                <div class="fs-form-group">
                    <label><input type="checkbox" name="is_billable" value="1" <?php checked($expense ? $expense->is_billable : 0, 1); ?>> <?php esc_html_e('Billable to client', 'freelance-studio'); ?></label>
                </div>
                
                <div class="fs-form-group">
                    <label><input type="checkbox" name="is_reimbursable" value="1" <?php checked($expense ? $expense->is_reimbursable : 0, 1); ?>> <?php esc_html_e('Reimbursable', 'freelance-studio'); ?></label>
                </div>
            </div>
            
            <div class="fs-form-group">
                <label for="notes"><?php esc_html_e('Notes', 'freelance-studio'); ?></label>
                <textarea name="notes" id="notes" rows="3"><?php echo $expense ? esc_textarea($expense->notes) : ''; ?></textarea>
            </div>
        </div>
        
        <!-- Recurring Expense Section -->
        <div class="fs-form-section">
            <h2>🔄 <?php esc_html_e('Recurring Expense', 'freelance-studio'); ?></h2>
            <p class="description"><?php esc_html_e('Set up automatic tracking for subscriptions like Adobe, hosting, or other regular expenses.', 'freelance-studio'); ?></p>
            
            <?php if ( function_exists( 'fs_studio_is_free_plan' ) && fs_studio_is_free_plan() ): ?>
            <div class="fs-pro-overlay-wrapper fs-pro-overlay-compact">
                <div class="fs-pro-overlay">
                    <div class="fs-pro-overlay-content">
                        <span class="fs-upgrade-badge">PRO</span>
                        <p><?php esc_html_e( 'Recurring expenses are available with Freelance Studio Pro.', 'freelance-studio' ); ?></p>
                        <a href="<?php echo function_exists( 'freelance_studio_fs' ) ? esc_url( freelance_studio_fs()->get_upgrade_url() ) : '#'; ?>" class="fs-btn fs-btn-primary fs-btn-small">
                            <?php esc_html_e( 'Upgrade to Pro', 'freelance-studio' ); ?>
                        </a>
                    </div>
                </div>
                <div class="fs-form-group" aria-hidden="true" style="opacity:0.5; pointer-events:none;">
                    <label>
                        <input type="checkbox" disabled>
                        <?php esc_html_e('This is a recurring expense', 'freelance-studio'); ?>
                    </label>
                </div>
            </div>
            <?php else: ?>
            <div class="fs-form-group">
                <label>
                    <input type="checkbox" name="is_recurring" id="is_recurring" value="1" <?php checked($expense && $expense->is_recurring, 1); ?>>
                    <?php esc_html_e('This is a recurring expense', 'freelance-studio'); ?>
                </label>
            </div>
            
            <div id="fs-recurring-options" style="<?php echo ($expense && $expense->is_recurring) ? '' : 'display: none;'; ?>">
                <div class="fs-form-row">
                    <div class="fs-form-group">
                        <label for="recurring_frequency"><?php esc_html_e('Frequency', 'freelance-studio'); ?></label>
                        <select name="recurring_frequency" id="recurring_frequency">
                            <?php foreach ($recurring_frequencies as $key => $label): ?>
                                <option value="<?php echo esc_attr($key); ?>" <?php selected($expense && $expense->recurring_frequency ? $expense->recurring_frequency : 'monthly', $key); ?>><?php echo esc_html($label); ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    
                    <div class="fs-form-group">
                        <label for="recurring_day"><?php esc_html_e('Day of Month', 'freelance-studio'); ?></label>
                        <select name="recurring_day" id="recurring_day">
                            <option value=""><?php esc_html_e('Same as start date', 'freelance-studio'); ?></option>
                            <?php for ($i = 1; $i <= 28; $i++): ?>
                                <option value="<?php echo  esc_attr( $i ); ?>" <?php selected($expense && $expense->recurring_day ? $expense->recurring_day : '', $i); ?>><?php echo  esc_attr( $i ); ?></option>
                            <?php endfor; ?>
                            <option value="31" <?php selected($expense && $expense->recurring_day ? $expense->recurring_day : '', 31); ?>><?php esc_html_e('Last day of month', 'freelance-studio'); ?></option>
                        </select>
                    </div>
                    
                    <div class="fs-form-group">
                        <label for="recurring_end_date"><?php esc_html_e('End Date (optional)', 'freelance-studio'); ?></label>
                        <input type="date" name="recurring_end_date" id="recurring_end_date" value="<?php echo $expense && $expense->recurring_end_date ? esc_attr($expense->recurring_end_date) : ''; ?>">
                        <p class="description"><?php esc_html_e('Leave blank to continue indefinitely', 'freelance-studio'); ?></p>
                    </div>
                </div>
            </div>
            <?php endif; ?>
        </div>
        
        <!-- Receipt Upload Section -->
        <div class="fs-form-section">
            <h2>🧾 <?php esc_html_e('Receipt Attachment', 'freelance-studio'); ?></h2>
            
            <?php if ($receipt_info): ?>
                <div class="fs-receipt-current">
                    <p><strong><?php esc_html_e('Current Receipt:', 'freelance-studio'); ?></strong> 
                        <a href="<?php echo esc_url($receipt_info['file_url']); ?>" target="_blank">
                            <?php echo esc_html($receipt_info['filename']); ?>
                        </a>
                        <span class="text-muted">(<?php echo  esc_html( FS_File_Handler::format_file_size($receipt_info['file_size']) ); ?>)</span>
                    </p>
                    
                    <?php if (FS_File_Handler::is_image($receipt_info['mime_type'])): ?>
                        <div class="fs-receipt-preview">
                            <a href="<?php echo esc_url($receipt_info['file_url']); ?>" target="_blank">
                                <img src="<?php echo esc_url($receipt_info['file_url']); ?>" alt="<?php esc_attr_e('Receipt', 'freelance-studio'); ?>">
                            </a>
                        </div>
                    <?php endif; ?>
                    
                    <div class="fs-receipt-actions">
                        <label>
                            <input type="checkbox" name="delete_receipt" value="1">
                            <?php esc_html_e('Delete this receipt', 'freelance-studio'); ?>
                        </label>
                    </div>
                </div>
                
                <hr class="fs-hr">
                <p class="description"><?php esc_html_e('Upload a new file to replace the current receipt:', 'freelance-studio'); ?></p>
            <?php endif; ?>
            
            <div class="fs-form-group">
                <label for="receipt_file"><?php esc_html_e('Upload Receipt', 'freelance-studio'); ?></label>
                <div class="fs-file-upload">
                    <input type="file" 
                           name="receipt_file" 
                           id="receipt_file" 
                           accept=".jpg,.jpeg,.png,.gif,.pdf,.doc,.docx">
                    <p class="description">
                        <?php esc_html_e('Accepted file types: JPG, PNG, GIF, PDF, DOC, DOCX. Maximum size: 10MB.', 'freelance-studio'); ?>
                    </p>
                </div>
            </div>
        </div>
        
        <p class="submit">
            <input type="submit" class="fs-btn fs-btn-submit" value="<?php echo  esc_attr( $is_new ? __('Add Expense', 'freelance-studio') : __('Update Expense', 'freelance-studio') ); ?>">
            <a href="<?php echo  esc_url( admin_url('admin.php?page=fs-finances&tab=expenses') ); ?>" class="fs-btn fs-btn-secondary"><?php esc_html_e('Cancel', 'freelance-studio'); ?></a>
        </p>
    </form>
<?php if (empty($GLOBALS['fs_in_finances'])): ?>
</div>
<?php endif; ?>

<script>
jQuery(document).ready(function($) {
    $('#is_recurring').on('change', function() {
        if ($(this).is(':checked')) {
            $('#fs-recurring-options').slideDown(200);
        } else {
            $('#fs-recurring-options').slideUp(200);
        }
    });
});
</script>
