<?php

namespace Ilabs\Inpost_International\Settings;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
} // Exit if accessed directly.

use Ilabs\Inpost_International\Helpers\Helper;
use Ilabs\Inpost_International\Plugin;

/**
 * Admin settings
 */
class Admin_Settings {

	/**
	 * Admin Settings wrap instance
	 *
	 * @var Settings_Helper
	 */
	protected $settings_helper;

	/**
	 * Settings constructor
	 *
	 * @param Settings_Helper $settings_helper Settings_Helper instance.
	 */
	public function __construct( $settings_helper ) {
		$this->settings_helper = $settings_helper;
		$this->hooks();
	}


	/**
	 * Enable Admins hooks
	 */
	public function hooks() {

		add_action( 'admin_init', array( $this, 'setup_fields' ), 9, 0 );
		add_filter( 'plugin_action_links_' . plugin_basename( INPOST_INTL_PLUGIN_FILE ), array( $this, 'setup_extra_links' ), 10, 1 );

		add_filter( 'install_plugins_nonmenu_tabs', array( $this, 'install_plugins_nonmenu_tabs' ) );

		add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
	}

	/**
	 * Enqueue admin scripts
	 */
	public function admin_enqueue_scripts() {

		$current_screen = get_current_screen();

		$plugin = new Plugin();

		if ( is_a(
			$current_screen,
			'WP_Screen'
		) && 'toplevel_page_inpost-international-api' === $current_screen->id ) {

			$helper = new Helper();

			$admin_js_path = $plugin->get_plugin_dir() . '/assets/js/admin.js';
			$admin_js_ver  = file_exists( $admin_js_path ) ? filemtime( $admin_js_path ) : INPOST_INTL_PLUGIN_VERSION;
			wp_enqueue_script(
				$plugin->get_plugin_prefix() . '_admin_js',
				$plugin->get_plugin_js_url() . '/admin.js',
				array( 'jquery', 'wp-color-picker', 'wp-i18n' ),
				$admin_js_ver,
				true
			);

			wp_set_script_translations( $plugin->get_plugin_prefix() . '_admin_js', 'inpost-international', $plugin->get_plugin_dir() . 'lang' );

			wp_localize_script(
				$plugin->get_plugin_prefix() . '_admin_js',
				'inpost_intl',
				array(
					'ajaxurl'      => admin_url( 'admin-ajax.php' ),
					'settings_url' => admin_url( 'admin.php?page=inpost-international-api' ),
					'logo_src'     => $plugin->get_plugin_images_url() . '/white.png',
					'plugin_slug'  => INPOST_INTL_PLUGIN_SLUG,
					'nonce'        => wp_create_nonce( 'inpost_international_ajax' ),
					'log_path'     => $helper->get_woocommerce_logs_section_url(),
					'jszip_path'   => $plugin->get_plugin_js_url() . '/jszip.min.js',
				)
			);

			$admin_css_path = $plugin->get_plugin_dir() . '/assets/css/admin.css';
			$admin_css_ver  = file_exists( $admin_css_path ) ? filemtime( $admin_css_path ) : INPOST_INTL_PLUGIN_VERSION;
			wp_enqueue_style(
				$plugin->get_plugin_prefix() . '_admin_css',
				$plugin->get_plugin_css_url() . '/admin.css',
				array(),
				$admin_css_ver
			);

		}

		if ( is_a( $current_screen, 'WP_Screen' ) && 'woocommerce_page_wc-settings' === $current_screen->id ) {
			if ( isset( $_GET['tab'] ) && 'shipping' === $_GET['tab'] && isset( $_GET['instance_id'] ) ) {
				$admin_css_path = $plugin->get_plugin_dir() . '/assets/css/shipping-settings.css';
				$admin_css_ver  = file_exists( $admin_css_path ) ? filemtime( $admin_css_path ) : INPOST_INTL_PLUGIN_VERSION;
				wp_enqueue_style(
					$plugin->get_plugin_prefix() . '_admin_shipping_settings_css',
					$plugin->get_plugin_css_url() . '/shipping-settings.css',
					array(),
					$admin_css_ver
				);
			}
		}
	}


	/**
	 * Add setting tab
	 *
	 * @param array $tabs Settings tabs.
	 *
	 * @return array
	 */
	public function install_plugins_nonmenu_tabs( $tabs ) {

		$tabs[] = INPOST_INTL_PLUGIN_SLUG;

		return $tabs;
	}


	/**
	 * WP filter hook,
	 * Creates "Settings" and "Support" links
	 * on plugins page
	 *
	 * @param array $links Initial WP links.
	 *
	 * @return array populated links
	 */
	public function setup_extra_links( $links ) {
		$extra_links = array(
			sprintf(
				'<a href="%s">%s</a>',
				add_query_arg( array( 'page' => 'inpost-international-api' ), admin_url( 'admin.php' ) ),
				esc_html__( 'Settings', 'inpost-international' )
			),
			'<a href="https://inpost.pl/formularz-wsparcie" target="_blank" style="color:#3db634;">' . esc_html__( 'Support', 'inpost-international' ) . '</a>',
		);

		return array_merge( $links, $extra_links );
	}


