<?php
	// Assets::add( 'tinymce' );
	// Assets::add( 'dropzone' );
	Assets::add( 'menus.min.js' );
	// Assets::add( 'menus.css' );
	AMIB::add_js_translation( trans( 'menu.js-msgs' ) );
?>
<?php $__env->startSection( 'head' ); ?>
	<script>
		var deleteUrl = "<?php echo route( 'delete-menu-ajax' ); ?>";
		var getDataUrl = "<?php echo route( 'get-menu-data-ajax' ); ?>";
		var uploadUrl = "<?php echo route( 'att-upload' ); ?>";
	</script>
<?php $__env->stopSection(); ?>

<?php $__env->startSection( 'content' ); ?>
	<?php echo Form::model( $old_search_data, array( 'method' => 'get', 'id' => 'search_form' ) ); ?>

		<?php echo Form::select( 's_type', $s_types_select ); ?>

		<?php echo Form::submit( trans( 'main.search' ) ); ?>

	<?php echo Form::close(); ?>


	<?php if( count( $menus_select ) ): ?>
		<table id="menus-table" class="default">
			<thead>
				<tr>
					<th><?php echo trans( 'menu.title' ); ?></th>
					<th><?php echo trans( 'menu.menu_order' ); ?></th>
					<th><?php echo trans( 'menu.url' ); ?></th>
					<th><?php echo trans( 'menu.type' ); ?></th>
					<th><?php echo trans( 'menu.access' ); ?></th>
					<th></th>
				</tr>
			</thead>
			<tbody>
				<?php echo create_menu_rows( $menus_select ); ?>

			</tbody>
		</table>
	<?php else: ?>
		<div class="info-message"><?php echo trans( 'main.empty' ); ?></div>
	<?php endif; ?>

	<hr>

	<?php echo HTML::link( '#', trans( 'menu.new' ), array( 'class' => 'button add-new' ) ); ?>


	<div id="add-edit-popup" class="white-popup mfp-hide mfp-with-anim">
		<?php echo Form::open( array( 'route' => 'add-edit-menu-ajax', 'files' => true ) ); ?>

			<?php echo Form::hidden( 'old_id', null, array( 'id' => 'old_id' ) ); ?>

			<table>
				<tr>
					<td><?php echo Form::label( 'title', trans( 'menu.title' ), array( 'class' => 'required' ) ); ?></td>
					<td><?php echo Form::text( 'title' ); ?></td>
				</tr>
				
				<tr>
					<td><?php echo Form::label( 'type', trans( 'menu.type' ) ); ?></td>
					<td><?php echo Form::select( 'type', $types_select ); ?></td>
				</tr>
				
				<tr>
					<td><?php echo Form::label( 'parent_id', trans( 'menu.parent_id' ) ); ?></td>
					<td>
						<select name="parent_id" id="parent_id">
							<option value="0">--</option>
							<?php echo generate_options( $menus_select ); ?>

						</select>
					</td>
				</tr>
				
				<tr>
					<td><?php echo Form::label( 'access', trans( 'menu.access' ) ); ?></td>
					<td><?php echo Form::select( 'access', $access_types_select ); ?></td>
				</tr>
				
				<tr>
					<td><?php echo Form::label( 'url', trans( 'menu.url' ), array( 'class' => 'required' ) ); ?></td>
					<td>
						<?php echo Form::select( 'system_url', $system_urls_select ); ?>

						<br>
						<?php echo Form::text( 'url', null, array( 'class' => 'ltr' ) ); ?>

					</td>
				</tr>
				
				<tr>
					<td><?php echo Form::label( 'menu_order', trans( 'menu.menu_order' ) ); ?></td>
					<td><?php echo Form::text( 'menu_order', '0' ); ?></td>
				</tr>
				
				<tr>
					<td colspan="2">
						<div class="progress"><div></div></div>
						<div class="error-container"></div>
						<?php echo Form::submit( trans( 'main.save' ) ); ?>

					</td>
				</tr>
			</table>
		<?php echo Form::close(); ?>

	</div>
<?php $__env->stopSection(); ?>
<?php
	function create_menu_rows( $hierarchical_array, $depth = 0 ) {
		// var_dump( $hierarchical_array );
		foreach( $hierarchical_array as $menu ) {
			$ret[] = "<tr data-id='{$menu[ 'id' ]}'>";
				$ret[] = "<td>" . str_repeat( '-', $depth * 4 ) . ' ' . e( $menu[ 'title' ] ) . "</td>";
				$ret[] = "<td>" . en2fa( $menu[ 'menu_order' ] ) . "</td>";
				$ret[] = "<td><input type='text' readonly value='{$menu[ 'url' ]}' class='ltr'></td>";
				$ret[] = "<td><span class='tag'>" . e( $menu[ 'type_text' ] ) . "</span></td>";
				$ret[] = "<td><span class='tag'>" . e( $menu[ 'access_text' ] ) . "</span></td>";
				$ret[] = "<td>";
					$ret[] = HTML::link( '#', trans( 'main.edit' ), array( 'class' => 'button edit' ) );
					$ret[] = HTML::link( '#', trans( 'main.delete' ), array( 'class' => 'button delete' ) );
				$ret[] = "</td>";
			$ret[] = "</tr>";

			if( isset( $menu[ 'children' ] ) ) {
				$ret[] = create_menu_rows( $menu[ 'children' ], $depth + 1 );
			}
		}

		return implode( '', $ret );
	}