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

@{
    ViewBag.Title = "Les Collèges " + Model.Annee_Lib;
    Layout = "~/Views/Shared/_Layout.cshtml";
    College college = Model.Obj;
}


<header>
    @if (Model.Acces == ModeAcces.Modification)
    {
        <h2>@college.Libelle</h2>
    }
    else
    {
        <h2>Nouveau collège</h2>
    }
</header>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

<fieldset>

    <legend>
        Informations générales
    </legend>

    <div class="form-horizontal">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        @if (Model.Acces == ModeAcces.Modification)
        {
        @Html.HiddenFor(model => college.Id)
        }
        @Html.HiddenFor(model => college.Annee_Id)
        @Html.HiddenFor(model => college.TokenId)
        @Html.HiddenFor(model => college.RestaurationType_Id)

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

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

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

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

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

        @Html.HiddenFor(model => college.Commune_Insee, new { data_picker = "commune.insee" })
        @Html.TextBoxFor(model => college.Commune, new { data_picker = "commune.nom", @class = "special_hidden" }) @*On est obligé d'utiliser cette méthode bizzare pour cacher le champ, sinon pas de validation coté user (pas de validation des données sur les champs cachés)*@
        @Html.HiddenFor(model => college.Canton, new { data_picker = "commune.canton" })
        @Html.HiddenFor(model => college.Territoire_Id, new { data_picker = "commune.code_tad" })
        @Html.HiddenFor(model => college.TAD, new { data_picker = "commune.tad" })
        @Html.HiddenFor(model => college.CDC, new { data_picker = "commune.cdc" })

        <div class="form-group">
            @Html.LabelFor(model => college.Commune, htmlAttributes: new { @class = "control-label col-md-2 required" })
            <div class="col-md-4">
                <a class='modal-window-events' href="http://referentiel.bas-rhin.fr/picker/cd67/com67/" title="Selection commune (nouvelle fenetre)">Sélectionner une commune</a>
                <br />
                <div class="panel panel-default" id="Commune_Panel" name="Commune_Panel" style="@if (college.Commune_Insee == 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-map-marker color1" aria-hidden="true"></i>
                            Localisation (commune)
                        </h4>
                        <div class="pull-right">
                            <span class="glyphicon glyphicon-trash removeItem btn btn-danger btn-sm" aria-hidden="true" data-type="commune"></span>
                        </div>
                    </div>
                    <div class="panel-body" style="text-align: center;">
                        <span>
                            <b><span data-picker="commune.nom">@college.Commune</span></b>
                            <br />Canton : <span data-picker="commune.canton">@college.Canton</span>
                            <br />TAD : <span data-picker="commune.tad">@college.TAD</span>
                            <br />CDC : <span data-picker="commune.cdc">@college.CDC</span>
                        </span>
                    </div>
                </div>
                @Html.ValidationMessageFor(model => college.Commune, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => college.Code_Postal, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                Code(s) postal(aux) associé(s) à la commune
                @if (Model.Sel_CodesPostaux.Count() > 1)
                {
                <span id="CP-warning" class="text-danger">(attention, plusieurs choix possibles)</span>
                }
                : <input id="code_postaux" data-picker="commune.code_postal" value="@String.Join(" ,", Model.Sel_CodesPostaux.Select(i=> i.Text))" class="disabled-input-as-label" />
                <br />
                @Html.DropDownListFor(model => college.Code_Postal, Model.Sel_CodesPostaux, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => college.Code_Postal, "", new { @class = "text-danger" })
            </div>
        </div>

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

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

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

        <div class="form-group">
            <label class="control-label col-md-2" for="college_Email">Personnel</label>

            <div class="col-md-4" data-picker-type="principal">
                @Html.HiddenFor(model => college.PrincipalSid, new { data_picker = "agent.id" })
                <a class='modal-window' href="http://referentiel.bas-rhin.fr/picker/cd67/externes/?type=principal&query=organisation:%22Principaux%20de%20coll%C3%A8ges%22" title="Selection du principal (nouvelle fenetre)">Sélectionner un principal</a>
                <br />
                <div class="panel panel-default" id="Principal_Panel" name="Principal_Panel" style="@if (college.PrincipalSid == 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>
                            @Html.LabelFor(model => college.PrincipalSid)
                        </h4>
                        <div class="pull-right">
                            <span class="glyphicon glyphicon-trash removeItem btn btn-danger btn-sm" aria-hidden="true" data-type="principal"></span>
                        </div>
                    </div>
                    <div class="panel-body" style="text-align: center;">
                        <b><span data-picker="agent.prenom">@Html.DisplayFor(model => college.principal.Prenom)</span> <span data-picker="agent.nom">@Html.DisplayFor(model => college.principal.Nom)</span></b><br />
                        <span data-picker="agent.chemin">@Html.DisplayFor(model => college.principal.Organisation)</span><br />
                        <span data-picker="agent.mail">@Html.DisplayFor(model => college.principal.Mail)</span><br />
                        <span data-picker="agent.telephone">@Html.DisplayFor(model => college.principal.Telephone)</span>
                    </div>
                </div>
            </div>

            <div class="col-md-4" data-picker-type="adjoint">
                @Html.HiddenFor(model => college.AdjointSid, new { data_picker = "agent.id" })
                <a class='modal-window' href="http://referentiel.bas-rhin.fr/picker/cd67/externes/?type=adjoint&query=organisation:%22Principaux%20de%20coll%C3%A8ges%22" title="Selection du principal adjoint (nouvelle fenetre)">Sélectionner un principal adjoint</a>
                <br />
                <div class="panel panel-default" id="Adjoint_Panel" name="Adjoint_Panel" style="@if (college.AdjointSid == 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>
                            @Html.LabelFor(model => college.AdjointSid)
                        </h4>
                        <div class="pull-right">
                            <span class="glyphicon glyphicon-trash removeItem btn btn-danger btn-sm" aria-hidden="true" data-type="adjoint"></span>
                        </div>
                    </div>
                    <div class="panel-body" style="text-align: center;">
                        <b><span data-picker="agent.prenom">@Html.DisplayFor(model => college.adjoint.Prenom)</span> <span data-picker="agent.nom">@Html.DisplayFor(model => college.adjoint.Nom)</span></b><br />
                        <span data-picker="agent.chemin">@Html.DisplayFor(model => college.adjoint.Organisation)</span><br />
                        <span data-picker="agent.mail">@Html.DisplayFor(model => college.adjoint.Mail)</span><br />
                        <span data-picker="agent.telephone">@Html.DisplayFor(model => college.adjoint.Telephone)</span>
                    </div>
                </div>
            </div>
        </div>


        <div class="form-group">
            <div class="col-md-offset-2 col-md-4" data-picker-type="gestionnaire">
                @Html.HiddenFor(model => college.Gestionnaire1Sid, new { data_picker = "agent.id" })
                <a class='modal-window' href="http://referentiel.bas-rhin.fr/picker/cd67/ad67/?type=gestionnaire" title="Selection du gestionnaire (nouvelle fenetre)">Sélectionner un gestionnaire</a>
                <br />
                <div class="panel panel-default" id="Gestionnaire_Panel" name="Gestionnaire_Panel" style="@if (college.Gestionnaire1Sid == 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>
                            @Html.LabelFor(model => college.Gestionnaire1Sid)
                        </h4>
                        <div class="pull-right">
                            <span class="glyphicon glyphicon-trash removeItem btn btn-danger btn-sm" aria-hidden="true" data-type="gestionnaire"></span>
                        </div>
                    </div>
                    <div class="panel-body" style="text-align: center;">
                        <b><span data-picker="agent.prenom">@Html.DisplayFor(model => college.gestionnaire1.Prenom)</span> <span data-picker="agent.nom">@Html.DisplayFor(model => college.gestionnaire1.Nom)</span></b><br />
                        <span data-picker="agent.chemin">@Html.DisplayFor(model => college.gestionnaire1.Organisation)</span><br />
                        <span data-picker="agent.mail">@Html.DisplayFor(model => college.gestionnaire1.Mail)</span><br />
                        <span data-picker="agent.telephone">@Html.DisplayFor(model => college.gestionnaire1.Telephone)</span>
                    </div>
                </div>
            </div>

            <div class="col-md-4" data-picker-type="gestionnaire2">
                @Html.HiddenFor(model => college.Gestionnaire2Sid, new { data_picker = "agent.id" })
                <a class='modal-window' href="http://referentiel.bas-rhin.fr/picker/cd67/ad67/?type=gestionnaire2" title="Selection du gestionnaire n°2 (nouvelle fenetre)">Sélectionner un gestionnaire</a>
                <br />
                <div class="panel panel-default" id="Gestionnaire2_Panel" name="Gestionnaire2_Panel" style="@if (college.Gestionnaire2Sid == 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>
                            @Html.LabelFor(model => college.Gestionnaire2Sid)
                        </h4>
                        <div class="pull-right">
                            <span class="glyphicon glyphicon-trash removeItem btn btn-danger btn-sm" aria-hidden="true" data-type="gestionnaire2"></span>
                        </div>
                    </div>
                    <div class="panel-body" style="text-align: center;">
                        <b><span data-picker="agent.prenom">@Html.DisplayFor(model => college.gestionnaire2.Prenom)</span> <span data-picker="agent.nom">@Html.DisplayFor(model => college.gestionnaire2.Nom)</span></b><br />
                        <span data-picker="agent.chemin">@Html.DisplayFor(model => college.gestionnaire2.Organisation)</span><br />
                        <span data-picker="agent.mail">@Html.DisplayFor(model => college.gestionnaire2.Mail)</span><br />
                        <span data-picker="agent.telephone">@Html.DisplayFor(model => college.gestionnaire2.Telephone)</span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <br />

    <div class="form-group btn-bar">
        @if (Model.Acces == ModeAcces.Creation)
        {
            <a href=@Url.Action("Index", new { annee_id = college.Annee_Id }) class="btn btn-default">Annuler</a>
        }
        else
        {
            <a href=@Url.Action("Details", new { Id = college.Id }) class="btn btn-default">Annuler</a>
        }
        <input type="submit" value="Enregistrer" class="btn btn-primary" />
    </div>

</fieldset>

}

