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

use SabaiApps\Directories\Component\Form;
use SabaiApps\Directories\Component\Payment\ISubscription;
use SabaiApps\Directories\Context;

class DashboardSubscriptions extends Form\Controller {

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

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

		// Get subscriptions
		$default_payment_component_name = $payment_component->paymentComponentName();
		foreach ( $payment_component->paymentGetUserSubscriptions( $this->getUser()->id ) as $subscription ) {
			$form['subscriptions']['#options'][ $default_payment_component_name . '-' . $subscription->paymentSubscriptionId() ] = $this->_getSubscriptionRow( $context, $subscription, $default_payment_component_name );
		}

		// Get active subscriptions from inactive payment components.
		foreach ( array_keys( $this->Payment_Components() ) as $payment_component_name ) {
			if ( $payment_component_name === $default_payment_component_name
				|| ( ! $payment_component = $this->Payment_Components_impl( $payment_component_name, true ) )
				|| ! $payment_component->paymentIsEnabled()
			) {
				continue;
			}

			foreach ( $payment_component->paymentGetUserSubscriptions( $this->getUser()->id ) as $subscription ) {
				if ( ! $subscription->paymentSubscriptionIsActive() ) {
					continue;
				}

				$form['subscriptions']['#options'][ $payment_component_name . '-' . $subscription->paymentSubscriptionId() ] = $this->_getSubscriptionRow( $context, $subscription, $payment_component_name );
			}
		}

		return $form;
	}

	protected function _getSubscriptionRow( Context $context, ISubscription $subscription, $paymentComponentName ) {
		$status = $subscription->paymentSubscriptionStatus();
		if ( is_array( $status ) ) {
			$status_color = $status[1];
			$status       = $status[0];
		} else {
			$status_color = 'secondary';
		}
		return array(
			'id'     => $this->LinkTo(
				$subscription->paymentSubscriptionNumber(),
				'#',
				array(
					'url'       => $this->_application->Dashboard_Panels_panelUrl(
						'payment_payments',
						$context->dashboard_panel_link,
						'/payment_subscriptions/' . $paymentComponentName . '-' . $subscription->paymentSubscriptionId(),
						array(),
						true
					),
					'container' => 'modal',
					'modalSize' => 'lg',
				),
				array(
					'data-modal-title' => sprintf( __( 'Subscription %s', 'directories-pro' ), $subscription->paymentSubscriptionNumber() ),
				)
			),
			'next'   => $subscription->paymentSubscriptionNextPaymentHtml(),
			'name'   => $this->H( $subscription->paymentSubscriptionName() ),
			'status' => sprintf( '<span class="%1$sbadge %1$sbg-%2$s">%3$s</span>', DRTS_BS_PREFIX, $status_color, $this->H( $status ) ),
			'total'  => $subscription->paymentSubscriptionTotalHtml(),
		);
	}
}
