@using CouponReport.Models.CouponMiddleware
@model CouponReportViewModel

@{
    ViewData["Title"] = "折扣報表";
}

<h2>折扣報表</h2>

<div class="card mb-4">
    <div class="card-body">
        <form method="post" asp-action="Index">
            <div class="row">
                <div class="col-md-4">
                    <div class="form-group">
                        <label for="startDate">開始日期</label>
                        <input type="date" class="form-control" id="startDate" name="startDate"
                               value="@Model.StartDate?.ToString("yyyy-MM-dd")" />
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="form-group">
                        <label for="endDate">結束日期</label>
                        <input type="date" class="form-control" id="endDate" name="endDate"
                               value="@Model.EndDate?.ToString("yyyy-MM-dd")" />
                    </div>
                </div>
                <div class="col-md-4">
                    <label>&nbsp;</label>
                    <div>
                        <button type="submit" class="btn btn-primary">查詢</button>
                    </div>
                </div>
            </div>
        </form>
    </div>
</div>

@if (Model.ReportItems != null && Model.ReportItems.Any())
{
    <div class="mb-3">
        <a href="@Url.Action("ExportToExcel", new { startDate = Model.StartDate, endDate = Model.EndDate })"
           class="btn btn-success">
            <i class="bi bi-file-excel"></i> 匯出 Excel
        </a>
        <a href="@Url.Action("ExportToPdf", new { startDate = Model.StartDate, endDate = Model.EndDate })"
           class="btn btn-danger">
            <i class="bi bi-file-pdf"></i> 匯出 PDF
        </a>
    </div>

    <style>
        .group-row-even {
            background-color: #f8f9fa;
        }
        .group-row-odd {
            background-color: #ffffff;
        }
    </style>

    <div class="table-responsive">
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th></th>
                    <th>店別(統編)</th>
                    <th>車號</th>
                    <th>發票日期</th>
                    <th>發票號碼</th>
                    <th>發票金額</th>
                    <th>折扣單位</th>
                    <th>折扣金額</th>
                    <th>折扣時間</th>
                    <th>入場時間</th>
                    <th>出場時間</th>
                    <th>停車金額</th>
                    <th>請款金額</th>
                </tr>
            </thead>
            <tbody>
                @{
                    int currentRowNumber = -1;
                    int rowSpanCount = 0;
                    int groupIndex = 0;
                }
                @foreach (var item in Model.ReportItems)
                {
                    bool isNewGroup = item.RowNumber != currentRowNumber;

                    if (isNewGroup)
                    {
                        // 計算這個編號有多少筆資料
                        rowSpanCount = Model.ReportItems.Count(x => x.RowNumber == item.RowNumber);
                        currentRowNumber = item.RowNumber;
                        groupIndex++;
                    }

                    var rowClass = (groupIndex % 2 == 0) ? "group-row-even" : "group-row-odd";

                    <tr class="@rowClass">
                        @if (isNewGroup)
                        {
                            <td rowspan="@rowSpanCount" class="align-middle">@item.RowNumber</td>
                        }
                        <td>@item.TenantCode</td>
                        @if (isNewGroup)
                        {
                            <td rowspan="@rowSpanCount" class="align-middle">@item.CarNumber</td>
                        }
                        <td>@item.InvoiceDate?.ToString("yyyy-MM-dd HH:mm:ss")</td>
                        <td>@item.InvoiceNumber</td>
                        <td class="text-end">@item.InvoiceAmount?.ToString("N0")</td>
                        @if (isNewGroup)
                        {
                            <td rowspan="@rowSpanCount" class="align-middle">@item.DiscountUnit</td>
                            <td rowspan="@rowSpanCount" class="align-middle text-end">@item.DiscountAmount?.ToString("N0")</td>
                            <td rowspan="@rowSpanCount" class="align-middle">@item.DiscountTime?.ToString("yyyy-MM-dd HH:mm:ss")</td>
                            <td rowspan="@rowSpanCount" class="align-middle">@item.EnterTime?.ToString("yyyy-MM-dd HH:mm:ss")</td>
                            <td rowspan="@rowSpanCount" class="align-middle">@item.ExitTime?.ToString("yyyy-MM-dd HH:mm:ss")</td>
                            <td rowspan="@rowSpanCount" class="align-middle text-end">@item.ParkingAmount?.ToString("N0")</td>
                            <td rowspan="@rowSpanCount" class="align-middle text-end">@item.ClaimAmount?.ToString("N0")</td>
                        }
                    </tr>
                }
            </tbody>
        </table>
    </div>

    <div class="mt-3">
        <p>總筆數: <strong>@Model.ReportItems.Count</strong></p>
    </div>
}
