<?php

namespace Clonable\Views;

use Clonable\Helpers\Html;
use Clonable\Helpers\Json;
use Clonable\Helpers\Session;
use Clonable\Models\MultiCurrency;
use Clonable\Traits\Forms;

class MultiCurrencyView implements ViewInterface {
	use Forms;

	public function render()
	{
		Html::include_fomantic_dropdown();
		Html::include_alpine();
		$this->render_fields(MultiCurrency::PAGE, MultiCurrency::$fields, 'Clonable Multi-Currency Settings');
		$this->render_form(MultiCurrency::PAGE);
	}

	public function description() {
		?>
			<p>
				Clonable does not provide a multi-currency module itself but relies on other multi-currency modules
				to handle the actual logic. <br/> Clonable only provides a convenient way for deciding which currency is visible on which clone.
				The options below show the different multi-currency plugins Clonable supports.
			</p>
		<?php
	}

	/**
	 * Render the aelia support checkbox
	 * @return void
	 */
	public function clonable_enable_aelia_plugin_support_field() {
		$this->create_checkbox(MultiCurrency::AELIA_SUPPORT_SETTING, 'off');
		?>
			<p>Make sure the <a target='_blank' href='https://aelia.freshdesk.com/support/solutions/120257'>Aelia Currency
					Switcher for WooCommerce</a> gets loaded on the correct clone.</p>
		<?php
	}

	/**
	 * Render the CURCY support checkbox.
	 * @return void
	 */
	public function clonable_enable_curcy_plugin_support_field()
	{
		$this->create_checkbox(MultiCurrency::CURCY_SUPPORT_SETTING, 'off');
		?>
		<p>Make sure the <a target='_blank' href='https://wordpress.org/plugins/woo-multi-currency/'>CURCY
				multi-currency plugin</a> gets loaded on the correct clone.</p>
		<?php
	}

	public function clonable_currency_per_country_field()
	{
		$table_data = Session::old(
            MultiCurrency::backwards_compatibility(MultiCurrency::MULTI_CURRENCY_BY_CLONE)
        );

		if (empty($table_data)) {
			$default_currencies = MultiCurrency::get_default_currencies();
			$table_data = Json::handle_output(json_encode($default_currencies));
		}
		$currencies = \Clonable\Helpers\MultiCurrency::get_all_currencies();
        $field_name = MultiCurrency::MULTI_CURRENCY_BY_CLONE;
		?>
		<p style="max-width: 75%">
			You can define which currency should be selected by default for each clone. <br/>
            Each clone has its own row in the table below.
		</p>
		<div class="wrap">
			<div x-data="{ currencies: <?php echo esc_js($table_data); ?> }">
				<!-- hidden input field for submitting the json value -->
				<label for="<?php echo $field_name ?>"></label>
				<input x-model="JSON.stringify(currencies)" name="<?php echo $field_name ?>"
				       id="<?php echo $field_name ?>" style="display: none" />
				<!-- Display data -->
				<table class="wp-list-table fat striped" style="width: 75%">
					<thead>
					<tr>
						<th>Target Region</th>
						<th>Currency</th>
					</tr>
					</thead>
					<tbody>
					<template x-for="(currency, index) in currencies" :key="index">
						<tr>
							<input x-model="currencies[index].target_region" type="hidden">
							<td><input disabled x-model="currencies[index].display_region" style="width: 100%" /></td>
							<td>
								<div class="field">
									<div class="ui search selection dropdown" style="width: 75%">
										<input x-model="currencies[index].currency" type="hidden">
										<i class="dropdown icon"></i>
										<div class="default text mt-0.5"></div>
										<div class="menu">
											<?php
											foreach ($currencies as $currency) {
												$symbol = get_woocommerce_currency_symbol($currency);
												$name = MultiCurrency::get_currency_name($currency);
												echo "<div @click='currencies[index].currency = \"" . esc_attr($currency) . "\"' class='item' data-value='" . esc_attr($currency) . "'>" . esc_html($name) . " ($symbol)</div>";
											}
											?>
										</div>
									</div>
								</div>
							</td>
						</tr>
					</template>
					</tbody>
				</table>
			</div>
		</div>
		<?php
		// phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped
		$this->render_error($field_name);
	}

    public function clonable_currency_force_per_clone() {
        $default_value = MultiCurrency::$fields[MultiCurrency::MULTI_CURRENCY_FORCE_PER_CLONE]['default'];
        $this->create_checkbox(MultiCurrency::MULTI_CURRENCY_FORCE_PER_CLONE, $default_value ? 'on' : 'off');
    }
}