<?php
/**
 * Admin View: CSV Import done.
 *
 * @package WooCommerce Bookings
 * @since   3.6.0
 * @version 3.6.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
?>
<div class="wc-progress-form-content woocommerce-importer">
	<section class="woocommerce-importer-done">
		<?php
		$results = array();

		if ( 0 < $imported ) {
			$results[] = sprintf(
				/* translators: %s: bookings count */
				_n( '%s booking imported', '%s bookings imported', $imported, 'woocommerce-bookings' ),
				'<strong>' . number_format_i18n( $imported ) . '</strong>'
			);
		}

		if ( 0 < $updated ) {
			$results[] = sprintf(
				/* translators: %s: bookings count */
				_n( '%s booking updated', '%s bookings updated', $updated, 'woocommerce-bookings' ),
				'<strong>' . number_format_i18n( $updated ) . '</strong>'
			);
		}

		if ( 0 < $skipped ) {
			$results[] = sprintf(
				/* translators: %s: bookings count */
				_n( '%s booking was skipped', '%s bookings were skipped', $skipped, 'woocommerce-bookings' ),
				'<strong>' . number_format_i18n( $skipped ) . '</strong>'
			);
		}

		if ( 0 < $failed ) {
			$results[] = sprintf(
				/* translators: %s: bookings count */
				_n( 'Failed to import %s booking', 'Failed to import %s bookings', $failed, 'woocommerce-bookings' ),
				'<strong>' . number_format_i18n( $failed ) . '</strong>'
			);
		}

		if ( count( $warnings ) ) {
			$results[] = sprintf(
				/* translators: %s: warnings count */
				_n( '%s warning', '%s warnings', count( $warnings ), 'woocommerce-bookings' ),
				'<strong>' . number_format_i18n( count( $warnings ) ) . '</strong>'
			);
		}

		if ( 0 < $failed || 0 < $skipped || count( $warnings ) ) {
			$results[] = '<a href="#" class="woocommerce-importer-done-view-errors">' . esc_html__( 'View import log', 'woocommerce-bookings' ) . '</a>';
		}

		if ( ! empty( $file_name ) ) {
			$results[] = sprintf(
				/* translators: %s: File name */
				__( 'File uploaded: %s', 'woocommerce-bookings' ),
				'<strong>' . esc_html( $file_name ) . '</strong>'
			);
		}

		echo wp_kses_post( __( 'Import complete!', 'woocommerce-bookings' ) . ' ' . implode( '. ', $results ) );
		?>
	</section>
	<section class="wc-importer-error-log" style="display:none">
		<table class="widefat wc-importer-error-log-table">
			<thead>
				<tr>
					<th><?php esc_html_e( 'Booking', 'woocommerce-bookings' ); ?></th>
					<th><?php esc_html_e( 'Reason for failure', 'woocommerce-bookings' ); ?></th>
				</tr>
			</thead>
			<tbody>
				<?php
				if ( count( $errors ) ) {
					foreach ( $errors as $row_error ) {
						if ( ! is_wp_error( $row_error ) ) {
							continue;
						}
						$error_data = $row_error->get_error_data();
						?>
						<tr>
							<th><code><?php echo isset( $error_data['row'] ) ? esc_html( $error_data['row'] ) : ''; ?></code></th>
							<td><?php echo esc_html( $row_error->get_error_message() ); ?></td>
						</tr>
						<?php
					}
				}
				?>
			</tbody>
		</table>
		<?php if ( count( $warnings ) ) : ?>
		<table class="widefat wc-importer-warning-log-table" style="margin-top:1em;">
			<thead>
				<tr>
					<th><?php esc_html_e( 'Booking', 'woocommerce-bookings' ); ?></th>
					<th><?php esc_html_e( 'Warning', 'woocommerce-bookings' ); ?></th>
				</tr>
			</thead>
			<tbody>
				<?php foreach ( $warnings as $warning ) : ?>
					<tr>
						<th><code><?php echo ! empty( $warning['booking_id'] ) ? esc_html( '#' . absint( $warning['booking_id'] ) ) : ''; ?></code></th>
						<td><?php echo esc_html( $warning['message'] ); ?></td>
					</tr>
				<?php endforeach; ?>
			</tbody>
		</table>
		<?php endif; ?>
	</section>
	<script type="text/javascript">
		jQuery( function() {
			jQuery( '.woocommerce-importer-done-view-errors' ).on( 'click', function() {
				jQuery( '.wc-importer-error-log' ).slideToggle();
				return false;
			} );
		} );
	</script>
	<div class="wc-actions">
		<a class="button button-primary" href="<?php echo esc_url( admin_url( 'edit.php?post_type=wc_booking' ) ); ?>"><?php esc_html_e( 'View bookings', 'woocommerce-bookings' ); ?></a>
	</div>
</div>
