<?php
if ( ! defined( 'ABSPATH' ) ) {
	die( 'Direct access is not allowed.' );
}

// Filter dropdown options (value => label) from FinalPOS_Dashboard::get_ecommerce_log_filter_options(), passed in when this view is included.
$source_options      = $filter_options['source'];
$type_options        = $filter_options['type'];
$entity_type_options = $filter_options['entity_type'];
$order_options       = $filter_options['sort_by'];
?>
<div class="finalpos-logs-tab">
	<form method="get" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>" class="finalpos-logs-filters">
		<input type="hidden" name="page" value="<?php echo esc_attr( FINALPOS_PAGE_LOGS ); ?>" />
		<div class="finalpos-logs-filters-row">
			<label for="finalpos-log-source"><?php esc_html_e( 'Source', 'finalpos' ); ?></label>
			<select name="source" id="finalpos-log-source" class="finalpos-log-filter">
				<?php foreach ( $source_options as $value => $label ) : ?>
					<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $filters['source'], $value ); ?>><?php echo esc_html( $label ); ?></option>
				<?php endforeach; ?>
			</select>
			<label for="finalpos-log-type"><?php esc_html_e( 'Type', 'finalpos' ); ?></label>
			<select name="type" id="finalpos-log-type" class="finalpos-log-filter">
				<?php foreach ( $type_options as $value => $label ) : ?>
					<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $filters['type'], $value ); ?>><?php echo esc_html( $label ); ?></option>
				<?php endforeach; ?>
			</select>
			<label for="finalpos-log-entity-type"><?php esc_html_e( 'Entity type', 'finalpos' ); ?></label>
			<select name="entity_type" id="finalpos-log-entity-type" class="finalpos-log-filter">
				<?php foreach ( $entity_type_options as $value => $label ) : ?>
					<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $filters['entity_type'], $value ); ?>><?php echo esc_html( $label ); ?></option>
				<?php endforeach; ?>
			</select>
			<label for="finalpos-log-entity-id"><?php esc_html_e( 'Entity ID', 'finalpos' ); ?></label>
			<input type="text" name="entity_id" id="finalpos-log-entity-id" class="finalpos-log-filter" value="<?php echo esc_attr( isset( $filters['entity_id'] ) ? $filters['entity_id'] : '' ); ?>" placeholder="<?php esc_attr_e( 'Search by entity ID', 'finalpos' ); ?>" />
			<label for="finalpos-log-sort-by"><?php esc_html_e( 'Sort by Created', 'finalpos' ); ?></label>
			<select name="sort_by" id="finalpos-log-sort-by" class="finalpos-log-filter">
				<?php foreach ( $order_options as $value => $label ) : ?>
					<option value="<?php echo esc_attr( $value ); ?>" <?php selected( isset( $filters['sort_by'] ) ? $filters['sort_by'] : 'desc', $value ); ?>><?php echo esc_html( $label ); ?></option>
				<?php endforeach; ?>
			</select>
			<button type="submit" class="button button-primary"><?php esc_html_e( 'Apply filters', 'finalpos' ); ?></button>
			<a href="<?php echo esc_url( admin_url( 'admin.php?page=' . FINALPOS_PAGE_LOGS ) ); ?>" class="button"><?php esc_html_e( 'Reset', 'finalpos' ); ?></a>
		</div>
	</form>

	<?php
	$logs_list_table->prepare_items();
	$logs_error = $logs_list_table->get_logs_error();
	?>
	<?php if ( $logs_error ) : ?>
		<div class="notice notice-error">
			<p><?php echo esc_html( $logs_error ); ?></p>
			<p>
				<a href="<?php echo esc_url( admin_url( 'admin.php?page=' . FINALPOS_PAGE_SETUP ) ); ?>" class="button">
				<?php esc_html_e( 'Disconnect from FinalPOS', 'finalpos' ); ?>
			</a>
			</p>
		</div>
	<?php else : ?>
		<div class="finalpos-logs-table-wrap">
			<?php $logs_list_table->display(); ?>
		</div>
	<?php endif; ?>
</div>
