<?php
/**
 * Attachment meta box view
 *
 * @package WebEquipe\PDFSearch
 * @since 1.0.0
 */

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

$webequipe_pdf_search_attachment_id = (int) $post->ID;
?>
<div class="webequipe-pdf-search-meta-box">
	<?php if ( $error ) : ?>
		<div class="notice notice-error inline">
			<p><strong><?php esc_html_e( 'Error:', 'webequipe-pdf-search' ); ?></strong> <?php echo esc_html( $error ); ?></p>
		</div>
	<?php endif; ?>

	<?php if ( ! empty( $warning ) ) : ?>
		<div class="notice notice-warning inline">
			<p><strong><?php esc_html_e( 'Warning:', 'webequipe-pdf-search' ); ?></strong> <?php echo esc_html( $warning ); ?></p>
		</div>
	<?php endif; ?>

	<?php if ( $indexed ) : ?>
		<p>
			<strong><?php esc_html_e( 'Pages:', 'webequipe-pdf-search' ); ?></strong>
			<?php
			if ( isset( $indexed->pages_indexed ) && (int) $indexed->pages_indexed > 0 && (int) $indexed->pages_indexed < (int) $indexed->page_count ) {
				echo esc_html( $indexed->pages_indexed . ' / ' . $indexed->page_count );
			} else {
				echo esc_html( $indexed->page_count );
			}
			?>
		</p>
		<p>
			<strong><?php esc_html_e( 'File Size:', 'webequipe-pdf-search' ); ?></strong>
			<?php echo esc_html( size_format( $indexed->pdf_filesize ) ); ?>
		</p>
		<p>
			<strong><?php esc_html_e( 'Indexed Date:', 'webequipe-pdf-search' ); ?></strong>
			<?php echo esc_html( date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), strtotime( $indexed->indexed_date ) ) ); ?>
		</p>
		<?php if ( ! empty( $indexed->pdf_content ) ) : ?>
			<p>
				<strong><?php esc_html_e( 'Text Preview:', 'webequipe-pdf-search' ); ?></strong>
			</p>
			<div class="text-preview">
				<?php echo esc_html( wp_trim_words( $indexed->pdf_content, 50, '...' ) ); ?>
			</div>
		<?php endif; ?>
	<?php endif; ?>

	<p>
		<strong><?php esc_html_e( 'Status:', 'webequipe-pdf-search' ); ?></strong>
	</p>
	<div class="webequipe-pdf-index-status" data-post-id="<?php echo esc_attr( $webequipe_pdf_search_attachment_id ); ?>">
		<?php if ( 'error' === $display_status && $error ) : ?>
			<span class="status-badge status-error" title="<?php echo esc_attr( $error ); ?>"><span class="dashicons dashicons-warning"></span> <?php esc_html_e( 'Error', 'webequipe-pdf-search' ); ?></span>
			<button type="button" class="button-link webequipe-pdf-error-details" data-error="<?php echo esc_attr( $error ); ?>" aria-label="<?php esc_attr_e( 'View error details and suggestions', 'webequipe-pdf-search' ); ?>"><span class="dashicons dashicons-info-outline"></span></button>
			<div class="webequipe-pdf-actions">
				<button type="button" class="button button-small webequipe-pdf-reindex-single" data-post-id="<?php echo esc_attr( $webequipe_pdf_search_attachment_id ); ?>"><?php esc_html_e( 'Retry', 'webequipe-pdf-search' ); ?></button>
				<span class="spinner"></span>
			</div>
		<?php elseif ( $excluded ) : ?>
			<span class="status-badge status-excluded"><span class="dashicons dashicons-hidden"></span> <?php esc_html_e( 'Excluded', 'webequipe-pdf-search' ); ?></span>
			<p class="description"><?php esc_html_e( 'Include this PDF to make it eligible for indexing, then click Index.', 'webequipe-pdf-search' ); ?></p>
			<div class="webequipe-pdf-actions">
				<button type="button" class="button button-small webequipe-pdf-include-single" data-post-id="<?php echo esc_attr( $webequipe_pdf_search_attachment_id ); ?>"><?php esc_html_e( 'Include', 'webequipe-pdf-search' ); ?></button>
				<span class="spinner"></span>
			</div>
		<?php elseif ( 'indexed' === $display_status && $indexed ) : ?>
			<span class="status-badge status-indexed"<?php echo $warning ? ' title="' . esc_attr( $warning ) . '"' : ''; ?>><span class="dashicons dashicons-yes-alt"></span> <?php esc_html_e( 'Indexed', 'webequipe-pdf-search' ); ?></span>
			<?php if ( $warning ) : ?>
				<button type="button" class="button-link webequipe-pdf-index-warning" data-warning="<?php echo esc_attr( $warning ); ?>" aria-label="<?php esc_attr_e( 'View partial index details', 'webequipe-pdf-search' ); ?>"><span class="dashicons dashicons-info-outline"></span></button>
			<?php endif; ?>
			<div class="webequipe-pdf-actions">
				<button type="button" class="button button-small webequipe-pdf-unindex-single" data-post-id="<?php echo esc_attr( $webequipe_pdf_search_attachment_id ); ?>"><?php esc_html_e( 'Unindex', 'webequipe-pdf-search' ); ?></button>
				<button type="button" class="button button-small webequipe-pdf-exclude-single" data-post-id="<?php echo esc_attr( $webequipe_pdf_search_attachment_id ); ?>"><?php esc_html_e( 'Exclude', 'webequipe-pdf-search' ); ?></button>
				<span class="spinner"></span>
			</div>
		<?php elseif ( 'processing' === $display_status ) : ?>
			<span class="status-badge status-processing"><span class="dashicons dashicons-update"></span> <?php esc_html_e( 'Processing...', 'webequipe-pdf-search' ); ?></span>
		<?php elseif ( 'scheduled' === $display_status ) : ?>
			<span class="status-badge status-scheduled"><span class="dashicons dashicons-calendar-alt"></span> <?php esc_html_e( 'Scheduled', 'webequipe-pdf-search' ); ?></span>
		<?php else : ?>
			<span class="status-badge status-not-indexed"><span class="dashicons dashicons-minus"></span> <?php esc_html_e( 'Not Indexed', 'webequipe-pdf-search' ); ?></span>
			<div class="webequipe-pdf-actions">
				<button type="button" class="button button-small webequipe-pdf-reindex-single" data-post-id="<?php echo esc_attr( $webequipe_pdf_search_attachment_id ); ?>"><?php esc_html_e( 'Index', 'webequipe-pdf-search' ); ?></button>
				<button type="button" class="button button-small webequipe-pdf-exclude-single" data-post-id="<?php echo esc_attr( $webequipe_pdf_search_attachment_id ); ?>"><?php esc_html_e( 'Exclude', 'webequipe-pdf-search' ); ?></button>
				<span class="spinner"></span>
			</div>
		<?php endif; ?>
	</div>
</div>