	/**
	 * Configure setting fields
	 */
	public function setup_fields() {

		$helper = Helper::init();

		// ======================== CONNECTION ========================

		$auth_section_id = 'inpost_international_settings_auth';

		$disabled = isset( get_option( 'inpost_international_settings_auth' )['api_refresh_token'] ) ? 1 : '';

		// Connection section.
		$this->settings_helper->add_section(
			array(
				'id'           => $auth_section_id,
				'icon'         => '<span class="dashicons dashicons-admin-plugins"></span>',
				'title'        => esc_html__( 'Auth', 'inpost-international' ),
				'need_to_save' => 1,
			)
		);

		// Client ID field.
		$this->settings_helper->add_field(
			$auth_section_id,
			array(
				'id'          => 'client_id',
				'type'        => 'text',
				'name'        => '',
				'desc'        => function () {
					?>
					<h4><?php esc_html_e( 'Add your Client ID and Client Secret to connect with InPost API', 'inpost-international' ); ?></h4>
						<?php
						printf(
							'<p class="inpost_intl_before_setting_description inpost_intl_padding_20"><a class="inpost_intl_before_setting_desc" href="%s" target="_blank">%s</a></p>',
							esc_url( 'https://dokumentacja-inpost.atlassian.net/wiki/spaces/PL/pages/980320261/PL+WooCommerce+International' ),
							esc_html__( 'How i can get credentials', 'inpost-international' )
						);
						?>
						<p class="inpost_intl_before_setting_description"><?php esc_html_e( 'Client ID', 'inpost-international' ); ?></p>
					<?php
				},
				'placeholder' => 'Client ID',
				'disabled'    => $disabled,
			)
		);

		// Client Secret field.
		$this->settings_helper->add_field(
			$auth_section_id,
			array(
				'id'          => 'client_secret',
				'type'        => 'text',
				'name'        => '',
				'placeholder' => 'Client Secret',
				'desc'        => function () {
					?>
					<p class="inpost_intl_before_setting_description"><?php esc_html_e( 'Client Secret', 'inpost-international' ); ?></p>
					<?php
				},
				'disabled'    => $disabled,
			)
		);

		// API type selection field.
		$this->settings_helper->add_field(
			$auth_section_id,
			array(
				'id'       => 'api_type',
				'type'     => 'select',
				'name'     => '',
				'desc'     => function () {
					?>
					<p class="inpost_intl_before_setting_description"><?php esc_html_e( 'API type', 'inpost-international' ); ?></p>
					<?php
				},
				'options'  => array(
					'production' => esc_html__( 'Production', 'inpost-international' ),
					'sandbox'    => esc_html__( 'Sandbox', 'inpost-international' ),
				),
				'disabled' => $disabled,
			)
		);

		// Connection button.
		$this->settings_helper->add_field(
			$auth_section_id,
			array(
				'id'   => 'connect',
				'type' => 'html',
				'name' => '',
				'desc' => function () {

					$connection_is_active = isset( get_option( 'inpost_international_settings_auth' )['api_refresh_token'] ) ? true : false;

					$nonce     = wp_create_nonce( 'inpost-intl-disconnection' );
					$settings_page = admin_url( 'admin.php?page=inpost-international-api' );

					$additional_class = $connection_is_active ? 'connected' : '';
					$is_disabled = $connection_is_active ? 'disabled' : '';

					$switch_checked = ! empty( get_option( 'inpost_international_only_for_admins' ) ) ? 'checked' : '';
					?>
					<p class="inpost_intl_before_setting_description">
						<?php esc_html_e( 'Enable only for administrators', 'inpost-international' ); ?>
					</p>
					<label class="inpost-intl-switch">
						<input type="checkbox"
								class="inpost-intl-switch-checkbox"
								data-id="enabled_only_for_admins"
								id="enabled_only_for_admins"
							<?php echo esc_attr( $switch_checked ); ?>
						>
						<span class="inpost-intl-slider"></span>
					</label>
					<img src="<?php echo esc_url( inpost_intl_core()->get_plugin_images_url() . '/big-round-preloader.gif' ); ?>" id="inpost-intl-enabled-only-for-admins-preloader" alt="iinpost intl pre-loader">
					<span class="status enabled-only-for-admins inpost-intl-action-status" title="OK"></span>


					<div class="inside inpost-intl-connection-row">
						<br>
						<a id="inpost-intl-connection" class="inpost-intl-connection <?php echo esc_attr( $additional_class ); ?>"
							<?php echo esc_attr( $is_disabled ); ?> title="Connection to API">
							<?php if ( $connection_is_active ) { ?>
								<span style="display:inline-block;" class="status inpost-intl-action-status" title="OK"></span>
								<?php
								esc_html_e( 'Connected', 'inpost-international' );
							} else {
								esc_html_e( 'Connect to API', 'inpost-international' );
							}
							?>
						</a>
						<img src="<?php echo esc_url( inpost_intl_core()->get_plugin_images_url() . '/big-round-preloader.gif' ); ?>" id="inpost-intl-connection-preloader" alt="inpost intl pre-loader">
						<input type="hidden" id="inpost-intl-connection-nonce" data-id="<?php echo esc_attr( wp_create_nonce( 'inpost-intl-connection' ) ); ?>">

						<?php if ( $connection_is_active ) {
                            wp_clear_scheduled_hook( 'inpost_intl_cron' );
                            ?>
						<a id="inpost-intl-disconnect" href="#" class="inpost-intl-connection disconnect" title="Disconnect API"  style="margin-left:30px;">
							<?php esc_html_e( 'Disconnect', 'inpost-international' ); ?>
						</a>
						<img src="<?php echo esc_url( inpost_intl_core()->get_plugin_images_url() . '/big-round-preloader.gif' ); ?>" id="inpost-intl-disconnection-preloader" alt="inpost intl pre-loader">
						<?php } ?>
						<div style="clear:both;"></div>
					</div>
					<?php
				},
			)
		);

		// ======================== SETTINGS TAB 2 (GEOWIDGET OPTIONS) ========================

		$fifth_tab_section_id = 'inpost_international_settings_geowidget';

		// Geowidget options tab.
		$this->settings_helper->add_section(
			array(
				'id'           => $fifth_tab_section_id,
				'icon'         => '',
				'title'        => esc_html__( 'Geowidget options', 'inpost-international' ),
				'need_to_save' => 1,
			)
		);

		$this->settings_helper->add_field(
			$fifth_tab_section_id,
			array(
				'id'   => 'geowidget_production_token',
				'type' => 'text',
				'name' => '',
				'desc' => function () {
					?>
					<p class="inpost_intl_before_setting_description"><?php esc_html_e( 'Geowidget production token', 'inpost-international' ); ?></p>
					<?php
				},
			)
		);

		$this->settings_helper->add_field(
			$fifth_tab_section_id,
			array(
				'id'   => 'geowidget_sandbox_token',
				'type' => 'text',
				'name' => '',
				'desc' => function () {
					?>
					<p class="inpost_intl_before_setting_description"><?php esc_html_e( 'Geowidget sandbox token', 'inpost-international' ); ?></p>
					<?php
				},

			)
		);

		// Color picker - map button.
		$this->settings_helper->add_field(
			$fifth_tab_section_id,
			array(
				'id'      => 'map_button_color',
				'type'    => 'text',
				'name'    => '',
				'default' => '#ffc107',
				'desc'    => esc_html__( 'Color of map button', 'inpost-international' ),
			)
		);

		$this->settings_helper->add_field(
			$fifth_tab_section_id,
			array(
				'id'      => 'geowidget_button_text_it',
				'type'    => 'text',
				'name'    => '',
				'default' => 'Selezionare un punto di ritiro',
				'desc'    => function () {
					?>
					<p class="inpost_intl_before_setting_description"><?php esc_html_e( 'Map button text for buyers from Italy', 'inpost-international' ); ?></p>
					<?php
				},
			)
		);

		$this->settings_helper->add_field(
			$fifth_tab_section_id,
			array(
				'id'      => 'geowidget_button_text_es',
				'type'    => 'text',
				'name'    => '',
				'default' => 'Seleccione un punto de recogida',
				'desc'    => function () {
					?>
					<p class="inpost_intl_before_setting_description"><?php esc_html_e( 'Map button text for buyers from Spain', 'inpost-international' ); ?></p>
					<?php
				},
			)
		);

		$this->settings_helper->add_field(
			$fifth_tab_section_id,
			array(
				'id'      => 'geowidget_button_text_pt',
				'type'    => 'text',
				'name'    => '',
				'default' => 'Selecionar um ponto de recolha',
				'desc'    => function () {
					?>
					<p class="inpost_intl_before_setting_description"><?php esc_html_e( 'Map button text for buyers from Portugal', 'inpost-international' ); ?></p>
					<?php
				},
			)
		);

		$this->settings_helper->add_field(
			$fifth_tab_section_id,
			array(
				'id'      => 'geowidget_button_text_nl',
				'type'    => 'text',
				'name'    => '',
				'default' => 'Levering van Locker of Relaispun',
				'desc'    => function () {
					?>
					<p class="inpost_intl_before_setting_description"><?php esc_html_e( 'Map button text for buyers from the Netherlands', 'inpost-international' ); ?></p>
					<?php
				},
			)
		);

		$this->settings_helper->add_field(
			$fifth_tab_section_id,
			array(
				'id'      => 'geowidget_button_text_at',
				'type'    => 'text',
				'name'    => '',
				'default' => 'Lieferung des Schließfachs oder Relaispunkts',
				'desc'    => function () {
					?>
					<p class="inpost_intl_before_setting_description"><?php esc_html_e( 'Map button text for buyers from Austria', 'inpost-international' ); ?></p>
					<?php
				},
			)
		);

		$this->settings_helper->add_field(
			$fifth_tab_section_id,
			array(
				'id'      => 'geowidget_button_text_fr',
				'type'    => 'text',
				'name'    => '',
				'default' => 'Sélectionner un point de collecte',
				'desc'    => function () {
					?>
					<p class="inpost_intl_before_setting_description"><?php esc_html_e( 'Map button text for buyers from France', 'inpost-international' ); ?></p>
					<?php
				},
			)
		);

		$this->settings_helper->add_field(
			$fifth_tab_section_id,
			array(
				'id'      => 'geowidget_button_text_be',
				'type'    => 'text',
				'name'    => '',
				'default' => 'Sélectionner un point de collecte',
				'desc'    => function () {
					?>
					<p class="inpost_intl_before_setting_description"><?php esc_html_e( 'Map button text for buyers from Belgium', 'inpost-international' ); ?></p>
					<?php
				},
			)
		);

        $this->settings_helper->add_field(
            $fifth_tab_section_id,
            array(
                'id'      => 'geowidget_button_text_hu',
                'type'    => 'text',
                'name'    => '',
                'default' => 'Válasszon egy átvételi pontot',
                'desc'    => function () {
                    ?>
                    <p class="inpost_intl_before_setting_description"><?php esc_html_e( 'Map button text for buyers from Hungary', 'inpost-international' ); ?></p>
                    <?php
                },
            )
        );

		$this->settings_helper->add_field(
			$fifth_tab_section_id,
			array(
				'id'      => 'geowidget_button_text_lu',
				'type'    => 'text',
				'name'    => '',
				'default' => 'Sélectionner un point de collecte',
				'desc'    => function () {
					?>
					<p class="inpost_intl_before_setting_description"><?php esc_html_e( 'Map button text for buyers from Luxembourg', 'inpost-international' ); ?></p>
					<?php
				},
			)
		);

		$options = $helper->get_checkout_hooks_for_map_button();

		// Select the hook where map button will be shown if classic checkout is used.
		$this->settings_helper->add_field(
			$fifth_tab_section_id,
			array(
				'id'      => 'map_button_output',
				'type'    => 'select',
				'name'    => '',
				'desc'    => function () {
					?>
					<hr>
					<p class="inpost_intl_before_setting_description"><?php esc_html_e( 'If no map button on checkout page', 'inpost-international' ); ?></p>
					<p class="inpost_intl_before_setting_description"><?php esc_html_e( '1. Try to change this setting - where to display map button (by default - after describing the delivery methods)', 'inpost-international' ); ?></p>
					<?php
				},
				'options' => $options,
			)
		);

		// Second variant of map button for classic CHeckout - render it in JS mode.
		$this->settings_helper->add_field(
			$fifth_tab_section_id,
			array(
				'id'      => 'map_button_output_js',
				'type'    => 'checkbox',
				'name'    => '',
				'desc'    => esc_html__( '2. Enable JS mode of the map button', 'inpost-international' ),
				'default' => 'off',
			)
		);

		// Disable assets enqueue only on Cart or Checkout.
		$this->settings_helper->add_field(
			$fifth_tab_section_id,
			array(
				'id'      => 'disable_assets_enqueue_by_page',
				'type'    => 'checkbox',
				'name'    => '',
				'desc'    => esc_html__( '3. Disable condition to enable geowidget scripts only on Checkout page', 'inpost-international' ),
				'default' => 'off',
			)
		);

		// ======================== SETTINGS TAB 3 ========================

		$second_tab_section_id = 'inpost_international_settings_merchant';

		// Settings section.
		$this->settings_helper->add_section(
			array(
				'id'           => $second_tab_section_id,
				'icon'         => '',
				'title'        => esc_html__( 'Merchant Settings', 'inpost-international' ),
				'need_to_save' => 1,
			)
		);

		// Merchant country field.
		$this->settings_helper->add_field(
			$second_tab_section_id,
			array(
				'id'      => 'country',
				'type'    => 'select',
				'name'    => '',
				'desc'    => function () {
					?>
					<p class="inpost_intl_before_setting_description"><?php esc_html_e( 'Country', 'inpost-international' ); ?></p>
					<?php
				},
				'options' => array(
					'PL' => esc_html__( 'Poland', 'inpost-international' ),
					'IT' => esc_html__( 'Italy', 'inpost-international' ),
				),
			)
		);

		// Merchant company field.
		$this->settings_helper->add_field(
			$second_tab_section_id,
			array(
				'id'          => 'company',
				'type'        => 'text',
				'name'        => '',
				'desc'        => function () {
					?>
					<p class="inpost_intl_before_setting_description"><?php esc_html_e( 'Company name', 'inpost-international' ); ?></p>
					<?php
				},
				'placeholder' => esc_html__( 'Company', 'inpost-international' ),
			)
		);

		// Merchant name field.
		$this->settings_helper->add_field(
			$second_tab_section_id,
			array(
				'id'          => 'name',
				'type'        => 'text',
				'name'        => '',
				'desc'        => function () {
					?>
					<p class="inpost_intl_before_setting_description"><?php esc_html_e( 'First name', 'inpost-international' ); ?></p>
					<?php
				},
				'placeholder' => esc_html__( 'Name', 'inpost-international' ),
			)
		);

		// Merchant surname field.
		$this->settings_helper->add_field(
			$second_tab_section_id,
			array(
				'id'          => 'surname',
				'type'        => 'text',
				'name'        => '',
				'desc'        => function () {
					?>
					<p class="inpost_intl_before_setting_description"><?php esc_html_e( 'Last name', 'inpost-international' ); ?></p>
					<?php
				},
				'placeholder' => esc_html__( 'Surname', 'inpost-international' ),
			)
		);

		// Merchant E-mail field.
		$this->settings_helper->add_field(
			$second_tab_section_id,
			array(
				'id'          => 'email',
				'type'        => 'email_input',
				'name'        => '',
				'desc'        => function () {
					?>
					<p class="inpost_intl_before_setting_description"><?php esc_html_e( 'Email', 'inpost-international' ); ?></p>
					<?php
				},
				'placeholder' => esc_html__( 'E-mail', 'inpost-international' ),
			)
		);

		// Merchant phone field.
		$this->settings_helper->add_field(
			$second_tab_section_id,
			array(
				'id'          => 'phone',
				'type'        => 'text',
				'name'        => '',
				'desc'        => function () {
					?>
					<p class="inpost_intl_before_setting_description"><?php esc_html_e( 'Phone', 'inpost-international' ); ?></p>
					<?php
				},
				'placeholder' => esc_html__( '+48 XXX-XXX-XXX', 'inpost-international' ),
			)
		);

		// ======================== SETTINGS TAB 4 ========================

		$third_tab_section_id = 'inpost_international_settings_dispatch_points';

		// Settings section.
		$this->settings_helper->add_section(
			array(
				'id'           => $third_tab_section_id,
				'icon'         => '',
				'title'        => esc_html__( 'Dispatch points', 'inpost-international' ),
				'need_to_save' => 1,
			)
		);

		$this->settings_helper->add_field(
			$third_tab_section_id,
			array(
				'id'   => 'points',
				'type' => 'html',
				'name' => '',
				'desc' => $this->render_dispatch_points_settings( $third_tab_section_id ),
			)
		);

		// ======================== SETTINGS TAB 5 ========================

		$fourth_tab_section_id = 'inpost_international_settings_sending';

		// Sending options.
		$this->settings_helper->add_section(
			array(
				'id'           => $fourth_tab_section_id,
				'icon'         => '',
				'title'        => esc_html__( 'Send options', 'inpost-international' ),
				'need_to_save' => 1,
			)
		);

		$this->settings_helper->add_field(
			$fourth_tab_section_id,
			array(
				'id'   => 'block_over_weight',
				'type' => 'html',
				'name' => '',
				'desc' => function () {
					$current_section_settings = get_option( 'inpost_international_settings_sending' );
					$switch_checked = ! empty( $current_section_settings['block_over_weight'] ) ? 'checked' : '';
					$switch_status = ! empty( $current_section_settings['block_over_weight'] ) ? 1 : '';
					?>
					<p class="inpost_intl_before_setting_description">
						<?php esc_html_e( 'Do not provide InPost parcel locker service if the weight of goods exceeds 25 kg', 'inpost-international' ); ?>
					</p>
					<label class="inpost-intl-switch">
						<input type="checkbox"
								class="inpost-intl-switch-checkbox"
								data-id="block_over_weight"
							<?php echo esc_attr( $switch_checked ); ?>
						>
						<span class="inpost-intl-slider"></span>
					</label>
					<input type="hidden" name="inpost_international_settings_sending[block_over_weight]" value="<?php echo esc_attr( $switch_status ); ?>" id="block_over_weight">
					<?php
				},
			)
		);

		$this->settings_helper->add_field(
			$fourth_tab_section_id,
			array(
				'id'   => 'order_status',
				'type' => 'html',
				'name' => '',
				'desc' => function () {
					$current_section_settings = get_option( 'inpost_international_settings_sending' );
					$switch_checked = ! empty( $current_section_settings['order_status'] ) ? 'checked' : '';
					$switch_status = ! empty( $current_section_settings['order_status'] ) ? 1 : '';
					?>
					<p class="inpost_intl_before_setting_description">
						<?php esc_html_e( 'Change order status to Completed after shipment created?', 'inpost-international' ); ?>
					</p>
					<label class="inpost-intl-switch">
						<input type="checkbox"
								class="inpost-intl-switch-checkbox"
								data-id="order_status"
							<?php echo esc_attr( $switch_checked ); ?>
						>
						<span class="inpost-intl-slider"></span>
					</label>
					<input type="hidden" name="inpost_international_settings_sending[order_status]" value="<?php echo esc_attr( $switch_status ); ?>" id="order_status">
					<?php
				},
			)
		);

		$this->settings_helper->add_field(
			$fourth_tab_section_id,
			array(
				'id'   => 'send_email_after_package_created',
				'type' => 'html',
				'name' => '',
				'desc' => function () {
					$current_section_settings = get_option( 'inpost_international_settings_sending' );
					$switch_checked = ! empty( $current_section_settings['send_email_after_package_created'] ) ? 'checked' : '';
					$switch_status = ! empty( $current_section_settings['send_email_after_package_created'] ) ? 1 : '';
					?>
					<p class="inpost_intl_before_setting_description">
						<?php esc_html_e( 'Send an email to the buyer about the start of the delivery process just after parcel created?', 'inpost-international' ); ?>
					</p>
					<label class="inpost-intl-switch">
						<input type="checkbox"
								class="inpost-intl-switch-checkbox"
								data-id="send_email_after_package_created"
							<?php echo esc_attr( $switch_checked ); ?>
						>
						<span class="inpost-intl-slider"></span>
					</label>
					<input type="hidden" name="inpost_international_settings_sending[send_email_after_package_created]" value="<?php echo esc_attr( $switch_status ); ?>" id="send_email_after_package_created">
					<?php
				},
			)
		);

		$this->settings_helper->add_field(
			$fourth_tab_section_id,
			array(
				'id'   => 'copy_order_note',
				'type' => 'html',
				'name' => '',
				'desc' => function () {
					$current_section_settings = get_option( 'inpost_international_settings_sending' );
					$switch_checked = ! empty( $current_section_settings['copy_order_note'] ) ? 'checked' : '';
					$switch_status = ! empty( $current_section_settings['copy_order_note'] ) ? 1 : '';
					?>
					<p class="inpost_intl_before_setting_description">
						<?php esc_html_e( "Automatically add buyer order comment to field 'Reference'", 'inpost-international' ); ?>
					</p>
					<label class="inpost-intl-switch">
						<input type="checkbox"
								class="inpost-intl-switch-checkbox"
								data-id="copy_order_note"
							<?php echo esc_attr( $switch_checked ); ?>
						>
						<span class="inpost-intl-slider"></span>
					</label>
					<input type="hidden" name="inpost_international_settings_sending[copy_order_note]" value="<?php echo esc_attr( $switch_status ); ?>" id="copy_order_note">
					<?php
				},
			)
		);

		// Default sending method field.
		$options = array(
			'address-to-point' => esc_html__( 'COURIER', 'inpost-international' ),
			'point-to-point'   => esc_html__( 'APM/PUDO/HUB', 'inpost-international' ),
		);

		$country_code   = 'PL';
		$sender_options = get_option( 'inpost_international_settings_merchant' );
		if ( ! empty( $sender_options['country'] ) ) {
			$country_code = $sender_options['country'];
		}

		if ( 'IT' === $country_code ) {
			$options = array(
				'address-to-point' => esc_html__( 'COURIER', 'inpost-international' ),
				'point-to-point'   => esc_html__( 'APM/PUDO', 'inpost-international' ),
			);
		}

		$this->settings_helper->add_field(
			$fourth_tab_section_id,
			array(
				'id'      => 'default_send_method',
				'type'    => 'select',
				'name'    => '',
				'desc'    => function () {
					?>
					<p class="inpost_intl_before_setting_description"><?php esc_html_e( 'Default send method', 'inpost-international' ); ?></p>
					<?php
				},
				'options' => $options,
			)
		);

		$this->settings_helper->add_field(
			$fourth_tab_section_id,
			array(
				'id'   => 'title',
				'type' => 'html',
				'name' => '',
				'desc' => function () {
					$img = inpost_intl_core()->get_plugin_images_url() . '/gabarytABC.jpg';
					$current_section_settings = get_option( 'inpost_international_settings_sending' );
					$switch_checked = ! empty( $current_section_settings['product_template'] ) ? 'checked' : '';
					$switch_status = ! empty( $current_section_settings['product_template'] ) ? 1 : '';
					?>
					<div class="inpost_pl_row">
						<h2><?php esc_html_e( 'Parcel templates', 'inpost-international' ); ?></h2>
						<div class="inpost_pl_row_item item_left" style="padding:5px 5px 5px 5px;" id="inpost-pl-dimensions-wrap">
							<p class="inpost_intl_before_setting_description">
							<?php esc_html_e( 'ON / OFF this option to fill dimensions from product settings or create own templates below and set any as default', 'inpost-international' ); ?>
							</p>
							<label class="inpost-intl-switch">
								<input type="checkbox"
										class="inpost-intl-switch-checkbox"
										data-id="product_template"
										<?php echo esc_attr( $switch_checked ); ?>
								>
								<span class="inpost-intl-slider"></span>
							</label>
							<input type="hidden" name="inpost_international_settings_sending[product_template]" value="<?php echo esc_attr( $switch_status ); ?>" id="product_template">
						</div>
						<div class="inpost_pl_row_item item_right" style="padding:5px 5px 5px 5px;">
						</div>
					</div>
					<?php
				},
			)
		);

		$this->settings_helper->add_field(
			$fourth_tab_section_id,
			array(
				'id'   => 'templates',
				'type' => 'html',
				'name' => '',
				'desc' => $this->render_dimensions_templates_settings( $fourth_tab_section_id ),
			)
		);

		// ======================== Product's Settings ========================

		$product_settings_section_id = 'inpost_international_settings_products';

		// Product's section tab.
		$this->settings_helper->add_section(
			array(
				'id'           => $product_settings_section_id,
				'icon'         => '',
				'title'        => __( 'Product\'s settings', 'inpost-international' ),
				'need_to_save' => 0,
			)
		);

		$this->settings_helper->add_field(
			$product_settings_section_id,
			array(
				'id'   => 'product_settings',
				'type' => 'html',
				'name' => '',
				'desc' => function () {},
			)
		);

		// ======================== Shipments Management Settings ========================

		$shipments_settings_section_id = 'inpost_international_settings_shipments';

		// Shipments section tab.
		$this->settings_helper->add_section(
			array(
				'id'           => $shipments_settings_section_id,
				'icon'         => '',
				'title'        => __( 'Shipments\'s table', 'inpost-international' ),
				'need_to_save' => 0,
			)
		);

		$this->settings_helper->add_field(
			$shipments_settings_section_id,
			array(
				'id'   => 'shipments_settings',
				'type' => 'html',
				'name' => '',
				'desc' => function () {},
			)
		);

		// ======================== SETTINGS TAB HELP ========================

		$sixth_tab_section_id = 'inpost_international_settings_help';

		// Help options.
		$this->settings_helper->add_section(
			array(
				'id'           => $sixth_tab_section_id,
				'icon'         => '',
				'title'        => esc_html__( 'Help', 'inpost-international' ),
				'need_to_save' => 0,
			)
		);

		$this->settings_helper->add_field(
			$sixth_tab_section_id,
			array(
				'id'   => 'help',
				'type' => 'html',
				'name' => '',
				'desc' => '',
			)
		);

		// for quick debug settings.
		if ( isset( $_GET['inpost_intl_debug_settings'] ) ) {
			wc_setcookie( 'inpost_intl_show_settings', 1, time() + 3600 * 24, false, true );
		}
	}


