<?php defined( 'ABSPATH' ) or die;

function render_rows( $order, $row_data ) {

	switch( $row_data[ 'type' ] ) {
		case ONE_COL:
			$data = [
				'type' => ONE_COL,
				'class' => 'tech360-row tech360-1-col',
				'title' => COL_NAMES[ ONE_COL ],
				'content' => [ esc_textarea( $row_data[ 'content' ] ) ],
				'settings' => $row_data[ 'settings' ]
			];
			break;
		case TWO_COL:
			$data = [
				'type' => TWO_COL,
				'class' => 'tech360-row tech360-2-col',
				'title' => COL_NAMES[ TWO_COL ],
				'content' => [ esc_textarea( $row_data[ 'content-1' ] ), esc_textarea( $row_data[ 'content-2' ] ) ],
				'settings' => $row_data[ 'settings' ]
			];
			break;
		case THREE_COL:
			$data = [
				'type' => THREE_COL,
				'class' => 'tech360-row tech360-3-col',
				'title' => COL_NAMES[ THREE_COL ],
				'content' => [ esc_textarea( $row_data[ 'content-1' ] ), esc_textarea( $row_data[ 'content-2' ] ), esc_textarea( $row_data[ 'content-3' ] ) ],
				'settings' => $row_data[ 'settings' ]
			];
			break;
		case FOUR_COL:
			$data = [
				'type' => FOUR_COL,
				'class' => 'tech360-row tech360-4-col',
				'title' => COL_NAMES[ FOUR_COL ],
				'content' => [ esc_textarea( $row_data[ 'content-1' ] ), esc_textarea( $row_data[ 'content-2' ] ), esc_textarea( $row_data[ 'content-3' ] ), esc_textarea( $row_data[ 'content-4' ] ) ],
				'settings' => $row_data[ 'settings' ]
			];
			break;
		case ONE_TWO:
			$data = [
				'type' => ONE_TWO,
				'class' => 'tech360-row tech360-1-2',
				'title' => COL_NAMES[ ONE_TWO ],
				'content' => [ esc_textarea( $row_data[ 'content-1' ] ), esc_textarea( $row_data[ 'content-2' ] ) ],
				'settings' => $row_data[ 'settings' ]
			];
			break;
		case TWO_ONE:
			$data = [
				'type' => TWO_ONE,
				'class' => 'tech360-row tech360-2-1',
				'title' => COL_NAMES[ TWO_ONE ],
				'content' => [ esc_textarea( $row_data[ 'content-1' ] ), esc_textarea( $row_data[ 'content-2' ] ) ],
				'settings' => $row_data[ 'settings' ]
			];
			break;
		case ONE_THREE:
			$data = [
				'type' => ONE_THREE,
				'class' => 'tech360-row tech360-1-3',
				'title' => COL_NAMES[ ONE_THREE ],
				'content' => [ esc_textarea( $row_data[ 'content-1' ] ), esc_textarea( $row_data[ 'content-2' ] ) ],
				'settings' => $row_data[ 'settings' ]
			];
			break;
		case THREE_ONE:
			$data = [
				'type' => THREE_ONE,
				'class' => 'tech360-row tech360-3-1',
				'title' => COL_NAMES[ THREE_ONE ],
				'content' => [ esc_textarea( $row_data[ 'content-1' ] ), esc_textarea( $row_data[ 'content-2' ] ) ]
			];
			break;
		default:
			return;
	}

	render_row( $order, $data );

}

function render_row( $order, $data ) {
?>
<div class="<?php echo $data[ 'class' ] ?>">
	<div class="tech360-row-title tech360-row-move">
		<span class="tech360-row-index"><?php echo $order ?></span>
		<h4 class="tech350-row-title-text"><?php echo $data[ 'title' ] ?></h4>
		<span class="tech360-row-remove dashicons dashicons-trash"></span>
	</div>
	<div class="tech360-row-fields">
		<?php
		foreach( $data[ 'content' ] as $i => $content ) {
			$id = 'tech360-editor-' . $order . '-' . (string) ( $i + 1 );
			$name = 'tech360[' . $order . '][content';
			$field = 'content';
			if( $data[ 'type' ] != ONE_COL ) {
				$name .= '-' . (string) ( $i + 1 ) . ']';
				$field .= '-' . (string) ( $i + 1 );
			} else {
				$name .= ']';
			}
		?>
		<div class="tech360-col">
			<textarea id="<?php echo $id ?>" class="tech360-row-input wp-editor" name="<?php echo $name ?>" data-field="<?php echo $field ?>"><?php echo $content ?></textarea>
		</div>
		<?php
		}
		?>
		<input class="tech360-row-input" type="hidden" name="tech360[<?php echo $order ?>][type]" data-field="type" value="<?php echo $data[ 'type' ] ?>">
	</div>
	<div class="tech360-row-settings">
		<div class="tech360-row-title no-border">
			<h4>Row settings</h4>
			<span class="tech360-row-expand dashicons dashicons-arrow-down"></span>
		</div>
		<div class="tech360-row-settings-inner">
		<?php
			$setting_id = 'tech360_id[' . $order . ']';
			$setting_name = 'tech360[' . $order . ']';
		?>
			<div class="tech360-row-setting">
				<label class="tech360-setting-label" for="<?php echo $setting_id ?>[row_class]">
					<strong>Row CSS class</strong>
					<em class="helper">Enter CSS classes that will be applied to this row, separated by comma.</em>
				</label>
				<div class="tech360-setting-value">
					<input id="<?php echo $setting_id ?>[row_class]" class="tech360-row-input tech360-setting" type="text" name="<?php echo $setting_name ?>[row_class]" data-field="row_class" value="<?php echo $data[ 'settings' ][ 'row_class' ] ?>">
				</div>
			</div>
			<div class="tech360-row-setting">
				<label class="tech360-setting-label" for="<?php echo $setting_id ?>[column_class]">
					<strong>Column CSS class</strong>
					<em class="helper">Enter CSS classes that will be applied to each column in this row, separated by comma.</em>
				</label>
				<div class="tech360-setting-value">
					<input id="<?php echo $setting_id ?>[column_class]" class="tech360-row-input tech360-setting" type="text" name="<?php echo $setting_name ?>[column_class]" data-field="column_class" value="<?php echo $data[ 'settings' ][ 'column_class' ] ?>">
				</div>
			</div>
		</div>
	</div>
</div>
<?php
}
