@page
@model Manage.PartnersModel
@using Microsoft.AspNetCore.Http
@using JtkMainServer.Managers
@{
    ViewData["Title"] = "가맹점";
    ViewData["LoggedInUserId"] = HttpContext.Session.GetString("LoggedInUserId");
    ViewData["LoggedIn"] = ViewData["LoggedInUserId"] != null;
    ViewData["ActivatedTabIndex"] = 2;

    var toDate = DateTime.Today;
    var fromDate = toDate + TimeSpan.FromDays(1);
    if (HttpContext.Request.Query.TryGetValue("from", out var fromDateTexts) == true && fromDateTexts.Count > 0 && string.IsNullOrWhiteSpace(fromDateTexts[0]) == false) {
        DateTime.TryParse(fromDateTexts[0], out fromDate);
    }
    if (HttpContext.Request.Query.TryGetValue("to", out var toDateTexts) == true && toDateTexts.Count > 0 && string.IsNullOrWhiteSpace(toDateTexts[0]) == false) {
        DateTime.TryParse(toDateTexts[0], out toDate);
    }
    if (fromDate > toDate) {
        fromDate = DateTime.Parse($"{toDate.Year}-{toDate.Month}-01");
    }
    var days = Math.Round((toDate - fromDate).TotalDays) + 1;

    var linkFormat = $"/Manage/Partner?id={{0}}&from={fromDate.ToShortDateString()}&to={toDate.ToShortDateString()}";

    var list = DbManager.GetPlacesExpense(fromDate, toDate);
}

<style type="text/css">
    .table-stats {
        width: 100%;
    }
    .table-stats th {
        font-weight: normal;
        font-size: large;
        padding: 8px 4px 8px 4px;
    }
    .table-stats td {
        text-align: right;
    }

    .table-daterange {
        width: 100%;
    }
    .table-daterange th {
        font-weight: normal;
    }
    .table-daterange td {
        text-align: right;
    }
    input[type=date] {
        width: unset;
        display: inline;
        margin: 0px 8px;
    }
    a.zz:visited, a.glyphicon:link, a.glyphicon:active {
        color: #337ab7;
        text-decoration:none;
    }
    a.zz:hover {
        color: black;
        text-decoration: none;
    }
</style>
<script type="text/javascript">
    function ConfirmDelete(placeId, placeName) {
        var txt;
        if (confirm(placeName + " 가맹점을 삭제하시겠습니까?") == true) {
            $.ajax({
                async: true,
                type: 'get',
                url: "DeletePartner",
                dataType: "text",
                timeout: 1000,
                cache: false,
                data: {
                    placeId: placeId
                },
                error: function (request, status, error) {
                    alert("삭제에 실패하였습니다.");
                },
                success: function (response, status, request) {
                    if (response != "ok") {
                        alert("삭제에 실패하였습니다.");
                    } else {
                        alert("삭제되었습니다.");
                        window.location.reload();
                    }
                }
            });
        }
    }
</script>
<div class="row" style="margin-top:20px">
    <div class="col-md-6">
        @Html.Partial("~/Views/ManageTabs.cshtml")
        <h3>기간</h3>
        <form method="get" id="form">
            <table class="table-daterange">
                <tr>
                    <th>
                        <input type="date" name="from" class="form-control" value="@fromDate.ToShortDateString()" />부터
                        <input type="date" name="to" class="form-control" value="@toDate.ToShortDateString()" />까지
                    </th>
                    <td>
                        @*<input type="submit" asp-page="Join" class="btn btn-default" value="조회" />*@
                        <a href="javascript:document.getElementById('form').submit()" class="btn btn-default"><span class="glyphicon glyphicon-search"></span> 조회</a>
                        <input type="submit" hidden="hidden" />
                    </td>
                </tr>
            </table>
        </form>
        <hr />
        <div><h3 style="display:inline;">가맹점 매출<span style="font-size: small"> @fromDate.ToShortDateString() ~ @toDate.ToShortDateString() (@(days)일간)</span></h3><a style="margin-top:0px; float:right" asp-page="./RegisterPartner" class="btn btn-default"><span class="glyphicon glyphicon-edit"></span> 가맹점 등록</a></div>
        <table class="table">
            <thead>
                <tr>
                    <th>PC방</th>
                    <th>총 매출</th>
                    <th>기간 매출</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in list) {
                <tr>
                    <td><a href="@string.Format(linkFormat, item.placeId)">@item.placeName</a></td>
                    <td>￦@item.totalExpense.ToString("N0")</td>
                    <td>￦@item.expense.ToString("N0")</td>
                    <td><a id="btnDelete" class="zz glyphicon glyphicon-trash" href="javascript:ConfirmDelete('@item.placeId', '@item.placeName')"></a></td>
                </tr>
        }
            </tbody>
        </table>
        <hr />
    </div>
</div>
<div>
    <a asp-page="Index">홈으로 이동</a>
</div>