using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using FastReport;
using FastReport.Export.Csv;
using FastReport.Export.Mht;
using FastReport.Export.OoXML;
using FastReport.Export.Pdf;
using Microsoft.AspNetCore.Mvc;
using rmutr_report.Models;
using rmutr_report.Models.Hr;
using Swashbuckle.AspNetCore.Annotations;

namespace rmutr_report.Controllers
{
    [SwaggerTag("สำหรับรายงาน_budget")]
    public class Budget: Controller
    {
         readonly Setting _setting;

        public Budget(Setting setting)
        {
            this._setting = setting;
        }

        [HttpPost, Route("reports/budget_report/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetSum1Report([FromRoute] string type, [FromBody] List<budget_report> budget_reports)
        {
            foreach (var x in budget_reports)
            {
               int sum1 = budget_reports.Sum(g => int.Parse(g.salaya));
               x.sum1 = sum1;
               int sum2 = budget_reports.Sum(g => int.Parse(g.bophitphimuk));
               x.sum2 = sum2;
               int sum3 = budget_reports.Sum(g => int.Parse(g.pohchang));
               x.sum3 = sum3;
               int sum4 = budget_reports.Sum(g => int.Parse(g.klai_kangwon));
               x.sum4 = sum4;
               int sum5 = budget_reports.Sum(g => int.Parse(g.total));
               x.sum5 = sum5;

            }
            Report report = new Report();
            report.Load(_setting.report_path + "budget_report.frx");
            report.RegisterData(budget_reports, "budget_report");
            report.Prepare();

            MemoryStream stream = new MemoryStream();
            switch (type)
            {
                case "pdf":
                    PDFExport pdf = new PDFExport();
                    report.Export(pdf, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/pdf");

                case "xls":
                    Excel2007Export excel = new Excel2007Export();
                    report.Export(excel, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/vnd.ms-excel");
                    break;
                case "mht":
                    MHTExport mht = new MHTExport();
                    report.Export(mht, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "multipart/related");
                    break;
                case "csv":
                    CSVExport csv = new CSVExport();
                    report.Export(csv, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "text/csv");
                    break;
            }

            return Ok();
        }
        
    }
}