@using WebApplication1.Models
@model Personne
@{
	ViewBag.Title = "Annuaire";
	IEnumerable<Titre> titres = ViewBag.Titres;
	IEnumerable<Personne> resultats = ViewBag.Resultats;
	string message = ViewBag.Message;
	string tri = ViewBag.Tri;
	Personne filtre = Model;
	Guid idEnEdition = ViewBag.IdEnEdition ?? Guid.Empty;
	int page = ViewBag.Page ?? 0;
	int nbPersonnes = ViewBag.NbPersonnes ?? 0;
	int taillePage = ViewBag.TaillePage;
	int nbPages = (int)Math.Ceiling( (double)nbPersonnes / taillePage );
}

<h2>@ViewBag.Title</h2>

<hr />
<h4>
	@taillePage enregistrements par page. @Html.ActionLink( "Changer", "Preferences" )
</h4>
<hr />

@using( Html.BeginForm( "Index", "ExerciceAnnuaire", FormMethod.Get ) ) {

	<div>
		<div class="row">
			@Html.LabelFor( m => m.TitreId, new { @class = "control-label col-sm-2" } )
			<div class="col-sm-10 ">
				@Html.DropDownListFor( m => m.TitreId, titres.Select( t => new SelectListItem() { Text = t.Civilite, Value = t.Id.ToString() } ), String.Empty, new { @class = "form-control" } )
			</div>
		</div>

		<div class="row">
			@Html.LabelFor( m => m.Prenom, new { @class = "control-label col-sm-2" } )
			<div class="col-sm-10">
				@Html.TextBoxFor( m => m.Prenom, new { @class = "form-control" } )
			</div>
		</div>

		<div class="row">
			@Html.LabelFor( m => m.Nom, new { @class = "control-label col-sm-2" } )
			<div class="col-sm-10">
				@Html.TextBoxFor( m => m.Nom, new { @class = "form-control" } )
			</div>
		</div>

		<div class="row">
			@Html.LabelFor( m => m.Telephone, new { @class = "control-label col-sm-2" } )
			<div class="col-sm-10">
				@Html.TextBoxFor( m => m.Telephone, new { @class = "form-control" } )
			</div>
		</div>

		<div class="row">
			<div class="col-sm-offset-2 col-sm-10">
				<input type="submit" name="btRechercher" value="Rechercher" class="btn btn-default" />
				@Html.ValidationSummary( true, string.Empty, new { @class = "text-danger" } )
			</div>
		</div>

	</div>

	@Html.Hidden( "tri", tri )

}

@if( message != null ) {
	<hr />
	<div class="panel panel-info">
		<div class="panel-heading">Message</div>
		<div class="panel-body">
			<h3>@message</h3>
		</div>
	</div>
}

@if( resultats != null ) {
	<hr />
	<div class="panel panel-default">
		<div class="panel-heading">Résultats</div>
		<div class="panel-body">
			@using( Html.BeginForm( "Valider", "ExerciceAnnuaire", new {
				tri = tri,
				page = page, // L'Id en édition viendra d'un input hidden du formulaire, ça me parrait plus juste
				filtre.Nom,
				filtre.Prenom,
				filtre.Telephone,
				filtre.TitreId
			}, FormMethod.Post ) ) {

				<table class="table table-bordered table-striped">
					<tr>
						<th>Édition</th>
						<th>
							<a href="@Url.Action( "Index", new {
										tri = tri == "civilite_asc" ? "civilite_desc" : "civilite_asc",
										filtre.Nom,
										filtre.Prenom,
										filtre.Telephone,
										filtre.TitreId
									} )">
								@Html.DisplayNameFor( p => p.TitreId )
							</a>
						</th>
						<th>@Html.LabelFor( p => p.Prenom )</th>
						<th>
							<a href="@Url.Action( "Index", new {
										tri = tri == "nom_asc" ? "nom_desc" : "nom_asc",
										filtre.Nom,
										filtre.Prenom,
										filtre.Telephone,
										filtre.TitreId
									} )">
								@Html.DisplayNameFor( p => p.Nom )
							</a>
						</th>
						<th>@Html.LabelFor( p => p.Telephone )</th>
					</tr>
					@foreach( var p in resultats ) {
						if( p.Id == idEnEdition ) {
							<tr>
								<td>
									<input type="submit" value="Valider" />
									@Html.Hidden( "IdEnEdition", idEnEdition )
								</td>
								<td>@p.Titre.Civilite</td>
								<td>@p.Prenom</td>
								<td>@p.Nom</td>
								<td>@Html.TextBox( "nvTelephone", p.Telephone )</td>
							</tr>
						}
						else {
							<tr>
								<td>
									@Html.ActionLink( "Éditer", "Index", new {
							 tri,
							 page,
							 filtre.Nom,
							 filtre.Prenom,
							 filtre.Telephone,
							 filtre.TitreId,
							 idEnEdition = p.Id
						 } )
								</td>
								<td>@p.Titre.Civilite</td>
								<td>@p.Prenom</td>
								<td>@p.Nom</td>
								<td>@p.Telephone</td>
							</tr>
						}

					}
				</table>
			}

		</div>
	</div>
	for( int p = 0; p < nbPages; p++ ) {
		if( p == page ) {
			<span class="btn btn-default disabled">@(p + 1)</span>
		}
		else {
			@Html.ActionLink( (p + 1).ToString(), "Index", new {
	 tri = tri,
	 page = p,
	 filtre.Nom,
	 filtre.Prenom,
	 filtre.Telephone,
	 filtre.TitreId
 } );
		}
		@Html.Raw( " " );
	}
}
