using CouponReport.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ClosedXML.Excel;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Kernel.Font;
using iText.IO.Font;
using iText.IO.Font.Constants;
using CouponReport.Models.CouponMiddleware;
using CouponReport.Models.Parkingeyes;
using CouponReport.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authentication.Cookies;

namespace CouponReport.Controllers;

[Authorize]
public class ReportController : Controller
{
    private readonly CouponMiddlewareContext _couponContext;
    private readonly ParkingEyesContext _parkingEyesContext;
    private readonly ReportService _reportService;

	public ReportController(CouponMiddlewareContext context, ParkingEyesContext parkingEyesContext, ReportService reportService)
    {
		_couponContext = context;
		_parkingEyesContext = parkingEyesContext;
		_reportService = reportService;
	}

    public async Task<IActionResult> Index(DateTime? startDate, DateTime? endDate)
    {
        // 如果沒有提供日期，預設為當月第一天到最後一天
        if (!startDate.HasValue && !endDate.HasValue)
        {
            var today = DateTime.Today;
            startDate = new DateTime(today.Year, today.Month, 1);
            endDate = startDate.Value.AddMonths(1).AddDays(-1);
        }

        return await LoadReportData(startDate, endDate);
    }

    [HttpPost]
    public async Task<IActionResult> Index(DateTime? startDate, DateTime? endDate, string action)
    {
        return await LoadReportData(startDate, endDate);
    }

    private async Task<IActionResult> LoadReportData(DateTime? startDate, DateTime? endDate)
    {
        // 建立包含日期篩選的查詢
        var query = _couponContext.Logs.AsQueryable()
            .Where(x => x.LogType == "Consume" && x.LogInfo.Contains("耗用成功"));

        if (startDate.HasValue)
        {
            query = query.Where(x => x.LogTime >= startDate.Value);
        }

        if (endDate.HasValue)
        {
            query = query.Where(x => x.LogTime <= endDate.Value.AddDays(1).AddSeconds(-1));
        }

        var consumeLogs = await query.OrderBy(x => x.LogTime).ToListAsync();
        var serialNo = consumeLogs.Select(x => x.SerialNo).Distinct().ToList();

        var evaluateLogsQuery = await _couponContext.Logs
            .Where(x => serialNo.Contains(x.SerialNo) && x.LogType == "Evaluate" && x.LogInfo.Contains("評估成功"))
            .ToListAsync();

        // 建立 SerialNo 到 Evaluate Log 的映射
        var evaluateLogDict = evaluateLogsQuery
            .GroupBy(x => x.SerialNo)
            .ToDictionary(g => g.Key, g => g.FirstOrDefault());

		//var carEnter = _parkingEyesContext.CarEnters.Where(x => serialNo.Contains(x.SerialNo)).ToList();
		//var carEnter = _mySqlContext.Cario.Where(x => serialNo.Contains(x.SerialNo)).ToList();

        // 建立 SerialNo 到編號的映射
        var serialNoToRowNumber = new Dictionary<string, int>();
        int currentRowNumber = 1;

        // 建立 SerialNo 到 CarEnter 的映射，方便快速查找
        //var carEnterDict = carEnter.GroupBy(x => x.SerialNo)
        //                            .ToDictionary(g => g.Key, g => g.FirstOrDefault());

        var reportItems = consumeLogs.Select(log =>
            {
                // 如果這個 SerialNo 還沒出現過，給它一個新編號
                if (!string.IsNullOrEmpty(log.SerialNo) && !serialNoToRowNumber.ContainsKey(log.SerialNo))
                {
                    serialNoToRowNumber[log.SerialNo] = currentRowNumber++;
                }

                // 從 evaluateLogDict 取得出場時間
                DateTime? exitTime = null;
                if (!string.IsNullOrEmpty(log.SerialNo) && evaluateLogDict.ContainsKey(log.SerialNo))
                {
                    exitTime = evaluateLogDict[log.SerialNo]?.LogTime;
                }

                var tenantCode = string.Empty;
				DateTime? invoiceDate = null;
                var invoiceNo = string.Empty;
				var invoiceAmount = 0m;

				//if (!string.IsNullOrEmpty(log.SerialNo) && carEnterDict.ContainsKey(log.SerialNo))
    //            {
    //                exitTime = carEnterDict[log.SerialNo]?.OutTime;
    //            }

                invoiceDate = _reportService.GetInvoiceDateTime(log.ExternalSystemKey);
				invoiceNo = _reportService.GetInvoiceNo(log.ExternalSystemKey);
				invoiceAmount = _reportService.GetInvoiceMoney(log.ExternalSystemKey);
                tenantCode = _reportService.GetTenantCode(log.ExternalSystemKey);

				return new CouponReportItem
                {
                    RowNumber = !string.IsNullOrEmpty(log.SerialNo) ? serialNoToRowNumber[log.SerialNo] : 0,
					TenantCode = tenantCode,
                    CarNumber = log.PlateNo,
                    InvoiceDate = invoiceDate,
                    InvoiceNumber = invoiceNo,
                    InvoiceAmount = invoiceAmount,
                    DiscountUnit = "金額",
                    DiscountAmount = log.DiscountAmount,
                    DiscountTime = log.LogTime,
                    EnterTime = log.EnterTime,
                    ExitTime = exitTime,
                    ParkingAmount = log.TotalAmount,
                    ClaimAmount = log.DiscountAmount
                };
            }).OrderBy(x => x.RowNumber).ThenBy(x => x.DiscountTime).ToList();

        var viewModel = new CouponReportViewModel
        {
            StartDate = startDate,
            EndDate = endDate,
            ReportItems = reportItems
        };

        return View(viewModel);
    }

