<?php

namespace Magento\Billdesk\Model;

use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;


class Button extends Field
{
    protected $_storeManager;
    protected $_urlInterface;


    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\UrlInterface $urlInterface,
        array $data = []
    ) {
        $this->_storeManager = $storeManager;
        $this->_urlInterface = $urlInterface;
        parent::__construct($context, $data);
    }

    protected function _getElementHtml(AbstractElement $element)
    {
        $ajaxurl = $this->_storeManager->getStore()->getBaseUrl();
        return '<input type ="button" id="pg_button" value="Test PG Connectivity" class="btn btn-info" data-value= "' . $ajaxurl . '" />';
    }
}
?>

<script type="text/javascript">
    require(["jquery",
        'Magento_Ui/js/modal/alert'
    ], function($, malert) {
        $(document).ready(function($) {
            $('#pg_button').click(function(event) {
                var base_url = event.target.getAttribute("data-value");
                var link = base_url + 'billdesk/payment/testOrder';

                var elements = document.querySelectorAll('[id$="_billdesk_merchant_key"]');
                for (var i = 0; i < elements.length; i++) {
                    var element = elements[1];
                    var _billdesk_merchant_key = element.id;
                }
                var elements2 = document.querySelectorAll('[id$="_billdesk_merchant_id"]');
                for (var i = 0; i < elements2.length; i++) {
                    var element2 = elements2[1];
                    var _billdesk_merchant_id = element2.id;
                }
                var elements3 = document.querySelectorAll('[id$="_billdesk_item_code"]');
                for (var i = 0; i < elements3.length; i++) {
                    var element3 = elements3[1];
                    var _billdesk_item_code = element3.id;
                }
                var elements4 = document.querySelectorAll('[id$="_billdesk_env_variable"]');
                for (var i = 0; i < elements4.length; i++) {
                    var element4 = elements4[1];
                    var _billdesk_env_variable = element4.id;
                }
                var elements5 = document.querySelectorAll('[id$="_billdesk_client_id"]');
                for (var i = 0; i < elements5.length; i++) {
                    var element5 = elements5[1];
                    var _billdesk_client_id = element5.id;
                }
                var elements6 = document.querySelectorAll('[id$="_billdesk_jose_encryption_key"]');
                for (var i = 0; i < elements6.length; i++) {
                    var element6 = elements6[1];
                    var _billdesk_jose_encryption_key = element6.id;
                }
                var elements7 = document.querySelectorAll('[id$="_billdesk_jose_signing_key"]');
                for (var i = 0; i < elements7.length; i++) {
                    var element7 = elements7[1];
                    var _billdesk_jose_signing_key = element7.id;
                }
                var elements8 = document.querySelectorAll('[id$="_billdesk_key_id"]');
                for (var i = 0; i < elements8.length; i++) {
                    var element8 = elements8[1];
                    var _billdesk_key_id = element8.id;
                }
                //var merchant_key = document.getElementById(_billdesk_merchant_key).value;
                var encryption_key = document.getElementById(_billdesk_jose_encryption_key).value;
                var signing_key = document.getElementById(_billdesk_jose_signing_key).value;
                var client_id = document.getElementById(_billdesk_client_id).value;
                var key_id = document.getElementById(_billdesk_key_id).value;
                var merchant_id = document.getElementById(_billdesk_merchant_id).value;
                var itemcode = document.getElementById(_billdesk_item_code).value;
                var env = document.getElementById(_billdesk_env_variable).value;

                if (client_id == "" || key_id == "" || encryption_key == "" || signing_key == "") {
                    var Errormess = 'Required parameter ';
                    if (encryption_key == "") {
                        Errormess = Errormess + ' Encryption key is missing';
                    } else if (client_id == "") {
                        Errormess = Errormess + ' Client id is missing';
                    } else if (signing_key == "") {
                        Errormess = Errormess + ' Signing key is missing';
                    } else if (key_id == "") {
                        Errormess = Errormess + ' Key id is missing';
                    }
                    document.getElementById('show_error').innerHTML += '<b><U>' + "ERROR_MESSAGE : " + Errormess + '</U></b></br>' + "ERROR_TYPE : " + "invalid_request_error" + "\n";
                } else {
                    jQuery.ajax({
                        type: "POST",
                        url: link,
                        data: {
                            //merchant_key: merchant_key,
                            encryption_key: encryption_key,
                            signing_key: signing_key,                            
                            client_id: client_id,
                            key_id: key_id,
                            merchant_id: merchant_id,
                            itemcode: itemcode,
                            env: env
                        },
                        success: function(response) {
                            console.log('Test Create order response: ', response);
                            if (response.status == '401' || response.status == '500' || response.status == '422' || response.status == '400') {
                                document.getElementById('show_error').innerHTML += '<b><U>' + "ERROR_MESSAGE : " + response.message + '</U></b></br>' + "ERROR_CODE : " + response.error_code + '</br>' + " ERROR_TYPE : " + response.error_type + "\n";
                            } else {
                                document.getElementById('show_mess').innerHTML += '<b><U>Successful PG Connectivity </U></b>';
                            }
                        },
                        error: function(response) {
                            document.getElementById('show_error').innerHTML += '<b><U>' + "ERROR_MESSAGE : " + response.message + '</U></b></br>' + "ERROR_CODE : " + response.error_code + '</br>' + " ERROR_TYPE : " + response.error_type + "\n";
                            // malert({
                            //     title: 'Error',
                            //     content: "ERROR_MESSAGE: " + response.message + "<br />" + " ERROR_CODE: " + response.error_code + "<br />" + " ERROR_TYPE: " + response.error_type,
                            //     clickableOverlay: false,
                            //     actions: {
                            //         always: function() {}
                            //     }
                            // });
                        }
                    });
                }
            })
        });

    });
</script>