<?php
/**
 * PufferPDF converter screen.
 *
 * @package PufferPDF
 */

defined( 'ABSPATH' ) || exit;

if ( ! isset( $pufferpdf_settings ) || ! is_array( $pufferpdf_settings ) ) {
	return;
}
?>
<div class="wrap pufferpdf" id="pufferpdf-app" data-pufferpdf-settings="<?php echo esc_attr( wp_json_encode( $pufferpdf_settings ) ); ?>">
	<header class="pufferpdf__header">
		<h1><?php esc_html_e( 'Turn PDFs into WordPress content.', 'pufferpdf' ); ?></h1>
		<p class="pufferpdf__intro"><?php esc_html_e( 'Classify a PDF, extract structured Markdown, review it, and create editable Gutenberg content.', 'pufferpdf' ); ?></p>
	</header>

	<div class="pufferpdf__workspace">
		<section class="pufferpdf__panel pufferpdf__source" aria-labelledby="pufferpdf-source-title">
			<div class="pufferpdf__section-heading">
				<span class="pufferpdf__step" aria-hidden="true">1</span>
				<div>
					<h2 id="pufferpdf-source-title"><?php esc_html_e( 'Choose a PDF', 'pufferpdf' ); ?></h2>
					<p><?php esc_html_e( 'Native-text documents work best.', 'pufferpdf' ); ?></p>
				</div>
			</div>

			<label class="pufferpdf__dropzone" id="pufferpdf-dropzone" for="pufferpdf-file">
				<input id="pufferpdf-file" type="file" accept="application/pdf,.pdf">
				<span class="dashicons dashicons-media-document" aria-hidden="true"></span>
				<strong><?php esc_html_e( 'Drop a PDF here or choose a file', 'pufferpdf' ); ?></strong>
				<span><?php esc_html_e( 'One PDF at a time · up to 20 MB', 'pufferpdf' ); ?></span>
			</label>

			<div class="pufferpdf__library-choice">
				<span><?php esc_html_e( 'or', 'pufferpdf' ); ?></span>
				<button type="button" class="button" id="pufferpdf-choose-media">
					<span class="dashicons dashicons-admin-media" aria-hidden="true"></span>
					<span class="pufferpdf__library-label"><?php esc_html_e( 'Choose from Media Library', 'pufferpdf' ); ?></span>
				</button>
			</div>

			<div class="pufferpdf__file" id="pufferpdf-file-summary" hidden>
				<div class="pufferpdf__file-icon" aria-hidden="true">PDF</div>
				<div class="pufferpdf__file-copy">
					<strong id="pufferpdf-file-name"></strong>
					<span id="pufferpdf-file-size"></span>
				</div>
				<button type="button" class="button-link" id="pufferpdf-remove-file"><?php esc_html_e( 'Remove', 'pufferpdf' ); ?></button>
			</div>

			<fieldset class="pufferpdf__options">
				<legend><?php esc_html_e( 'Conversion options', 'pufferpdf' ); ?></legend>
				<label>
					<span><?php esc_html_e( 'Markdown profile', 'pufferpdf' ); ?></span>
					<select id="pufferpdf-profile">
						<option value="fidelity"><?php esc_html_e( 'Fidelity — preserve source structure', 'pufferpdf' ); ?></option>
						<option value="compact"><?php esc_html_e( 'Compact — reduce source padding', 'pufferpdf' ); ?></option>
					</select>
				</label>
				<label class="pufferpdf__check">
					<input type="checkbox" id="pufferpdf-page-markers" checked>
					<span><?php esc_html_e( 'Add page dividers', 'pufferpdf' ); ?></span>
				</label>
			</fieldset>

			<div class="pufferpdf__password" id="pufferpdf-password-wrap" hidden>
				<label for="pufferpdf-password">
					<span><?php esc_html_e( 'PDF password', 'pufferpdf' ); ?></span>
					<input type="password" id="pufferpdf-password" autocomplete="off" autocapitalize="none" spellcheck="false" aria-describedby="pufferpdf-password-help">
				</label>
				<p class="description" id="pufferpdf-password-help"><?php esc_html_e( 'Used for this conversion only and never saved.', 'pufferpdf' ); ?></p>
			</div>

			<div class="pufferpdf__source-actions">
				<button type="button" class="button button-primary button-hero" id="pufferpdf-convert" disabled>
					<?php esc_html_e( 'Convert PDF', 'pufferpdf' ); ?>
				</button>
				<p id="pufferpdf-file-error" class="pufferpdf__message pufferpdf__message--error" role="alert" hidden></p>
			</div>

		</section>

		<section class="pufferpdf__panel pufferpdf__results" aria-labelledby="pufferpdf-results-title">
			<div class="pufferpdf__section-heading">
				<span class="pufferpdf__step" aria-hidden="true">2</span>
				<div>
					<h2 id="pufferpdf-results-title"><?php esc_html_e( 'Review the result', 'pufferpdf' ); ?></h2>
					<p><?php esc_html_e( 'Check extraction warnings before importing the available text.', 'pufferpdf' ); ?></p>
				</div>
			</div>

			<div class="pufferpdf__empty" id="pufferpdf-empty-state">
				<span class="dashicons dashicons-text-page" aria-hidden="true"></span>
				<strong><?php esc_html_e( 'Your converted document will appear here.', 'pufferpdf' ); ?></strong>
				<p><?php esc_html_e( 'You will see the document type, OCR routing, classification confidence, and Markdown preview.', 'pufferpdf' ); ?></p>
			</div>

			<div class="pufferpdf__processing" id="pufferpdf-processing-state" role="status" aria-live="polite" hidden>
				<span class="spinner is-active" aria-hidden="true"></span>
				<div>
					<strong><?php esc_html_e( 'Inspecting the PDF…', 'pufferpdf' ); ?></strong>
					<p><?php esc_html_e( 'The parser is loading and reconstructing the document structure.', 'pufferpdf' ); ?></p>
				</div>
				<button type="button" class="button" id="pufferpdf-cancel"><?php esc_html_e( 'Cancel', 'pufferpdf' ); ?></button>
			</div>

			<div class="pufferpdf__result" id="pufferpdf-result-state" hidden>
				<div class="pufferpdf__metrics" aria-label="<?php esc_attr_e( 'PDF inspection results', 'pufferpdf' ); ?>">
					<div><span><?php esc_html_e( 'Document type', 'pufferpdf' ); ?></span><strong id="pufferpdf-type">—</strong></div>
					<div><span><?php esc_html_e( 'Pages', 'pufferpdf' ); ?></span><strong id="pufferpdf-pages">—</strong></div>
					<div><span><?php esc_html_e( 'Parse time', 'pufferpdf' ); ?></span><strong id="pufferpdf-time">—</strong></div>
					<div><span><?php esc_html_e( 'Classification confidence', 'pufferpdf' ); ?></span><strong id="pufferpdf-confidence">—</strong></div>
				</div>

				<div class="pufferpdf__notice" id="pufferpdf-ocr-notice" role="status"></div>

				<label class="pufferpdf__title-field" for="pufferpdf-post-title">
					<span><?php esc_html_e( 'Document title', 'pufferpdf' ); ?></span>
					<input type="text" class="regular-text" id="pufferpdf-post-title" maxlength="200" dir="auto">
				</label>

				<div class="pufferpdf__tabs" role="tablist" aria-label="<?php esc_attr_e( 'Converted content views', 'pufferpdf' ); ?>">
					<button type="button" role="tab" id="pufferpdf-preview-tab" aria-controls="pufferpdf-preview-panel" aria-selected="true"><?php esc_html_e( 'Preview', 'pufferpdf' ); ?></button>
					<button type="button" role="tab" id="pufferpdf-markdown-tab" aria-controls="pufferpdf-markdown-panel" aria-selected="false" tabindex="-1"><?php esc_html_e( 'Markdown', 'pufferpdf' ); ?></button>
					<button type="button" role="tab" id="pufferpdf-outline-tab" aria-controls="pufferpdf-outline-panel" aria-selected="false" tabindex="-1"><?php esc_html_e( 'Outline', 'pufferpdf' ); ?></button>
				</div>

				<div class="pufferpdf__content-panel pufferpdf__preview" id="pufferpdf-preview-panel" role="tabpanel" aria-labelledby="pufferpdf-preview-tab" tabindex="0" dir="auto"></div>
				<div class="pufferpdf__content-panel" id="pufferpdf-markdown-panel" role="tabpanel" aria-labelledby="pufferpdf-markdown-tab" tabindex="0" hidden>
					<textarea id="pufferpdf-markdown" readonly spellcheck="false" aria-label="<?php esc_attr_e( 'Generated Markdown', 'pufferpdf' ); ?>"></textarea>
				</div>
				<div class="pufferpdf__content-panel pufferpdf__outline" id="pufferpdf-outline-panel" role="tabpanel" aria-labelledby="pufferpdf-outline-tab" tabindex="0" hidden>
					<div id="pufferpdf-outline-content"></div>
				</div>

				<label class="pufferpdf__acknowledgement" id="pufferpdf-incomplete-wrap" hidden>
					<input type="checkbox" id="pufferpdf-incomplete-ack">
					<span id="pufferpdf-incomplete-label"><?php esc_html_e( 'I understand that this draft may omit pages requiring OCR.', 'pufferpdf' ); ?></span>
				</label>

				<div class="pufferpdf__result-actions">
					<button type="button" class="button" id="pufferpdf-copy"><?php esc_html_e( 'Copy Markdown', 'pufferpdf' ); ?></button>
					<button type="button" class="button" id="pufferpdf-download"><?php esc_html_e( 'Download .md', 'pufferpdf' ); ?></button>
					<button type="button" class="button" id="pufferpdf-import"><?php esc_html_e( 'Create single draft', 'pufferpdf' ); ?></button>
					<button type="button" class="button button-primary" id="pufferpdf-open-publishing"><?php esc_html_e( 'Plan WordPress content', 'pufferpdf' ); ?></button>
				</div>
				<p class="pufferpdf__message pufferpdf__message--warning" id="pufferpdf-import-help" role="status" hidden></p>
				<p class="pufferpdf__message pufferpdf__message--warning" id="pufferpdf-publishing-source-help" role="status" hidden><?php esc_html_e( 'Choose a Media Library PDF that WordPress can verify, or use the file picker.', 'pufferpdf' ); ?></p>

				<div class="pufferpdf__message" id="pufferpdf-import-status" role="status" aria-live="polite" hidden></div>
			</div>
		</section>
	</div>

	<section class="pufferpdf__publishing" id="pufferpdf-publishing" aria-labelledby="pufferpdf-publishing-title" hidden>
		<div class="pufferpdf__section-heading pufferpdf__publishing-heading">
			<span class="pufferpdf__step" aria-hidden="true">3</span>
			<div>
				<h2 id="pufferpdf-publishing-title" tabindex="-1"><?php esc_html_e( 'Plan WordPress content', 'pufferpdf' ); ?></h2>
				<p><?php esc_html_e( 'Choose how the reviewed document should be divided and where the approved items should go. Previewing never writes content.', 'pufferpdf' ); ?></p>
			</div>
		</div>

		<form id="pufferpdf-plan-form">
			<div class="pufferpdf__publishing-grid">
				<fieldset class="pufferpdf__publishing-group">
					<legend><?php esc_html_e( 'Output', 'pufferpdf' ); ?></legend>
					<label class="pufferpdf__radio">
						<input type="radio" name="pufferpdf-output-mode" id="pufferpdf-output-single" value="single" checked>
						<span><?php esc_html_e( 'Single document', 'pufferpdf' ); ?></span>
					</label>
					<label class="pufferpdf__radio">
						<input type="radio" name="pufferpdf-output-mode" id="pufferpdf-output-split" value="heading">
						<span><?php esc_html_e( 'Split at headings', 'pufferpdf' ); ?></span>
					</label>
					<label class="pufferpdf__compact-field" id="pufferpdf-split-level-wrap" for="pufferpdf-split-level" hidden>
						<span><?php esc_html_e( 'Heading level', 'pufferpdf' ); ?></span>
						<select id="pufferpdf-split-level"></select>
					</label>
					<p class="description" id="pufferpdf-split-help"><?php esc_html_e( 'Only heading levels that produce at least two items are available.', 'pufferpdf' ); ?></p>
				</fieldset>

				<fieldset class="pufferpdf__publishing-group">
					<legend><?php esc_html_e( 'Destination', 'pufferpdf' ); ?></legend>
					<label class="pufferpdf__compact-field" for="pufferpdf-post-type">
						<span><?php esc_html_e( 'Post type', 'pufferpdf' ); ?></span>
						<select id="pufferpdf-post-type" disabled>
							<option value=""><?php esc_html_e( 'Loading destinations…', 'pufferpdf' ); ?></option>
						</select>
					</label>
					<label class="pufferpdf__compact-field" for="pufferpdf-post-status">
						<span><?php esc_html_e( 'Status', 'pufferpdf' ); ?></span>
						<select id="pufferpdf-post-status" disabled></select>
					</label>
					<label class="pufferpdf__compact-field" id="pufferpdf-parent-wrap" for="pufferpdf-parent-control" hidden>
						<span><?php esc_html_e( 'Existing parent', 'pufferpdf' ); ?></span>
						<select id="pufferpdf-parent-control"></select>
					</label>
					<label class="pufferpdf__compact-field" id="pufferpdf-author-wrap" for="pufferpdf-author-control" hidden>
						<span><?php esc_html_e( 'Author', 'pufferpdf' ); ?></span>
						<select id="pufferpdf-author-control"></select>
					</label>
					<div id="pufferpdf-taxonomy-controls" class="pufferpdf__taxonomy-controls" hidden></div>
				</fieldset>

				<fieldset class="pufferpdf__publishing-group">
					<legend><?php esc_html_e( 'Extras', 'pufferpdf' ); ?></legend>
					<label class="pufferpdf__check" id="pufferpdf-create-toc-wrap">
						<input type="checkbox" id="pufferpdf-create-toc" aria-describedby="pufferpdf-toc-help">
						<span><?php esc_html_e( 'Create a table of contents', 'pufferpdf' ); ?></span>
					</label>
					<p class="description" id="pufferpdf-toc-help" hidden><?php esc_html_e( 'This post type has no public or preview URLs, so a linked table of contents is unavailable.', 'pufferpdf' ); ?></p>
					<label class="pufferpdf__check">
						<input type="checkbox" id="pufferpdf-add-citations">
						<span><?php esc_html_e( 'Add source PDF and page citations', 'pufferpdf' ); ?></span>
					</label>
					<label class="pufferpdf__check" id="pufferpdf-companion-wrap">
						<input type="checkbox" id="pufferpdf-create-companion" aria-describedby="pufferpdf-companion-help">
						<span><?php esc_html_e( 'Create an accessible HTML companion page', 'pufferpdf' ); ?></span>
					</label>
					<p class="description" id="pufferpdf-companion-help"><?php esc_html_e( 'A companion requires a Media Library PDF so the original download remains available.', 'pufferpdf' ); ?></p>
				</fieldset>
			</div>

			<div class="pufferpdf__plan-actions">
				<button type="submit" class="button button-primary" id="pufferpdf-preview-plan"><?php esc_html_e( 'Preview publishing plan', 'pufferpdf' ); ?></button>
				<p class="pufferpdf__inline-status" id="pufferpdf-plan-status" role="status" aria-live="polite"></p>
			</div>
		</form>

		<div class="pufferpdf__plan-preview" id="pufferpdf-plan-preview" hidden>
			<div class="pufferpdf__plan-summary">
				<div>
					<h3 id="pufferpdf-plan-preview-title" tabindex="-1"><?php esc_html_e( 'Publishing plan', 'pufferpdf' ); ?></h3>
					<p id="pufferpdf-plan-summary"></p>
				</div>
				<span class="pufferpdf__state" id="pufferpdf-plan-state"></span>
			</div>

			<div class="pufferpdf__plan-table-wrap" role="region" aria-labelledby="pufferpdf-plan-preview-title" tabindex="0">
				<table class="widefat pufferpdf__plan-table">
					<caption class="screen-reader-text"><?php esc_html_e( 'Proposed WordPress content and validation state', 'pufferpdf' ); ?></caption>
					<thead>
						<tr>
							<th scope="col" class="pufferpdf__plan-approve-heading"><?php esc_html_e( 'Approve', 'pufferpdf' ); ?></th>
							<th scope="col"><?php esc_html_e( 'Content', 'pufferpdf' ); ?></th>
							<th scope="col"><?php esc_html_e( 'Slug', 'pufferpdf' ); ?></th>
							<th scope="col"><?php esc_html_e( 'Source', 'pufferpdf' ); ?></th>
							<th scope="col" id="pufferpdf-plan-parent-heading"><?php esc_html_e( 'Parent', 'pufferpdf' ); ?></th>
							<th scope="col"><?php esc_html_e( 'Validation', 'pufferpdf' ); ?></th>
						</tr>
					</thead>
					<tbody id="pufferpdf-plan-body"></tbody>
				</table>
			</div>

			<div class="pufferpdf__plan-actions pufferpdf__plan-actions--create">
				<button type="button" class="button button-primary" id="pufferpdf-create-run" disabled><?php esc_html_e( 'Create approved drafts', 'pufferpdf' ); ?></button>
				<p class="pufferpdf__inline-status" id="pufferpdf-create-status" role="status" aria-live="polite"></p>
			</div>
		</div>

		<div class="pufferpdf__creation-report" id="pufferpdf-creation-report" hidden>
			<div class="pufferpdf__plan-summary">
				<div>
					<h3 id="pufferpdf-report-title" tabindex="-1"><?php esc_html_e( 'Creation report', 'pufferpdf' ); ?></h3>
					<p id="pufferpdf-report-summary"></p>
				</div>
			</div>
			<div class="pufferpdf__report-table-wrap" role="region" aria-labelledby="pufferpdf-report-title" tabindex="0">
				<table class="widefat pufferpdf__report-table">
					<caption class="screen-reader-text"><?php esc_html_e( 'Objects created by this publishing run', 'pufferpdf' ); ?></caption>
					<thead><tr><th scope="col"><?php esc_html_e( 'Object', 'pufferpdf' ); ?></th><th scope="col"><?php esc_html_e( 'Role', 'pufferpdf' ); ?></th><th scope="col"><?php esc_html_e( 'Status', 'pufferpdf' ); ?></th><th scope="col"><?php esc_html_e( 'Action', 'pufferpdf' ); ?></th></tr></thead>
					<tbody id="pufferpdf-report-body"></tbody>
				</table>
			</div>
			<div class="pufferpdf__report-actions">
				<button type="button" class="button-link-delete" id="pufferpdf-review-undo"><?php esc_html_e( 'Review undo', 'pufferpdf' ); ?></button>
			</div>
			<div class="pufferpdf__undo-panel" id="pufferpdf-undo-panel" hidden>
				<h4><?php esc_html_e( 'Undo this publishing run', 'pufferpdf' ); ?></h4>
				<div id="pufferpdf-undo-details"></div>
				<label class="pufferpdf__acknowledgement" id="pufferpdf-undo-confirm-wrap" hidden>
					<input type="checkbox" id="pufferpdf-undo-confirm">
					<span id="pufferpdf-undo-confirm-label"></span>
				</label>
				<div class="pufferpdf__plan-actions">
					<button type="button" class="button" id="pufferpdf-confirm-undo" disabled><?php esc_html_e( 'Move created content to Trash', 'pufferpdf' ); ?></button>
					<p class="pufferpdf__inline-status" id="pufferpdf-undo-status" role="status" aria-live="polite"></p>
				</div>
			</div>
		</div>
	</section>

	<section class="pufferpdf__inventory" aria-labelledby="pufferpdf-inventory-title">
		<div class="pufferpdf__inventory-heading">
			<div>
				<h2 id="pufferpdf-inventory-title"><?php esc_html_e( 'Media Library PDFs', 'pufferpdf' ); ?></h2>
				<p><?php esc_html_e( 'Review conversion history and safely reprocess an existing attachment without creating another copy.', 'pufferpdf' ); ?></p>
			</div>
			<button type="button" class="button" id="pufferpdf-refresh-inventory"><?php esc_html_e( 'Refresh', 'pufferpdf' ); ?></button>
		</div>

		<div class="pufferpdf__inventory-table-wrap" id="pufferpdf-inventory-region" aria-busy="true">
			<table class="widefat striped pufferpdf__inventory-table">
				<thead>
					<tr>
						<th scope="col"><?php esc_html_e( 'Document', 'pufferpdf' ); ?></th>
						<th scope="col"><?php esc_html_e( 'State', 'pufferpdf' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Result', 'pufferpdf' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Last conversion', 'pufferpdf' ); ?></th>
						<th scope="col"><?php esc_html_e( 'Target', 'pufferpdf' ); ?></th>
						<th scope="col" class="pufferpdf__inventory-action-heading"><?php esc_html_e( 'Action', 'pufferpdf' ); ?></th>
					</tr>
				</thead>
				<tbody id="pufferpdf-inventory-body">
					<tr><td colspan="6"><?php esc_html_e( 'Loading Media Library PDFs…', 'pufferpdf' ); ?></td></tr>
				</tbody>
			</table>
		</div>
		<nav class="pufferpdf__inventory-pagination" id="pufferpdf-inventory-pagination" aria-label="<?php esc_attr_e( 'Media Library PDF pages', 'pufferpdf' ); ?>" hidden>
			<span id="pufferpdf-inventory-page-summary"></span>
			<div class="pufferpdf__inventory-pagination-actions">
				<button type="button" class="button button-small" id="pufferpdf-inventory-previous" aria-controls="pufferpdf-inventory-body"><?php esc_html_e( 'Previous', 'pufferpdf' ); ?></button>
				<button type="button" class="button button-small" id="pufferpdf-inventory-next" aria-controls="pufferpdf-inventory-body"><?php esc_html_e( 'Next', 'pufferpdf' ); ?></button>
			</div>
		</nav>
		<p class="screen-reader-text" id="pufferpdf-inventory-status" role="status" aria-live="polite"><?php esc_html_e( 'Loading Media Library PDFs.', 'pufferpdf' ); ?></p>
		<p class="pufferpdf__message pufferpdf__message--error" id="pufferpdf-inventory-error" role="alert" hidden></p>
	</section>
</div>
