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

/** @var $this \Woocommerce\Pagarme\Block\Adminhtml\System\Config\Form\Field\Hub\Integration */

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

$confirm = 'Are you shure you wish to delete the keys? This action removes the integration from your ' .
           'store, but first you must access Pagar.me Hub and click on Delete.';
$success = 'Integration keys successfully removed. Reload the page.';
?>

<a href="<?= esc_url($this->config->getHubUrl()); ?>" class="button button-primary pagarme-hub-integration-btn">
    <?= esc_attr($this->getIntegrationButtonLabel()); ?>
    <img alt="Pagarme Avatar"
            src="<?= plugins_url('/pagarme-payments-for-woocommerce/assets/images/pagarme-avatar-white.svg') ?>">
</a>

<?php if ($this->userCanDesintegrate()) : ?>
    <a id="pagarme-hub-desintegration-link" class="button button-secondary">
        <?= __('Delete integration', 'woo-pagarme-payments'); ?>
        <span class="dashicons dashicons-no"></span>
    </a>

    <script>
        jQuery(function ($) {
            swal.close();
            const content = {
                title: '<?= esc_attr(__('Disintegrate?', 'woo-pagarme-payments')); ?>',
                text: '<?= esc_attr(__($confirm, 'woo-pagarme-payments')); ?>',
                icon: 'warning',
                showCancelButton: true,
                confirmButtonColor: '#3085d6',
                cancelButtonColor: '#d33',
                cancelButtonText: '<?= __('Cancel', 'woo-pagarme-payments') ?>',
                confirmButtonText: '<?= esc_attr(__('Yes, remove keys!', 'woo-pagarme-payments')); ?>'
            };
            $('#pagarme-hub-desintegration-link').on('click', function (e) {
                try {
                    swal.fire(content).then((result) => {
                        if (result.isConfirmed) {
                            swal.fire({
                                title: ' ',
                                text: '<?= esc_attr(__('Processing', 'woo-pagarme-payments')); ?>',
                                allowOutsideClick: false
                            });
                            swal.showLoading();
                            $.ajax({
                                url: '<?= site_url('/wc-api/pagarme-hubcommand'); ?>',
                                type: 'POST',
                                dataType: "json",
                                data: JSON.stringify({
                                    command: 'Uninstall',
                                    force: true
                                }),
                                success: function () {
                                    swal.fire({
                                        title: '<?= esc_attr(
                                            __( 'Disintegration Complete', 'woo-pagarme-payments' )
                                        ); ?>',
                                        text: '<?= esc_attr( __( $success, 'woo-pagarme-payments' ) ); ?>',
                                        icon: 'success'
                                    })
                                        .then(() => {
                                            document.location.reload(true);
                                        });
                                },
                                fail: function () {
                                    swal.fire({
                                        title: '<?= esc_attr(
                                            __( 'Disintegration Error', 'woo-pagarme-payments' )
                                        ); ?>',
                                        text: '<?= esc_attr(
                                            __( 'Integration keys not removed.', 'woo-pagarme-payments' )
                                        ); ?>',
                                        icon: 'error'
                                    });
                                }
                            });
                        }
                    });
                } catch (e) {
                    swal({
                        title: '<?= esc_attr( __( 'Disintegration Error', 'woo-pagarme-payments' ) ); ?>',
                        text: '<?= esc_attr( __( 'Integration keys not removed.', 'woo-pagarme-payments' ) ); ?>',
                        icon: 'error'
                    });
                }
            });
        });
    </script>
<?php endif; ?>
