<?php
namespace SabaiApps\Directories\Component\View\Controller\Admin;

use SabaiApps\Directories\Component\Form;
use SabaiApps\Directories\Request;
use SabaiApps\Directories\Context;

class Views extends Form\Controller {

	protected function _doGetFormSettings( Context $context, array &$storage ) {
		// Load scripts for add directory modal form
		$this->Form_Scripts( array( 'addmore', 'slider', 'latinise' ) );
		$this->getPlatform()->loadJqueryUiJs( array( 'effects-highlight' ) );

		$admin_view_path = rtrim( $context->getRoute(), '/' );
		$form            = array(
			'#bundle'   => $context->bundle,
			'views'     => array(
				'#type'           => 'tableselect',
				'#header'         => array(
					'name'    => __( 'Name', 'directories-pro' ),
					'mode'    => __( 'View Mode', 'directories-pro' ),
					'default' => __( 'Default', 'directories-pro' ),
					'links'   => '',
				),
				'#disabled'       => true,
				'#multiple'       => true,
				'#js_select'      => true,
				'#options'        => array(),
				'#class'          => 'drts-data-table',
				'#row_attributes' => array(
					'@all' => array(
						'name'    => array(
							'style' => 'width:25%;',
						),
						'mode'    => array(
							'style' => 'width:25%;',
						),
						'default' => array(
							'style' => '',
						),
						'links'   => array(
							'style' => 'white-space:nowrap;text-align:' . ( $this->getPlatform()->isRtl() ? 'left' : 'right' ) . ';',
						),
					),
				),
				'#views'          => array(),
			),
			'#js_ready' => array(
				'var params = {}; params[DRTS.params.ajax] = "#drts-modal"; $.get("' . $this->Url( rtrim( $context->getRoute(), '/' ) . '/add', array(), '', '&' ) . '", params, function (_data) {
                    DRTS.cache("drts-view-add-view", _data);
                });',
			),
		);

		foreach ( $this->getModel( 'View', 'View' )->bundleName_is( $context->bundle->name )->fetch( 0, 0, array( 'view_name', 'view_mode' ), array( 'ASC', 'ASC' ) ) as $view ) {
			if ( ! $view_mode = $this->View_Modes_impl( $view->mode, $context->bundle, true ) ) {
				continue;
			}

			$form['#views'][ $view->id ]            = $view;
			$form['views']['#options'][ $view->id ] = array(
				'name'    => $this->H( $view->getLabel() ) . ' <small>(' . $this->H( $view->name ) . ')</small>',
				'mode'    => $this->H( $view_mode->viewModeInfo( 'label' ) ),
				'default' => '<span role="button" class="drts-action-button drts-view-set-default ' . DRTS_BS_PREFIX . 'text-' . ( $view->isDefault() ? 'success' : 'muted' ) . '"'
					. ' data-action-url="' . $this->Url( $admin_view_path . '/set_default', array(), '', '&' ) . '" data-action-status-param="success"'
					. ' data-action-token="' . $this->_application->Form_Form_createToken( 'view_admin_views', 1800, true ) . '"'
					. ' data-action-data-name="' . $this->H( $view->name ) . '" data-action-id="' . $view->id . '" data-action-container=".drts-view-admin-views">'
					. '<i class="fas fa-check-circle fa-xl"></i></span>',
				'links'   => implode(
					PHP_EOL,
					array(
						$this->LinkTo(
							'',
							$this->Url( $admin_view_path . '/' . $view->id ),
							array(
								'no_escape' => true,
								'icon'      => 'fas fa-cog',
								'container' => 'modal',
								'modalSize' => 'xl',
							),
							array(
								'class'            => DRTS_BS_PREFIX . 'btn ' . DRTS_BS_PREFIX . 'btn-outline-secondary',
								'data-modal-title' => __( 'Edit View', 'directories-pro' ) . ' - ' . $view->getLabel(),
								'rel'              => 'sabaitooltip',
								'title'            => __( 'Edit View', 'directories-pro' ),
							)
						),
						$this->LinkTo(
							'',
							$this->Url( $admin_view_path . '/' . $view->id . '/clone' ),
							array(
								'no_escape' => true,
								'icon'      => 'far fa-clone',
								'container' => 'modal',
								'modalSize' => 'xl',
							),
							array(
								'class'            => DRTS_BS_PREFIX . 'btn ' . DRTS_BS_PREFIX . 'btn-outline-secondary',
								'data-modal-title' => __( 'Clone View', 'directories-pro' ) . ' - ' . $view->getLabel(),
								'rel'              => 'sabaitooltip',
								'title'            => __( 'Clone View', 'directories-pro' ),
							)
						),
						$this->LinkTo(
							'',
							$this->Url( $admin_view_path . '/' . $view->id . '/delete' ),
							array(
								'no_escape' => true,
								'icon'      => 'fas fa-trash-can',
								'container' => 'modal',
								'modalSize' => 'lg',
							),
							array(
								'class'            => DRTS_BS_PREFIX . 'btn ' . DRTS_BS_PREFIX . 'btn-outline-danger',
								'data-modal-title' => __( 'Delete View', 'directories-pro' ) . ' - ' . $view->getLabel(),
								'rel'              => 'sabaitooltip',
								'title'            => __( 'Delete View', 'directories-pro' ),
							)
						),
					)
				),
			);
		}

		return $form;
	}
}
