<?php
/**
 * Form Builder page template.
 *
 * Drag & drop form builder with palette, canvas, and properties panel.
 *
 * @package GoValid_QR
 */

defined( 'ABSPATH' ) || exit;

// Only administrators/editors may access this page (defence-in-depth; menu already restricts access).
if ( ! current_user_can( 'edit_posts' ) ) {
	wp_die( esc_html__( 'You do not have permission to access this page.', 'govalid-qr-validator' ) );
}

// $govalid_form_id and $govalid_saved are pre-set by render_form_builder_page() after nonce verification.
?>
<div class="wrap govalid-form-builder-wrap">
	<div class="govalid-page-header">
		<img src="<?php echo esc_url( GOVALID_QR_PLUGIN_URL . 'assets/images/govalid-hero.png' ); ?>"
			 alt="GoValid" class="govalid-page-header-logo" />
		<div style="flex: 1;">
			<div style="display: flex; align-items: center; gap: 12px;">
				<input type="text" id="govalid-fb-title"
					   class="govalid-fb-title-input"
					   placeholder="<?php esc_attr_e( 'Form Title', 'govalid-qr-validator' ); ?>"
					   value="" />
				<span id="govalid-fb-status-badge" class="govalid-fb-badge govalid-fb-badge--draft">
					<?php esc_html_e( 'Draft', 'govalid-qr-validator' ); ?>
				</span>
			</div>
		</div>
		<div class="govalid-fb-actions">
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=govalid-qr-forms' ) ); ?>"
			   class="button"><?php esc_html_e( 'Back to Forms', 'govalid-qr-validator' ); ?></a>
			<button type="button" id="govalid-fb-save" class="button">
				<span class="dashicons dashicons-saved" style="margin-top: 4px;"></span>
				<?php esc_html_e( 'Save Draft', 'govalid-qr-validator' ); ?>
			</button>
			<button type="button" id="govalid-fb-publish" class="button button-primary">
				<span class="dashicons dashicons-yes-alt" style="margin-top: 4px;"></span>
				<?php esc_html_e( 'Publish', 'govalid-qr-validator' ); ?>
			</button>
		</div>
	</div>

	<?php if ( $govalid_saved ) : ?>
		<div class="notice notice-success is-dismissible" style="max-width: none;">
			<p><?php esc_html_e( 'Form saved successfully.', 'govalid-qr-validator' ); ?></p>
		</div>
	<?php endif; ?>

	<!-- Shortcode banner (shown after save for published forms) -->
	<div id="govalid-fb-shortcode-bar" class="govalid-card" style="max-width: none; display: none; padding: 14px 20px;">
		<div style="display: flex; align-items: center; gap: 12px;">
			<span class="dashicons dashicons-shortcode" style="color: var(--gv-primary);"></span>
			<code id="govalid-fb-shortcode-text" style="background: var(--gv-gray-100); padding: 4px 10px; border-radius: 6px; font-size: 13px;"></code>
			<button type="button" class="button button-small" id="govalid-fb-copy-shortcode">
				<?php esc_html_e( 'Copy', 'govalid-qr-validator' ); ?>
			</button>
		</div>
	</div>

	<!-- Three-column layout -->
	<div class="govalid-fb-layout">
		<!-- LEFT: Field Palette -->
		<div class="govalid-fb-palette">
			<h3><?php esc_html_e( 'Fields', 'govalid-qr-validator' ); ?></h3>
			<div class="govalid-fb-palette-search-wrap">
				<span class="dashicons dashicons-search"></span>
				<input type="text" id="govalid-fb-palette-search" placeholder="<?php esc_attr_e( 'Search fields...', 'govalid-qr-validator' ); ?>" />
			</div>
			<div class="govalid-fb-palette-scroll">
				<div class="govalid-fb-palette-list" id="govalid-fb-palette">
					<?php
					$govalid_field_categories = array(
						__( 'Basic', 'govalid-qr-validator' ) => array(
							'text'      => array( 'label' => __( 'Text', 'govalid-qr-validator' ),      'icon' => 'dashicons-editor-textcolor' ),
							'textarea'  => array( 'label' => __( 'Textarea', 'govalid-qr-validator' ),   'icon' => 'dashicons-editor-paragraph' ),
							'email'     => array( 'label' => __( 'Email', 'govalid-qr-validator' ),      'icon' => 'dashicons-email' ),
							'phone'     => array( 'label' => __( 'Phone', 'govalid-qr-validator' ),      'icon' => 'dashicons-phone' ),
							'number'    => array( 'label' => __( 'Number', 'govalid-qr-validator' ),     'icon' => 'dashicons-calculator' ),
							'url'       => array( 'label' => __( 'URL', 'govalid-qr-validator' ),        'icon' => 'dashicons-admin-links' ),
						),
						__( 'Choices', 'govalid-qr-validator' ) => array(
							'select'    => array( 'label' => __( 'Select', 'govalid-qr-validator' ),     'icon' => 'dashicons-arrow-down-alt2' ),
							'radio'     => array( 'label' => __( 'Radio', 'govalid-qr-validator' ),      'icon' => 'dashicons-marker' ),
							'checkbox'  => array( 'label' => __( 'Checkbox', 'govalid-qr-validator' ),   'icon' => 'dashicons-yes' ),
							'gender'    => array( 'label' => __( 'Sex / Gender', 'govalid-qr-validator' ), 'icon' => 'dashicons-admin-users' ),
						),
						__( 'Upload & Media', 'govalid-qr-validator' ) => array(
							'file'      => array( 'label' => __( 'File', 'govalid-qr-validator' ),       'icon' => 'dashicons-upload' ),
							'camera'    => array( 'label' => __( 'Camera / Selfie', 'govalid-qr-validator' ), 'icon' => 'dashicons-camera' ),
							'date'      => array( 'label' => __( 'Date', 'govalid-qr-validator' ),       'icon' => 'dashicons-calendar-alt' ),
							'time'      => array( 'label' => __( 'Time', 'govalid-qr-validator' ),       'icon' => 'dashicons-clock' ),
							'rating'    => array( 'label' => __( 'Rating', 'govalid-qr-validator' ),     'icon' => 'dashicons-star-filled' ),
							'signature' => array( 'label' => __( 'Signature', 'govalid-qr-validator' ),  'icon' => 'dashicons-edit-page' ),
						),
						__( 'Social', 'govalid-qr-validator' ) => array(
							'whatsapp'  => array( 'label' => __( 'WhatsApp', 'govalid-qr-validator' ),   'icon' => 'dashicons-smartphone' ),
							'linkedin'  => array( 'label' => __( 'LinkedIn', 'govalid-qr-validator' ),   'icon' => 'dashicons-linkedin' ),
							'facebook'  => array( 'label' => __( 'Facebook', 'govalid-qr-validator' ),   'icon' => 'dashicons-facebook-alt' ),
							'instagram' => array( 'label' => __( 'Instagram', 'govalid-qr-validator' ),  'icon' => 'dashicons-instagram' ),
							'twitter'   => array( 'label' => __( 'X / Twitter', 'govalid-qr-validator' ), 'icon' => 'dashicons-twitter' ),
							'youtube'   => array( 'label' => __( 'YouTube', 'govalid-qr-validator' ),    'icon' => 'dashicons-video-alt3' ),
							'tiktok'    => array( 'label' => __( 'TikTok', 'govalid-qr-validator' ),     'icon' => 'dashicons-controls-play' ),
							'telegram'  => array( 'label' => __( 'Telegram', 'govalid-qr-validator' ),   'icon' => 'dashicons-airplane' ),
						),
						__( 'Academic', 'govalid-qr-validator' ) => array(
							'orcid'           => array( 'label' => __( 'ORCID', 'govalid-qr-validator' ),           'icon' => 'dashicons-id-alt' ),
							'scopus'          => array( 'label' => __( 'Scopus', 'govalid-qr-validator' ),          'icon' => 'dashicons-book-alt' ),
							'google_scholar'  => array( 'label' => __( 'Google Scholar', 'govalid-qr-validator' ),  'icon' => 'dashicons-welcome-learn-more' ),
							'researchgate'    => array( 'label' => __( 'ResearchGate', 'govalid-qr-validator' ),    'icon' => 'dashicons-clipboard' ),
							'academia'        => array( 'label' => __( 'Academia.edu', 'govalid-qr-validator' ),    'icon' => 'dashicons-building' ),
							'pubmed'          => array( 'label' => __( 'PubMed', 'govalid-qr-validator' ),          'icon' => 'dashicons-heart' ),
							'web_of_science'  => array( 'label' => __( 'Web of Science', 'govalid-qr-validator' ),  'icon' => 'dashicons-analytics' ),
							'semantic_scholar' => array( 'label' => __( 'Semantic Scholar', 'govalid-qr-validator' ), 'icon' => 'dashicons-search' ),
						),
						__( 'Layout', 'govalid-qr-validator' ) => array(
							'heading'   => array( 'label' => __( 'Heading', 'govalid-qr-validator' ),    'icon' => 'dashicons-heading' ),
							'separator'    => array( 'label' => __( 'Divider', 'govalid-qr-validator' ),      'icon' => 'dashicons-minus' ),
							'image_banner' => array( 'label' => __( 'Image Banner', 'govalid-qr-validator' ), 'icon' => 'dashicons-format-image' ),
							'page_break'   => array( 'label' => __( 'Page Break', 'govalid-qr-validator' ),   'icon' => 'dashicons-editor-break' ),
						),
					);
					foreach ( $govalid_field_categories as $govalid_cat_label => $govalid_cat_fields ) :
					?>
						<div class="govalid-fb-palette-category" data-category="<?php echo esc_attr( sanitize_title( $govalid_cat_label ) ); ?>">
							<div class="govalid-fb-palette-cat-label"><?php echo esc_html( $govalid_cat_label ); ?></div>
							<?php foreach ( $govalid_cat_fields as $type => $govalid_ft ) : ?>
								<div class="govalid-fb-palette-item" data-type="<?php echo esc_attr( $type ); ?>" data-search="<?php echo esc_attr( strtolower( $govalid_ft['label'] ) ); ?>">
									<span class="dashicons <?php echo esc_attr( $govalid_ft['icon'] ); ?>"></span>
									<span><?php echo esc_html( $govalid_ft['label'] ); ?></span>
								</div>
							<?php endforeach; ?>
						</div>
					<?php endforeach; ?>
				</div>
			</div>
		</div>

		<!-- CENTER: Canvas -->
		<div class="govalid-fb-canvas-wrap">
			<div class="govalid-fb-canvas" id="govalid-fb-canvas">
				<div class="govalid-fb-canvas-empty" id="govalid-fb-empty-msg">
					<span class="dashicons dashicons-move" style="font-size: 32px; color: var(--gv-gray-300); margin-bottom: 8px;"></span>
					<p><?php esc_html_e( 'Drag fields from the left panel to start building your form.', 'govalid-qr-validator' ); ?></p>
				</div>
			</div>
		</div>

		<!-- RIGHT: Properties Panel -->
		<div class="govalid-fb-properties" id="govalid-fb-properties">
			<div class="govalid-fb-props-empty">
				<span class="dashicons dashicons-admin-generic" style="font-size: 28px; color: var(--gv-gray-300);"></span>
				<p><?php esc_html_e( 'Click a field to edit its properties.', 'govalid-qr-validator' ); ?></p>
			</div>

			<div class="govalid-fb-props-form" id="govalid-fb-props-form" style="display: none;">
				<h3><?php esc_html_e( 'Field Properties', 'govalid-qr-validator' ); ?></h3>

				<div class="govalid-fb-prop-group">
					<label><?php esc_html_e( 'Label', 'govalid-qr-validator' ); ?></label>
					<input type="text" id="govalid-fb-prop-label" class="govalid-fb-prop-input" />
				</div>

				<div class="govalid-fb-prop-group" id="govalid-fb-prop-placeholder-wrap">
					<label><?php esc_html_e( 'Placeholder', 'govalid-qr-validator' ); ?></label>
					<input type="text" id="govalid-fb-prop-placeholder" class="govalid-fb-prop-input" />
				</div>

				<div class="govalid-fb-prop-group" id="govalid-fb-prop-required-wrap">
					<label>
						<input type="checkbox" id="govalid-fb-prop-required" />
						<?php esc_html_e( 'Required', 'govalid-qr-validator' ); ?>
					</label>
				</div>

				<div class="govalid-fb-prop-group" id="govalid-fb-prop-options-wrap" style="display: none;">
					<label><?php esc_html_e( 'Options (one per line)', 'govalid-qr-validator' ); ?></label>
					<textarea id="govalid-fb-prop-options" class="govalid-fb-prop-input" rows="4"></textarea>
				</div>

				<!-- Separator settings -->
			<div class="govalid-fb-prop-group" id="govalid-fb-prop-separator-wrap" style="display: none;">
				<label><?php esc_html_e( 'Line Style', 'govalid-qr-validator' ); ?></label>
				<select id="govalid-fb-prop-separator-style" class="govalid-fb-prop-input">
					<option value="solid"><?php esc_html_e( 'Solid', 'govalid-qr-validator' ); ?></option>
					<option value="dashed"><?php esc_html_e( 'Dashed', 'govalid-qr-validator' ); ?></option>
					<option value="dotted"><?php esc_html_e( 'Dotted', 'govalid-qr-validator' ); ?></option>
					<option value="none"><?php esc_html_e( 'No Line', 'govalid-qr-validator' ); ?></option>
				</select>
				<label style="margin-top: 8px;"><?php esc_html_e( 'Height (px)', 'govalid-qr-validator' ); ?></label>
				<input type="number" id="govalid-fb-prop-separator-height" class="govalid-fb-prop-input" min="0" max="100" value="16" />
			</div>

			<!-- Image Banner settings -->
			<div class="govalid-fb-prop-group" id="govalid-fb-prop-banner-wrap" style="display: none;">
				<label><?php esc_html_e( 'Image URL', 'govalid-qr-validator' ); ?></label>
				<div style="display: flex; gap: 6px;">
					<input type="text" id="govalid-fb-prop-banner-url" class="govalid-fb-prop-input" placeholder="<?php esc_attr_e( 'https://...', 'govalid-qr-validator' ); ?>" />
					<button type="button" id="govalid-fb-prop-banner-browse" class="button" style="flex-shrink: 0;">
						<?php esc_html_e( 'Browse', 'govalid-qr-validator' ); ?>
					</button>
				</div>
				<div id="govalid-fb-prop-banner-preview" style="margin-top: 8px; display: none;">
					<img id="govalid-fb-prop-banner-img" src="" alt="" style="max-width: 100%; max-height: 120px; border-radius: 6px; border: 1px solid var(--gv-gray-200);" />
				</div>
			</div>

			<!-- Rating settings -->
		<div class="govalid-fb-prop-group" id="govalid-fb-prop-rating-wrap" style="display: none;">
			<label><?php esc_html_e( 'Max Stars', 'govalid-qr-validator' ); ?></label>
			<input type="number" id="govalid-fb-prop-rating-max" class="govalid-fb-prop-input" min="3" max="10" value="5" />
		</div>

		<!-- Signature settings -->
		<div class="govalid-fb-prop-group" id="govalid-fb-prop-signature-wrap" style="display: none;">
			<label><?php esc_html_e( 'Canvas Height (px)', 'govalid-qr-validator' ); ?></label>
			<input type="number" id="govalid-fb-prop-signature-height" class="govalid-fb-prop-input" min="100" max="400" value="200" />
		</div>

		<!-- Page Break settings -->
		<div class="govalid-fb-prop-group" id="govalid-fb-prop-pagebreak-wrap" style="display: none;">
			<label><?php esc_html_e( 'Step Label', 'govalid-qr-validator' ); ?></label>
			<input type="text" id="govalid-fb-prop-pagebreak-label" class="govalid-fb-prop-input" placeholder="<?php esc_attr_e( 'e.g. Step 2', 'govalid-qr-validator' ); ?>" />
		</div>

		<!-- Conditional Logic -->
				<div class="govalid-fb-prop-group" id="govalid-fb-prop-conditional-wrap">
					<label>
						<input type="checkbox" id="govalid-fb-prop-conditional-enabled" />
						<?php esc_html_e( 'Conditional Logic', 'govalid-qr-validator' ); ?>
					</label>
					<div id="govalid-fb-conditional-config" style="display: none; margin-top: 8px;">
						<div style="display: flex; gap: 8px; align-items: center; flex-wrap: wrap;">
							<select id="govalid-fb-cond-action" class="govalid-fb-prop-input" style="width: auto;">
								<option value="show"><?php esc_html_e( 'Show', 'govalid-qr-validator' ); ?></option>
								<option value="hide"><?php esc_html_e( 'Hide', 'govalid-qr-validator' ); ?></option>
							</select>
							<span><?php esc_html_e( 'when', 'govalid-qr-validator' ); ?></span>
							<select id="govalid-fb-cond-field" class="govalid-fb-prop-input" style="width: auto; min-width: 120px;"></select>
							<select id="govalid-fb-cond-operator" class="govalid-fb-prop-input" style="width: auto;">
								<option value="equals"><?php esc_html_e( 'equals', 'govalid-qr-validator' ); ?></option>
								<option value="not_equals"><?php esc_html_e( 'not equals', 'govalid-qr-validator' ); ?></option>
								<option value="contains"><?php esc_html_e( 'contains', 'govalid-qr-validator' ); ?></option>
								<option value="not_empty"><?php esc_html_e( 'is not empty', 'govalid-qr-validator' ); ?></option>
							</select>
							<input type="text" id="govalid-fb-cond-value" class="govalid-fb-prop-input"
								   placeholder="<?php esc_attr_e( 'Value', 'govalid-qr-validator' ); ?>" style="width: 120px;" />
						</div>
					</div>
				</div>

				<div style="margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--gv-gray-200);">
					<button type="button" id="govalid-fb-remove-field" class="button" style="color: var(--gv-danger);">
						<span class="dashicons dashicons-trash" style="margin-top: 4px;"></span>
						<?php esc_html_e( 'Remove Field', 'govalid-qr-validator' ); ?>
					</button>
				</div>
			</div>
		</div>
	</div>

	<!-- Form Settings (below the builder) -->
	<div class="govalid-card" style="max-width: none; margin-top: 20px;">
		<h3 style="margin-top: 0;">
			<span class="dashicons dashicons-admin-generic" style="margin-right: 6px;"></span>
			<?php esc_html_e( 'Form Settings', 'govalid-qr-validator' ); ?>
		</h3>
		<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px;">
			<div class="govalid-fb-prop-group">
				<label><?php esc_html_e( 'Submit Button Text', 'govalid-qr-validator' ); ?></label>
				<input type="text" id="govalid-fb-setting-submit-text" class="govalid-fb-prop-input"
					   value="<?php esc_attr_e( 'Submit', 'govalid-qr-validator' ); ?>" />
			</div>
			<div class="govalid-fb-prop-group">
				<label><?php esc_html_e( 'Success Message', 'govalid-qr-validator' ); ?></label>
				<input type="text" id="govalid-fb-setting-success-msg" class="govalid-fb-prop-input"
					   value="<?php esc_attr_e( 'Thank you! Your submission has been received.', 'govalid-qr-validator' ); ?>" />
			</div>
			<div class="govalid-fb-prop-group">
				<label>
					<input type="checkbox" id="govalid-fb-setting-email-notification" />
					<?php esc_html_e( 'Email notification on new submission', 'govalid-qr-validator' ); ?>
				</label>
			</div>
			<div class="govalid-fb-prop-group" id="govalid-fb-notif-email-wrap" style="display: none;">
				<label><?php esc_html_e( 'Notification Email', 'govalid-qr-validator' ); ?></label>
				<input type="email" id="govalid-fb-setting-notif-email" class="govalid-fb-prop-input"
					   value="<?php echo esc_attr( get_option( 'admin_email' ) ); ?>" />
			</div>
		</div>
	</div>
</div>

