<?php

/**
 * @author      Open Source Team
 * @copyright   2022 Pagar.me (https://pagar.me)
 * @license     https://pagar.me Copyright
 *
 * @link        https://pagar.me
 */

if (!defined('ABSPATH')) {
    exit;
}
/** @var \Woocommerce\Pagarme\Block\Adminhtml\System\Config\Page\PageSettings $this */
?>

<div class="wrap">
    <h2><?php echo esc_html(get_admin_page_title()); ?></h2>

    <?php settings_errors(); ?>

    <form method="post" action="options.php">
        <?php
        settings_fields($this->options);
        do_settings_sections($this->options);
        submit_button();
        ?>
    </form>

    <table class="wc_gateways widefat" cellspacing="0" aria-describedby="payment_gateways_options-description">
        <thead>
        <tr>
            <?php
            $default_columns = array(
                'name' => __('Method', 'woocommerce'),
                'status' => __('Enabled', 'woocommerce'),
                'description' => __('Description', 'woocommerce'),
                'action' => '',
            );

            $columns = apply_filters('woocommerce_payment_gateways_setting_columns', $default_columns);

            foreach ($columns as $key => $column) {
                echo '<th class="' . esc_attr($key) . '">' . esc_html($column) . '</th>';
            }
            ?>
        </tr>
        </thead>
        <tbody>
        <?php
        foreach ($this->paymentGateways as $gateway) {

            echo '<tr data-gateway_id="' . esc_attr($gateway->id) . '">';

            foreach ($columns as $key => $column) {
                if (!array_key_exists($key, $default_columns)) {
                    do_action('woocommerce_payment_gateways_setting_column_' . $key, $gateway);
                    continue;
                }

                $width = '';

                if (in_array($key, array('sort', 'status', 'action'), true)) {
                    $width = '1%';
                }

                $method_title = $gateway->get_method_title() ? $gateway->get_method_title() : $gateway->get_title();
                $custom_title = $gateway->get_title();

                echo '<td class="' . esc_attr($key) . '" width="' . esc_attr($width) . '">';

                switch ($key) {
                    case 'name':
                        echo '<a href="' . esc_url(admin_url('admin.php?page=wc-settings&tab=checkout&section=' . strtolower($gateway->id))) . '" class="wc-payment-gateway-method-title">' . wp_kses_post($method_title) . '</a>';

                        if ($method_title !== $custom_title) {
                            echo '<span class="wc-payment-gateway-method-name">&nbsp;&ndash;&nbsp;' . wp_kses_post($custom_title) . '</span>';
                        }
                        break;
                    case 'description':
                        echo wp_kses_post($gateway->get_method_description());
                        break;
                    case 'action':
                        if (wc_string_to_bool($gateway->enabled)) {
                            /* Translators: %s Payment gateway name. */
                            echo '<a class="button alignright" aria-label="' . esc_attr(sprintf(__('Manage the "%s" payment method', 'woocommerce'), $method_title)) . '" href="' . esc_url(admin_url('admin.php?page=wc-settings&tab=checkout&section=' . strtolower($gateway->id))) . '">' . esc_html__('Manage', 'woocommerce') . '</a>';
                        } else {
                            if (
                                'WooCommerce Payments' === $method_title &&
                                class_exists('WC_Payments_Account')
                            ) {
                                $setup_url = WC_Payments_Account::get_connect_url();
                            } else {
                                $setup_url = admin_url('admin.php?page=wc-settings&tab=checkout&section=' . strtolower($gateway->id));
                            }
                            /* Translators: %s Payment gateway name. */
                            echo '<a class="button alignright" aria-label="' . esc_attr(sprintf(__('Set up the "%s" payment method', 'woocommerce'), $method_title)) . '" href="' . esc_url($setup_url) . '">' . esc_html__('Finish set up', 'woocommerce') . '</a>';
                        }
                        break;
                    case 'status':
                        echo '<a class="wc-payment-gateway-method-toggle-enabled" href="' . esc_url(admin_url('admin.php?page=wc-settings&tab=checkout&section=' . strtolower($gateway->id))) . '">';
                        if (wc_string_to_bool($gateway->enabled)) {
                            /* Translators: %s Payment gateway name. */
                            echo '<span class="woocommerce-input-toggle woocommerce-input-toggle--enabled" aria-label="' . esc_attr(sprintf(__('The "%s" payment method is currently enabled', 'woocommerce'), $method_title)) . '">' . esc_attr__('Yes', 'woocommerce') . '</span>';
                        } else {
                            /* Translators: %s Payment gateway name. */
                            echo '<span class="woocommerce-input-toggle woocommerce-input-toggle--disabled" aria-label="' . esc_attr(sprintf(__('The "%s" payment method is currently disabled', 'woocommerce'), $method_title)) . '">' . esc_attr__('No', 'woocommerce') . '</span>';
                        }
                        echo '</a>';
                        break;
                }

                echo '</td>';
            }

            echo '</tr>';
        }
        ?>
        </tbody>
    </table>
</div>
