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_performance_result : Controller
    {
        readonly Setting _setting;

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

        [HttpPost, Route("reports/kpi_performance_result/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetDetailKpiReport([FromRoute] string type, [FromBody] kpi_performance_results _kpi)
        {
            var workbook = new XLWorkbook();
            var ws = workbook.Worksheets.Add("kpi");
            ws.Range("A1:J1").Merge().Value = "ผลการปฏิบัติงานตัวชี้วัดแผนกลยุทธ์มหาวิทยาลัยเทคโนโลยีราชมงคลรัตนโกสินทร์ ประจำปีงบประมาณ พ.ศ. " + _kpi.budget_year;
            ws.Cell("A1").Style.Font.Bold = true;
            ws.Cell("A1").Style.Alignment.WrapText = true;
            ws.Range("A1:J1").Style.Fill.BackgroundColor = XLColor.Yellow;
            ws.Cell("A2").Value = "ลำดับตัวชี้วัด";
            ws.Cell("A2").Style.Font.Bold = true;
            ws.Cell("B2").Value = "ตัวชี้วัด";
            ws.Cell("B2").Style.Font.Bold = true;
            ws.Cell("B2").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Cell("C2").Value = "หน่วยนับ";
            ws.Cell("C2").Style.Font.Bold = true;
            ws.Cell("C2").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Cell("D2").Value = "ผู้กำกับ";
            ws.Cell("D2").Style.Font.Bold = true;
            ws.Cell("D2").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Cell("E2").Value = "ผู้รับผิดชอบ";
            ws.Cell("E2").Style.Font.Bold = true;
            ws.Cell("E2").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Cell("F2").Value = "ผู้รวบรวมและรายงาน";
            ws.Cell("F2").Style.Font.Bold = true;
            ws.Cell("F2").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Cell("G2").Value = "แผนปี" + _kpi.plan_year.Substring(2,2);
            ws.Cell("G2").Style.Font.Bold = true;
            ws.Cell("G2").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Cell("H2").Value = "ผลลัพธ์";
            ws.Cell("H2").Style.Font.Bold = true;
            ws.Cell("H2").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Cell("I2").Value = "บรรลุ √ ไม่บรรลุ X";
            ws.Cell("I2").Style.Font.Bold = true;
            ws.Cell("I2").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Cell("J2").Value = "หมายเหตุ";
            ws.Cell("J2").Style.Font.Bold = true;
            ws.Cell("J2").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("A2:J2").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("A2:J2").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range("A2:J2").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("A1:J1").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("A1:J1").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("A1:J1").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Column(1).Width = 12;
            ws.Column(2).Width = 40;
            ws.Column(3).Width = 20;
            ws.Column(4).Width = 30;
            ws.Column(5).Width = 30;
            ws.Column(6).Width = 30;
            ws.Column(7).Width = 10;
            ws.Column(8).Width = 10;
            ws.Column(9).Width = 20;
            ws.Column(10).Width = 30;
        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;
            int col = 1;
            int row = 3;
            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.PurpleX11;
                        ws.Cell(row, 1).Style.Border.OutsideBorder =
                            XLBorderStyleValues.None;
                        ws.Range(ws.Cell(row, 1), ws.Cell(row, 10)).Merge().Style.Fill.BackgroundColor = XLColor.PurpleX11;
                       
                        ws.Cell(row, 1).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left;
                        ws.Cell(row, 1).Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                        ws.Cell(row,10).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.name;
                            ws.Cell(row, 3).Value = data1.unit;
                            ws.Cell(row, 4).Value = data1.director;
                            ws.Cell(row, 5).Value = data1.responsible_person;
                            ws.Cell(row, 6).Value = data1.compiler_reporter;
                            ws.Cell(row, 7).Value = data1.value;
                            ws.Cell(row, 8).Value = data1.result;
                            ws.Cell(row, 9).Value = data1.achieve;
                            ws.Cell(row, 10).Value = data1.note;
                            
                            ws.Range(ws.Cell(row, 1),ws.Cell(row,10)).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Range(ws.Cell(row, 1),ws.Cell(row,10)).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.Range(ws.Cell(row, 1),ws.Cell(row,10)).Style.Alignment.WrapText = true;
                           ws.Range(ws.Cell(row, 1),ws.Cell(row,10)).Style.Font.FontName = "TH Sarabun New";
                           ws.Range(ws.Cell(row, 1),ws.Cell(row,10)).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",
                    date + ".xlsx");
            }
        }
       
    }
}