@using DAL
@model Personne
@{
	string title = "Annuaire";
	ViewBag.Title = title;
	string message = ViewBag.Message;

	IDictionary<short, Titre> dicoTitres = ViewBag.Titres;
	var titreItems = dicoTitres.Values.Select( t => new SelectListItem() { Text = t.Civilite, Value = t.Id.ToString() } );

	IEnumerable<Personne> personnes = ViewBag.Personnes;

}

<h2>@title</h2>

@using( Html.BeginForm( "Rechercher", "Annuaire", FormMethod.Get, new { @class = "form-horizontal", role = "form" } ) ) {

	<div class="form-group">
		<label class="col-sm-2 control-label" for="TitreId">@Html.DisplayNameFor( m => m.TitreId )</label>
		<div class="col-sm-10">
			@Html.DropDownListFor( m => m.TitreId, titreItems, string.Empty, new { @class = "form-control" } )
		</div>
	</div>

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

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

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

	<div class="form-group">
		<div class="col-sm-offset-2 col-sm-10">
			<input type="submit" value="Rechercher" class="btn btn-primary" />
		</div>
	</div>

}

@if( personnes != null ) {

	<hr />
	<p>@message</p>

	using( Ajax.BeginForm( "Edition", new AjaxOptions() { HttpMethod = "POST", UpdateTargetId = "LigneEnEdition", InsertionMode = InsertionMode.ReplaceWith } ) ) {

		<input type="hidden" name="id" value="" id="idPersonneAEditer" />

		<table class="table table-bordered table-condensed table-striped">
			<tr>
				<th>Actions</th>
				<th>@Html.DisplayNameFor( m => m.TitreId ) </th>
				<th>@Html.DisplayNameFor( m => m.Prenom ) </th>
				<th>@Html.DisplayNameFor( m => m.Nom ) </th>
				<th>@Html.DisplayNameFor( m => m.Telephone ) </th>
			</tr>
			@foreach( var p in personnes ) {
				<tr>
					<td>
						<input type="submit" name="Edit" value="Éditer" class="btn btn-default"
									 onclick="preparerEdition(this)" data-id="@p.Id" />
					</td>
					<td>@dicoTitres[p.TitreId].Civilite</td>
					<td>@p.Prenom</td>
					<td>@p.Nom</td>
					<td>@p.Telephone</td>
				</tr>
			}
		</table>
	}
}

@section scripts {

	<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>

	<script type="text/javascript">
		function preparerEdition(btEdition) {
			var $ligneDuBouton = $(btEdition).closest("tr");
			$ligneDuBouton.prop("id", "LigneEnEdition");
			$("#idPersonneAEditer").val($(btEdition).attr( "data-id" ));
		}
	</script>
}
