<?php
/**
 *
 *
 * @var int    $form_id         Which form we are dealing with.
 * @var array  $entries         Which entries we are modifying.
 * @var array  $selected_fields Which fields were selected.
 * @var string $view            Which view are in.
 * @var mixed  $form            Form settings.
 */

use GravityKit\GravityActions\Assets;
use GravityKit\GravityActions\Actions\EditAction;
use GravityKit\GravityActions\Utils;

$fields_available      = '<span class="gv-bulk-edit-all-fields-counter">' . count( $form['fields'] ) . '</span>';
$count_selected_fields = count( $selected_fields );

$formula_fields = Utils\Form::get_formula_fields( $form );
?>

<form
	data-js="gk-gravityactions-modal-form"
	id="gk-gravityactions-edit-step-1"
>
	<?php $this->render( 'components/modal-fields', [ 'bulk_action' => EditAction::get_key() ] ); ?>
	<?php $this->render( 'components/header' ); ?>
	<?php $this->render( 'edit/fields-hidden' ); ?>

	<div class="gk-gravityactions-modal-body">

		<div class="gv-bulk-edit-step gv-bulk-edit-step-1">

			<div class="gv-bulk-edit-step-help-side">

				<img width="288" height="236" src="<?php echo esc_url( Assets::get_url( 'src/assets/images/edit_step_1_character.svg' ) ); ?>" alt=""/>

				<span class="gk-gravityactions-modal-help-title">
					<?php esc_html_e( 'Select fields to edit', 'gk-gravityactions' ); ?>
				</span>

				<span class="gk-gravityactions-modal-help-subtitle">
					<?php esc_html_e( 'Let’s select some fields from these entries to edit.', 'gk-gravityactions' ); ?>
				</span>

				<div class="gk-gravityactions-modal-help-link">
					<img src="<?php echo esc_url( Assets::get_url( 'src/assets/images/help_question.svg' ) ); ?>" alt="Help"/>
					<a href='https://docs.gravitykit.com/article/801-bulk-edit-step-what-are-these-fields' data-beacon-article='615c5acde5648623c88e19b5' target='_blank' rel='noopener noreferrer'><?php esc_html_e( 'What are these fields?', 'gk-gravityactions' ); ?></a>
				</div>

			</div>

			<div class="gv-bulk-edit-step-content-side">

				<div class="gv-bulk-edit-step-content-side-header">
					<div class="gv-bulk-edit-step-1-fields-title">
						<?php printf( _n( 'Available Field (%1$s)', 'Available Fields (%1$s)', count( $form['fields'] ), 'gk-gravityactions' ), $fields_available ); ?>
					</div>

					<div
						class="gv-bulk-edit-selected-fields-counter gk-gravityactions-edit-selected-field-count"
						data-count-selected="<?php echo esc_attr( $count_selected_fields ); ?>"
						data-none-selected="<?php esc_attr_e( 'None selected', 'gk-gravityactions' ); ?>"
						data-one-selected="<?php esc_attr_e( 'One selected', 'gk-gravityactions' ); ?>"
						data-multiple-selected="<?php esc_attr_e( '%s selected', 'gk-gravityactions' ); ?>"
					>
						<?php if ( 0 === $count_selected_fields ) : ?>
							<?php esc_html_e( 'None selected', 'gk-gravityactions' ); ?>
						<?php elseif ( 1 === $count_selected_fields ) : ?>
							<?php esc_html_e( 'One selected', 'gk-gravityactions' ); ?>
						<?php else : ?>
							<?php echo esc_html( sprintf( __( '%1$d selected', 'gk-gravityactions' ), $count_selected_fields ) ); ?>
						<?php endif; ?>
					</div>
				</div>

				<div class="gv-bulk-edit-search-box">
					<label>
						<span class="screen-reader-text"><?php esc_attr_e( 'Search Fields...', 'gk-gravityactions' ); ?></span>
						<input type="text" placeholder="<?php esc_attr_e( 'Search Fields...', 'gk-gravityactions' ); ?>" class="gv-bulk-edit-search"/>
					</label>

					<img class="gv-bulk-edit-search-icon" src="<?php echo esc_url( Assets::get_url( 'src/assets/images/search.svg' ) ); ?>" alt="<?php esc_attr_e( 'Search', 'gk-gravityactions' ); ?>"/>
				</div>

				<ul class="gv-bulk-edit-step-1-fields">
					<?php foreach ( $form['fields'] as $field ) : ?>
						<?php
						if ( ! EditAction::is_valid_field_type( $field ) ) {
							continue;
						}

						if ( $field->has_calculation() ) {
							continue;
						}

						$field_unique_id = uniqid( 'gk-gravityactions-field-', true );
						?>
						<li
							class="gk-gravityactions-edit-field-item gk-gravityactions-edit-field-parent"
							data-filter-name="<?php echo esc_attr( mb_strtolower( $field->label ) ); ?>"
							data-filter-type="parent field"
							data-id="<?php echo esc_attr( $field->id ); ?>"
						>
							<label
								class="gv-bulk-edit-step-1-field"
								for="<?php echo sanitize_html_class( $field_unique_id ); ?>"
							>
								<input
									class="gk-gravityactions-edit-selected-field"
									id="<?php echo sanitize_html_class( $field_unique_id ); ?>"
									type="checkbox"
									value="<?php echo esc_attr( $field->id ); ?>"
									<?php checked( in_array( $field->id, $selected_fields ) ); ?>
								/>
								<span
									class="gk-gravityactions-edit-field-name"
								>
									<?php echo esc_html( $field->label ); ?>
								</span>
								<?php if ( in_array( $field->id, $formula_fields )  ) { ?>
									<span
										class="gk-gravityactions-edit-field-identifier"
										title="<?php esc_attr_e( 'This field is part of a formula so it will prompt you to update the fields affected by the changes.', 'gk-gravityactions' ); ?>"
									>
										<span class="dashicons dashicons-calculator"></span>
									</span>
								<?php } ?>
							</label>
						</li>
						<?php if ( ! empty( $field->inputs ) && ! $field instanceof GF_Field_Time ) : ?>
							<?php foreach ( $field->inputs as $input ) :
								$input_unique_id = uniqid( 'gk-gravityactions-field-', true );
								?>
								<li
									class="gk-gravityactions-edit-field-item gk-gravityactions-edit-field-group gk-gravityactions-edit-field-child"
									data-filter-name="<?php echo esc_attr( mb_strtolower( $field->label ) . ' ' . mb_strtolower( $input['label'] ) ); ?>"
									data-filter-type="child field"
									data-id="<?php echo esc_attr( $field->id ); ?>"
								>
									<label
										class="gv-bulk-edit-step-1-field"
										for="gk-gravityactions-edit-field-<?php echo esc_attr( $input_unique_id ); ?>"
									>
										<input
											class="gk-gravityactions-edit-selected-field"
											id="gk-gravityactions-edit-field-<?php echo esc_attr( $input_unique_id ); ?>"
											type="checkbox"
											value="<?php echo esc_attr( $input['id'] ); ?>"
											<?php checked( in_array( $input['id'], $selected_fields ) ); ?>
										/>
										<span
											class="gk-gravityactions-edit-field-name"
										>
											<?php echo esc_html( $input['label'] ); ?>
										</span>
									</label>
								</li>
							<?php endforeach; ?>
						<?php endif; ?>
					<?php endforeach; ?>
					<?php foreach ( EditAction::get_supported_form_meta() as $meta ) : ?>
						<li
							class="gk-gravityactions-edit-field-item gk-gravityactions-edit-field-meta"
							data-filter-name="<?php echo esc_attr( mb_strtolower( $meta->label ) ); ?>"
							data-filter-type="meta"
							data-id="<?php echo esc_attr( $meta->id ); ?>"
						>
							<label
								class="gv-bulk-edit-step-1-field"
								for="gk-gravityactions-edit-field-<?php echo esc_attr( $meta->id ); ?>"
							>
								<input
									class="gk-gravityactions-edit-selected-field"
									id="gk-gravityactions-edit-field-<?php echo esc_attr( $meta->id ); ?>"
									type="checkbox"
									value="<?php echo esc_attr( $meta->id ); ?>"
									<?php checked( in_array( $meta->id, $selected_fields ) ); ?>
								/>
								<span
									class="gk-gravityactions-edit-field-name"
								>
									<?php echo esc_html( $meta->label ); ?>
								</span>
								<span class="gk-gravityactions-label">
									<?php esc_html_e( 'Meta', 'gk-gravityactions' ); ?>
								</span>
							</label>
						</li>
					<?php endforeach; ?>
				</ul>

				<?php foreach ( $form['fields'] as $field ) : ?>
					<input
						class="gk-gravityactions-hidden-selected-fields"
						type="hidden"
						name="selected_fields"
						value="<?php echo esc_attr( $field->id ); ?>"
						<?php disabled( ! in_array( $field->id, $selected_fields ) ); ?>
					/>

					<?php if ( ! empty( $field->inputs ) ) : ?>
						<?php
							foreach ( $field->inputs as $input ) :
								if ( empty( $input['id'] ) ) {
									continue;
								}
						?>
						<input
							class="gk-gravityactions-hidden-selected-fields"
							type="hidden"
							name="selected_fields"
							data-parent-id="<?php echo esc_attr( $field->id ); ?>"
							value="<?php echo esc_attr( $input['id'] ); ?>"
							<?php disabled( ! in_array( $field['id'], $selected_fields ) ); ?>
						/>
						<?php endforeach; ?>
					<?php endif; ?>

				<?php endforeach; ?>
				<?php foreach ( EditAction::get_supported_form_meta() as $meta ) : ?>
					<input
						class="gk-gravityactions-hidden-selected-fields"
						type="hidden"
						name="selected_fields"
						value="<?php echo esc_attr( $meta->id ); ?>"
						<?php disabled( ! in_array( $meta->id, $selected_fields ) ); ?>
					/>
				<?php endforeach; ?>
			</div>

		</div>
	</div>

	<?php $this->render( 'components/footer', [
		'current_step'     => 0,
		'total_steps'      => 3,
		'left_navigation'  => '<span class="featherlight-close">' . esc_html__( 'Close', 'gk-gravityactions' ) . '</span>',
		'right_navigation' => get_submit_button( __( 'Next', 'gk-gravityactions' ), 'primary', 'submit', false, [
			'data-js'         => 'gk-gravityactions-modal-submit',
			'data-modal-view' => 'edit/step-2',
		] ),
	] ); ?>

</form>