@using CD67.FicheCollege.MVC.Models
@model TerritoireViewModel

@{
    ViewBag.Title = "Administration - Les Territoires";
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
    Territoire territoire = Model.Obj;
}

@if (Model.Acces == ModeAcces.Modification)
{
    <h2>@territoire.Libelle</h2>
}
else
{
    <h2>Nouveau Territoire</h2>
}

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => territoire.Ordre)

        @if (Model.Acces == ModeAcces.Modification)
        {
            @Html.HiddenFor(model => territoire.Id)
        }
        else
        {
            <div class="form-group">
                @Html.LabelFor(model => territoire.Id, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => territoire.Id, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => territoire.Id, "", new { @class = "text-danger" })
                </div>
            </div>
        }

        <div class="form-group">
            @Html.LabelFor(model => territoire.Libelle, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => territoire.Libelle, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => territoire.Libelle, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.Label("Référent", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-4" data-picker-type="referent">
                @Html.HiddenFor(model => territoire.Referent_SID, new { data_picker = "agent.id" })
                @Html.HiddenFor(model => territoire.Referent_Nom, new { data_picker = "agent.nom" })
                @Html.HiddenFor(model => territoire.Referent_Prenom, new { data_picker = "agent.prenom" })
                @Html.HiddenFor(model => territoire.Referent_Structure, new { data_picker = "agent.chemin" })
                @Html.HiddenFor(model => territoire.Referent_Login, new { data_picker = "agent.login" })
                @Html.HiddenFor(model => territoire.Referent_Email, new { data_picker = "agent.mail" })
                <a class='modal-window' href="http://referentiel.bas-rhin.fr/picker/cd67/ad67/?type=referent" title="Selection d'un référent (nouvelle fenetre)">Sélectionner un référent</a>
                <br />
                <div class="panel panel-default" id="Referent_Panel" name="Referent_Panel" style="@if (territoire.Referent_SID == null) { <text>display: none;</text> }">
                    <div class="panel-heading clearfix">
                        <h4 class="panel-title pull-left" style="padding-top: 7.5px;">
                            <i class="fa fa-user color1" aria-hidden="true"></i>
                            Agent
                        </h4>
                        <div class="pull-right">
                            <span class="glyphicon glyphicon-trash removeItem btn btn-danger btn-sm" aria-hidden="true" data-type="referent"></span>
                        </div>
                    </div>
                    <div class="panel-body" style="text-align: center;">
                        <b><span data-picker="agent.prenom">@territoire.Referent_Prenom</span> <span data-picker="agent.nom">@territoire.Referent_Nom</span></b>
                        <br /><span data-picker="agent.chemin">@territoire.Referent_Structure</span>
                    </div>
                </div>
            </div>
        </div>

        <div class="form-group btn-bar">
            <a href=@Url.Action("Index") class="btn btn-default">Annuler</a>
            <input type="submit" value="Enregistrer" class="btn btn-primary" />
        </div>
    </div>
}

@section Scripts {
    <script type="text/javascript">
        $(".removeItem").click(function () {
            switch ($(this).data("type")) {
                case 'referent':
                    $('#Referent_SID').val(null);
                    $('#Referent_Nom').val(null);
                    $('#Referent_Prenom').val(null);
                    $('#Referent_Structure').val(null);
                    $('#Referent_Login').val(null);
                    $('#Referent_Email').val(null);
                    $('#Referent_Panel').hide();
                    break;
                default:
                    return false;
            }
        });
    </script>
}
