import './style.scss';

export default new Class({
	Extends: Hedera.Form,
	Template: require('./ui.xml'),

	activate() {
		this.tpv = new Hedera.Tpv({
			conn: this.conn,
			hash: this.hash
		});
		const tpvStatus = this.tpv.check();
		if (tpvStatus === 'ko') this.$.errorDialog.show();
	},

	onBasketClick() {
		this.hash.setAll({form: 'ecomerce/basket'});
	},
	
	repeaterFunc(res, form) {
		res.$.link.href = this.hash.make({
			form: 'ecomerce/ticket',
			ticket: form.$.id
		});
	},

	// TPV
	
	balanceConditionalFunc(_, value) {
		if (value >= 0)
			Vn.Node.removeClass(this.$.balance, 'negative');
		else
			Vn.Node.addClass(this.$.balance, 'negative');
	},
	
	async onPayButtonClick() {
		var amount = -this.$.debt.value;
		amount = amount <= 0 ? null : amount;

		var defaultAmountStr = '';
		
		if (amount !== null)
			defaultAmountStr = Vn.Value.format(amount, '%.2d');
			
		amount = prompt(_('AmountToPay:'), defaultAmountStr);

		if (amount != null) {
			amount = parseFloat(amount.replace(',', '.'));
			await this.tpv.pay(amount, null);
		}
	},
	
	async onDialogResponse(_, response) {
		if (response == Htk.Dialog.Button.RETRY)
			await this.tpv.retryPay();
	}
});