@page
@inject IAntiforgery _antiforgery
@using Microsoft.AspNetCore.Antiforgery
@using MediaManager.database
@model Movies
@{
	ViewData["Title"] = "Movies";
	Layout = "_LayoutNoContainer";
	var tokenSet = _antiforgery.GetAndStoreTokens(HttpContext);
}

<div class="text-center">
	<h1 class="display-5">
		Movies
		<a class="btn btn-lg btn-primary" href="/AddMovie">Add</a>

	</h1>
	<table class="table table-striped table-hover">
		<thead>
		<tr>
			<th scope="col">Title</th>
			<th scope="col">Rating</th>
			<th scope="col">Rewatchability</th>
			<th scope="col">Watch count</th>
			<th scope="col">Last Watch</th>
			<th scope="col">Comment</th>
			<th scope="col">Actions</th>
		</tr>
		</thead>
		<tbody>
		@foreach (var movie in new Database.DbConn().Movies.Where(p => p.UserId == Model.AuthorizedUser.UserId).OrderBy(p => p.Title.ToLower())) {
			<tr id="movie_@movie.MovieId">
				<td>
					<b>@movie.Title</b>
					<br/>
					<small>@movie.Year</small>
				</td>
				<td class="td-progress">
					@if (movie.Rating > 0) {
						<div class="progress">
							<div class="progress-bar progress-@(movie.Rating)0" role="progressbar" style="width: @(movie.Rating * 10)%">@(movie.Rating)</div>
						</div>
					}
					else {
						<div class="progress">
							<div class="progress-bar progress-00" role="progressbar" style="width: 0"></div>
						</div>
					}
				</td>
				<td class="td-progress">
					@if (movie.Rewatchability > 0) {
						<div class="progress">
							<div class="progress-bar progress-@(movie.Rewatchability)0" role="progressbar" style="width: @(movie.Rewatchability * 10)%">@(movie.Rewatchability)</div>
						</div>
					}
					else {
						<div class="progress">
							<div class="progress-bar progress-00" role="progressbar" style="width: 0"></div>
						</div>
					}
				</td>
				<td class="td-progress">
					<div class="progress">
						<div class="progress-bar progress-@(Math.Min(movie.WatchCount, 5) * 2)0" role="progressbar" style="width: @(Math.Min(movie.WatchCount, 5) * 20)%">@(movie.WatchCount)</div>
					</div>
				</td>
				<td>
					@if (movie.LastSeen.Year > 1800) {
						@movie.LastSeen.ToString("yyyy-MM-dd")
					}
					else {
						@Html.Raw("-")
					}
				</td>
				<td>
					@movie.Comment
				</td>
				<td>
					<div class="btn-group" role="group">
						<button class="btn btn-sm btn-secondary" onclick="ajax_and_reload('/EditMovie/@movie.MovieId', 'autoinc')">W+1</button>
						<a class="btn btn-sm btn-primary" href="/EditMovie/@movie.MovieId">Edit</a>
					</div>
				</td>
			</tr>
		}
		</tbody>
	</table>
</div>

<script>
	function ajax_and_reload(url, action, target, value) {	
		if (window.location.hash.length > 0){
			window.location.replace("#");
			if (typeof window.history.replaceState == 'function') {
				history.replaceState({}, '', window.location.href.slice(0, -1));
			}
		}
		$.ajax(url, {method: 'POST', data: {action: action, target: target, value: value, '__RequestVerificationToken' : '@tokenSet.RequestToken'}, success: function () { location.reload() }})
	}
</script>
