@page
@model Manage.SalesModel
@using Microsoft.AspNetCore.Http
@using JtkMainServer.Managers
@{
    ViewData["Title"] = "매출";
    ViewData["LoggedInUserId"] = HttpContext.Session.GetString("LoggedInUserId");
    ViewData["LoggedIn"] = ViewData["LoggedInUserId"] != null;
    ViewData["ActivatedTabIndex"] = 1;
    
    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/Sale?type={{0}}&from={fromDate.ToShortDateString()}&to={toDate.ToShortDateString()}";

    var income = DbManager.GetSalesUserChargeIncome(fromDate, toDate);
    var expense = DbManager.GetSalesPlacePaymentExpense(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;
    }
</style>

<script src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />

<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 />
        <h3>통계<span style="font-size: small"> @fromDate.ToShortDateString() ~ @toDate.ToShortDateString() (@(days)일간)</span></h3>
        <table class="table-stats">
            <tr>
                <th>수입액</th>
                <td>￦@income.ToString("N0")</td>
            </tr>
            <tr>
                <th>지출액</th>
                <td>￦@expense.ToString("N0")</td>
            </tr>
        </table>
        <hr />
        <h3>내역<span style="font-size: small"> @fromDate.ToShortDateString() ~ @toDate.ToShortDateString() (@(days)일간)</span></h3>
        <p>항목을 클릭하면 자세히 볼 수 있습니다.</p>
        <table class="table">
            <thead>
                <tr>
                    <th>구분</th>
                    <th>내용</th>
                    <th>금액</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>수입</td>
                    <td><a href="@string.Format(linkFormat, "charge")">회원 충전</a></td>
                    <td>￦@income.ToString("N0")</td>
                </tr>
                <tr>
                    <td>수입</td>
                    <td>광고 노출</td>
                    <td>￦0</td>
                </tr>
                <tr>
                    <td>지출</td>
                    <td><a href="@string.Format(linkFormat, "payment")">가맹점 매출액 지급</a></td>
                    <td>￦@expense.ToString("N0")</td>
                </tr>
                @*<tr>
                    <td>지출</td>
                    <td>사원 급여</td>
                    <td>￦0</td>
                </tr>*@
                @*@foreach (var tuple in userPingLogs) {
                    <tr>
                        <td>@tuple.loginTime.ToString("yyyy-MM-dd HH:mm")</td>
                        <td>@tuple.placeName</td>
                        <td>@($"{Math.Floor(tuple.usedTime.TotalHours)}시간 {tuple.usedTime.Minutes}분 (￦{tuple.totalCashUse:N0})")</td>
                    </tr>
                }*@
            </tbody>

        </table>
        <hr />
    </div>
</div>

<div>
    <a asp-page="Index">홈으로 이동</a>
</div>