using System;
using System.Collections.Generic;
using System.IO;
using ClosedXML.Excel;
using Microsoft.AspNetCore.Mvc;
using rmutr_report.Models;
using Swashbuckle.AspNetCore.Annotations;

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

        public repaircost(Setting setting)
        {
            this._setting = setting;
        }
        [HttpPost, Route("reports/repair_cost/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetRepairReport([FromRoute] string type,
            [FromBody] repair_cost _repair_cost)
        {
            //var t = _teaching_fee_extra.ToArray();
            {
                var workbook = new XLWorkbook();
                var ws = workbook.Worksheets.Add("ค่าซ่อม");
                ws.Cell("E1").Value = "แบบ ง.2";
                ws.Cell("E1").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("E1").Style.Font.FontSize = 16;
                ws.Cell("E1").Style.Font.Bold = true;
                ws.Cell("E1").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Cell("E1").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Range("A2:E2").Merge().Value = "รายละเอียดค่าซ่อมแซมยานพาหนะและขนส่ง ปีงบประมาณ " + _repair_cost.budget_year;
                ws.Range("A2:E2").Style.Alignment.WrapText = true;
                ws.Range("A2:E2").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("A2:E2").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("A2").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("A2").Style.Font.FontSize = 16;
                ws.Range("A2:E2").Style.Font.Bold = true;
                ws.Range("A3:E3").Merge().Value = "แผน " + _repair_cost.plan+" ผลผลิต "+_repair_cost.product;
                ws.Range("A3:E3").Style.Alignment.WrapText = true;
                ws.Range("A3:E3").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("A3:E3").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("A3").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("A3").Style.Font.FontSize = 16;
                ws.Range("A3:E3").Style.Font.Bold = true;
                 ws.Range("A4:E4").Merge().Value = "หน่วยงาน " + _repair_cost.agency+" พื้นที่ "+_repair_cost.area;
                ws.Range("A4:E4").Style.Alignment.WrapText = true;
                ws.Range("A4:E4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("A4:E4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("A4").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("A4").Style.Font.FontSize = 16;
                ws.Range("A4:E4").Style.Font.Bold = true;
                
                ws.Range("A5:A6").Merge().Value = "ที่";
                ws.Range("A5:A6").Style.Font.FontName = "TH SarabunPSK";
                ws.Range("A5:A6").Style.Font.FontSize = 16;
                ws.Range("A5:A6").Style.Font.Bold = true;
                ws.Range("A5:A6").Style.Alignment.WrapText = true;
                ws.Range("B5:B6").Merge().Value = "ประเภทยานพาหนะและขนส่ง";
                ws.Cell("B5").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("B5").Style.Font.FontSize = 16;
                ws.Cell("B5").Style.Font.Bold = true;
                ws.Cell("B5").Style.Alignment.WrapText = true;
                ws.Cell("C5").Value = "จำนวน";
                ws.Cell("C5").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("C5").Style.Font.FontSize = 16;
                ws.Cell("C5").Style.Font.Bold = true;
                ws.Cell("C5").Style.Alignment.WrapText = true;
                ws.Cell("C6").Value = "(คัน)";
                ws.Cell("C6").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("C6").Style.Font.FontSize = 16;
                ws.Cell("C6").Style.Font.Bold = true;
                ws.Cell("C6").Style.Alignment.WrapText = true;
                ws.Cell("D5").Value = "อัตราค่าซ่อมแซม";
                ws.Cell("D5").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("D5").Style.Font.FontSize = 16;
                ws.Cell("D5").Style.Font.Bold = true;
                ws.Cell("D5").Style.Alignment.WrapText = true;
                ws.Cell("D6").Value = "บาท/คัน/ปี";
                ws.Cell("D6").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("D6").Style.Font.FontSize = 16;
                ws.Cell("D6").Style.Font.Bold = true;
                ws.Cell("D6").Style.Alignment.WrapText = true;
                // ws.Cell("E5").Value = "ค่าซ่อมแซม";
                // ws.Cell("E5").Style.Font.FontName = "TH SarabunPSK";
                // ws.Cell("E5").Style.Font.FontSize = 16;
                // ws.Cell("E5").Style.Font.Bold = true;
                // ws.Cell("E5").Style.Alignment.WrapText = true;
                // ws.Cell("E6").Value = "บาท/ปี";
                // ws.Cell("E6").Style.Font.FontName = "TH SarabunPSK";
                // ws.Cell("E6").Style.Font.FontSize = 16;
                // ws.Cell("E6").Style.Font.Bold = true;
                // ws.Cell("E6").Style.Alignment.WrapText = true;
                
                ws.Range("A5:D6").Style.Fill.BackgroundColor = XLColor.Gainsboro;
                ws.Range("A5:D6").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("A5:D6").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Range("A5:D6").Style.Border.OutsideBorder =
                    XLBorderStyleValues.Thin;
                ws.Range("A5:A6").Style.Border.RightBorder =
                    XLBorderStyleValues.Thin;
                ws.Range("B5:B6").Style.Border.RightBorder =
                    XLBorderStyleValues.Thin;
                ws.Range("C5:C6").Style.Border.RightBorder =
                    XLBorderStyleValues.Thin;
                ws.Range("D5:D6").Style.Border.RightBorder =
                    XLBorderStyleValues.Thin;
             
                ws.Column(1).Width = 10;
                ws.Column(2).Width = 40;
                ws.Column(3).Width = 10;
                ws.Column(4).Width = 15;
                //ws.Column(5).Width = 15;
               
                int row = 7;
                //int row2 = 9;
                //ws.Cell(row, 3).SetDataType(XLDataType.Number);
                if (_repair_cost != null)
                {
                     foreach (var house in _repair_cost.data)
                     {
                         if (house.type_vehicle_transport != "รวม")
                         {
                             ws.Cell(row, 1).Value = house.no;
                             ws.Cell(row, 2).Value = house.type_vehicle_transport;
                             ws.Cell(row, 3).Value = house.quantity;
                             ws.Cell(row, 4).Value = house.repair_rate;
                             //ws.Cell(row, 5).Value = house.amount;

                             ws.Range(ws.Cell(row, 1), ws.Cell(row, 4)).Style.Font.FontName =
                                 "TH SarabunPSK";
                             ws.Range(ws.Cell(row, 1), ws.Cell(row, 4)).Style.Font.FontSize = 16;
                             ws.Range(ws.Cell(row, 1), ws.Cell(row, 4)).Style.Alignment.WrapText = true;
                             ws.Range(ws.Cell(row, 1), ws.Cell(row, 4)).Style.Alignment.Vertical =
                                 XLAlignmentVerticalValues.Center;

                             ws.Range(ws.Cell(row, 1), ws.Cell(row, 4)).Style.Border.OutsideBorder =
                                 XLBorderStyleValues.Thin;
                             ws.Range(ws.Cell(row, 1), ws.Cell(row, 4)).Style.Border.RightBorder =
                                 XLBorderStyleValues.Thin;
                             ws.Cell(row, 1).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                             ws.Cell(row, 2).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left;
                             ws.Cell(row, 3).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                             ws.Cell(row, 4).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                             //ws.Cell(row, 5).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;

                             ws.Cell(row, 4).SetDataType(XLDataType.Number);
                             ws.Cell(row, 4).Style.NumberFormat.SetFormat("#,#");
                             row++;
                         }

                         if (house.type_vehicle_transport == "รวม")
                            {
                           ws.Cell(row, 1).Value = house.no;
                            ws.Cell(row, 2).Value = house.type_vehicle_transport;
                            ws.Cell(row, 3).Value = house.quantity;
                            ws.Cell(row, 4).Value = house.repair_rate;
                            //ws.Cell(row, 5).Value = house.amount;
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 4)).Style.Font.FontName =
                                "TH SarabunPSK";
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 4)).Style.Font.FontSize = 16;
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 4)).Style.Alignment.WrapText = true;
                            ws.Cell(row, 2).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Cell(row, 1).Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                            ws.Cell(row, 2).Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                            ws.Cell(row, 3).Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                            ws.Cell(row, 4).Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                            //ws.Cell(row, 5).Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                            ws.Cell(row, 1).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Cell(row, 3).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Cell(row, 4).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            //ws.Cell(row, 5).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 2)).Style.Border.LeftBorder =
                                XLBorderStyleValues.None;
                            ws.Range(ws.Cell(row, 2), ws.Cell(row, 4)).Style.Border.RightBorder =
                                XLBorderStyleValues.Thin;
                            ws.Cell(row, 1).Style.Border.RightBorder =
                                XLBorderStyleValues.None;
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 4)).Style.Border.OutsideBorder =
                                XLBorderStyleValues.Thin;
                           ws.Cell(row, 4).SetDataType(XLDataType.Number);
                            ws.Cell(row, 4).Style.NumberFormat.SetFormat("#,#");
                            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",
                        "repair_cost_" + date + ".xlsx");
                }
            }
        }

        [HttpPost, Route("reports/fuel_cost/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetFuelReport([FromRoute] string type,
            [FromBody] repair_cost _repair_cost)
        {
            //var t = _teaching_fee_extra.ToArray();
            {
                var workbook = new XLWorkbook();
                var ws = workbook.Worksheets.Add("ค่าน้ำมันเชื้อเพลิง");
                ws.Cell("E1").Value = "แบบ ง.2";
                ws.Cell("E1").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("E1").Style.Font.FontSize = 16;
                ws.Cell("E1").Style.Font.Bold = true;
                ws.Cell("E1").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Cell("E1").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Range("A2:E2").Merge().Value =
                    "รายละเอียดค่าน้ำมันเชื้อเพลิง ปีงบประมาณ " + _repair_cost.budget_year;
                ws.Range("A2:E2").Style.Alignment.WrapText = true;
                ws.Range("A2:E2").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("A2:E2").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("A2").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("A2").Style.Font.FontSize = 16;
                ws.Range("A2:E2").Style.Font.Bold = true;
                ws.Range("A3:E3").Merge().Value = "แผน " + _repair_cost.plan + " ผลผลิต " + _repair_cost.product;
                ws.Range("A3:E3").Style.Alignment.WrapText = true;
                ws.Range("A3:E3").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("A3:E3").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("A3").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("A3").Style.Font.FontSize = 16;
                ws.Range("A3:E3").Style.Font.Bold = true;
                ws.Range("A4:E4").Merge().Value = "หน่วยงาน " + _repair_cost.agency + " พื้นที่ " + _repair_cost.area;
                ws.Range("A4:E4").Style.Alignment.WrapText = true;
                ws.Range("A4:E4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("A4:E4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("A4").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("A4").Style.Font.FontSize = 16;
                ws.Range("A4:E4").Style.Font.Bold = true;

                ws.Range("A5:A6").Merge().Value = "ที่";
                ws.Range("A5:A6").Style.Font.FontName = "TH SarabunPSK";
                ws.Range("A5:A6").Style.Font.FontSize = 16;
                ws.Range("A5:A6").Style.Font.Bold = true;
                ws.Range("A5:A6").Style.Alignment.WrapText = true;
                ws.Range("B5:B6").Merge().Value = "ประเภทยานพาหนะและขนส่ง";
                ws.Cell("B5").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("B5").Style.Font.FontSize = 16;
                ws.Cell("B5").Style.Font.Bold = true;
                ws.Cell("B5").Style.Alignment.WrapText = true;
                ws.Cell("C5").Value = "จำนวน";
                ws.Cell("C5").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("C5").Style.Font.FontSize = 16;
                ws.Cell("C5").Style.Font.Bold = true;
                ws.Cell("C5").Style.Alignment.WrapText = true;
                ws.Cell("C6").Value = "(คัน)";
                ws.Cell("C6").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("C6").Style.Font.FontSize = 16;
                ws.Cell("C6").Style.Font.Bold = true;
                ws.Cell("C6").Style.Alignment.WrapText = true;
                ws.Cell("D5").Value = "อัตราค่าน้ำมันเชื้อเพลิง";
                ws.Cell("D5").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("D5").Style.Font.FontSize = 16;
                ws.Cell("D5").Style.Font.Bold = true;
                ws.Cell("D5").Style.Alignment.WrapText = true;
                ws.Cell("D6").Value = "บาท/คัน/ปี";
                ws.Cell("D6").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("D6").Style.Font.FontSize = 16;
                ws.Cell("D6").Style.Font.Bold = true;
                ws.Cell("D6").Style.Alignment.WrapText = true;
                // ws.Cell("E5").Value = "ค่าน้ำมันเชื้อเพลิง";
                // ws.Cell("E5").Style.Font.FontName = "TH SarabunPSK";
                // ws.Cell("E5").Style.Font.FontSize = 16;
                // ws.Cell("E5").Style.Font.Bold = true;
                // ws.Cell("E5").Style.Alignment.WrapText = true;
                // ws.Cell("E6").Value = "บาท/ปี";
                // ws.Cell("E6").Style.Font.FontName = "TH SarabunPSK";
                // ws.Cell("E6").Style.Font.FontSize = 16;
                // ws.Cell("E6").Style.Font.Bold = true;
                // ws.Cell("E6").Style.Alignment.WrapText = true;

                ws.Range("A5:D6").Style.Fill.BackgroundColor = XLColor.Gainsboro;
                ws.Range("A5:D6").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("A5:D6").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Range("A5:D6").Style.Border.OutsideBorder =
                    XLBorderStyleValues.Thin;
                ws.Range("A5:A6").Style.Border.RightBorder =
                    XLBorderStyleValues.Thin;
                ws.Range("B5:B6").Style.Border.RightBorder =
                    XLBorderStyleValues.Thin;
                ws.Range("C5:C6").Style.Border.RightBorder =
                    XLBorderStyleValues.Thin;
                ws.Range("D5:D6").Style.Border.RightBorder =
                    XLBorderStyleValues.Thin;

                ws.Column(1).Width = 10;
                ws.Column(2).Width = 40;
                ws.Column(3).Width = 10;
                ws.Column(4).Width = 25;
                //ws.Column(5).Width = 25;

                int row = 7;
                //int row2 = 9;
                //ws.Cell(row, 3).SetDataType(XLDataType.Number);
                if (_repair_cost != null)
                {
                    foreach (var house in _repair_cost.data)
                    {
                        if (house.type_vehicle_transport != "รวม")
                        {
                            ws.Cell(row, 1).Value = house.no;
                            ws.Cell(row, 2).Value = house.type_vehicle_transport;
                            ws.Cell(row, 3).Value = house.quantity;
                            ws.Cell(row, 4).Value = house.repair_rate;
                            //ws.Cell(row, 5).Value = house.amount;

                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 4)).Style.Font.FontName =
                                "TH SarabunPSK";
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 4)).Style.Font.FontSize = 16;
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 4)).Style.Alignment.WrapText = true;
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 4)).Style.Alignment.Vertical =
                                XLAlignmentVerticalValues.Center;

                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 4)).Style.Border.OutsideBorder =
                                XLBorderStyleValues.Thin;
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 4)).Style.Border.RightBorder =
                                XLBorderStyleValues.Thin;
                            ws.Cell(row, 1).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Cell(row, 2).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left;
                            ws.Cell(row, 3).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Cell(row, 4).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            //ws.Cell(row, 5).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 4)).Style.Alignment.Vertical =
                                XLAlignmentVerticalValues.Center;

                            ws.Cell(row, 4).SetDataType(XLDataType.Number);
                            ws.Cell(row, 4).Style.NumberFormat.SetFormat("#,#");
                            row++;
                        }

                        if (house.type_vehicle_transport == "รวม")
                        {
                            ws.Cell(row, 1).Value = house.no;
                            ws.Cell(row, 2).Value = house.type_vehicle_transport;
                            ws.Cell(row, 3).Value = house.quantity;
                            ws.Cell(row, 4).Value = house.repair_rate;
                            //ws.Cell(row, 5).Value = house.amount;
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 4)).Style.Font.FontName =
                                "TH SarabunPSK";
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 4)).Style.Font.FontSize = 16;
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 4)).Style.Alignment.WrapText = true;
                            ws.Cell(row, 2).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Cell(row, 1).Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                            ws.Cell(row, 2).Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                            ws.Cell(row, 3).Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                            ws.Cell(row, 4).Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                            //ws.Cell(row, 5).Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                            ws.Cell(row, 1).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Cell(row, 3).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Cell(row, 4).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            //ws.Cell(row, 5).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 2)).Style.Border.LeftBorder =
                                XLBorderStyleValues.None;
                            ws.Range(ws.Cell(row, 2), ws.Cell(row, 4)).Style.Border.RightBorder =
                                XLBorderStyleValues.Thin;
                            ws.Cell(row, 1).Style.Border.RightBorder =
                                XLBorderStyleValues.None;
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 4)).Style.Border.OutsideBorder =
                                XLBorderStyleValues.Thin;
                          ws.Cell(row, 4).SetDataType(XLDataType.Number);
                            ws.Cell(row, 4).Style.NumberFormat.SetFormat("#,#");
                            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",
                        "fuel_cost_" + date + ".xlsx");
                }
            }
        }
    }
}