<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * @var array $rates
 * @var array $template_rates previously saved templates for current shipping method
 */

?>

<?php if ( ! empty( $template_rates ) ) { ?>
<tr class="inpost-international-rates-settings_templates" style="display: table-row;">
	<th scope="row" class="titledesc">
	</th>
	<td class="forminp">
		<table id="inpost-international-templates-rates-table_templates" style="display:table-row;max-width:600px;" class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_rates wc_input_table sortable widefat">
			<thead>
			<tr>
				<th><?php esc_html_e( 'Template name', 'inpost-international' ); ?></th>
				<th><?php esc_html_e( 'Cost', 'inpost-international' ); ?></th>
			</tr>
			</thead>
			<tbody>
			<?php $count = 0; ?>
			<?php

			foreach ( $template_rates as $key => $template ) {
				++$count;

				if ( is_array( $template ) ) {
					$cost = ! empty( $template['cost'] ) ? $template['cost'] : '';
					?>
					<tr>
						<td>
							<input class="input-text regular-input" type="text" disabled value="<?php echo esc_attr( $template['name'] ); ?>">
							<input type="hidden" value="<?php echo esc_attr( $template['name'] ); ?>" name=template_rates[<?php echo esc_attr( $count ); ?>][name]>
						</td>
						<td>
							<input class="input-text regular-input" type="number" style="" value="<?php echo esc_attr( $cost ); ?>" placeholder="0.00" step="any" min="0" name=template_rates[<?php echo esc_attr( $count ); ?>][cost]>
						</td>
					</tr>
					<?php
				}
			}

			?>
			</tbody>
			<tfoot>
			</tfoot>
		</table>
	</td>
</tr>
<?php } ?>


<tr class="inpost-international-rates-settings" style="display:none;">
	<th scope="row" class="titledesc">
	</th>
	<td class="forminp">
		<table id="inpost-international-rates-table" style="display:none;max-width:600px;" class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_rates wc_input_table sortable widefat">
			<thead>
			<tr>
				<th class="sort">&nbsp;</th>
				<th><?php esc_html_e( 'Min', 'inpost-international' ); ?></th>
				<th><?php esc_html_e( 'Max', 'inpost-international' ); ?></th>
				<th><?php esc_html_e( 'Cost', 'inpost-international' ); ?></th>
				<th><?php esc_html_e( 'Action', 'inpost-international' ); ?></th>
			</tr>
			</thead>
			<tbody>
			<?php $count = 0; ?>
			<?php

			foreach ( $rates as $key => $rate ) {
				++$count;

				if ( is_array( $rate ) ) {
					?>
					<tr>
						<td class="sort"></td>
						<td>
							<input class="input-text regular-input" type="number" style="" value="<?php echo esc_attr( $rate['min'] ); ?>" placeholder="0.00" step="any" min="0" name=rates[<?php echo esc_attr( $count ); ?>][min]>
						</td>
						<td>
							<input class="input-text regular-input" type="number" style="" value="<?php echo esc_attr( $rate['max'] ); ?>" placeholder="0.00" step="any" min="0" name=rates[<?php echo esc_attr( $count ); ?>][max]>
						</td>
						<td>
							<input class="input-text regular-input" type="number" style="" value="<?php echo esc_attr( $rate['cost'] ); ?>" placeholder="0.00" step="any" min="0" name=rates[<?php echo esc_attr( $count ); ?>][cost]>
						</td>
						<td>
							<a id="delete_rate_<?php echo esc_attr( $count ); ?>" href="#" class="button inpost_intl_delete_rate" data-id="<?php echo esc_attr( $count ); ?>"><?php esc_html_e( 'Delete row', 'inpost-international' ); ?></a>
						</td>
					</tr>
					<?php
				}
			}

			?>
			</tbody>
			<tfoot>
			<tr>
				<th colspan="5">
					<a id="insert_rate" href="#" class="button plus insert">
						<?php esc_html_e( 'Insert row', 'inpost-international' ); ?>
					</a>
				</th>
			</tr>
			</tfoot>
		</table>
	</td>
</tr>

<?php wp_nonce_field( 'inpost-international-save-rates-table', 'inpost-international-save-rates-table-nonce' ); ?>
