<div id="media_container">
<div class="white_container">

	<h2 style="margin-bottom:20px;"><i class="fa fa-comments"></i><span> <?php echo Lang::get('media.media'); ?></span></h2>
	<div class="col-sm-12" style="padding-left:0px; padding-right:0px;">

		<div class="table-responsive">
			<table class="table table-striped">
				<tr class="table-header">
					<th><a href="?sort=title&order=<?php if(isset($_GET['order']) && $_GET['order'] == 'ASC'){ echo 'DESC'; }else{ echo 'ASC'; } ?>"><?php echo Lang::get('common.title'); ?> <i class="fa fa-sort"></i></a></th>
					<th><?php echo Lang::get('category.category'); ?></th>
					<th style="min-width:100px;"><a href="?sort=active&order=<?php if(isset($_GET['order']) && $_GET['order'] == 'ASC'){ echo 'DESC'; }else{ echo 'ASC'; } ?>"><?php echo Lang::get('common.active'); ?> <i class="fa fa-sort"></i></th>
					
					<th><a href="?sort=created_at&order=<?php if(isset($_GET['order']) && $_GET['order'] == 'ASC'){ echo 'DESC'; }else{ echo 'ASC'; } ?>"><?php echo Lang::get('common.date_added'); ?> <i class="fa fa-sort"></i></th>
					<th><?php echo Lang::get('common.actions'); ?></th>
					<?php foreach($media as $item): ?>
					<tr>
						<td><a href="<?php echo URL::to('media') . '/' . $item->slug; ?>" data-toggle="modal">
							<?php if(strlen($item->title) > 40){
									echo substr($item->title, 0, 40) . '...';
								  } else {
								  	echo $item->title;
								  }
							?>
							</a>
						</td>
						<td><?php if($item->category): ?><a href="<?php echo URL::to('category') . '/' . strtolower($item->category->name); ?>"><?php echo $item->category->name; ?></a><?php endif; ?></td>
						<td class="active-<?php echo $item->id; ?>"><?php echo $item->active; ?></th>
						<td><?php echo date('M d, Y - h:ia', strtotime($item->created_at)); ?></td>
						<td style="min-width:152px;"><a href="#" data-toggle="modal" data-target="#edit-<?php echo $item->id; ?>" class="btn btn-xs btn-primary edit-media" style="margin-right:10px;"><span class="fa fa-edit"></span></a><a href="#_" data-href="<?php echo URL::to('media/delete') . '/' . $item->id; ?>" onclick="confirm_delete(this)" class="btn btn-xs btn-danger" style="margin-right:10px;"><span class="fa fa-trash-o"></span></a><a href="#_" data-id="<?php echo $item->id; ?>" data-href="<?php echo URL::to('admin/media/toggle_active') . '/' . $item->id; ?>" onclick="toggle_active(this)" class="btn btn-xs <?php if($item->active): ?> btn-danger <?php else: ?> btn-success <?php endif; ?> active-toggle"><span class="fa fa-minus-circle"></span> <span class="text"><?php if($item->active): ?> <?php echo Lang::get('common.set_inactive'); ?> <?php else: ?> <?php echo Lang::get('common.set_active'); ?> <?php endif; ?></span></a></td>
						
					</tr>
					<?php endforeach; ?>
			</table>
		</div>

	</div>

	<?php echo $media->appends(array('sort' => Input::get('sort'), 'order' => Input::get('order')))->links(); ?>

</div>
</div>

<script type="text/javascript" src="<?php echo URL::asset('assets/js/bootstrap-confirmation.js'); ?>"></script>
<script>
$(document).ready(function(){
	$('.active-toggle').click(function(){
		if($(this).hasClass('btn-danger')){
			$(this).removeClass('btn-danger').addClass('btn-success');
			$(this).children('span.fa').removeClass('fa-minus-circle').addClass('fa-plus-circle');
			$(this).children('span.text').text("<?php echo Lang::get('common.set_active'); ?>");
			active_class = '.active-' + $(this).data('id');
			$(active_class).text(0);
		} else {
			$(this).removeClass('btn-success').addClass('btn-danger');
			$(this).children('span.fa').removeClass('fa-plus-circle').addClass('fa-minus-circle');
			$(this).children('span.text').text("<?php echo Lang::get('common.set_inactive'); ?>");
			active_class = '.active-' + $(this).data('id');
			$(active_class).text(1);
		}

	});

	$('.pagination a').click(function(e) {
		e.preventDefault();
	  	var url = $(this).attr('href');
	 	$('div#admin_section').load(url, function(data){
	 		console.log('hit');
	 	}); // load the html response into a DOM element
	});
});

