@page
@model Manage.SaleModel
@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 type = ViewData["Type"].ToString();
    var listName = "";
    var income = 0.0;
    var expense = 0.0;
    var incomeList = type == "charge" ? DbManager.GetSalesUserChargeIncomesList(fromDate, toDate) : null;
    var expenseList = type == "payment" ? DbManager.GetSalesPlacePaymentExpensesList(fromDate, toDate, exceptZero: true) : null;
    switch (type) {
        case "charge":
            income = DbManager.GetSalesUserChargeIncome(fromDate, toDate);
            listName = "회원 충전 내역";
            break;
        case "payment":
            expense = DbManager.GetSalesPlacePaymentExpense(fromDate, toDate);
            listName = "가맹점 매출액 지급 내역";
            break;
    }
    ViewData["Title"] = listName;
}

<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")*@
        <h1><a href="./Sales?from=@(fromDate.ToShortDateString())&to=@(toDate.ToShortDateString())" class="glyphicon glyphicon-menu-left" aria-hidden="true"></a>@listName</h1>
        <h3>기간</h3>
        <form method="get" id="form">
            <table class="table-daterange">
                <tr>
                    <th>
                        <input type="hidden" name="type" value="@type" />
                        <input type="date" name="from" class="form-control" value="@fromDate.ToShortDateString()" />부터
                        <input type="date" name="to" class="form-control" value="@toDate.ToShortDateString()" />까지
                    </th>
                    <td>
                        <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">
            @if (type == "charge") {
            <tr>
                <th>기간 수입액</th>
                <td>￦@income.ToString("N0")</td>
            </tr>
            <tr>
                <th>일 평균 수입액</th>
                <td>￦@((income / days).ToString("N1"))</td>
            </tr>
    } else if (type == "payment") {
        <tr>
            <th>기간 지출액</th>
            <td>￦@expense.ToString("N0")</td>
        </tr>
        <tr>
            <th>일 평균 지출액</th>
            <td>￦@((expense / days).ToString("N1"))</td>
        </tr>
}
        </table>
        <hr />
        <h3>@listName<span style="font-size: small"> @fromDate.ToShortDateString() ~ @toDate.ToShortDateString() (@(days)일간)</span></h3>
        @*<p>항목을 클릭하면 자세히 볼 수 있습니다.</p>*@
        <table class="table">
            <thead>
                <tr>
                    @if (type == "charge") {
                    <th>충전일시</th>
                    <th>회원</th>
                    <th>충전액</th>
            } else if (type == "payment") {
                <th>지급일시</th>
                <th>가맹점</th>
                <th>지급액</th>
        }
                </tr>
            </thead>
            <tbody>
                @if (type == "charge") {
                @foreach (var tuple in incomeList) {
                <tr>
                    <td>@tuple.time.ToString("yyyy-MM-dd HH:mm")</td>
                    <td>@tuple.userId</td>
                    <td>￦@tuple.cash.ToString("N0")</td>
                </tr>
        }
        } else if (type == "payment") {
            @foreach (var tuple in expenseList) {
            <tr>
                <td>@tuple.time.ToString("yyyy-MM-dd HH:mm")</td>
                <td>@tuple.placeName</td>
                <td>￦@tuple.cash.ToString("N0")</td>
            </tr>
    }
    }
            </tbody>
        </table>
        @if ((incomeList?.Length ?? 0) == 0 && (expenseList?.Length ?? 0)== 0) {
        <center>내용이 없습니다.</center>
}
        <hr />
    </div>
</div>

<div>
    <a href="./Sales?from=@(fromDate.ToShortDateString())&to=@(toDate.ToShortDateString())">이전 페이지로</a>
</div>