<?php

namespace WC_NEXT_SYNC;

use WC_NEXT_SYNC\Common\Menu as Base_Menu;

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

/**
 * Plugins' menu class.
 */
class Menu extends Base_Menu
{
    /**
     * Render menu page HTML.
     *
     * @param boolean $echo Should put render to the output buffer.
     * @return string|null $render Page content.
     */
    protected static function render_page($echo = true)
    {
        $store_settings = static::store()->store();

        $connection = $store_settings['connection'];
        $is_connected = (bool) $connection->connected;
        $filepath = $connection->filepath;
        $ajax_enabled = $is_connected && !empty($filepath);

        $tabs = [];
        foreach ($store_settings as $setting) {
            $setting_name = $setting->option();
            $tabs[$setting_name] = esc_html(static::tab_title($setting_name));
        }

        $current_tab = isset($_GET['tab'])
            ? sanitize_text_field(wp_unslash($_GET['tab']))
            : array_key_first($tabs);

        ob_start();
        ?>
			<div class="wrap">
			<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
				<form method="post" action="options.php">
					<nav class="nav-tab-wrapper">
					<?php foreach ($tabs as $tab => $name) {
         $current = $tab === $current_tab ? 'nav-tab-active' : '';
         $url = add_query_arg(['page' => static::slug(), 'tab' => $tab], '');
         printf(
             '<a class="nav-tab %s" href="%s">%s</a>',
             esc_attr($current),
             esc_url($url),
             esc_html($name)
         );
     } ?>
					</nav>
				<?php
    settings_fields($current_tab);
    do_settings_sections($current_tab);
    submit_button();
    ?>
				</form>
				<form method="post" action="<?php echo admin_url('admin-ajax.php'); ?>">
					<input type="hidden" name="_ajax_nonce" value="<?php echo wp_create_nonce(
         'wc-next-sync'
     ); ?>" />
					<input type="hidden" name="action" value="wc_next_sync_synchronize" />
					<input type="hidden" name="referer" value="<?php echo $current_tab; ?>" />
					<input type="submit" name="submit" id="submit" value="Synchronize" class="button button-secondary" <?php echo $ajax_enabled
         ? ''
         : 'disabled'; ?> />
				</form>
			</div>
			<?php
   $output = ob_get_clean();

   if ($echo) {
       echo $output;
   }

   return $output;
    }

    protected static function tab_title($setting_name)
    {
        switch ($setting_name) {
            case 'wc-next-sync_connection':
                return __('Connection', 'wc-next-sync');
            case 'wc-next-sync_orders':
                return __('Orders', 'wc-next-sync');
            case 'wc-next-sync_products':
                return __('Products', 'wc-next-sync');
            case 'wc-next-sync_customers':
                return __('Customers', 'wc-next-sync');
            case 'wc-next-sync_coupons':
                return __('Coupons', 'wc-next-sync');
            default:
                return $setting_name;
        }
    }
}