function confirm_delete(obj){
	var delete_link = $(obj).data('href');
	var result = confirm("<?php echo Lang::get('common.delete_item_confirm'); ?>");
	if(result){
		location.href=delete_link;
	}
}

function toggle_active(obj){
	url = $(obj).data('href');
	$.get(url);
}
	//$('.edit-media').confirmation();
</script>


<?php $categories = Category::orderBy('order', 'ASC')->get(); ?>
<?php $settings = Setting::first(); ?>

	<?php foreach($media as $item): ?>
	<!-- Modal -->
	<div class="modal fade" id="edit-<?php echo $item->id; ?>" data-id="<?php echo $item->id; ?>" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog">
	    <div class="modal-content">
	      <div class="modal-header">
	        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
	        <h4 class="modal-title" id="myModalLabel"><?php echo Lang::get('common.editing'); ?> <?php echo $item->title; ?></h4>
	      </div>
	      <div class="modal-body">
	      
	      	<?php echo Form::model($media, array('method' => 'PATCH', 'route' => array('media.update', $item->id))); ?>
			<ul>
		        <li>
		            <label for="title"><?php echo Lang::get('common.title'); ?></label>
		            <input type="text" class="form-control" name="title" id="title" value="<?php echo $item->title; ?>" />
		        </li>

		        <li>
		        	<label for="title"><?php echo Lang::get('category.category'); ?></label>
		        	<select class="form-control" id="category" name="category">
		        		<?php foreach($categories as $category): ?>
		        			<option value="<?php echo $category->id; ?>" <?php if($category->id == $item->category_id): ?> selected="selected" <?php endif; ?>><?php echo $category->name; ?></option>
		        		<?php endforeach; ?>
		        	</select>
		        </li>

		        <?php if($settings->media_description): ?>
		        	<li>
		        		<label for="description"><?php echo Lang::get('common.description'); ?></label>
	                	<p><textarea name="description" class="form-control" id="description" placeholder="<?php echo Lang::get('common.description'); ?>"></textarea></p>   
		        	</li>
		        <?php endif; ?>

		        <li>
		            <label for="source"><?php echo Lang::get('common.source'); ?></label>
		            <input type="text" class="form-control" name="source" id="source" value="<?php echo $item->link_url; ?>" />
		        </li>

		        <li>
		            <label for="tags"><?php echo Lang::get('common.tags'); ?></label>
		            <input class="form-control tags_input" name="tags" id="tags" value="<?php echo $item->tags; ?>" style="width:100%; height:auto;" />
		        </li>

		        <li>
		            <label for="slug"><?php echo Lang::get('common.url'); ?></label>
		            <input type="text" class="form-control" name="slug" id="slug" value="<?php echo $item->slug; ?>" />
		        </li>

			</ul>
			<input type="hidden" id="id" name="id" value="<?php echo $item->id; ?>" />
			<input type="hidden" id="redirect" name="redirect" value="<?php echo URL::to('admin'); ?>" />

	      </div>
	      <div class="modal-footer">
	        <button type="button" class="btn btn-default" data-dismiss="modal"><?php echo Lang::get('common.cancel'); ?></button>
	        <input type="submit" class="btn btn-color" value="<?php echo Lang::get('common.update_media'); ?>" />
	      </div>
	      <?php echo Form::close(); ?>
	    </div><!-- /.modal-content -->
	  </div><!-- /.modal-dialog -->
	</div><!-- /.modal -->



	<?php endforeach; ?>

	<script type="text/javascript" src="<?php echo URL::asset('assets/js/tagsinput/jquery.tagsinput.js'); ?>"></script>
	<script>
		$(document).ready(function(){
			$('.tags_input').tagsInput();
		});
	</script>
