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; using PSTW_CentralSystem.Areas.MMS.Models.PDFGenerator; using System.Globalization; namespace PSTW_CentralSystem.Areas.MMS.Controllers { [Area("MMS")] //[Authorize(Policy = "RoleModulePolicy")] public class MarineController(MMSSystemContext context) : Controller { private readonly MMSSystemContext _context = context; public IActionResult Index() { return View(); // This will look for Index.cshtml in Areas/MMS/Views/Marine } //what to require from database into front-end (here is logic for calling, etc.) public IActionResult TarBallForm() { var marineTarballs = _context.MarineTarballs .Where(t=>t.StationID != "1") //Exclude test data with station named "1" .Select(t => new { t.Id, // Include Id property Date = t.DateSample.ToString("yyyy/MM/dd"), // Format DateSample as needed Station = t.StationID }) .ToList(); // For debugging Console.WriteLine("Fetched Data:"); foreach (var item in marineTarballs) { Console.WriteLine($"Date: {item.Date}, Station: {item.Station}"); } return View(marineTarballs); } //what to do for generating report public IActionResult GenerateReport(int id) { Console.WriteLine($"Requested ID in GenerateReport: {id}"); try { var tarballData = (from marine in _context.MarineTarballs join station in _context.MarineStations on marine.StationID equals station.StationID join state in _context.States on station.StateID equals state.StateID where marine.Id == id select new { state.StateName, marine.StationID, station.LocationName, marine.Longitude, marine.Latitude, marine.DateSample, marine.TimeSample }).FirstOrDefault(); if (tarballData == null) { return NotFound("The specified record was not found."); } Console.WriteLine($"DateSample: {tarballData.DateSample}, TimeSample: {tarballData.TimeSample}"); var pdfDocument = new TarBallPDF( tarballData.StateName, tarballData.StationID, tarballData.LocationName, tarballData.Longitude, tarballData.Latitude, tarballData.DateSample, // Pass DateSample directly (raw value) tarballData.TimeSample // Pass TimeSample directly (raw value) ); var pdf = pdfDocument.GeneratePdf(); Console.WriteLine("PDF generation completed."); var formattedDate = tarballData.DateSample.ToString("yyyyMMdd"); var fileName = $"TbReport_{tarballData.StationID}_{formattedDate}.pdf"; return File(pdf, "application/pdf", fileName); } catch (Exception ex) { Console.WriteLine($"Error in GenerateReport: {ex.Message}"); return StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while generating the PDF. Generate PDF error. " + ex.Message); } } public IActionResult ViewPDF(int id) { Console.WriteLine($"Requested ID in ViewReport: {id}"); try { var tarballData = (from marine in _context.MarineTarballs join station in _context.MarineStations on marine.StationID equals station.StationID join state in _context.States on station.StateID equals state.StateID where marine.Id == id select new { state.StateName, marine.StationID, station.LocationName, marine.Longitude, marine.Latitude, marine.DateSample, marine.TimeSample }).FirstOrDefault(); if (tarballData == null) { return NotFound("The specified record was not found."); } Console.WriteLine($"DateSample: {tarballData.DateSample}, TimeSample: {tarballData.TimeSample}"); // Generate the PDF - Make sure parameter order matches the constructor var document = new TarBallPDF( tarballData.StateName, tarballData.StationID, tarballData.LocationName, tarballData.Longitude, tarballData.Latitude, tarballData.DateSample, tarballData.TimeSample ); var pdf = document.GeneratePdf(); // Return the file without forcing download return File(pdf, "application/pdf"); } catch (Exception ex) { Console.WriteLine($"Error in ViewPDF: {ex.Message}"); return StatusCode(StatusCodes.Status500InternalServerError, "An error occurred while viewing the PDF. " + ex.Message); } } } }