import './style.scss';

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

	async open() {
		await this.loadOrder();
		if (this.orderId) {
			await Hedera.Form.prototype.open.call(this);
			this.$.lot.assign({id: this.orderId});
		}
	},

	activate() {
		this.$.items.setInfo('bi', 'myOrderRow', 'hedera');
	},

	async onHashChange() {
		if (!this.isOpen) return;
		await this.loadOrder();
		if (this.orderId)
			this.$.lot.assign({id: this.orderId});
	},

	async loadOrder() {
		const basket = new Hedera.Basket(this.app);
		if (this.hash.$.id) {
			this.orderId = this.hash.$.id;
		} else if (await basket.check()) {
			this.orderId = basket.orderId;
		}
	},

	onOrderReady(form) {
		if (form.row < 0)
			return;
		
		if (form.$.method != 'PICKUP') {
			Vn.Node.show(this.$.address);
			Vn.Node.setText(this.$.method, _('Agency'));
		} else {
			Vn.Node.hide(this.$.address);
			Vn.Node.setText(this.$.method, _('Warehouse'));
		}
	},

	onConfigureClick() {
		Htk.Toast.showWarning(_('RememberReconfiguringImpact'));
		this.hash.setAll({
			form: 'ecomerce/checkout',
			id: this.orderId
		});
	},

	async onCatalogClick() {
		const basket = new Hedera.Basket(this.app);
		await basket.load(this.orderId);
	},

	onConfirmClick() {
		this.hash.setAll({
			form: 'ecomerce/confirm',
			id: this.orderId
		});
	},

	onDeleteClick(form) {
		if (confirm(_('ReallyDelete')))
			form.deleteRow();
	},

	subtotal(form) {
		return form.$.amount * form.$.price;
	}
});