<?php
/**
 * Admin Feature Requests Page Template
 *
 * @package AskAny
 */

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

// Check pro status
$plugin = \Askany\Plugin::get_instance();
$is_pro = $plugin->is_pro_active();

// Get database instance
$database = new AskAny\Database();
$feature_requests = $database->get_feature_requests(null, 100);
?>

<div class="wrap">
	<h1><?php esc_html_e('Feature Requests', 'askany'); ?></h1>
	<p class="description"><?php esc_html_e('Manage feature requests submitted by users through the chat widget.', 'askany'); ?></p>

	<div class="card <?php echo ! $is_pro ? 'askany-pro-locked' : ''; ?>">
		<?php if ( ! $is_pro ) : ?>
			<div class="askany-pro-overlay">
				<div class="askany-pro-notice-small">
					<div class="askany-pro-notice-icon">
						<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
							<rect x="3" y="11" width="18" height="11" rx="2" ry="2"></rect>
							<path d="M7 11V7a5 5 0 0 1 10 0v4"></path>
						</svg>
					</div>
					<div class="askany-pro-notice-content">
						<h4><?php esc_html_e( 'Feature Requests is a Pro Feature', 'askany' ); ?></h4>
						<p><?php esc_html_e( 'Collect and manage feature requests from your users directly through the chat widget.', 'askany' ); ?></p>
						<button type="button" class="button button-primary askany-open-pro-modal">
							<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="currentColor" style="margin-right: 6px;">
								<polygon points="12 2 15.09 8.26 22 9.27 17 14.14 18.18 21.02 12 17.77 5.82 21.02 7 14.14 2 9.27 8.91 8.26 12 2"></polygon>
							</svg>
							<?php esc_html_e( 'Upgrade to Pro', 'askany' ); ?>
						</button>
					</div>
				</div>
			</div>
		<?php endif; ?>
		
		<!-- Filter Tabs -->
		<div class="askany-filter-tabs <?php echo ! $is_pro ? 'askany-pro-locked-form' : ''; ?>">
			<button class="filter-tab active" data-status="all"><?php esc_html_e('All', 'askany'); ?> <span class="count" id="count-all">0</span></button>
			<button class="filter-tab" data-status="pending"><?php esc_html_e('Pending', 'askany'); ?> <span class="count" id="count-pending">0</span></button>
			<button class="filter-tab" data-status="in-progress"><?php esc_html_e('In Progress', 'askany'); ?> <span class="count" id="count-in-progress">0</span></button>
			<button class="filter-tab" data-status="completed"><?php esc_html_e('Completed', 'askany'); ?> <span class="count" id="count-completed">0</span></button>
			<button class="filter-tab" data-status="rejected"><?php esc_html_e('Rejected', 'askany'); ?> <span class="count" id="count-rejected">0</span></button>
		</div>

		<!-- Feature Requests Table -->
		<div class="askany-table-container <?php echo ! $is_pro ? 'askany-pro-locked-form' : ''; ?>">
			<table class="wp-list-table widefat fixed striped askany-requests-table">
				<thead>
					<tr>
						<th class="column-id"><?php esc_html_e('ID', 'askany'); ?></th>
						<th class="column-title"><?php esc_html_e('Feature Title', 'askany'); ?></th>
						<th class="column-user"><?php esc_html_e('User', 'askany'); ?></th>
						<th class="column-status"><?php esc_html_e('Status', 'askany'); ?></th>
						<th class="column-priority"><?php esc_html_e('Priority', 'askany'); ?></th>
						<th class="column-date"><?php esc_html_e('Submitted', 'askany'); ?></th>
						<th class="column-actions"><?php esc_html_e('Actions', 'askany'); ?></th>
					</tr>
				</thead>
				<tbody id="feature-requests-tbody">
					<?php if ( empty($feature_requests) ) : ?>
						<tr class="no-items">
							<td colspan="7" class="colspanchange">
								<div class="askany-empty-state">
									<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
										<path d="M12 2v20M2 12h20"></path>
									</svg>
									<p><?php esc_html_e('No feature requests yet.', 'askany'); ?></p>
									<p class="description"><?php esc_html_e('Feature requests submitted by users will appear here.', 'askany'); ?></p>
								</div>
							</td>
						</tr>
					<?php else : ?>
						<?php foreach ( $feature_requests as $request ) : ?>
							<tr data-id="<?php echo esc_attr($request->id); ?>" data-status="<?php echo esc_attr($request->status); ?>">
								<td class="column-id">
									<strong><?php echo esc_html($request->unique_id ?? '#' . $request->id); ?></strong>
								</td>
								<td class="column-title">
									<strong><?php echo esc_html($request->feature_title); ?></strong>
								</td>
								<td class="column-user">
									<?php if ( $request->user_name ) : ?>
										<div class="user-info">
											<strong><?php echo esc_html($request->user_name); ?></strong>
											<?php if ( $request->user_email ) : ?>
												<br><a href="mailto:<?php echo esc_attr($request->user_email); ?>"><?php echo esc_html($request->user_email); ?></a>
											<?php endif; ?>
										</div>
									<?php else : ?>
										<span class="anonymous"><?php esc_html_e('Anonymous', 'askany'); ?></span>
									<?php endif; ?>
								</td>
								<td class="column-status">
									<span class="status-badge status-<?php echo esc_attr($request->status); ?>">
										<?php echo esc_html(ucfirst(str_replace('-', ' ', $request->status))); ?>
									</span>
								</td>
								<td class="column-priority">
									<span class="priority-badge priority-<?php echo esc_attr($request->priority); ?>">
										<?php echo esc_html(ucfirst($request->priority)); ?>
									</span>
								</td>
								<td class="column-date">
									<?php echo esc_html(date_i18n(get_option('date_format'), strtotime($request->created_at))); ?>
								</td>
								<td class="column-actions">
									<button class="button button-small view-details" data-id="<?php echo esc_attr($request->id); ?>">
										<?php esc_html_e('View', 'askany'); ?>
									</button>
									<button class="button button-small button-link-delete delete-request" data-id="<?php echo esc_attr($request->id); ?>">
										<?php esc_html_e('Delete', 'askany'); ?>
									</button>
								</td>
							</tr>
						<?php endforeach; ?>
					<?php endif; ?>
				</tbody>
			</table>
		</div>
	</div>
</div>

<!-- Feature Request Details Modal -->
<div id="feature-request-modal" class="askany-modal" style="display: none;">
	<div class="askany-modal-content">
		<div class="askany-modal-header">
			<h3><?php esc_html_e('Feature Request Details', 'askany'); ?></h3>
			<button class="askany-modal-close">&times;</button>
		</div>
		<div class="askany-modal-body" id="feature-request-details">
			<!-- Content loaded via JavaScript -->
		</div>
	</div>
</div>
