@using SharpConnector.Helpers;
@using System.Configuration;
@using SharpConnector.Constants;
@model SharpConnector.Models.Message.Response

<div id="btn_wrapper">
    <div class="left">
        <input type="button" value="Back" onclick="navigateTo('@Url.Action("Index", "Inbox")');" />
    </div>
    <div id="title">
        <h3 class="text-center">Message #@Model.MessageNo</h3>
    </div>
    <div class="right">
        <input type="button" value="Print" onclick="navigateTo('@Url.Action("Print", "Inbox", new { id = Model.MessageID })');" />
    </div>
</div>

<div class="inner_wrapper stretch_to_bottom">
    <input type="hidden" id="@Model.MessageID" />
    <table align="center" valign="top" cellspacing="0" cellpadding="0">
        <tr>
            <td width="40%" align="center">
                <img id="imgFax" src="@Model.ImageUrl">
                <input id="imgUrl" type="hidden" value="@Model.ImageUrl" />

                <table align="center" valign="top" cellspacing="0" cellpadding="0" border="0">
                    <tr>
                        <td width="33" align="right">
                            <button id="btn-left" class="btn-pagination disabled" type="button"></button>
                        </td>
                        <td align="center" width="75">
                            <font size="3">
                                <span id="currentPage">1</span>/<span id="totalPage">@(Model.Transactions.FirstOrDefault()?.PageCount)</span>
                            </font>
                        </td>
                        <td width="33" align="left">
                            <button id="btn-right" class="btn-pagination" type="button"></button>
                        </td>
                    </tr>
                </table>
            </td>
            <td>
                <table cellspacing="0" cellpadding="10">
                    <tr>
                        <td>Subject</td>
                        <td><input type="text" id="subject" value="@Model.Subject" disabled /></td>
                    </tr>
                    <tr>
                        <td>Remote CSID</td>
                        <td><input type="text" id="remoteCSID" value="@(Model.Transactions.FirstOrDefault()?.RemoteCSID)" disabled /></td>
                    </tr>
                    <tr>
                        <td>Status</td>
                        <td><input type="text" id="status" value="@Model.Status" disabled /></td>
                    </tr>
                    <tr>
                        <td>Created Date</td>
                        <td><input type="text" id="createdDate" value="@Helper.FormatDate(Model.CreatedDate)" disabled /></td>
                    </tr>
                    <tr>
                        <td>Caller ID</td>
                        <td><input type="text" id="callerID" value="@(Model.Transactions.FirstOrDefault()?.Attempts.FirstOrDefault()?.CallingNumber)" disabled /></td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</div>

@section Scripts {
    <script type="text/javascript">
        setImagePagination();

        $('#btn-left').onclick = function () {
            getImage(false);
        };

        $('#btn-right').onclick = function () {
            getImage(true);
        };

        var isDocumentReady = '@Model.IsDownloaded.ToString()';

        if (isDocumentReady.toLowerCase() == 'true') {
            ExecutePrintJob();
        }

        function getImage(isNext) {
            var page = Number($('#currentPage').innerText);
            page = isNext ? page += 1 : page -= 1;

            $('#imgFax').src = $('#imgUrl').value + "&page=" + page;
            $('#currentPage').innerText = page;

            setImagePagination();
        }

        function setImagePagination() {
            var total = Number($('#totalPage').innerText);
            var pageIndex = Number($('#currentPage').innerText);

            if (pageIndex > 1) {
                removeClass($('#btn-left'), 'disabled');
            }
            else {
                addClass($('#btn-left'), 'disabled');
            }

            if (pageIndex == total) {
                addClass($('#btn-right'), 'disabled');
            }
            else {
                removeClass($('#btn-right'), 'disabled');
            }
        }

        function ExecutePrintJob() {
            var _mfpApi = '@ConfigurationManager.AppSettings["MFP:apiUrl"]';
            var _mfpVer = '@ConfigurationManager.AppSettings["MFP:apiVersion"]';
            var projectUrl = '@ConfigurationManager.AppSettings["MFP:projectUrl"]';
            var fileFormat = '@ConfigurationManager.AppSettings["File:format"]';

            var fileName = '@Model.MessageID' + '.' + fileFormat;

            var reqStr = _mfpApi + 'mfpprint/ExecuteJob/' + _mfpVer + '?res=jsonp&callback=errorCheckFunc';
            reqStr += '&vkey=' + '@HttpUtility.UrlEncode(HttpContext.Current.Session[MFP.APIKey].ToString())';
            reqStr += '&HttpUrl=' + projectUrl + 'GetFile/' + fileName;

            jsonpRequest(reqStr, "ExecuteJobCallback");
        }

        function ExecuteJobCallback(_retParam) {
            var JobId = _retParam["result"]["ExecuteJobResponse"]["jobId"];
        }
    </script>
}