	/**
	 * Generate table for dispatch points addresses
	 *
	 * @param string $setting_id $setting_id.
	 *
	 * @return false|string
	 */
	public function render_dispatch_points_settings( $setting_id ) {

		$saved_points    = null;
		$current_options = get_option( $setting_id );
		if ( ! empty( $current_options['points'] ) ) {
			$saved_points = $current_options['points'];
		}

		\ob_start();
		?>
		<tr class="inpost_intl_manage_dispatch_points">

			<th scope="row" class="titledesc">
				<label for="woo_inpost_dpoint_0">
					<?php esc_html_e( 'Add or delete points', 'inpost-international' ); ?>
				</label>
			</th>
			<td class="forminp forminp-text">

				<table id="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dpoint-cell">
					<thead>
						<th class="inpost_intl_manage_point_th"><?php esc_html_e( 'Selected', 'inpost-international' ); ?></th>
						<th class="inpost_intl_manage_point_street"><?php esc_html_e( 'Street', 'inpost-international' ); ?></th>
						<th class="inpost_intl_manage_point_bn"><?php esc_html_e( 'Building number', 'inpost-international' ); ?></th>
						<th class="inpost_intl_manage_point_bn"><?php esc_html_e( 'Flat number', 'inpost-international' ); ?></th>
						<th class="inpost_intl_manage_point_pc"><?php esc_html_e( 'Postal code', 'inpost-international' ); ?></th>
						<th class="inpost_intl_manage_point_city"><?php esc_html_e( 'City', 'inpost-international' ); ?></th>
					</thead>

					<?php
					if ( is_array( $saved_points ) && ! empty( $saved_points ) ) {
						$k = 0;
						foreach ( $saved_points as $i => $point ) {
							if ( ! is_numeric( $i ) ) {
								continue;
							}

							$current_index_selected = 0;
							$checked                = '';
							if ( isset( $point['selected'] ) ) {
								$current_index_selected = $i;
								$checked                = 'checked';
							}
							?>
							<tr id="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dpoint_<?php echo esc_attr( $k ); ?>"
								class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dpoint-cell-wraper" data-index="<?php echo esc_attr( $k ); ?>">
								<td class="inpost_intl_manage_point_td">
									<input <?php echo esc_attr( $checked ); ?>
										type="radio"
										data-id="selected"
										value="<?php echo esc_attr( $k ); ?>"
										class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dpoint_selected"
										name="<?php echo esc_attr( $setting_id ); ?>[points][<?php echo esc_attr( $current_index_selected ); ?>][selected]"
									>
									<input
										type="button"
										value="X"
										class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dpoint-remove"
										data-remove="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dpoint_<?php echo esc_attr( $k ); ?>"
									>
								</td>
								<td>
									<input
										class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dpoint"
										value="<?php echo esc_html( $point['street'] ); ?>"
										data-id="street"
										name="<?php echo esc_attr( $setting_id ); ?>[points][<?php echo esc_attr( $k ); ?>][street]"
										>
								</td>
								<td>
									<input
										class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dpoint"
										value="<?php echo esc_html( $point['building_number'] ); ?>"
										style="max-width: 80px;"
										data-id="building_number"
										name="<?php echo esc_attr( $setting_id ); ?>[points][<?php echo esc_attr( $k ); ?>][building_number]"
										>
								</td>
								<td>
									<input
										class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dpoint"
										value="<?php echo esc_html( $point['flat_number'] ); ?>"
										style="max-width: 80px;"
										data-id="flat_number"
										name="<?php echo esc_attr( $setting_id ); ?>[points][<?php echo esc_attr( $k ); ?>][flat_number]"
										>
								</td>
								<td>
									<input
										class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dpoint"
										value="<?php echo esc_html( $point['postal_code'] ); ?>"
										style="max-width: 80px;"
										data-id="postal_code"
										name="<?php echo esc_attr( $setting_id ); ?>[points][<?php echo esc_attr( $k ); ?>][postal_code]"
										>
								</td>
								<td>
									<input
										class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dpoint"
										value="<?php echo esc_html( $point['city'] ); ?>"
										data-id="city"
										name="<?php echo esc_attr( $setting_id ); ?>[points][<?php echo esc_attr( $k ); ?>][city]"
										>
								</td>
							</tr>
							<?php
							++$k;
						}
					} else {
						$k = 0;
						?>
						<tr id="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dpoint_<?php echo esc_attr( $k ); ?>"
							class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dpoint-cell-wraper" data-index="<?php echo esc_attr( $k ); ?>">
							<td>
								<input type="radio" checked
										value="<?php echo esc_attr( $k ); ?>"
										data-id="selected"
										class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dpoint_selected"
										name="<?php echo esc_attr( $setting_id ); ?>[points][<?php echo esc_attr( $k ); ?>][selected]"
								>
								<input type="button"
										value="X"
										data-point-id="<?php echo esc_attr( $k ); ?>"
										class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dpoint-remove"
										data-remove="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dpoint_<?php echo esc_attr( $k ); ?>"
								>
							</td>
							<td>
								<input
									class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dpoint"
									value=""
									data-id="street"
									name="<?php echo esc_attr( $setting_id ); ?>[points][<?php echo esc_attr( $k ); ?>][street]"
									>
							</td>
							<td>
								<input
									class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dpoint"
									value=""
									style="max-width: 80px;"
									data-id="building_number"
									name="<?php echo esc_attr( $setting_id ); ?>[points][<?php echo esc_attr( $k ); ?>][building_number]"
									>
							</td>
							<td>
								<input
									class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dpoint"
									value=""
									style="max-width: 80px;"
									data-id="flat_number"
									name="<?php echo esc_attr( $setting_id ); ?>[points][<?php echo esc_attr( $k ); ?>][flat_number]"
									>
							</td>
							<td>
								<input
									class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dpoint"
									value=""
									style="max-width: 80px;"
									data-id="postal_code"
									name="<?php echo esc_attr( $setting_id ); ?>[points][<?php echo esc_attr( $k ); ?>][postal_code]"
									>
							</td>
							<td>
								<input
									class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dpoint"
									value=""
									data-id="city"
									name="<?php echo esc_attr( $setting_id ); ?>[points][<?php echo esc_attr( $k ); ?>][city]"
									>
							</td>
						</tr>
						<?php
					}
					?>

				</table>
			</td>
		</tr>
		<tr>
			<td>
				<span id="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dpoint_add" class="button button-secondary">
					<?php esc_html_e( 'Add dispath point', 'inpost-international' ); ?>
				</span>
			</td>
		</tr>
		<?php
		$output = \ob_get_contents();
		\ob_end_clean();
		return $output;
	}




