@{
    ViewBag.Title = "Vacation requests";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model WF.Sample.Models.DocumentListModel<InboxDocumentModel>

<div class="wfe-application-toolbar">
    <a href="@Url.Action("Edit", "Document")" class="ui primary button">Create</a>
    <a onclick="DeleteSelected()" href="#" class="ui secondary button">Delete</a>
</div>
<table class="grid">
    <tbody>
    <tr class="grid-header">
        <th style="width:20px"></th>
        <th style="width:20px">Number</th>
        <th>State</th>
        <th>Name</th>
        <th>Comment </th>
        <th>Author</th>
        <th>Manager</th>
        <th>Sum</th>
        <th>Adding date</th>
        <th>Available commands</th>
    </tr>
    @foreach (var item in Model.Docs)
    {
        var editUrl = item.IsCorrect ? @Url.Action("Edit", "Document", new{Id = item.Id}) : "";
        <tr ondblclick="SetHref('@editUrl')">
            <td><div class="ui checkbox"><input type="checkbox" name="checkedbox" class="selectedValues" value="@item.Id" /><label></label></div> </td>
            <td><a href=@editUrl>@item.Number</a></td>
            <td>@item.StateName </td>
            <td><a href=@editUrl>@item.Name</a></td>
            <td>@item.Comment </td>
            <td>@item.AuthorName </td>
            <td>@item.ManagerName</td>
            <td>@item.Sum.ToString("n2")</td>
            <td>@item.AddingDate</td>
            <td>
                <div>
                    @foreach(var command in item.AvailableCommands)
                    {
                        var executeCommandUrl = @Url.Action("ExecuteCommand", "Document", new {Id = item.Id, Command = @command.Name});
                        <button disabled=@(!item.IsCorrect) onclick="SetHref('@executeCommandUrl')" name="button" value="@command.Name" class="ui floated button">@(command.LocalizedName??command.Name)</button>
                    }
                </div>

            </td>
        </tr>
    }
</table>

@(await Html.PartialAsync("Partial/Paging"))

<script>

   function SetHref(url) {
       if (url)
        document.location.href = url;
    }
    function DeleteSelected() {
        var data = new Array();
        var selectedValues = $('.selectedValues:checked');

        if (selectedValues.length < 1) {
            alert('Please, select rows for deleting!');
            return;
        }

        for (var i = 0; i < selectedValues.length; i++) {
            data.push({ name: 'ids', value: selectedValues[i].value });
        }

        $.ajax({
            type: "POST",
            url: "@Url.Action("DeleteRows", "Document")",
            data: data,
            success: function(msg) {
                alert(msg);
                location.reload();
            }
        });
    }
</script>