@using System.Configuration;
@using SharpConnector.Models;
@using SharpConnector.Constants;
@using SharpConnector.Dtos;

@model Record<ContactDto>

@{
    bool isSupportDomainAddressBookEnabled =
        bool.TryParse(ConfigurationManager.AppSettings["SupportDomainAddressBook"], out var parsed) && parsed;
}

@using (Html.BeginForm("AddRecipient", "AddressBook", FormMethod.Post))
{
    <input type="hidden" name="id" id="hf_id" value="@ViewBag.ID" />
    <input type="hidden" name="page" id="hf_page" value="@Model.Pagination.Page" />
    <input type="hidden" name="intent" id="hf_intent" value="add" />
    <input type="hidden" name="search" id="hf_search" value="@ViewBag.Search" />

    <div id="btn_wrapper">
        <div class="left">
            @if (isSupportDomainAddressBookEnabled)
            {
                <input type="button" value="Back" onclick="navigateTo('@Url.Action("Index", "AddressBook")?type=@Session["AddrBookType"]');" />
            }
            else
            {
                <input type="button" value="Back" onclick="navigateTo('@Url.Action("Index", "AddressBook")');" />
            }
        </div>
        <div id="title">
            <h3 class="text-center">@ViewBag.Name</h3>
        </div>
        <div class="right">
            <input type="submit" value="Add" id="btn_add" />
            <input type="submit" value="Done" id="btn_done" onclick="document.getElementById('hf_intent').value='done';" />
        </div>
    </div>

    <div class="inner_wrapper stretch_to_bottom">
        @if (Model != null && Model.Pagination != null)
        {
            <table id="pagination" class="pagination" align="center" valign="top" cellspacing="0" cellpadding="0" border="0">
                <tr>
                    <td align="left">
                        Showing <span id="startIndex">@Model.Pagination.StartIndex</span> to <span id="endIndex">@Model.Pagination.EndIndex</span> of <span id="totalRecords">@Model.Pagination.Total</span> records
                    </td>
                    <td width="33">
                        <button type="submit" id="btn-left" class="btn-pagination disabled" onclick="document.getElementById('hf_page').value='@(Model.Pagination.Page - 1)'; document.getElementById('hf_intent').value='next';"></button>
                    </td>
                    <td align="center" width="112">
                        <font size="3">
                            <span id="currentPage">@Model.Pagination.Page</span>/<span id="totalPage">@Model.Pagination.TotalPage</span> page
                        </font>
                    </td>
                    <td width="33">
                        <button type="submit" id="btn-right" class="btn-pagination" onclick="document.getElementById('hf_page').value='@(Model.Pagination.Page + 1)'; document.getElementById('hf_intent').value='next';"></button>
                    </td>
                </tr>
            </table>
        }
        <div class="search_wrapper right">
            <input type="text" id="search_input" value="@ViewBag.Search" />
            <button type="button" id="btn_search" onclick="document.getElementById('hf_search').value=document.getElementById('search_input').value; document.getElementById('hf_intent').value='next'; document.getElementById('hf_page').value='1'; document.forms[0].submit();">Search</button>
        </div>
        <div class="clearfix"></div>
        <div>
            <table id="table_contacts" class="tbl_content" align="center" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF" border="1">
                <thead>
                    <tr>
                        <th>Name</th>
                        <th>Company</th>
                        <th>Fax No.</th>
                        <th>&nbsp;</th>
                    </tr>
                </thead>
                <tbody>
                    @if (Model != null && Model.Records != null && Model.Records.Any())
                    {
                        for (var i = 0; i < Model.Records.Count; i++)
                        {
                            var c = Model.Records[i];

                            <tr id="c-@c.ContactID">
                                <td width="33%">@c.DisplayName</td>
                                <td width="33%">@c.CompanyName</td>
                                <td width="33%">@c.Address</td>
                                <td align="center">
                                    @Html.Hidden($"Contacts[{i}].ContactID", c.ContactID)
                                    @Html.Hidden($"Contacts[{i}].DisplayName", c.DisplayName)
                                    @Html.Hidden($"Contacts[{i}].CompanyName", c.CompanyName)
                                    @Html.Hidden($"Contacts[{i}].Address", c.Address)
                                    @Html.Hidden($"Contacts[{i}].AddrType", c.AddrType)
                                    @Html.Hidden($"Contacts[{i}].NotifyAddress", c.NotifyAddress)
                                    @Html.CheckBox($"Contacts[{i}].IsChecked", c.IsChecked, c.IsChecked ? (object)new { @disabled = "disabled" } : (object)new { })
                                </td>
                            </tr>
                        }
                    }
                    else
                    {
                        <tr>
                            <td colspan="4"><div class="no-records">@Msg.NoRecords</div></td>
                        </tr>
                    }
                </tbody>
            </table>
        </div>
    </div>
}
