<%@ page contentType="text/html; charset=UTF-8"%><%@ include file="/WEB-INF/jspf/include/taglibs.jsp"%>

<bg:pageTitle/>

<div class="tab tile ${requestScope.paymentName} balance" style="text-align: center; padding: 10px 20px; background-color: #fff; margin-top: 0;">
	<form action="" method="GET" id="paymentForm">
<%--		<input type="hidden" name="action" value="DoTransaction">
		<input type="hidden" name="module" value="">
		<input type="hidden" name="mid" value="">
		<input type="hidden" name="operation" value="pay" />
  		<input type="hidden" name="contractId" value="">
--%>
		<input type="hidden" name="sbp" value="false" />
	
	<c:if test="${not empty dataMap['customerAddressEnable'] and dataMap['customerAddressEnable']}">
		<c:if test="${not empty dataMap['customerAddressRequired'] and dataMap['customerAddressRequired']}"><c:set var="customerAddressRequiredFunction">checkCustomerAddress( form.customerAddress.value, '${dataMap['customerAddressTitle']}' ) &&</c:set></c:if>
		<div class="textField" style="margin-bottom: 5px;"><input type="text" name="customerAddress" value="${dataMap['customerAddress']}" placeholder="${dataMap['customerAddressTitle']}" style="text-align: center; font-size: 1.5rem;"/></div>
	</c:if>
	<%-- телефон, который доп. к оплате (не фискализация), если в модуле none->block, то проверяется обязательность --%>
	<c:set var="paymentPhone" scope="request"><div class="textField" id="paymentPhoneField" style="margin-bottom: 5px; display: none;"><input type="text" name="payment.phone" placeholder="Телефон в формате +79XXXXXXXXX" style="text-align: center; font-size: 1.5rem;" onkeyup="onKeyUpPaymentPhone(this);" value="${paymentPhone}"/></div></c:set>
	<c:if test="${empty requestScope.paymentPaySum}"><c:set var="sumText">Сумма (от <fmt:formatNumber groupingUsed="true" maxFractionDigits="2" minFractionDigits="2" value="${sumMin}" /> до <fmt:formatNumber groupingUsed="true" maxFractionDigits="2" minFractionDigits="2" value="${sumMax}" />)</c:set><c:set var="paymentPaySum" scope="request"><div class="textField"><input type="text" name="sum" placeholder="${sumText}" style="text-align: center; font-size: 1.5rem;" onkeyup="onKeyUpPaymentSum(this);"/></div></c:set></c:if>
	<c:if test="${empty requestScope.paymentPayButton}">
		<c:set var="paymentPayButton" scope="request">
			<c:choose>
				<c:when test="${not empty modulePaymentMethods}">
				<div class="mt-3 text-start">
				<c:forEach var="modulePayment" items="${modulePaymentMethods}">
				<bg:blockTitle title="${modulePayment.bgModule.title}"/>
				<div style="display: flex; flex-direction: row; align-items: center; justify-content: center;">
					<c:forEach var="paymentMethod" items="${modulePayment.paymentMethods}">
						<c:choose>
							<c:when test="${paymentMethod eq 'SBP'}"><div class="paymentMethod" onclick="doPaymentSBP( '${modulePayment.bgModule.name}', '${modulePayment.bgModule.id}' ); return false;"><img src="${pageContext.request.contextPath}/img/default/payment_sbp.png" title="СБП - система быстрых платежей"  style="height: 3rem;"></div></c:when>
							<c:when test="${paymentMethod eq 'CARD'}"><div class="paymentMethod" onclick="doPaymentCard( '${modulePayment.bgModule.name}', '${modulePayment.bgModule.id}', '${modulePayment.paymentMode}' ); return false;"><img src="${pageContext.request.contextPath}/img/default/payment_mir.png" title="Оплата банковской картой" style="height: 3rem;"></div></c:when>
						</c:choose>
					</c:forEach>
				</div>
				</c:forEach>
				</div>
				</c:when>
				<c:otherwise><div class="formButtons"><button type="submit" onclick="if ( ${customerAddressRequiredFunction} checkPaymentSum( { sum: this.form.sum.value, sumMin: ${sumMin}, sumMax: ${sumMax}, currency: '${currencyTitleMedium}', phoneNeed: false } ) ) { return doPay( this.form ) } return false;" class="btn btn-primary" style="margin-left: 5px;">Оплатить</button></div></c:otherwise>
			</c:choose>
		</c:set>
	</c:if>
		<div>${requestScope.paymentPaySumBefore}${requestScope.paymentPhone}${requestScope.paymentPaySum}${requestScope.paymentPaySumAfter}</div>${requestScope.paymentPayButton}${requestScope.paymentPayNoteMessage}
	</form>
	<form method="POST" id="paymentRedirectForm"></form>
</div>

<script>
function doPaymentSBP( module, moduleId )
{
	let form = document.getElementById('paymentForm');
	form.action = "${pageContext.request.contextPath}/webexecuter/m" + moduleId + "/DoTransaction/pay/${contract.id}";
//	form.module.value = module;
//	form.mid.value = moduleId;
	form.sbp.value = 'true';
	let checkPaymentSumParam = { 
		sum: form.sum.value, 
		sumMin: ${sumMin}, 
		sumMax: ${sumMax}, 
		currency: '${currencyTitleMedium}', 
		phoneNeed: false 
	};
	if ( ${customerAddressRequiredFunction} checkPaymentSum( checkPaymentSumParam ) ) 
	{ 
		if ( doPay( form ) ) form.submit(); 
	}
}

function doPaymentCard( module, moduleId, paymentMode )
{
	let form = document.getElementById('paymentForm'); 
	let checkPaymentSumParam = {
			sum: form.sum.value,
			sumMin: ${sumMin},
			sumMax: ${sumMax},
			currency: '${currencyTitleMedium}',
			phoneNeed: false,
			phone: form['payment.phone'] ? form['payment.phone'].value : '+70000000000'
		}
	if ( paymentMode == "FORM" )
	{
		if ( ${customerAddressRequiredFunction} checkPaymentSum( checkPaymentSumParam ) ) 
		{ 
			jQuery.get( "${pageContext.request.contextPath}/" + webexecuter, { action: 'DoTransaction', operation: 'pay', module: module, mid: moduleId, contractId: ${contract.id}, ct: 'json', sum: form.sum.value.replace(',','.'), customerAddress: getCustomerAddressValue( form ) }, 
				function( data )
				{
					//alert( JSON.stringify(data) );
					let redirectForm = document.getElementById('paymentRedirectForm');
					setFormFields( redirectForm, data );
					redirectForm.submit();
				}
			);
		}
	}
	else
	{
		checkPaymentSumParam.phoneNeed = document.getElementById('paymentPhoneField').style.display != 'none';
		form.action = "${pageContext.request.contextPath}/webexecuter/m" + moduleId + "/DoTransaction/pay/${contract.id}";
	//	form.module.value = module;
	//	form.mid.value = moduleId;
		form.sbp.value = 'false';
		if ( ${customerAddressRequiredFunction} checkPaymentSum( checkPaymentSumParam ) ) 
		{ 
			if ( doPay( form ) ) form.submit(); 
		}
	}
}
</script>