	/**
	 * Generate table for dimensions templates
	 *
	 * @param string $setting_id $setting_id.
	 *
	 * @return false|string
	 */
	public function render_dimensions_templates_settings( $setting_id ) {

		$saved_templates = null;
		$current_options = get_option( $setting_id );
		if ( ! empty( $current_options['templates'] ) ) {
			$saved_templates = $current_options['templates'];
		}

		\ob_start();
		?>
		<tr class="inpost_intl_manage_dispatch_templates">

			<th scope="row" class="titledesc">
			</th>
			<td class="forminp forminp-text">

				<table id="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dmtemplate-cell">
					<thead>
					<th class="inpost_intl_manage_point_th"><?php esc_html_e( 'Selected', 'inpost-international' ); ?></th>
					<th class="inpost_intl_manage_template_name"><?php esc_html_e( 'Name of template', 'inpost-international' ); ?></th>
					<th class="inpost_intl_manage_template_length"><?php esc_html_e( 'Length (cm)', 'inpost-international' ); ?></th>
					<th class="inpost_intl_manage_template_width"><?php esc_html_e( 'Width (cm)', 'inpost-international' ); ?></th>
					<th class="inpost_intl_manage_template_height"><?php esc_html_e( 'Height (cm)', 'inpost-international' ); ?></th>
					<th class="inpost_intl_manage_template_weight"><?php esc_html_e( 'Weight (kg)', 'inpost-international' ); ?></th>
					</thead>

					<?php
					if ( is_array( $saved_templates ) && ! empty( $saved_templates ) ) {
						$k = 0;
						foreach ( $saved_templates as $i => $dim_template ) {
							if ( ! is_numeric( $i ) || empty( $dim_template['name'] ) ) {
								continue;
							}

							$dim_template_slug = ! empty( $dim_template['slug'] ) ? $dim_template['slug'] : '';

							$current_index_selected = 0;
							$checked                = '';
							if ( ! empty( $dim_template['selected'] ) ) {
								$current_index_selected = $k;
								$checked                = 'checked';
							}

							?>
							<tr id="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dmtemplate_<?php echo esc_attr( $k ); ?>"
								class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dmtemplate-cell-wraper" data-index="<?php echo esc_attr( $k ); ?>">
								<td class="inpost_intl_manage_point_td">
									<input <?php echo esc_attr( $checked ); ?>
										type="radio"
										data-id="selected"
										value="<?php echo esc_attr( $k ); ?>"
										class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dmtemplate_selected"
										name="<?php echo esc_attr( $setting_id ); ?>[templates][<?php echo esc_attr( $current_index_selected ); ?>][selected]"
									>
									<input
										type="button"
										value="X"
										class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dmtemplate-remove"
										data-remove="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dmtemplate_<?php echo esc_attr( $k ); ?>"
									>
								</td>
								<td>
									<input
										class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dmtemplate"
										value="<?php echo esc_html( $dim_template['name'] ); ?>"
										data-id="name"
										type="text"
										name="<?php echo esc_attr( $setting_id ); ?>[templates][<?php echo esc_attr( $k ); ?>][name]"
										>
									<input
										class="inpost_intl_template_slug"
										value="<?php echo esc_attr( $dim_template_slug ); ?>"
										data-id="slug"
										type="hidden"
										name="<?php echo esc_attr( $setting_id ); ?>[templates][<?php echo esc_attr( $k ); ?>][slug]"
									>
								</td>
								<td>
									<input
										class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dmtemplate inpost_intl_validated_input"
										value="<?php echo esc_html( $dim_template['length'] ); ?>"
										data-id="length"
										type="number"
										step="0.1"
										min="2.1"
										max="64"
										name="<?php echo esc_attr( $setting_id ); ?>[templates][<?php echo esc_attr( $k ); ?>][length]"
										>
								</td>
								<td>
									<input
										class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dmtemplate inpost_intl_validated_input"
										value="<?php echo esc_html( $dim_template['width'] ); ?>"
										data-id="width"
										type="number"
										step="0.1"
										min="2.1"
										max="38"
										name="<?php echo esc_attr( $setting_id ); ?>[templates][<?php echo esc_attr( $k ); ?>][width]"
										>
								</td>
								<td>
									<input
										class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dmtemplate inpost_intl_validated_input"
										value="<?php echo esc_html( $dim_template['height'] ); ?>"
										data-id="height"
										type="number"
										step="0.1"
										min="2.1"
										max="39"
										name="<?php echo esc_attr( $setting_id ); ?>[templates][<?php echo esc_attr( $k ); ?>][height]"
										>
								</td>
								<td>
									<input
										class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dmtemplate inpost_intl_validated_input"
										value="<?php echo esc_html( $dim_template['weight'] ); ?>"
										data-id="weight"
										type="number"
										step="0.1"
										min="0"
										max="25"
										name="<?php echo esc_attr( $setting_id ); ?>[templates][<?php echo esc_attr( $k ); ?>][weight]"
										>
								</td>
							</tr>
							<?php
							++$k;
						}
					} else {
						$k = 0;
						?>
						<tr id="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dmtemplate_<?php echo esc_attr( $k ); ?>"
							class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dmtemplate-cell-wraper" data-index="<?php echo esc_attr( $k ); ?>">
							<td>
								<input type="radio" checked
										value="<?php echo esc_attr( $k ); ?>"
										data-id="selected"
										class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dmtemplate_selected"
										name="<?php echo esc_attr( $setting_id ); ?>[templates][<?php echo esc_attr( $k ); ?>][selected]"
								>
								<input type="button"
										value="X"
										style="display: none"
										data-point-id="<?php echo esc_attr( $k ); ?>"
										class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dmtemplate-remove"
										data-remove="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dmtemplate_<?php echo esc_attr( $k ); ?>"
								>
							</td>
							<td>
								<input
									class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dmtemplate"
									value=""
									type="text"
									data-id="name"
									name="<?php echo esc_attr( $setting_id ); ?>[templates][<?php echo esc_attr( $k ); ?>][name]"
									>
								<input
									class="inpost_intl_template_slug"
									value=""
									data-id="slug"
									type="hidden"
									name="<?php echo esc_attr( $setting_id ); ?>[templates][<?php echo esc_attr( $k ); ?>][slug]"
								>
							</td>
							<td>
								<input
									class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dmtemplate inpost_intl_validated_input"
									value=""
									data-id="length"
									type="number"
									step="0.1"
									min="0"
									name="<?php echo esc_attr( $setting_id ); ?>[templates][<?php echo esc_attr( $k ); ?>][length]"
									>
							</td>
							<td>
								<input
									class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dmtemplate inpost_intl_validated_input"
									value=""
									data-id="width"
									type="number"
									step="0.1"
									min="0"
									name="<?php echo esc_attr( $setting_id ); ?>[templates][<?php echo esc_attr( $k ); ?>][width]"
									>
							</td>
							<td>
								<input
									class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dmtemplate inpost_intl_validated_input"
									value=""
									data-id="height"
									type="number"
									step="0.1"
									min="0"
									name="<?php echo esc_attr( $setting_id ); ?>[templates][<?php echo esc_attr( $k ); ?>][height]"
									>
							</td>
							<td>
								<input
									class="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dmtemplate inpost_intl_validated_input"
									value=""
									data-id="weight"
									type="number"
									step="0.1"
									min="0"
									name="<?php echo esc_attr( $setting_id ); ?>[templates][<?php echo esc_attr( $k ); ?>][weight]"
									>
							</td>
						</tr>
						<?php
					}
					?>

				</table>
			</td>
		</tr>
		<tr id="inpost_intl_add_template_row">
			<td>
				<span id="<?php echo esc_attr( INPOST_INTL_PLUGIN_SLUG ); ?>_dmtemplate_add" class="button button-secondary">
					<?php esc_html_e( 'Add template', 'inpost-international' ); ?>
				</span>
			</td>
		</tr>
		<?php
		$output = \ob_get_contents();
		\ob_end_clean();
		return $output;
	}
}
