<?php
	require_once( $GLOBALS["site_root"]."/includes/widgets/BackendGrid.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminTextbox.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminSelect.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminArticleCategory.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminSuperGridHeader.inc.php" );
	require_once( $GLOBALS["site_root"]."/includes/widgets/AdminUserNameTag.inc.php" );

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

	class AdminPaymentsGridHeader extends AdminSuperGridHeader {
		public $oKeywordFilter;

		public function __construct( $id , $parent = null ) {
			parent::__construct( $id , $parent );
			$this->oKeywordFilter = new AdminTextbox( "keyword_filter" , $this );
			$this->oKeywordFilter->setTitle( __("Keyword") );
			$this->oKeywordFilter->setShowTitle( true );
		}
	}

	class AdminPaymentsGrid extends BackendGrid {
		protected $larticle_title_filter;
		protected $article_name_filter;
		protected $bIsSelector = false;

		public function __construct( $id , $parent ) {
			parent::__construct( $id , $parent );
			$this->sTableName = "cc_transactions";
			$this->sTitle = __("payments_Payments");
			$this->setSelectable( true );

			$this->addToolbarButton( "filter" , __("Filter","attr") , "search",$this->renderStandardAction( "filter" ) );
			$this->addToolbarButton( "finish_unfinisheds" , __("payments_Finish_unfinisheds","attr") , "page_process" , $this->renderStandardAction( "finish_unfinisheds" ) );
			// $this->addToolbarButton( "modify" , __("Modify","attr") , "page_edit" , $this->renderStandardAction( "modify" ) , false );
			// $this->addToolbarButton( "delete" , __("Delete","attr") , "page_remove",$this->renderStandardAction( "delete" ) , false );
			FNTH_include_3rdparty( "contextmenu" );
			FNTH_include_3rdparty( "tooltip" );
		}

		public function setSelector( $v ) { $this->bIsSelector = $v; }

		protected function addSuperGridHeaderFactory( $id , $parent ) {
			return new AdminPaymentsGridHeader( $id , $parent );
		}

		protected function prepareDataset() {
			$retval = array();

			if (empty( $this->aData )) return;
			$otp_ids = array();
			foreach ($this->aData as $row) if ($row["bankname"]=="otp") $otp_ids[]=$row["bank_transaction_id"];
			if (!empty( $otp_ids )) {
				$query = sprintf( "select * from otp_transactions where otp_transaction_id in (%s)" , implode( "," , $otp_ids ) );
				$ds = FNTH_db_get_resultset( $query );
				foreach ($ds as $otprow) $otp[$otprow["otp_transaction_id"]]=$otprow;
				unset( $ds );
				foreach ($this->aData as $id => $row)
					if ($row["bankname"]=="otp" && isset( $row["bank_transaction_id"] ) && isset( $otp[$row["bank_transaction_id"]] ))
						$this->aData[$id]["bank_specific"]=$otp[$row["bank_transaction_id"]];
			}
		}

		protected function whereClause() {
			$filters = array();

			$f = $this->cc_status_filter->getValue();
			if (strlen( $f )) {
				switch ($f) {
				case "": break;
				case "paid":$filters[]="paid_tstamp is not null";break;
				case "canceled":$filters[]="cancel_tstamp is not null";break;
				case "unfinished":$filters[]="(paid_tstamp is null and cancel_tstamp is null)";break;
				}
			}

			if (empty( $filters )) return "";
			return " where ".implode( " and " , $filters );
		}

		public function orderClause() {
			switch ($this->sOrdering) {
			default: return " order by cc_transaction_id desc";
			case "cc_transaction_id":$retval="cc_transaction_id";break;
			case "ctstamp": $retval="ctstamp";break;
			}
			if (empty($retval)) return "";
			return sprintf( " order by %s%s",
				$retval,($this->bAscending) ? "" : " desc" );
		}

		protected function fieldsClause() {
			return "cc_transactions.cc_transaction_id as id,
				cc_transactions.ctstamp,
				cc_transactions.amount,
				cc_transactions.user_id,
				cc_transactions.paid_tstamp,
				cc_transactions.cancel_tstamp,
				cc_transactions.bankname,
				cc_transactions.bank_transaction_id";
		}

		public function constructColumns() {
			$this->addColumn( "cc_transaction_id" , __("payments_Transaction_id","attr") , true );
			$this->addColumn( "ctstamp" , __("payments_Created_at","attr") , true );
			$this->addColumn( "user" , __("payments_User","attr") , false );
			$this->addColumn( "status" , __("payments_Status","attr") , false );
			$this->addColumn( "amount" , __("payments_Amount","attr") , false );
			$this->addColumn( "info" , ("Info") , false );
		}

		public function constructRow( $datarow , $rownum ) {
			$retval = parent::constructRow( $datarow , $rownum );
			if (isset( $datarow["paid_tstamp"] )) {
				$c = "paid";
			} else if (isset( $datarow["cancel_tstamp"] )) {
				$c = "cancel";
			} else {
				$c = "unfinished";
			}
			$retval->setAttribute( "class" , $c );
			return $retval;
		}

		public function constructFilterHeader() {
			$headerrow = new FNTHGridRow( "filterrow" , $this , "header" );

			foreach ($this->aColumns as $colname => $column) {
				$field = new FNTHGridField( "fld_".$colname, $headerrow );	// ures
				switch ($colname) {
				default:break;
				case "status":
					$this->cc_status_filter = new AdminSelect( "cc_status_filter" , $field );
					$this->cc_status_filter->addOption( "" , __("payments_filter_All","attr") );
					$this->cc_status_filter->addOption( "paid" , __("payments_filter_Paid","attr") );
					$this->cc_status_filter->addOption( "canceled" , __("payments_filter_Canceled","attr") );
					$this->cc_status_filter->addOption( "unfinished" , __("payments_filter_Unfinished","attr") );
					break;
				}
			}
		}

		public function handleEvents() {
			// simple POST tasks
			if (isset( $this->aP["task"] )) {
				switch ($this->aP["task"]) {
				case "filter":
					$u = $this->aG;
					foreach ($this->aP as $k => $v) {
						if ($k=="task") continue;
						$u[$k]=$v;
					}
					unset( $u[ $this->getId()."_pagenum"] );
					FNTH_redirect( FNTH_render_url( $_SERVER["PHP_SELF"] , $u ) );
					fo_exit();
				}
			}

			// ajax events
			if (!isset( $this->aP["ajaxevent"] )) {
				return parent::handleEvents();
			}
			if (!isset( $this->aP["tbl"] )) {
				return parent::handleEvents();
			}
			if ($this->aP["tbl"]!=$this->getId()) {
				return parent::handleEvents();
			}
			$action_reply = "";
			switch ($this->aP["action"]) {
			case "finish_unfinisheds":
				$tm = new TaskManagerModel();
				$tm->newTask();
				$tm->setTaskName( sprintf( "finish transactions" ) );
				$tm->setTaskDescription( "finishing unfinished transactions" );
				$tm->setTaskType( "finish_unfinisheds" );
				$task_id = $tm->saveNewTask();
				$action_reply = "document.location.href=document.location.href;";

			}	
			FNTH_direct_echo( $action_reply );
			fo_exit();
		}

		protected function generateLangDefs() {
			fo3_add_js_langdef( "payments_confirm_Finish_transactions" );
		}

		public function constructField( $rownum , $row , $colname ) {
			$field = parent::constructField( $rownum , $row , $colname );
			$rd = $this->aData[$rownum];

			switch ($colname) {
			case "cc_transaction_id":
				$field->setValue( $rd["id"] );
				$field->setAttribute( "align" , "right" );
				break;
			case "ctstamp":
				$field->setValue( fo3_format_timestamp_to_screen( $rd["ctstamp"] ) );
				$field->setAttribute( "align" , "center" );
				break;
			case "user":
				$user = new AdminUserNameTag( "user_" . $rd["id"] , $field );
				$user->setId( $rd["user_id"] );
				$field->setValue( "" );
				break;
			case "amount":
				$field->setAttribute( "align" , "right" );
				break;
			case "status":
				$val = "";
				if (isset( $rd["paid_tstamp"] )) {
					$val.=__("payments_Paid_transaction");
					$val.="<br/>" . fo3_format_timestamp_to_screen( $rd["paid_tstamp"] );
				} else if (isset( $rd["cancel_tstamp"] )) {
					$val.=__("payments_Canceled_transaction" );
					$val.="<br/>" . fo3_format_timestamp_to_screen( $rd["cancel_tstamp"] );
				} else {
					$val.=__("payments_Unfinished_transaction" );
				}
				$field->setValue( $val );
				$field->setAttribute( "align" , "center" );
				break;
			case "info":
				$field->setAttribute( "align" , "center" );
				if (isset( $rd["paid_tstamp"] )) {
					$field->setValue( "---");
					break;
				} else if (isset( $rd["cancel_tstamp"] )) {
					if (isset( $rd["bank_specific"]["comments"] ))
						$field->setValue( $rd["bank_specific"]["comments"] );
					else
						$field->setValue( "???" );
				}
				break;
			}
			return $field;
		}

		public function render() {
			$this->generateLangdefs();
			return parent::render();
		}

	}

?>
