diff --git a/Areas/MMS/Controllers/MarineController.cs b/Areas/MMS/Controllers/MarineController.cs index c3fe67f..a2fcafc 100644 --- a/Areas/MMS/Controllers/MarineController.cs +++ b/Areas/MMS/Controllers/MarineController.cs @@ -1,7 +1,10 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using PSTW_CentralSystem.DBContext; +using PSTW_CentralSystem.Models; using PSTW_CentralSystem.Areas.MMS; +using System.Linq; using QuestPDF.Fluent; namespace PSTW_CentralSystem.Areas.MMS.Controllers @@ -11,24 +14,64 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers //[Authorize(Policy = "RoleModulePolicy")] public class MarineController : Controller { + private readonly MMSSystemContext _context; + public MarineController(MMSSystemContext context) + { + _context = context; + } public IActionResult Index() { return View(); // This will look for Index.cshtml in Areas/MMS/Views/Marine } public IActionResult TarBallForm() { + var marineTarballs = _context.MarineTarballs + .Select(t => new + { + t.Id, //Include Id property + Date = t.DateSample, + Station = t.StationID + }) + .ToList(); - return View(); + // For debugging + Console.WriteLine("Fetched Data:"); + foreach (var item in marineTarballs) + { + Console.WriteLine($"Date: {item.Date}, Station: {item.Station}"); + } + + return View(marineTarballs); } - public IActionResult GenerateReport() + public IActionResult GenerateReport(int id) { try { - // Retrieve specific data based on the id, if required - var document = new TarBallPDF(); // Use id to customize the report if needed + // Retrieve the specific record based on the id + var tarballData = _context.MarineTarballs + .Where(t => t.Id == id) + .Select(t => new + { + t.StationID, + t.DateSample + }) + .FirstOrDefault(); + + if (tarballData == null) + { + return NotFound("The specified record was not found."); + } + + // Generate the PDF + var document = new TarBallPDF(); // Customize the report if needed var pdf = document.GeneratePdf(); - var fileName = $"TarBallReport_{DateTime.Now:yyyyMMdd_HHmmss}.pdf"; + + // Construct the filename + var formattedDate = tarballData.DateSample.ToString("ddMMyyyy"); + var fileName = $"TbReport_{tarballData.StationID}_{formattedDate}.pdf"; + + // Return the file return File(pdf, "application/pdf", fileName); } catch (Exception ex) @@ -38,13 +81,34 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers } } - public IActionResult ViewPDF() + public IActionResult ViewPDF(int id) { try { - // Retrieve specific data based on the id, if required - var document = new TarBallPDF(); // Use id to customize the PDF if needed + // Retrieve the specific record based on the id + var tarballData = _context.MarineTarballs + .Where(t => t.Id == id) + .Select(t => new + { + t.StationID, + t.DateSample + }) + .FirstOrDefault(); + + if (tarballData == null) + { + return NotFound("The specified record was not found."); + } + + // Generate the PDF + var document = new TarBallPDF(); // Customize the PDF if needed var pdf = document.GeneratePdf(); + + //For filename + var formattedDate = tarballData.DateSample.ToString("ddMMyyyy"); + var fileName = $"TbReport_{tarballData.StationID}_{formattedDate}.pdf"; + + // Return the file return File(pdf, "application/pdf"); } catch (Exception ex) @@ -53,5 +117,6 @@ namespace PSTW_CentralSystem.Areas.MMS.Controllers return StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while viewing the PDF. " + ex.Message); } } + } } diff --git a/Areas/MMS/Views/Marine/TarBallForm.cshtml b/Areas/MMS/Views/Marine/TarBallForm.cshtml index ecf3353..cfd611d 100644 --- a/Areas/MMS/Views/Marine/TarBallForm.cshtml +++ b/Areas/MMS/Views/Marine/TarBallForm.cshtml @@ -57,7 +57,6 @@ color: #333; /* Dark grey for active state */ font-weight: bold; /* Optional: Make it bold for emphasis */ } -
@@ -76,7 +75,7 @@