<?php
namespace SabaiApps\Directories\Component\Payment\Controller;

use SabaiApps\Directories\Component\Form;
use SabaiApps\Directories\Context;
use SabaiApps\Framework\Paginator\CustomPaginator;

class DashboardOrders extends Form\Controller {

	protected function _doGetFormSettings( Context $context, array &$formStorage ) {
		$payment_component = $this->getComponent( 'Payment' )->getPaymentComponent( true );

		// Init form
		$form = array(
			'orders' => array(
				'#type'             => 'tableselect',
				'#header'           => array(
					'id'     => '#',
					'date'   => __( 'Date', 'directories-pro' ),
					'name'   => __( 'Name', 'directories-pro' ),
					'total'  => __( 'Total', 'directories-pro' ),
					'status' => __( 'Status', 'directories-pro' ),
				),
				'#options'          => array(),
				'#options_disabled' => array(),
				'#disabled'         => true,
				'#multiple'         => true,
				'#class'            => 'drts-data-table',
			),
		);

		// Paginate orders
		$pager = new CustomPaginator(
			array( $payment_component, 'paymentCountUserOrders' ),
			array( $payment_component, 'paymentGetUserOrders' ),
			20,
			array(),
			array( $this->getUser()->id )
		);
		$pager->setCurrentPage( $context->getRequest()->asInt( $this->getPlatform()->getPageParam(), 1 ) );
		foreach ( $pager->getElements() as $order ) {
			$status = $order->paymentOrderStatus();
			if ( is_array( $status ) ) {
				$status_color = $status[1];
				$status       = $status[0];
			} else {
				$status_color = 'secondary';
			}
			$row = array(
				'id'     => $this->LinkTo(
					$order->paymentOrderNumber(),
					'#',
					array(
						'url'       => $this->_application->Dashboard_Panels_panelUrl(
							'payment_payments',
							$context->dashboard_panel_link,
							'/payment_orders/' . $order->paymentOrderId(),
							array(),
							true
						),
						'container' => 'modal',
						'modalSize' => 'lg',
					),
					array(
						'data-modal-title' => $order->paymentOrderNumber(),
					)
				),
				'date'   => ( $ts = $order->paymentOrderTimestamp() ) ? $this->_application->System_Date( $ts ) : '',
				'name'   => $this->H( $order->paymentOrderName() . ' - ' . $this->_application->Payment_Util_actionLabel( $order->paymentOrderAction() ) ),
				'status' => sprintf( '<span class="%1$sbadge %1$sbg-%2$s">%3$s</span>', DRTS_BS_PREFIX, $status_color, $this->H( $status ) ),
				'total'  => $order->paymentOrderTotalHtml(),
			);
			$form['orders']['#options'][ $order->paymentOrderId() ] = $row;
		}

		$page_url      = $this->_application->Dashboard_Panels_panelUrl(
			'payment_payments',
			$context->dashboard_panel_link,
			'/payment_orders',
			array(),
			true
		);
		$form['pager'] = array(
			'#type'   => 'markup',
			'#markup' => '<div class="' . DRTS_BS_PREFIX . 'float-end ' . DRTS_BS_PREFIX . 'mt-4">'
				. $this->System_PageNav( $context->getContainer(), $pager, $page_url )
				. '</div>',
		);

		return $form;
	}
}
