<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/Widget.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/CommunityToolbar.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/FrontendSubmit.inc.php" );

	require_once( $GLOBALS["site_root"]."/includes/models/Member.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/MyMember.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/models/Constants.inc.php" );

	require_once( $GLOBALS["site_root"]."/includes/models/CreditCard.inc.php" );

	class OtpPaymentPane extends Widget {
		protected $bAckMode = false;

		public function setAckMode( $v ) { $this->bAckMode = $v; }

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->oCommunityToolbar = new CommunityToolbar( "CommunityToolbar" , $this );
			$this->mm = new MemberModel();
			$this->mmm = new MyMemberModel();
			$this->ccm = new CreditCardModel();

			$this->oSubmit = new FrontendSubmit( "smb_payment" , $this );
			$this->oSubmit->setValue( "Tovább a fizetéshez" );
		}

		protected function initializeWidgets() {
			$this->oCommunityToolbar->setTitle( __("Pay_with_OTP") );
		}
		protected function generateLangDefs() {
			fo3_add_js_langdef( "Please_choose_subscription_length" );
		}

		protected function handleAjaxEvents() {
			if (!isset( $this->aP["inajax"] )) return parent::handleEvents();
			if (!isset( $this->aP["ajaxevent"] )) return parent::handleEvents();
			if ($this->aP["ajaxevent"]!="check_otp_transaction") return parent::handleEvents();
			if (!isset( $this->aP["tran_id"] )) return parent::handleEvents();
			$tran_id = $this->aP["tran_id"];

			$retval = array();
			if (!$this->ccm->isTransactionExists( $tran_id )) {
				// te mi a rakot kerdezel leeee???
				$retval["error"]="Nem létező tranzakció";								// LANGFIX
				FNTH_echo_json_response( $retval );
				fo3_exit();
			}
			if (!$this->ccm->isUserTransaction( $this->mmm->getLoggedinUserId() , $tran_id )) {
				// te kiet kerdezed leeee????
				$retval["error"]="Ön nem jogosult a tranzakció lekérdezésére";			// LANGFIX
				FNTH_echo_json_response( $retval );
				fo3_exit();
			}
			if ($this->ccm->isTransactionClosed( $tran_id )) {
				// mar lezart tranyo adatait akarja (ami ugyan eleg furcsa, de legyen)
				$retval["evaluate"]="document.location.href=document.location.href;";
				FNTH_echo_json_response( $retval );
				fo3_exit();
			}
			// itt van a spenot, you down with OTP, yeah you now me!
			$status = $this->ccm->finishTransaction( $tran_id );
			switch ($status) {
			case CC_STATUS_SUCCESSFUL:
				$retval["evaluate"]="document.location.href=document.location.href;";
				break;
			case CC_STATUS_CANCELED:
				$retval["evaluate"]="document.location.href=document.location.href;";
				break;
			default:
				$retval["error"]="Nem tudtuk lekérdezni a tranzakció sikerességét.<br/>Kérjük, próbálja újra! Amennyiben a hiba továbbra is fenn áll, kérjük keresse fel szerkesztőségünket!";
				break;
			}

			FNTH_echo_json_response( $retval );
			fo3_exit();
		}

		public function handleEvents() {
			// init
			if (isset( $this->aG["tran_id"] )) {
				// tranid van
			} else {
				$this->aG["tran_id"]=$this->ccm->getLastUnclosedUserTransaction( $this->mmm->getLoggedinUserId() );
			}
			$this->setParam( "tran_id" , $this->aG["tran_id"] );

			// ajax eventek
			if (isset( $this->aP["inajax"] )) return $this->handleAjaxEvents();
			// maradek eventek
			if ($this->bAckMode) {
				// letezik a tranyo?
				$this->bTransactionExists = $this->ccm->isTransactionExists( $this->aG["tran_id"] );
				$this->setParam( "bTransactionExists" , $this->bTransactionExists );
				if (!$this->bTransactionExists) return parent::handleEvents();

				// user sajat tranyoja?
				$this->bUserTransaction = $this->ccm->isUserTransaction( $this->mmm->getLoggedinUserId() , $this->aG["tran_id"] );
				$this->setParam( "bUserTransaction" , $this->bUserTransaction );
				if (!$this->bUserTransaction) return parent::handleEvents();

				// le van zarva a tranyo?
				$this->bTransactionClosed = $this->ccm->isTransactionClosed( $this->aG["tran_id"] );
				$this->setParam( "bTransactionClosed" , $this->bTransactionClosed );
				if ($this->bTransactionClosed) {
					$tran = $this->ccm->getTransactionFromTransactionId( $this->aG["tran_id"] );
					$this->setParam( "tran" , $tran );
					$member = $this->mm->getMemberFromMemberId( $this->mmm->getLoggedInMemberId() );
					$this->setParam( "member" , $member );
				}
			} else {
				$this->refreshChildValuesFromArray();
				if (!$this->oSubmit->isSubmitted()) return parent::handleEvents();
				switch ($this->aP["subscription_length"]) {
				default:
				case 6:
					$prolong_days = SystemParametersModel::getVar( "half_year_length_in_days" );break;
				case 12:
					$prolong_days = SystemParametersModel::getVar( "whole_year_length_in_days" );break;
				}

				$price = $this->mm->getMemberProlongPrice(
						$this->mmm->getLoggedInMemberid() ,
						$this->aP["subscription_length"] );
				if (AdminIpModel::isAdminIp( fo3_get_remote_ipaddress() )) {
					$price = (int) ($price / 100);
				}
				$this->ccm = new CreditCardModel();
				$this->ccm->startTransaction(
					$this->mmm->getLoggedinUserId() ,	// user_id
					$price ,
					"huf",								// currency
					sprintf( "fordit.hu előfizetés vásárlásra %d hónapra",
						$this->aP["subscription_length"] ),	// comment
					array( "prolong_days" => $prolong_days ) );
			}
			return parent::handleEvents();
		}

		protected function calculatePrices() {
			$member_id = $this->mmm->getLoggedInMemberid();
			$price_6m = $this->mm->getMemberProlongPrice( $member_id , 6 );
			$price_12m = $this->mm->getMemberProlongPrice( $member_id , 12 );

			$this->setParam( "price_6m" , $price_6m );
			$this->setParam( "price_12m" , $price_12m );
		}

		public function render() {
			if ($this->sRendered) return $this->sRendered;
			$this->initializeWidgets();
			$this->generateLangDefs();
			$this->setParam( "bAckMode" , $this->bAckMode );
			if ($this->bAckMode) {

			} else {
				$cm = new ConstantsModel( $this );
				$cm->addSystemParametersToTemplate();
				$mmm = new MyMemberModel();
				$member_id = $mmm->getLoggedInMemberId();
				$cm->addMemberConstantsToTemplate( $member_id );
				$this->calculatePrices();
			}
			// FNTH_da( $this->aParams );
			return parent::render();
		}

	}


?>