    public async Task<IActionResult> ExportToExcel(DateTime? startDate, DateTime? endDate)
    {
		var query = _couponContext.Logs.AsQueryable()
			.Where(x => x.LogType == "Consume" && x.LogInfo.Contains("耗用成功"));

		if (startDate.HasValue)
        {
            query = query.Where(x => x.LogTime >= startDate.Value);
        }

        if (endDate.HasValue)
        {
            query = query.Where(x => x.LogTime <= endDate.Value.AddDays(1).AddSeconds(-1));
        }

        var consumeLogs = await query.OrderBy(x => x.LogTime).ToListAsync();
		var serialNo = consumeLogs.Select(x => x.SerialNo).Distinct().ToList();

		var evaluateLogsQuery = await _couponContext.Logs
            .Where(x => serialNo.Contains(x.SerialNo) && x.LogType == "Evaluate" && x.LogInfo.Contains("評估成功"))
            .ToListAsync();

        // 建立 SerialNo 到 Evaluate Log 的映射
        var evaluateLogDict = evaluateLogsQuery
            .GroupBy(x => x.SerialNo)
            .ToDictionary(g => g.Key, g => g.FirstOrDefault());

		// 建立 SerialNo 到編號的映射
		var serialNoToRowNumber = new Dictionary<string, int>();
		int currentRowNumber = 1;

		// 建立報表資料列表
		var reportItems = new List<(int rowNumber, string tenantCode, string carNumber, DateTime? invoiceDate,
									 string invoiceNo, decimal invoiceAmount, string discountUnit, decimal? discountAmount,
									 DateTime discountTime, DateTime? enterTime, DateTime? exitTime,
									 decimal? parkingAmount, decimal? claimAmount)>();

		foreach (var log in consumeLogs)
		{
			if (!string.IsNullOrEmpty(log.SerialNo) && !serialNoToRowNumber.ContainsKey(log.SerialNo))
			{
				serialNoToRowNumber[log.SerialNo] = currentRowNumber++;
			}

			DateTime? exitTime = null;
			if (!string.IsNullOrEmpty(log.SerialNo) && evaluateLogDict.ContainsKey(log.SerialNo))
			{
				exitTime = evaluateLogDict[log.SerialNo]?.LogTime;
			}

			var invoiceDate = _reportService.GetInvoiceDateTime(log.ExternalSystemKey);
			var invoiceNo = _reportService.GetInvoiceNo(log.ExternalSystemKey);
			var invoiceAmount = _reportService.GetInvoiceMoney(log.ExternalSystemKey);
			var tenantCode = _reportService.GetTenantCode(log.ExternalSystemKey);

			int rowNumber = !string.IsNullOrEmpty(log.SerialNo) ? serialNoToRowNumber[log.SerialNo] : 0;

			reportItems.Add((rowNumber, tenantCode, log.PlateNo, invoiceDate, invoiceNo, invoiceAmount,
							 "金額", log.DiscountAmount, log.LogTime, log.EnterTime, exitTime,
							 log.TotalAmount, log.DiscountAmount));
		}

		// 按 RowNumber 排序，確保相同編號的資料連續，避免合併儲存格錯誤
		reportItems = reportItems.OrderBy(x => x.rowNumber).ThenBy(x => x.discountTime).ToList();

		using var workbook = new XLWorkbook();
        var worksheet = workbook.Worksheets.Add("折扣報表");

        // 匯出資訊（合併儲存格以避免影響欄寬）
        worksheet.Range(1, 1, 1, 13).Merge();
        worksheet.Cell(1, 1).Value = $"匯出時間：{DateTime.Now:yyyy-MM-dd HH:mm:ss}";

        worksheet.Range(2, 1, 2, 13).Merge();
        worksheet.Cell(2, 1).Value = $"篩選條件：{startDate?.ToString("yyyy-MM-dd")} ~ {endDate?.ToString("yyyy-MM-dd")}";

        // 標題列
        worksheet.Cell(4, 1).Value = "編號";
        worksheet.Cell(4, 2).Value = "店別(統編)";
        worksheet.Cell(4, 3).Value = "車號";
        worksheet.Cell(4, 4).Value = "發票日期";
        worksheet.Cell(4, 5).Value = "發票號碼";
        worksheet.Cell(4, 6).Value = "發票金額";
        worksheet.Cell(4, 7).Value = "折扣單位";
        worksheet.Cell(4, 8).Value = "折扣金額";
        worksheet.Cell(4, 9).Value = "折扣時間";
        worksheet.Cell(4, 10).Value = "入場時間";
        worksheet.Cell(4, 11).Value = "出場時間";
        worksheet.Cell(4, 12).Value = "停車金額";
        worksheet.Cell(4, 13).Value = "請款金額";

        // 設定標題列樣式
        var headerRange = worksheet.Range(4, 1, 4, 13);
        headerRange.Style.Font.Bold = true;
        headerRange.Style.Fill.BackgroundColor = XLColor.LightGray;
        headerRange.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
        headerRange.Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;

        // 填入資料並記錄合併範圍
        int row = 5;
        var mergedCells = new Dictionary<int, (int startRow, int endRow)>();

        foreach (var item in reportItems)
        {
            // 填入資料
            worksheet.Cell(row, 1).Value = item.rowNumber;
            worksheet.Cell(row, 2).Value = item.tenantCode;
            worksheet.Cell(row, 3).Value = item.carNumber;
            worksheet.Cell(row, 4).Value = item.invoiceDate?.ToString("yyyy-MM-dd");
            worksheet.Cell(row, 5).Value = item.invoiceNo;
            worksheet.Cell(row, 6).Value = $"NT$ {item.invoiceAmount:N0}";
            worksheet.Cell(row, 7).Value = item.discountUnit;
            worksheet.Cell(row, 8).Value = $"NT$ {item.discountAmount:N0}";
            worksheet.Cell(row, 9).Value = item.discountTime.ToString("yyyy-MM-dd HH:mm:ss");
            worksheet.Cell(row, 10).Value = item.enterTime?.ToString("yyyy-MM-dd HH:mm:ss");
            worksheet.Cell(row, 11).Value = item.exitTime?.ToString("yyyy-MM-dd HH:mm:ss");
            worksheet.Cell(row, 12).Value = $"NT$ {item.parkingAmount:N0}";
            worksheet.Cell(row, 13).Value = $"NT$ {item.claimAmount:N0}";

            // 記錄需要合併的儲存格範圍（按 rowNumber 分組）
            if (item.rowNumber > 0)
            {
                if (!mergedCells.ContainsKey(item.rowNumber))
                {
                    mergedCells[item.rowNumber] = (row, row);
                }
                else
                {
                    mergedCells[item.rowNumber] = (mergedCells[item.rowNumber].startRow, row);
                }
            }

            row++;
        }

        // 執行合併儲存格
        foreach (var (rowNumber, (startRow, endRow)) in mergedCells)
        {
            if (startRow < endRow)
            {
                // 合併：編號(1)、車號(3)、折扣單位(7)、折扣金額(8)、折扣時間(9)、入場時間(10)、出場時間(11)、停車金額(12)、請款金額(13)
                int[] mergeCols = { 1, 3, 7, 8, 9, 10, 11, 12, 13 };
                foreach (int col in mergeCols)
                {
                    var range = worksheet.Range(startRow, col, endRow, col);
                    range.Merge();
                    range.Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                    range.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                }
            }
        }

        // 自動調整欄寬，並設定最小寬度以顯示完整表頭
        worksheet.Columns().AdjustToContents();
        for (int col = 1; col <= 13; col++)
        {
            if (worksheet.Column(col).Width < 12)
            {
                worksheet.Column(col).Width = 12;
            }
        }

        // 將所有資料儲存格置中
        var dataRange = worksheet.Range(5, 1, row - 1, 13);
        dataRange.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
        dataRange.Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;

        var stream = new MemoryStream();
        workbook.SaveAs(stream);
        stream.Position = 0;

        var fileName = $"折扣報表_{DateTime.Now:yyyyMMddHHmmss}.xlsx";
        return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
    }

