<?php
namespace JetApplicationModule\Admin\ManageAccess\Visitors\Roles;

use Jet\UI;
use JetApplication\Auth_Visitor_Role;
use JetApplication\Auth_Visitor_User;
use Jet\Form;
use Jet\Form_Renderer;
use Jet\Tr;
use Jet\MVC_View;

/**
 * @var MVC_View $this
 * @var Auth_Visitor_Role $role
 * @var Form $form
 * @var array $available_privileges_list
 */
$role = $this->getRaw( 'role' );
$form = $this->getRaw( 'form' );
$available_privileges_list = $this->getRaw( 'available_privileges_list' );

$form->renderer()->setDefaultLabelWidth( [Form_Renderer::LJ_SIZE_MEDIUM => 1] );
$form->renderer()->setDefaultFieldWidth( [Form_Renderer::LJ_SIZE_MEDIUM => 4] );

?>
<?= $form->start() ?>
<div class="row">
	<div class="col-md-12" style="padding: 0">
		<div class="toolbar" id="main-toolbar">
			<?= UI::button_goBack()->setUrl($this->getString( 'list_url' )) ?>

			<?php if( !$form->getIsReadonly() ): ?>
				<?= UI::button_save() ?>
			<?php endif; ?>
		</div>

		<div class="container-fluid">
			<div class="row main-row">
				<div class="col-md-12 main-col">
					<?= $form->field( 'id' ) ?>
					<?= $form->field( 'name' ) ?>
					<?= $form->field( 'description' ) ?>

					<fieldset>
						<legend><?= Tr::_( 'Privileges' ); ?></legend>
						<?php foreach( $available_privileges_list as $privilege ):
							$fl = $form->field( '/privileges/' . $privilege . '/values' );

							$fl->input()->addCustomCssStyle( 'height:300px' );
							?>
							<?= $fl ?>
						<?php endforeach; ?>
					</fieldset>


					<?php if( $role && count( $role->getUsers() ) ): ?>
						<fieldset style="margin-top: 30px">
							<legend><?= Tr::_( 'Users' ); ?></legend>
							<table class="table table-striped">
								<thead>
								<tr>
									<th><?= Tr::_( 'Username' ); ?></th>
								</tr>
								</thead>
								<tbody>
								<?php foreach( $role->getUsers() as $user ):
									/**
									 * @var Auth_Visitor_User $user
									 */
									?>
									<tr>
										<td>
											<?= $user->getUsername(); ?>
										</td>
									</tr>
								<?php endforeach; ?>
								</tbody>
							</table>
						</fieldset>
					<?php endif; ?>

				</div>
			</div>
		</div>
	</div>
</div>
<?= $form->end() ?>
