@model turf_tasker.Models.LawnCareEvent

@{
    ViewData["Title"] = "Edit";
}

<h1>Edit</h1>

<h4>Lawn Care Event</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Edit">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            
            <!-- This hidden field is essential for the update to work -->
            <input type="hidden" asp-for="Id" />
            
            <div class="form-group mb-3">
                <label asp-for="EventType" class="control-label"></label>
                <select asp-for="EventType" class="form-control" asp-items="Html.GetEnumSelectList<LawnCareEventType>()"></select>
                <span asp-validation-for="EventType" class="text-danger"></span>
            </div>
            <div class="form-group mb-3">
                <label asp-for="EventDate" class="control-label"></label>
                <input asp-for="EventDate" class="form-control" />
                <span asp-validation-for="EventDate" class="text-danger"></span>
            </div>
            
            <!-- Wrapper div for our conditional JavaScript logic -->
            <div id="mowingPatternGroup">
                <div class="form-group mb-3">
                    <label asp-for="MowingPattern" class="control-label"></label>
                    <select asp-for="MowingPattern" class="form-control" asp-items="Html.GetEnumSelectList<MowingPattern>()"></select>
                    <span asp-validation-for="MowingPattern" class="text-danger"></span>
                </div>
            </div>
            
            <div class="form-group mb-3">
                <label asp-for="Notes" class="control-label"></label>
                <textarea asp-for="Notes" class="form-control"></textarea>
                <span asp-validation-for="Notes" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Save" class="btn btn-primary" />
                <a asp-action="Delete" asp-route-id="@Model.Id" class="btn btn-danger ms-2">Delete</a>
            </div>
        </form>
    </div>
</div>

<div class="mt-3">
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}

    <script type="text/javascript">
        // This script ensures the Mowing Pattern field only shows up
        // when the "Mowing" activity type is selected.
        $(document).ready(function () {

            function toggleMowingPattern() {
                // LawnCareEventType.Mowing has an integer value of 0
                var selectedEventType = $("#EventType").val();

                if (selectedEventType == '0') { // '0' corresponds to Mowing
                    $("#mowingPatternGroup").show();
                } else {
                    $("#mowingPatternGroup").hide();
                }
            }

            // Run the function once on page load to set the correct initial state
            toggleMowingPattern();

            // Re-run the function every time the dropdown value changes
            $("#EventType").on("change", function () {
                toggleMowingPattern();
            });
        });
    </script>
}