using System;
using System.IO;
using ClosedXML.Excel;
using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualBasic;
using rmutr_report.Models;
using rmutr_report.Models.Hr;
using Swashbuckle.AspNetCore.Annotations;

namespace rmutr_report.Controllers
{
    [SwaggerTag("สำหรับรายงานตัวชี้วัดแผนกลยุทธ์ทางการเงิน")]
    public class kpi_financial_strategy : Controller
    {
        readonly Setting _setting;

        public kpi_financial_strategy(Setting setting)
        {
            _setting = setting;
        }

        [HttpPost, Route("reports/kpi_financial_strategy/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetDetailKpiReport([FromRoute] string type, [FromBody] kpi_financial _kpi)
        {
            var workbook = new XLWorkbook();
            var ws = workbook.Worksheets.Add("kpi");
            ws.Range("A1:O1").Merge().Value = "ตัวชี้วัดแผนกลยุทธ์ทางการเงิน พ.ศ. " + _kpi.year_range+" มหาวิทยาลัยเทคโนโลยีราชมงคลรัตนโกสินทร์ ประจำปีงบประมาณ พ.ศ. "+_kpi.budget_year;
            ws.Cell("A1").Style.Font.Bold = true;
            ws.Cell("A1").Style.Alignment.WrapText = true;
            //ws.Range("A1:O1").Style.Fill.BackgroundColor = XLColor.Yellow;
            ws.Range("A2:A3").Merge().Value = "ลำดับ";
            ws.Cell("A2").Style.Font.Bold = true;
            ws.Range("A2:A3").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("B2:B3").Merge().Value = "มิติ";
            ws.Cell("B2").Style.Font.Bold = true;
            ws.Range("B2:B3").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("C2:C3").Merge().Value = "วัตถุประสงค์เชิงยุทธ์ศาสตร์";
            ws.Cell("C2").Style.Font.Bold = true;
            ws.Range("C2:C3").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("D2:D3").Merge().Value = "ตัวชี้วัด";
            ws.Cell("D2").Style.Font.Bold = true;
            ws.Range("D2:D3").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("E2:E3").Merge().Value = "หน่วยนับ";
            ws.Cell("E2").Style.Font.Bold = true;
            ws.Range("E2:E3").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("F2:F3").Merge().Value = "แผนปี " +_kpi.plan_year1.Substring(2,2);
            ws.Cell("F2").Style.Font.Bold = true;
            ws.Range("F2:F3").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("G2:G3").Merge().Value = "แผนปี " + _kpi.plan_year2.Substring(2,2);
            ws.Cell("G2").Style.Font.Bold = true;
            ws.Range("G2:G3").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("H2:H3").Merge().Value =  "แผนปี " + _kpi.plan_year3.Substring(2,2);
            ws.Cell("H2").Style.Font.Bold = true;
            ws.Range("H2:H3").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("I2:I3").Merge().Value =  "แผนปี " + _kpi.plan_year4.Substring(2,2);
            ws.Cell("I2").Style.Font.Bold = true;
            ws.Range("I2:I3").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("J2:J3").Merge().Value =  "แผนปี " + _kpi.plan_year5.Substring(2,2);
            ws.Cell("J2").Style.Font.Bold = true;
            ws.Range("J2:J3").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("K2:K3").Merge().Value =  "แผนปี " + _kpi.plan_year6.Substring(2,2);
            ws.Cell("K2").Style.Font.Bold = true;
            ws.Cell("K2").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("K2:K3").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("L2:L3").Merge().Value =  "ผู้กำกับ";
            ws.Cell("L2").Style.Font.Bold = true;
            ws.Range("L2:L3").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("M2:M3").Merge().Value =  "ผู้รับผิดชอบตัวชี้วัด";
            ws.Cell("M2").Style.Font.Bold = true;
            ws.Cell("M2").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("N2:N3").Merge().Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Cell("N2").Value =  "ผู้รวบรวมและรายงานผล";
            ws.Cell("N2").Style.Font.Bold = true;
            ws.Cell("N2").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("N2:N3").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("O2:O3").Merge().Value =  "กลยุทธ์";
            ws.Cell("O2").Style.Font.Bold = true;
            ws.Range("O2:O3").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("A2:O3").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("A2:O3").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range("A2:O3").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("A1:O1").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("A1:O1").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Column(1).Width = 10;
            ws.Column(2).Width = 30;
            ws.Column(3).Width = 40;
            ws.Column(4).Width = 30;
            ws.Column(5).Width = 20;
            ws.Column(6).Width = 15;
            ws.Column(7).Width = 15;
            ws.Column(8).Width = 15;
            ws.Column(9).Width = 15;
            ws.Column(10).Width = 15;
            ws.Column(11).Width = 15;
            ws.Column(12).Width = 40;
            ws.Column(13).Width = 40;
            ws.Column(14).Width = 40;
            ws.Column(15).Width = 40;
            ws.Column(1).Style.Font.FontName = "TH Sarabun New";
            ws.Column(1).Style.Font.FontSize = 16;
            ws.Column(2).Style.Font.FontName = "TH Sarabun New";
            ws.Column(2).Style.Font.FontSize = 16;
            ws.Column(3).Style.Font.FontName = "TH Sarabun New";
            ws.Column(3).Style.Font.FontSize = 16;
            ws.Column(4).Style.Font.FontName = "TH Sarabun New";
            ws.Column(4).Style.Font.FontSize = 16;
            ws.Column(5).Style.Font.FontName = "TH Sarabun New";
            ws.Column(5).Style.Font.FontSize = 16;
            ws.Column(6).Style.Font.FontName = "TH Sarabun New";
            ws.Column(6).Style.Font.FontSize = 16;
            ws.Column(7).Style.Font.FontName = "TH Sarabun New";
            ws.Column(7).Style.Font.FontSize = 16;
            ws.Column(8).Style.Font.FontName = "TH Sarabun New";
            ws.Column(8).Style.Font.FontSize = 16;
            ws.Column(9).Style.Font.FontName = "TH Sarabun New";
            ws.Column(9).Style.Font.FontSize = 16;
            ws.Column(10).Style.Font.FontName = "TH Sarabun New";
            ws.Column(10).Style.Font.FontSize = 16;
            ws.Column(11).Style.Font.FontName = "TH Sarabun New";
            ws.Column(11).Style.Font.FontSize = 16;
            ws.Column(12).Style.Font.FontName = "TH Sarabun New";
            ws.Column(12).Style.Font.FontSize = 16;
            ws.Column(13).Style.Font.FontName = "TH Sarabun New";
            ws.Column(13).Style.Font.FontSize = 16;
            ws.Column(14).Style.Font.FontName = "TH Sarabun New";
            ws.Column(14).Style.Font.FontSize = 16;
            ws.Column(15).Style.Font.FontName = "TH Sarabun New";
            ws.Column(15).Style.Font.FontSize = 16;
            int col = 1;
            int row = 4;
            int no = 1;
            int rowno = 1;
            // foreach (var kpis in _kpi)
            // {
                if (_kpi != null)
                {
                   foreach (var header in _kpi.header_data)
                    {
                        string kpiname = "ประเด็นยุทธ์ศาสตร์ที่ " + no +" " + header.strategic_issues;
                        no++;
                        ws.Cell(row, 1).SetValue(kpiname).Style.Font.Bold = true;
                       // ws.Cell(row, 2).Value = header.header_1;
                        ws.Cell(row, 1).Style.Fill.BackgroundColor = XLColor.Bisque;
                        ws.Cell(row, 1).Style.Border.OutsideBorder =
                            XLBorderStyleValues.None;
                        ws.Range(ws.Cell(row, 1), ws.Cell(row, 15)).Merge().Style.Fill.BackgroundColor = XLColor.Bisque;
                       
                        ws.Cell(row, 1).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left;
                        ws.Cell(row, 1).Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                        ws.Cell(row,15).Style.Border.RightBorder = XLBorderStyleValues.Thin;
                        row++;
                        foreach (var data1 in header.data)
                        {
                        //     if ( rowno > 1 )
                        //     {
                        //        ws.Range(ws.Cell(row, 1), ws.Cell(row,1)).Merge()
                        //     }
                            ws.Cell(row, 1).Value = rowno;
                            ws.Cell(row, 2).Value = data1.dimension;
                            ws.Cell(row, 3).Value = data1.objective;
                            ws.Cell(row, 4).Value = data1.kpi;
                            ws.Cell(row, 5).Value = data1.unit;
                            ws.Cell(row, 6).Value = data1.plan_value1;
                            ws.Cell(row, 7).Value = data1.plan_value2;
                            ws.Cell(row, 8).Value = data1.plan_value3;
                            ws.Cell(row, 9).Value = data1.plan_value4;
                            ws.Cell(row, 10).Value = data1.plan_value5;
                            ws.Cell(row, 11).Value = data1.plan_value6;
                            ws.Cell(row, 12).Value = data1.director;
                            ws.Cell(row, 13).Value = data1.responsible_person;
                            ws.Cell(row, 14).Value = data1.compiler_reporter;
                            ws.Cell(row, 15).Value = data1.strategy;
                            
                            ws.Range(ws.Cell(row, 1),ws.Cell(row,15)).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Range(ws.Cell(row, 1),ws.Cell(row,15)).Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                            ws.Cell(row, 1).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                            ws.Cell(row, 1).Style.Border.LeftBorder = XLBorderStyleValues.Thin;
                            ws.Cell(row, 1).Style.Border.RightBorder = XLBorderStyleValues.Thin;
                           ws.Cell(row, 2).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                           ws.Cell(row, 3).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                           ws.Cell(row, 4).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                           ws.Cell(row, 5).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                           ws.Cell(row, 6).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                           ws.Cell(row, 7).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                           ws.Cell(row, 8).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                           ws.Cell(row, 9).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                           ws.Cell(row, 10).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                           ws.Cell(row, 11).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                           ws.Cell(row, 12).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                           ws.Cell(row, 13).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                           ws.Cell(row, 14).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                           ws.Cell(row, 15).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                           ws.Range(ws.Cell(row, 1),ws.Cell(row,15)).Style.Alignment.WrapText = true;
                           ws.Range(ws.Cell(row, 1),ws.Cell(row,15)).Style.Font.FontName = "TH Sarabun New";
                           ws.Range(ws.Cell(row, 1),ws.Cell(row,15)).Style.Font.FontSize = 16;
                            row++;
                            rowno++;
                        }
                        //row++;
                    }
                    }
            using (var stream = new MemoryStream())
            {
                workbook.SaveAs(stream);
                var content = stream.ToArray();
                string date = DateTime.Now.ToString("yyyyMMddHHmmss");
                return File(
                    content,
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                    "kpi_financial"+date + ".xlsx");
            }
        }
       
    }
}