@model PerformerProfile
@{
    ViewData["Title"] = "Setup your performer profile";
}

@section header{
    <script src="~/js/jquery-ui.js"></script>
    <script src="~/lib/jquery-validation/jquery.validate.js"></script>
    <script src="~/lib/jquery-validation/additional-methods.js"></script>
    <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
    <script src="~/js/validator.js"></script>
    <script src="https://maps.googleapis.com/maps/api/js?key=@ViewBag.GoogleSettings.BrowserApiKey"></script>
    <script src="~/js/google.geocode.js"></script>
    <style>
      #map {
        width: 100%;
        height: 250px;
      }
    </style>
}

@section scripts{
    <script>

     $(document).ready(function(){
var gmap = new google.maps.Map(document.getElementById('map'), {
	    zoom: 16,
	    center: { lat: 48.862854, lng: 2.2056466 }
	  });
    var marker;
    function chooseLoc(loc) {
        $('#Location_Address').val(loc.formatted_address);
        var pos = loc.geometry.location;
        var lat = new Number(pos.lat);
        var lng = new Number(pos.lng);
        $('#OrganizationAddress_Latitude').val(lat.toLocaleString());
        $('#OrganizationAddress_Longitude').val(lng.toLocaleString());
        gmap.setCenter(pos);
        if (marker) { 
            marker.setMap(null);
        }
        marker = new google.maps.Marker({
            map: gmap,
            draggable: true,
            animation: google.maps.Animation.DROP,
            position: pos
            });
        google.maps.event.addListener(marker, 'dragend', function() {
            // TODO reverse geo code
            var pos = marker.getPosition();
                $('#OrganizationAddress_Latitude').val(pos.lat);
                $('#OrganizationAddress_Longitude').val(pos.lng);
        });
        $('#Location_Address').valid();
        return true;
    };

          $('#Location_Address').geocode( { submit: 'btngeocode' ,
          inputLatitude: 'OrganizationAddress_Longitude',
          inputLongitude: 'OrganizationAddress_Latitude',
          combo: 'dropdownItems1',
          onValidated: function (e) {
              if (e.success) {
                  var firstres = e.results[0];
                  console.log(firstres);
                  console.log(firstres.geometry);
                $('#AddressError').html("");
              }
              else {
                 if (e.status=="ZERO_RESULTS") {
                    $('#AddressError').html("aucun résultat");
                  }
                  else  $('#AddressError').html(e.status);
              }
   } } );


          $('#OrganizationAddress').rules("add",
    {
        remote: {
            url: 'http://maps.googleapis.com/maps/api/geocode/json',
            type: 'get',
            data: {
                sensor: false,
                address: function () { return $('#Location_Address').val() }
                },
            dataType: 'json',
            dataFilter: function(datastr,type) {
                $('#Location_combo').html("");
                var data = JSON.parse(datastr);
                data.results.forEach(function(element) {
                    if (element.formatted_address !== $('#Location_Address').val()) {
                        $('<li>'+element.formatted_address+'</li>')
                        .data("geoloc",element)
                        .click(function() { chooseLoc($(this).data("geoloc")) })
                        .appendTo($('#Location_combo'));}});
                return (data.status === 'OK') && (data.results.length == 1);
                },
            error: function(xhr, textStatus, errorThrown)
                {
                console.log('ajax loading error ... '+textStatus+' ... '+ errorThrown);
                return false;
                }
         }
    });

    });
    </script>
    @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
 }

<h2>@ViewData["Title"].</h2>

@{ await Html.RenderPartialAsync("_PerformerPartial", Model) ; }

<form id="FrmSetAct" asp-controller="Manage" asp-action="SetActivity" method="post" class="form-horizontal" role="form">
    <h4>@SR["Choose below your main activity"]:</h4>
    <hr />
    <div asp-validation-summary="ValidationSummary.All" class="text-danger"></div>
    <div class="form-group">
        <label asp-for="ActivityCode" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <select asp-for="ActivityCode" asp-items=@ViewBag.Activities class="form-control" >
            </select>
            <span asp-validation-for="ActivityCode" class="text-danger"></span>
        </div>
     </div>
     <div class="form-group">
        <label asp-for="AcceptNotifications" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <input asp-for="AcceptNotifications" class="form-control" />
            <span asp-validation-for="AcceptNotifications" class="text-danger"></span>
        </div>
     </div>
     <div class="form-group">

        <label asp-for="AcceptPublicContact" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <input asp-for="AcceptPublicContact" class="form-control" />

            <span asp-validation-for="AcceptPublicContact" class="text-danger"></span>
        </div>
     </div>
     <div class="form-group">

        <label asp-for="AcceptGeoLocalization" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <input asp-for="AcceptGeoLocalization" class="form-control" />

            <span asp-validation-for="AcceptGeoLocalization" class="text-danger"></span>
        </div>
     </div>
     <div class="form-group">

        <label asp-for="WebSite" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <input asp-for="WebSite" class="form-control" />

            <span asp-validation-for="WebSite" class="text-danger"></span>
        </div>
     </div>
     <div class="form-group">

        <label asp-for="MinDailyCost" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <input asp-for="MinDailyCost" class="form-control" />

            <span asp-validation-for="MinDailyCost" class="text-danger"></span>
        </div>
     </div>
     <div class="form-group">

        <label asp-for="MaxDailyCost" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <input asp-for="MaxDailyCost" class="form-control" />

            <span asp-validation-for="MaxDailyCost" class="text-danger"></span>
        </div>
     </div>
     <div class="form-group">

        <label asp-for="Active" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <input asp-for="Active"  class="form-control" />

            <span asp-validation-for="Active" class="text-danger"></span>
        </div>
     </div>
     <div class="form-group">

        <label asp-for="SIREN" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <input asp-for="SIREN"  class="form-control" />

            <span asp-validation-for="SIREN" class="text-danger"></span>
        </div>
     </div>
     <div class="form-group">

        <label asp-for="OrganizationAddress.Address" class="col-md-2 control-label"></label>
        <div class="col-md-10">
            <input asp-for="OrganizationAddress.Address" class="form-control" type="text" />

            <span id="AddressError" asp-validation-for="OrganizationAddress.Address" class="text-danger"></span>
            <div id="map"></div>
        </div>
     </div>
        @Html.Hidden("OrganizationAddress.Latitude")
        @Html.Hidden("OrganizationAddress.Longitude")
        @Html.Hidden("PerformerId")
    <button type="submit" class="btn btn-default">Save these settings</button>

</form>

<form asp-controller="Manage" asp-action="UnsetActivity" method="post" class="form-horizontal" role="form">
    @Html.Hidden("PerfomerId")
    <button type="submit" class="btn btn-default">Remove my professional profile</button>
</form>
