<?php
/*
check admin details
*/
$adminDetails = $block->checkAdminDetails();
$allowedCountries = $block->getAllowedCountries();
if (!$adminDetails) {
    ?>
    <div class="pp-buttons-container">
        <?= /*@noEscape*/ __('Admin has not configured this Payment Method yet.'); ?>
    </div>
    <?php
} else {
    $statusData = $block->getMerchantStatus();
    if (!$statusData) {
        $referalData = $block->getPartnerRefferalUrl();
        $isRegistered = false;
    } else {
        $isRegistered = true;
    }
    ?>
    <div class="pp-buttons-container">
        <?php
        if (!$isRegistered) {
            if (!$referalData['url']) {
                if (isset($referalData['message'])) {
                    ?>
                    <p><?= $block->escapeHtml($referalData['message']) ?></p>
                    <?php
                }
                ?>
                <form action="<?=
                    $block->escapeUrl($block->getUrl(
                        'mppaypal/seller/index',
                        ['_secure' => $block->getRequest()->isSecure()]
                    )) ?>"
                    enctype="multipart/form-data"
                    method="get"
                    id="mppaypal-seller-onboard"
                    data-mage-init='{"validation":{}}'
                >
                    <fieldset class="fieldset info wk-mp-fieldset">
                        <div class="field required country-div">
                            <label class="label"><?= /*@noEscape*/ __('Select Country') ?></label>
                            <div class="control">
                                <select name="country_code" class="required-entry">
                                    <?php
                                    foreach ($allowedCountries as $country) {
                                        ?>
                                        <option value="<?= $block->escapeHtml($country['value']) ?>">
                                            <?= $block->escapeHtml($country['label']); ?>
                                        </option>
                                        <?php
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="legend">
                            <button class="button action primary wk-mp-btn" title="Save" type="submit">
                                <span><span><?= /*@noEscape*/__('Proceed') ?></span></span>
                            </button>
                        </div>
                    </fieldset>
                </form>
                <?php
            } else {
                ?>
                <div dir="ltr" style="text-align: left;" trbidi="on">
                    <script>
                        (function(d, s, id){
                            var js, ref = d.getElementsByTagName(s)[0];
                            if (!d.getElementById(id)){
                                js = d.createElement(s); js.id = id; js.async = true;
                                js.src = "https://www.paypal.com/webapps/merchantboarding/js/lib/lightbox/partner.js";
                                ref.parentNode.insertBefore(js, ref); }
                        }(document, "script", "paypal-js"));
                    </script>
        
                    <a class="action-default"
                    data-paypal-button="true"
                    href="<?= /* @noEscape */ $block->escapeUrl($referalData['url']. '&displayMode=minibrowser') ?>"
                    target="PPFrame"><?= /*@noEscape*/ __('Enable PayPal'); ?></a>
                </div>
                <?php
            }
        } else {
            ?>
                <div class="setup-complete">
                    <?= /*@noEscape*/ __('Setup is Complete'); ?>
                </div>
                <div class="missing-permission">
                <?php
                if (!$statusData['payments_receivable']) {
                    ?>
                    <?= /*@noEscape*/ __('Not authorized to Receive Payments please contact Admin'); ?>
                    <?php
                }
                if (!$statusData['primary_email_confirmed']) {
                    ?>
                    <?= /*@noEscape*/
                    __('Please confirm you primary email address in order to receive the payments'); ?>
                    <?php
                }
                ?>
                </div>
            <?php
        }
        ?>
        
    </div>
    <?php
}
?>
<script>
require(['jquery'], function ($) {
    
    $('#mppaypal-seller-onboard').on("submit", function() {
        if($(this).valid()) {
            $('body').trigger('processStart');
        }
    });
});
</script>