<?php
/**
 * @package MatrixAddons
 * @subpackage trainifyframework
 * @since trainifyframework 1.0.0
 * @version 1.0.0
 * @description Repeater field for widget
 */
$tff_repeater_row_title = isset($widget_field['tff_repeater_row_title']) ? $widget_field['tff_repeater_row_title'] : esc_html__('Row', 'trainify');
$tff_repeater_addnew_label = isset($widget_field['tff_repeater_addnew_label']) ? $widget_field['tff_repeater_addnew_label'] : esc_html__('Add row', 'trainify');
$trainify_framework_widget_field_options = isset($widget_field['trainify_framework_widget_field_options']) ? $widget_field['trainify_framework_widget_field_options'] : array();
$coder_repeater_depth = 'coderRepeaterDepth_'.'0';
$tff_repeater_main_key = $trainify_framework_widget_field_name;
?>
<div class="tff-widget-field-wrapper tff-widget-repeater-wrapper <?php echo esc_attr($trainify_framework_widget_field_wraper); ?>">
	<label class="tff-widget-repeater-heading" for="<?php echo esc_attr( $trainifyframeworkwidget->get_field_id( $trainify_framework_widget_field_name ) ); ?>"><?php echo esc_html( $trainify_framework_widget_field_title ); ?>:</label>
	<div class="trainify-framework-main-repeater">
		<?php
		$repeater_count = 0;
		if( is_array( $trainify_framework_widget_field_value ) && count( $trainify_framework_widget_field_value ) > 0 ){
			foreach ($trainify_framework_widget_field_value as $repeater_key=>$repeater_details){
				?>
				<div class="tff-widget-repeater-table">
					<div class="trainify-framework-repeater-top">
						<div class="trainify-framework-repeater-title-action">
							<button type="button" class="trainify-framework-repeater-action">
								<span class="te-toggle-indicator" aria-hidden="true"></span>
							</button>
						</div>
						<div class="trainify-framework-repeater-title">
							<h3><?php echo esc_html($tff_repeater_row_title); ?><span class="trainify-framework-main-repeater-inner-title"></span></h3>
						</div>
					</div>
					<div class='trainify-framework-inside-repeater hidden'>
						<?php
						foreach($trainify_framework_widget_field_options as $repeater_slug => $repeater_data){
							
							$tff_repeater_child_field_name = (isset($repeater_data['trainify_framework_widget_field_name'] ) ) ? esc_attr($repeater_data['trainify_framework_widget_field_name']) : '';
							$repeater_field_id  = esc_attr($trainifyframeworkwidget->get_field_id( $trainify_framework_widget_field_name).$tff_repeater_child_field_name);
							$trainify_framework_widget_field_name = esc_attr($tff_repeater_main_key.'['.$repeater_count.']['.$tff_repeater_child_field_name.']');
							$repeater_data['trainify_framework_widget_field_name'] = $trainify_framework_widget_field_name;
							$trainify_framework_widget_field_default = (isset($repeater_data['trainify_framework_widget_field_default']) ) ? $repeater_data['trainify_framework_widget_field_default'] : '';
							$trainify_framework_widget_field_value = ( isset($repeater_details[$tff_repeater_child_field_name] ) ) ? $repeater_details[$tff_repeater_child_field_name] : $trainify_framework_widget_field_default;
							trainify_framework_widgets_show_widget_field( $trainifyframeworkwidget, $repeater_data, $trainify_framework_widget_field_value );
						}
						?>
						<div class="trainify-framework-main-repeater-control-actions">
							<button type="button" class="button-link button-link-delete trainify-framework-main-repeater-remove"><?php esc_html_e('Remove','trainify');?></button> |
							<button type="button" class="button-link trainify-framework-main-repeater-close"><?php esc_html_e('Close','trainify');?></button>
						</div>
					</div>
				</div>
				<?php
				$repeater_count++;
			}
		}

		?>
		<script type="text/html" class="tff-code-for-repeater">
			<div class="tff-widget-repeater-table">
				<div class="trainify-framework-repeater-top">
					<div class="trainify-framework-repeater-title-action">
						<button type="button" class="trainify-framework-repeater-action">
							<span class="te-toggle-indicator" aria-hidden="true"></span>
						</button>
					</div>
					<div class="trainify-framework-repeater-title">
						<h3><?php echo esc_html($tff_repeater_row_title); ?><span class="trainify-framework-main-repeater-inner-title"></span></h3>
					</div>
				</div>
				<div class='trainify-framework-inside-repeater hidden'>
					<?php
					
					foreach($trainify_framework_widget_field_options as $repeater_slug => $repeater_data){
						/**/
						$tff_repeater_child_field_name = (isset($repeater_data['trainify_framework_widget_field_name'] ) ) ? esc_attr($repeater_data['trainify_framework_widget_field_name']) : '';
						$repeater_field_id  = esc_attr($trainifyframeworkwidget->get_field_id( $trainify_framework_widget_field_name).$tff_repeater_child_field_name);
						$trainify_framework_widget_field_name = esc_attr($tff_repeater_main_key.'['.$coder_repeater_depth.']['.$tff_repeater_child_field_name.']');
						$repeater_data['trainify_framework_widget_field_name'] = $trainify_framework_widget_field_name;
						$trainify_framework_widget_field_default = isset($repeater_data['trainify_framework_widget_field_default']) ? $repeater_data['trainify_framework_widget_field_default'] : '';
						trainify_framework_widgets_show_widget_field( $trainifyframeworkwidget, $repeater_data, $trainify_framework_widget_field_default );
					}
					?>
					<div class="trainify-framework-main-repeater-control-actions">
						<button type="button" class="button-link button-link-delete trainify-framework-main-repeater-remove"><?php esc_html_e('Remove','trainify');?></button> |
						<button type="button" class="button-link trainify-framework-main-repeater-close"><?php esc_html_e('Close','trainify');?></button>
					</div>
				</div>
			</div>
		</script>

		<input class="tff-total-repeater-counter" type="hidden" value="<?php echo esc_attr( $repeater_count ) ?>">
		<span class="button tff-add-repeater" id="<?php echo esc_attr( $coder_repeater_depth ); ?>"><?php echo esc_html($tff_repeater_addnew_label); ?></span><br/>

	</div>
	<?php if ( isset( $trainify_framework_widget_field_description ) ) { ?>
		<small><?php echo esc_html( $trainify_framework_widget_field_description ); ?></small>
	<?php } ?>
</div>