define(
    [
		'ko',
		'iban',
        'Magento_Checkout/js/view/payment/default',
    ],
    function (ko,IBAN,Component) {
        'use strict';
        return Component.extend({
            defaults: {
                template: 'Threepartment_Debitpayment/payment/debitpayment',
            },
			
			getInstructions: function () {
                return window.checkoutConfig.payment.instructions[this.item.method];
            },
			getRemoteURL: function (){
				return window.checkoutConfig.payment.remoteURL[this.item.method];
			},
			getCheckbox: function (){
				if(window.checkoutConfig.payment.checkbox.debitpayment!=""){
					if(window.checkoutConfig.payment.checkbox.debitpayment=="1"){
						return true;
					}else{
						return false;
					}
				}else return false;
			},
			getMoreInfo: function (){
				return window.checkoutConfig.payment.moreinfo[this.item.method];
			},
			getCheckboxText: function (){
				return window.checkoutConfig.payment.checkboxtext[this.item.method];
			},
			getAutoPopulate: function (){
				return window.checkoutConfig.payment.autopopulate[this.item.method];
			},
			getData: function () {
                return {
					'method': this.item.method,
					'additional_data': {
						'bank_account_owner': jQuery('#bankaccountowner').val(),
						'iban': jQuery('#iban').val(),
						'bic': jQuery('#bic').val(),
						'bank_company': jQuery('#bankcompany').val()
					}
                };
			},
			displayValidateButton:function(){
				if(window.checkoutConfig.payment.autopopulate.debitpayment!=""){
					if(window.checkoutConfig.payment.autopopulate.debitpayment=="1"){
						return true;
					}else{
						return false;
					}
				}
			},
			validate: function() {
				var erred = false;
				jQuery('#'+this.item.method+'-form input').each(function(i,v){
					if(jQuery(v).val()==''){
						jQuery(this).parents('div.field').addClass('_error');
						erred = true;
						jQuery(this).next('div').show();
					}else{
						jQuery(this).parents('div.field').removeClass('_error');
						jQuery(this).next('div').hide();
					}
					
					if(!IBAN.isValid(jQuery('#iban').val())){
						erred=true;	
						jQuery('#iban').parents('div.field').addClass('_error');
						jQuery('#iban-error').show();
					}else{
						jQuery('#iban').parents('div.field').removeClass('_error');	
						jQuery('#iban-error').hide();
					}
					
				});
				if(window.checkoutConfig.payment.checkbox[this.item.method]=='1'){
					if(!jQuery('#required-checkbox').is(':checked')){
						jQuery('#required-checkbox').parents('div.field').addClass('_error');
						jQuery('#required-checkbox-err').show();
						erred = true;
					}else{
						jQuery('#required-checkbox').parents('div.field').removeClass('_error');
						jQuery('#required-checkbox-err').hide();	
						erred = false;
					}
				}
				if(erred){ return false }else {return true};
            }
        });	
    }
);
//Retrieve BIC and Bank Name from IBAN
jQuery(document).ready(function(){
	function showErrorMessage(){
		jQuery('#iban').parents('div.field').addClass('_error');
		jQuery('#iban-error').show();
	}
	jQuery('form').on('click','#validate-iban-btn', function(e){
		e.preventDefault();
		jQuery('#iban-error').hide();
		var url = jQuery(this).attr('data-remote-url');
		var iban = jQuery('#iban').val();
		jQuery('#bic').val('');
		jQuery('#bankcompany').val('');
		if(url!="" && iban!=""){
			url+=iban+"?getBIC=true&validateBankCode=true";
			jQuery.getJSON( url, function( data ) {
				if(data.valid){
					jQuery('#bic').val(data.bankData.bic);
					jQuery('#bankcompany').val(data.bankData.name);
				}else{
					showErrorMessage();
				}
			}).fail(function(){showErrorMessage();});
		}

	});
	
	
});
