<?php
/**
 * Admin Bug Reports 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();
$bug_reports = $database->get_bug_reports(null, 100);
?>

<div class="wrap">
	<h1><?php esc_html_e('Bug Reports', 'askany'); ?></h1>
	<p class="description"><?php esc_html_e('Manage bug reports 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( 'Bug Reports is a Pro Feature', 'askany' ); ?></h4>
						<p><?php esc_html_e( 'Collect and manage bug reports 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('Fixed', 'askany'); ?> <span class="count" id="count-completed">0</span></button>
			<button class="filter-tab" data-status="rejected"><?php esc_html_e('Won\'t Fix', 'askany'); ?> <span class="count" id="count-rejected">0</span></button>
		</div>

		<!-- Bug Reports Table -->
		<div class="askany-table-container <?php echo ! $is_pro ? 'askany-pro-locked-form' : ''; ?>">
			<table class="wp-list-table widefat fixed striped askany-reports-table">
				<thead>
					<tr>
						<th class="column-id"><?php esc_html_e('ID', 'askany'); ?></th>
						<th class="column-title"><?php esc_html_e('Bug 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-severity"><?php esc_html_e('Severity', 'askany'); ?></th>
						<th class="column-date"><?php esc_html_e('Reported', 'askany'); ?></th>
						<th class="column-actions"><?php esc_html_e('Actions', 'askany'); ?></th>
					</tr>
				</thead>
				<tbody id="bug-reports-tbody">
					<?php if ( empty($bug_reports) ) : ?>
						<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="M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"></path>
										<line x1="12" y1="9" x2="12" y2="13"></line>
										<line x1="12" y1="17" x2="12.01" y2="17"></line>
									</svg>
									<p><?php esc_html_e('No bug reports yet.', 'askany'); ?></p>
									<p class="description"><?php esc_html_e('Bug reports submitted by users will appear here.', 'askany'); ?></p>
								</div>
							</td>
						</tr>
					<?php else : ?>
						<?php foreach ( $bug_reports as $report ) : ?>
							<tr data-id="<?php echo esc_attr($report->id); ?>" data-status="<?php echo esc_attr($report->status); ?>">
								<td class="column-id">
									<strong><?php echo esc_html($report->unique_id ?? '#' . $report->id); ?></strong>
								</td>
								<td class="column-title">
									<strong><?php echo esc_html($report->bug_title); ?></strong>
								</td>
								<td class="column-user">
									<?php if ( $report->user_name ) : ?>
										<div class="user-info">
											<strong><?php echo esc_html($report->user_name); ?></strong>
											<?php if ( $report->user_email ) : ?>
												<br><a href="mailto:<?php echo esc_attr($report->user_email); ?>"><?php echo esc_html($report->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($report->status); ?>">
										<?php 
										$status_label = $report->status === 'completed' ? 'Fixed' : ($report->status === 'rejected' ? 'Won\'t Fix' : ucfirst(str_replace('-', ' ', $report->status)));
										echo esc_html($status_label); 
										?>
									</span>
								</td>
								<td class="column-severity">
									<span class="severity-badge severity-<?php echo esc_attr($report->severity); ?>">
										<?php echo esc_html(ucfirst($report->severity)); ?>
									</span>
								</td>
								<td class="column-date">
									<?php echo esc_html(date_i18n(get_option('date_format'), strtotime($report->created_at))); ?>
								</td>
								<td class="column-actions">
									<button class="button button-small view-details" data-id="<?php echo esc_attr($report->id); ?>">
										<?php esc_html_e('View', 'askany'); ?>
									</button>
									<button class="button button-small button-link-delete delete-report" data-id="<?php echo esc_attr($report->id); ?>">
										<?php esc_html_e('Delete', 'askany'); ?>
									</button>
								</td>
							</tr>
						<?php endforeach; ?>
					<?php endif; ?>
				</tbody>
			</table>
		</div>
	</div>
</div>

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