<?php
/**
 * Admin PDF import template for Ask Any plugin
 *
 * This template displays the PDF import interface with:
 * - PDF upload form
 * - Import progress tracking
 * - Import history
 * - Bulk actions
 * - File validation
 *
 * @package Askany
 * @since 1.0.0
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
?>
<div class="wrap">
	<h1><?php esc_html_e( 'Import PDFs for Knowledge Base', 'askany' ); ?></h1>
	
	<div class="askany-admin-stats">
		<div class="askany-stat-box">
			<h3><?php esc_html_e('PDF Statistics', 'askany'); ?></h3>
			<p>
				<?php esc_html_e('Total PDFs:', 'askany'); ?> 
				<strong><?php echo esc_html($pdf_stats['total_pdfs']); ?></strong>
			</p>
			<p>
				<?php esc_html_e('Total Size:', 'askany'); ?> 
				<strong><?php echo esc_html(size_format($pdf_stats['total_size'])); ?></strong>
			</p>
			<p>
				<?php esc_html_e('Last Updated:', 'askany'); ?> 
				<strong><?php echo esc_html($pdf_stats['last_updated']); ?></strong>
			</p>
		</div>
	</div>
	
	<div class="card">
		<h2><?php esc_html_e( 'Upload PDF', 'askany' ); ?></h2>
		<p><?php esc_html_e( 'Upload PDF files containing questions and answers to enhance the chat assistant knowledge.', 'askany' ); ?></p>
		
		<div class="askany-admin-actions">
			<input type="file" id="pdf-file-input" accept=".pdf" style="display: none;">
			<button type="button" class="button button-primary" id="askany-upload-pdf">
				<?php esc_html_e('Upload PDF', 'askany'); ?>
			</button>
		</div>
		
		<div id="upload-progress" style="display: none;">
			<p><?php esc_html_e('Uploading...', 'askany'); ?></p>
			<div class="progress-bar">
				<div class="progress-fill"></div>
			</div>
		</div>
	</div>
	
	<h2><?php esc_html_e( 'Imported PDFs', 'askany' ); ?></h2>
	
	<?php
	global $wpdb;
	$pdf_table = $wpdb->prefix . 'askany_pdf_index';
	$pdf_files = $wpdb->get_results("SELECT * FROM {$pdf_table} ORDER BY last_updated DESC");
	?>
	
	<div id="pdf-files-list">
		<?php if ( empty($pdf_files) ) : ?>
			<p><?php esc_html_e('No PDF files have been imported yet.', 'askany'); ?></p>
		<?php else : ?>
			<table class="wp-list-table widefat fixed striped">
				<thead>
					<tr>
						<th><?php esc_html_e('File Name', 'askany'); ?></th>
						<th><?php esc_html_e('Size', 'askany'); ?></th>
						<th><?php esc_html_e('Date Imported', 'askany'); ?></th>
						<th><?php esc_html_e('Actions', 'askany'); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $pdf_files as $pdf ) { ?>
						<tr>
							<td title="<?php echo esc_attr($pdf->file_name); ?>">
								<?php echo esc_html(wp_html_excerpt($pdf->file_name, 35, '...')); ?>
							</td>
							<td><?php echo esc_html(size_format($pdf->file_size)); ?></td>
							<td><?php echo esc_html(date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($pdf->created_at))); ?></td>
							<td>
								<button type="button" class="button button-small delete-pdf" 
										data-id="<?php echo esc_attr($pdf->id); ?>" 
										data-nonce="<?php echo esc_attr(wp_create_nonce('askany_chat_nonce')); ?>">
									<?php esc_html_e('Delete', 'askany'); ?>
								</button>
							</td>
						</tr>
					<?php } ?>
				</tbody>
			</table>
		<?php endif; ?>
	</div>
</div>
