<?php
/**
 * Forms tab — what to send when somebody submits a form.
 *
 * @package TextTango
 *
 * @var array<string, mixed> $args View data supplied by SettingsPage::render().
 */

declare( strict_types=1 );

defined( 'ABSPATH' ) || exit;

use TextTango\Forms\FormSettings;
use TextTango\Forms\Forms;
use TextTango\Forms\Submission;

$texttango_providers = (array) ( $args['form_providers'] ?? array() );
$texttango_config    = (array) ( $args['form_config'] ?? array() );
?>
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
	<input type="hidden" name="action" value="<?php echo esc_attr( (string) $args['save_action'] ); ?>" />
	<input type="hidden" name="texttango_tab" value="<?php echo esc_attr( (string) $args['tab'] ); ?>" />
	<?php wp_nonce_field( (string) $args['save_nonce'] ); ?>

	<div class="tt-card tt-mb-4">
		<h2 class="tt-m-0 tt-mb-1 tt-p-0 tt-text-lg tt-font-semibold tt-text-slate-900">
			<?php esc_html_e( 'Forms', 'texttango' ); ?>
		</h2>
		<p class="tt-m-0 tt-mb-4 tt-max-w-2xl tt-text-sm tt-text-slate-500">
			<?php esc_html_e( 'Text yourself when somebody fills in a form, and text them back. Every form is off until you switch it on and say which field holds the phone number — there is no way to guess that, and guessing wrong would send a message to whatever else was in the box.', 'texttango' ); ?>
		</p>

		<?php if ( empty( $texttango_providers ) ) : ?>
			<?php
			load_template(
				TEXTTANGO_PATH . 'templates/admin/partials/empty-state.php',
				false,
				array(
					'title' => __( 'No form plugin found.', 'texttango' ),
					'lead'  => __( 'This works with Contact Form 7, WPForms, Gravity Forms and Fluent Forms. Install and activate one and its forms will appear here.', 'texttango' ),
				)
			);
			?>
		<?php endif; ?>

		<?php foreach ( $texttango_providers as $texttango_provider ) : ?>
			<section class="tt-mb-8 last:tt-mb-0">
				<h3 class="tt-eyebrow tt-m-0 tt-mb-3 tt-p-0"><?php echo esc_html( (string) $texttango_provider['label'] ); ?></h3>

				<?php if ( empty( $texttango_provider['forms'] ) ) : ?>
					<p class="tt-hint tt-m-0">
						<?php esc_html_e( 'No forms yet. Build one and it will appear here.', 'texttango' ); ?>
					</p>
				<?php endif; ?>

				<?php foreach ( (array) $texttango_provider['forms'] as $texttango_form ) : ?>
					<?php
					$texttango_key    = Submission::key_for( (string) $texttango_provider['slug'], (string) $texttango_form['id'] );
					$texttango_form_c = $texttango_config[ $texttango_key ] ?? FormSettings::defaults();
					$texttango_name   = 'forms[' . $texttango_key . ']';
					$texttango_id     = 'texttango-form-' . sanitize_html_class( $texttango_key );
					$texttango_fields = (array) $texttango_form['fields'];
					?>
					<?php
					$texttango_said  = FormSettings::describe( $texttango_form_c );
					$texttango_state = (string) $texttango_said['state'];

					/*
					 * Never opened for us. It used to open every form that was
					 * switched on, which meant the more a merchant had configured
					 * the longer the screen got — twenty set-up forms were twenty
					 * expanded panels. The row below says enough to leave closed.
					 */
					$texttango_row = FormSettings::STATE_ON === $texttango_state ? 'tt-stage-done' : '';
					$texttango_row = FormSettings::STATE_ATTENTION === $texttango_state ? 'tt-stage-blocked' : $texttango_row;
					?>
					<details class="tt-stage">
						<summary class="tt-stage-row <?php echo esc_attr( $texttango_row ); ?>">
							<span class="tt-stage-mark" aria-hidden="true">
								<?php echo FormSettings::STATE_ATTENTION === $texttango_state ? '!' : '&check;'; ?>
							</span>
							<span class="tt-stage-name"><?php echo esc_html( (string) $texttango_form['title'] ); ?></span>
							<?php
							/*
							 * Their own wrapper rather than `.tt-stage-meta`, which is
							 * monospace and gets recoloured amber by `.tt-stage-blocked` —
							 * both wrong for something that carries its own colour.
							 */
							?>
							<span class="tt-shrink-0 tt-flex tt-flex-wrap tt-items-center tt-justify-end tt-gap-2">
								<?php foreach ( (array) $texttango_said['badges'] as $texttango_badge ) : ?>
									<span
										class="tt-badge tt-badge-<?php echo esc_attr( (string) $texttango_badge['tone'] ); ?>"
										<?php if ( '' !== (string) $texttango_badge['title'] ) : ?>
											title="<?php echo esc_attr( (string) $texttango_badge['title'] ); ?>"
										<?php endif; ?>
									><?php echo esc_html( (string) $texttango_badge['label'] ); ?></span>
								<?php endforeach; ?>
							</span>
							<span class="tt-stage-chevron" aria-hidden="true">&rsaquo;</span>
						</summary>

						<div class="tt-stage-body">
							<label class="tt-mb-4 tt-flex tt-items-start tt-gap-2 tt-text-sm tt-text-slate-700">
								<input type="checkbox" class="tt-mt-0.5" name="<?php echo esc_attr( $texttango_name ); ?>[enabled]" value="1"
									<?php checked( ! empty( $texttango_form_c['enabled'] ) ); ?> />
								<span><?php esc_html_e( 'Send something when this form is submitted', 'texttango' ); ?></span>
							</label>

							<div class="tt-grid tt-grid-cols-1 tt-gap-4 sm:tt-grid-cols-2">
								<div>
									<label class="tt-label" for="<?php echo esc_attr( $texttango_id ); ?>-phone">
										<?php esc_html_e( 'Which field holds the phone number', 'texttango' ); ?>
									</label>
									<?php
									load_template(
										TEXTTANGO_PATH . 'templates/admin/partials/field-picker.php',
										false,
										array(
											'id'     => $texttango_id . '-phone',
											'name'   => $texttango_name . '[phone_field]',
											'value'  => (string) $texttango_form_c['phone_field'],
											'fields' => $texttango_fields,
											'none'   => __( 'Do not reply to whoever submitted', 'texttango' ),
										)
									);
									?>
									<span class="tt-hint tt-block">
										<?php esc_html_e( 'Without this, nothing can be sent to the person who submitted.', 'texttango' ); ?>
									</span>
								</div>

								<div>
									<label class="tt-label" for="<?php echo esc_attr( $texttango_id ); ?>-consent">
										<?php esc_html_e( 'Marketing consent checkbox', 'texttango' ); ?>
									</label>
									<?php
									load_template(
										TEXTTANGO_PATH . 'templates/admin/partials/field-picker.php',
										false,
										array(
											'id'     => $texttango_id . '-consent',
											'name'   => $texttango_name . '[consent_field]',
											'value'  => (string) $texttango_form_c['consent_field'],
											'fields' => $texttango_fields,
											'none'   => __( 'This form does not ask', 'texttango' ),
										)
									);
									?>
									<span class="tt-hint tt-block">
										<?php esc_html_e( 'Ticked, the number is added to your consent list so campaigns can reach it. Nothing is sent by ticking it.', 'texttango' ); ?>
									</span>
								</div>
							</div>

							<hr class="tt-my-5 tt-border-0 tt-border-t tt-border-solid tt-border-slate-200" />

							<label class="tt-mb-2 tt-flex tt-items-start tt-gap-2 tt-text-sm tt-text-slate-700">
								<input type="checkbox" class="tt-mt-0.5" name="<?php echo esc_attr( $texttango_name ); ?>[alert]" value="1"
									<?php checked( ! empty( $texttango_form_c['alert'] ) ); ?> />
								<span>
									<?php esc_html_e( 'Tell me when somebody submits', 'texttango' ); ?>
									<span class="tt-hint tt-block">
										<?php esc_html_e( 'Goes to the staff numbers on the Notifications tab.', 'texttango' ); ?>
									</span>
								</span>
							</label>
							<textarea class="tt-input tt-mb-5" rows="2" name="<?php echo esc_attr( $texttango_name ); ?>[alert_template]"
								placeholder="<?php echo esc_attr( Forms::default_alert() ); ?>"
							><?php echo esc_textarea( (string) $texttango_form_c['alert_template'] ); ?></textarea>

							<label class="tt-mb-2 tt-flex tt-items-start tt-gap-2 tt-text-sm tt-text-slate-700">
								<input type="checkbox" class="tt-mt-0.5" name="<?php echo esc_attr( $texttango_name ); ?>[reply]" value="1"
									<?php checked( ! empty( $texttango_form_c['reply'] ) ); ?> />
								<span>
									<?php esc_html_e( 'Text the person who submitted', 'texttango' ); ?>
									<span class="tt-hint tt-block">
										<?php esc_html_e( 'A confirmation of the thing they just did. Your opt-out list still applies.', 'texttango' ); ?>
									</span>
								</span>
							</label>
							<textarea class="tt-input" rows="2" name="<?php echo esc_attr( $texttango_name ); ?>[reply_template]"
								placeholder="<?php echo esc_attr( Forms::default_reply() ); ?>"
							><?php echo esc_textarea( (string) $texttango_form_c['reply_template'] ); ?></textarea>

							<?php if ( ! empty( $texttango_fields ) ) : ?>
								<p class="tt-hint tt-mt-3 tt-mb-0">
									<?php esc_html_e( 'Merge tags:', 'texttango' ); ?>
									<code class="tt-text-xs">{form_name}</code>
									<code class="tt-text-xs">{site_name}</code>
									<?php foreach ( $texttango_fields as $texttango_field ) : ?>
										<?php $texttango_tag = Submission::tag_for( (string) $texttango_field ); ?>
										<?php if ( '{}' !== $texttango_tag ) : ?>
											<code class="tt-text-xs" title="<?php echo esc_attr( (string) $texttango_field ); ?>"><?php echo esc_html( $texttango_tag ); ?></code>
										<?php endif; ?>
									<?php endforeach; ?>
								</p>
							<?php endif; ?>
						</div>
					</details>
				<?php endforeach; ?>
			</section>
		<?php endforeach; ?>
	</div>

	<?php if ( ! empty( $texttango_providers ) ) : ?>
		<button type="submit" class="tt-btn tt-btn-primary"><?php esc_html_e( 'Save settings', 'texttango' ); ?></button>
	<?php endif; ?>
</form>