@section Scripts {
<script type="text/javascript">
    $(".removeItem").click(function () {
        switch ($(this).data("type")) {
            case 'commune':
                $('#contenu_Code_Postal').val(null);
                $('#contenu_Commune_Insee').val(null);
                $('#contenu_Commune').val(null);
                $('#contenu_Canton').val(null);
                $('#contenu_Territoire_Id').val(null);
                $('#contenu_TAD').val(null);
                $('#contenu_CDC').val(null);
                $('#Commune_Panel').hide();

                //suppression du champ en dessous
                $('#code_postaux').val(null);
                $('#contenu_Code_Postal').empty();
                $('#CP-warning').hide();
                break;
            case 'principal':
                $('#college_PrincipalSid').val(null);
                $('#Principal_Panel').hide();
                break;
            case 'adjoint':
                $('#college_AdjointSid').val(null);
                $('#Adjoint_Panel').hide();
                break;
            case 'gestionnaire':
                $('#college_Gestionnaire1Sid').val(null);
                $('#Gestionnaire_Panel').hide();
                break;
            case 'gestionnaire2':
                $('#college_Gestionnaire2Sid').val(null);
                $('#Gestionnaire2_Panel').hide();
                break;
            default:
                return false;
        }
    });

    // Function à implémenter pour éviter une erreur
    function onModalOpen() {
        $('#CP-warning').hide();
    }

    function onModalClose() {
        var code_postaux = $('#code_postaux').val();
        if (code_postaux != '') {
            //On vide la liste actuelle
            $('#college_Code_Postal').empty();

            var liste = code_postaux.split(',');
            liste.forEach(function (element) {
                //On ajoute les choix
                $('#college_Code_Postal').append($('<option>', {
                    value: element,
                    text: element
                }));
            });

            if (liste.length > 1) $('#CP-warning').show();
        }
    }
</script>

    @Scripts.Render("~/bundles/jqueryval")
}