    public async Task<IActionResult> ExportToPdf(DateTime? startDate, DateTime? endDate)
    {
		var query = _couponContext.Logs.AsQueryable()
			.Where(x => x.LogType == "Consume" && x.LogInfo.Contains("耗用成功"));

		if (startDate.HasValue)
        {
            query = query.Where(x => x.LogTime >= startDate.Value);
        }

        if (endDate.HasValue)
        {
            query = query.Where(x => x.LogTime <= endDate.Value.AddDays(1).AddSeconds(-1));
        }

        var consumeLogs = await query.OrderBy(x => x.LogTime).ToListAsync();
		var serialNo = consumeLogs.Select(x => x.SerialNo).Distinct().ToList();

		var evaluateLogsQuery = await _couponContext.Logs
            .Where(x => serialNo.Contains(x.SerialNo) && x.LogType == "Evaluate" && x.LogInfo.Contains("評估成功"))
            .ToListAsync();

        // 建立 SerialNo 到 Evaluate Log 的映射
        var evaluateLogDict = evaluateLogsQuery
            .GroupBy(x => x.SerialNo)
            .ToDictionary(g => g.Key, g => g.FirstOrDefault());

		// 建立 SerialNo 到編號的映射
		var serialNoToRowNumber = new Dictionary<string, int>();
		int currentRowNumber = 1;

		// 建立臨時資料列表以便排序
		var tempReportItems = new List<(int rowNumber, string tenantCode, string carNumber, DateTime? invoiceDate,
										 string invoiceNo, decimal invoiceAmount, string discountUnit, decimal? discountAmount,
										 DateTime discountTime, DateTime? enterTime, DateTime? exitTime,
										 decimal? parkingAmount, decimal? claimAmount)>();

		foreach (var log in consumeLogs)
		{
			if (!string.IsNullOrEmpty(log.SerialNo) && !serialNoToRowNumber.ContainsKey(log.SerialNo))
			{
				serialNoToRowNumber[log.SerialNo] = currentRowNumber++;
			}

			DateTime? exitTime = null;
			if (!string.IsNullOrEmpty(log.SerialNo) && evaluateLogDict.ContainsKey(log.SerialNo))
			{
				exitTime = evaluateLogDict[log.SerialNo]?.LogTime;
			}

			var invoiceDate = _reportService.GetInvoiceDateTime(log.ExternalSystemKey);
			var invoiceNo = _reportService.GetInvoiceNo(log.ExternalSystemKey);
			var invoiceAmount = _reportService.GetInvoiceMoney(log.ExternalSystemKey);
			var tenantCode = _reportService.GetTenantCode(log.ExternalSystemKey);

			int rowNumber = !string.IsNullOrEmpty(log.SerialNo) ? serialNoToRowNumber[log.SerialNo] : 0;

			tempReportItems.Add((rowNumber, tenantCode, log.PlateNo, invoiceDate, invoiceNo, invoiceAmount,
								 "金額", log.DiscountAmount, log.LogTime, log.EnterTime, exitTime,
								 log.TotalAmount, log.DiscountAmount));
		}

		// 按 RowNumber 排序，確保相同編號的資料連續，避免合併儲存格錯誤
		tempReportItems = tempReportItems.OrderBy(x => x.rowNumber).ThenBy(x => x.discountTime).ToList();

		// 建立報表資料並分組
		var reportGroups = new Dictionary<int, List<(string tenantCode, DateTime? invoiceDate, string invoiceNo, decimal invoiceAmount)>>();
		var groupData = new Dictionary<int, (int rowNumber, string carNumber, string discountUnit, decimal? discountAmount,
											  DateTime discountTime, DateTime? enterTime, DateTime? exitTime,
											  decimal? parkingAmount, decimal? claimAmount)>();

		foreach (var item in tempReportItems)
		{
			// 記錄群組資料
			if (!groupData.ContainsKey(item.rowNumber))
			{
				groupData[item.rowNumber] = (item.rowNumber, item.carNumber, item.discountUnit, item.discountAmount, item.discountTime,
										item.enterTime, item.exitTime, item.parkingAmount, item.claimAmount);
				reportGroups[item.rowNumber] = new List<(string, DateTime?, string, decimal)>();
			}

			reportGroups[item.rowNumber].Add((item.tenantCode, item.invoiceDate, item.invoiceNo, item.invoiceAmount));
		}

        var stream = new MemoryStream();
        var writer = new PdfWriter(stream);
        writer.SetCloseStream(false); // 防止關閉 MemoryStream
        var pdf = new PdfDocument(writer);
        var document = new Document(pdf, iText.Kernel.Geom.PageSize.A4.Rotate());

        // 設定中文字型
        var fontPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "kaiu.ttf");
        if (!System.IO.File.Exists(fontPath))
        {
            fontPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "msjh.ttc,0");
        }

        PdfFont font;
        try
        {
            font = PdfFontFactory.CreateFont(fontPath, PdfEncodings.IDENTITY_H);
        }
        catch
        {
            font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
        }

        document.SetFont(font);

        // 標題
        var title = new Paragraph("折扣報表")
            .SetTextAlignment(TextAlignment.CENTER)
            .SetFontSize(18);
        document.Add(title);

        // 匯出資訊
        var exportInfo = new Paragraph($"匯出時間：{DateTime.Now:yyyy-MM-dd HH:mm:ss}")
            .SetFontSize(10)
            .SetMarginTop(10);
        document.Add(exportInfo);

        var filterInfo = new Paragraph($"篩選條件：{startDate?.ToString("yyyy-MM-dd")} ~ {endDate?.ToString("yyyy-MM-dd")}")
            .SetFontSize(10)
            .SetMarginBottom(10);
        document.Add(filterInfo);

        // 建立表格
        var table = new Table(13).UseAllAvailableWidth();
        table.SetFontSize(8);

        // 標題列
        table.AddHeaderCell(new Cell().Add(new Paragraph("編號")).SetTextAlignment(TextAlignment.CENTER));
        table.AddHeaderCell(new Cell().Add(new Paragraph("店別")).SetTextAlignment(TextAlignment.CENTER));
        table.AddHeaderCell(new Cell().Add(new Paragraph("車號")).SetTextAlignment(TextAlignment.CENTER));
        table.AddHeaderCell(new Cell().Add(new Paragraph("發票日期")).SetTextAlignment(TextAlignment.CENTER));
        table.AddHeaderCell(new Cell().Add(new Paragraph("發票號碼")).SetTextAlignment(TextAlignment.CENTER));
        table.AddHeaderCell(new Cell().Add(new Paragraph("發票金額")).SetTextAlignment(TextAlignment.CENTER));
        table.AddHeaderCell(new Cell().Add(new Paragraph("折扣單位")).SetTextAlignment(TextAlignment.CENTER));
        table.AddHeaderCell(new Cell().Add(new Paragraph("折扣金額")).SetTextAlignment(TextAlignment.CENTER));
        table.AddHeaderCell(new Cell().Add(new Paragraph("折扣時間")).SetTextAlignment(TextAlignment.CENTER));
        table.AddHeaderCell(new Cell().Add(new Paragraph("入場時間")).SetTextAlignment(TextAlignment.CENTER));
        table.AddHeaderCell(new Cell().Add(new Paragraph("出場時間")).SetTextAlignment(TextAlignment.CENTER));
        table.AddHeaderCell(new Cell().Add(new Paragraph("停車金額")).SetTextAlignment(TextAlignment.CENTER));
        table.AddHeaderCell(new Cell().Add(new Paragraph("請款金額")).SetTextAlignment(TextAlignment.CENTER));

        // 資料列（使用合併儲存格）
        foreach (var (rowNum, group) in groupData.OrderBy(x => x.Key))
        {
            var invoices = reportGroups[rowNum];
            int rowSpan = invoices.Count;

            for (int i = 0; i < invoices.Count; i++)
            {
                var invoice = invoices[i];
                bool isFirstRow = i == 0;

                if (isFirstRow)
                {
                    // 第一行：顯示合併的欄位 - 編號
                    table.AddCell(new Cell(rowSpan, 1).Add(new Paragraph(group.rowNumber.ToString())).SetTextAlignment(TextAlignment.CENTER).SetVerticalAlignment(VerticalAlignment.MIDDLE));
                }

                // 每一行：店別
                table.AddCell(new Cell().Add(new Paragraph(invoice.tenantCode ?? "")).SetTextAlignment(TextAlignment.CENTER));

                if (isFirstRow)
                {
                    // 車號
                    table.AddCell(new Cell(rowSpan, 1).Add(new Paragraph(group.carNumber ?? "")).SetTextAlignment(TextAlignment.CENTER).SetVerticalAlignment(VerticalAlignment.MIDDLE));
                }

                // 每一行：發票日期、發票號碼、發票金額
                table.AddCell(new Cell().Add(new Paragraph(invoice.invoiceDate?.ToString("yyyy-MM-dd") ?? "")).SetTextAlignment(TextAlignment.CENTER));
                table.AddCell(new Cell().Add(new Paragraph(invoice.invoiceNo ?? "")).SetTextAlignment(TextAlignment.CENTER));
                table.AddCell(new Cell().Add(new Paragraph($"NT$ {invoice.invoiceAmount:N0}")).SetTextAlignment(TextAlignment.CENTER));

                if (isFirstRow)
                {
                    // 合併欄位：折扣單位、折扣金額、折扣時間、入場時間、出場時間、停車金額、請款金額
                    table.AddCell(new Cell(rowSpan, 1).Add(new Paragraph(group.discountUnit ?? "")).SetTextAlignment(TextAlignment.CENTER).SetVerticalAlignment(VerticalAlignment.MIDDLE));
                    table.AddCell(new Cell(rowSpan, 1).Add(new Paragraph($"NT$ {group.discountAmount:N0}")).SetTextAlignment(TextAlignment.CENTER).SetVerticalAlignment(VerticalAlignment.MIDDLE));
                    table.AddCell(new Cell(rowSpan, 1).Add(new Paragraph(group.discountTime.ToString("yyyy-MM-dd HH:mm"))).SetTextAlignment(TextAlignment.CENTER).SetVerticalAlignment(VerticalAlignment.MIDDLE));
                    table.AddCell(new Cell(rowSpan, 1).Add(new Paragraph(group.enterTime?.ToString("yyyy-MM-dd HH:mm") ?? "")).SetTextAlignment(TextAlignment.CENTER).SetVerticalAlignment(VerticalAlignment.MIDDLE));
                    table.AddCell(new Cell(rowSpan, 1).Add(new Paragraph(group.exitTime?.ToString("yyyy-MM-dd HH:mm") ?? "")).SetTextAlignment(TextAlignment.CENTER).SetVerticalAlignment(VerticalAlignment.MIDDLE));
                    table.AddCell(new Cell(rowSpan, 1).Add(new Paragraph($"NT$ {group.parkingAmount:N0}")).SetTextAlignment(TextAlignment.CENTER).SetVerticalAlignment(VerticalAlignment.MIDDLE));
                    table.AddCell(new Cell(rowSpan, 1).Add(new Paragraph($"NT$ {group.claimAmount:N0}")).SetTextAlignment(TextAlignment.CENTER).SetVerticalAlignment(VerticalAlignment.MIDDLE));
                }
            }
        }

        document.Add(table);
        document.Close();

        stream.Position = 0;
        var fileName = $"折扣報表_{DateTime.Now:yyyyMMddHHmmss}.pdf";
        return File(stream, "application/pdf", fileName);
    }
}
