LocalGovIMS_localgov-ims/src/Admin/Views/ImportProcessingRuleCondition/_EditList.cshtml
@model Admin.Models.ImportProcessingRuleCondition.ListViewModel

@if (Model.Items != null && Model.Items.Any())
{
    var groupCount = 0;
    foreach (var group in Model.Items.Select(x => x.Group).Distinct().OrderBy(x => x))
    {
        groupCount++;
        if (groupCount > 1)
        {
            <div class="logical-operator" style="font-weight:bold; padding: 1em 0;">Or</div>
        }
        <div class="table-responsive">
            <table class="table table-bordered mt-2">
                <thead>
                    <tr class="visually-hidden">
                        <th scope="col">
                            Logical operator
                        </th>
                        <th scope="col">
                            Condition summary
                        </th>
                        <th scope="col">
                            <span class="visually-hidden">Actions</span>
                        </th>
                    </tr>
                </thead>
                @{ var conditionCount = 0; }
                @foreach (var condition in Model.Items.Where(x => x.Group == group).OrderBy(x => x.Id))
                {
                    conditionCount++;
                    <tr>
                        <td style="width:5%;" class="align-middle">
                            @if (conditionCount > 1)
                            {
                                <span>@(string.IsNullOrEmpty(condition.LogicalOperator) ? string.Empty : condition.LogicalOperator.ToSentenceCase())</span>
                            }
                            else
                            {
                                <span>Where</span>
                            }
                        </td>
                        <td class="align-middle">
                            <span class="rounded-1 p-2 bg-primary fw-normal text-white">@condition.DisplaySummary()</span>
                        </td>
                        <td style="width:35%;" class="text-end">
                            @Html.ActionLink("Edit", "Edit", "ImportProcessingRuleCondition", new { id = condition.Id }, new { @class = "btn btn-secondary" })
                            @Html.ActionLink("Delete", "Delete", "ImportProcessingRuleCondition", new { id = condition.Id, importProcessingRuleId = condition.ImportProcessingRuleId }, new { @class = "btn btn-secondary delete-condition" })
                        </td>
                    </tr>
                    if (conditionCount == Model.Items.Where(x => x.Group == group).Count())
                    {
                        <tr>
                            <td colspan="3" class="text-end">
                                @Html.ActionLink("Add another condition", "Create", "ImportProcessingRuleCondition", new { importProcessingRuleId = Model.Items.First().ImportProcessingRuleId, group = group }, new { @class = "btn btn-secondary" })
                            </td>
                        </tr>
                    }
                }
            </table>
        </div>
    }
}
else
{
    <div class="alert alert-info my-3">
        <p class="mb-0">
            There are no conditions specified
        </p>
    </div>
}

<script type="text/javascript">
    $(document).ready(function () {

        $(".delete-condition").on("click",
            function (e) {
                if (window.confirm("Are you sure you want to delete this condition?")) {

                } else {
                    e.preventDefault();
                    e.stopPropogation();
                    return false;
                }
            });

    });
</script>
