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("สำหรับรายงานง.1")]
    public class houseforrent : Controller
    {
        readonly Setting _setting;

        public houseforrent(Setting setting)
        {
            this._setting = setting;
        }
        [HttpPost, Route("reports/house_for_rent/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetRentReport([FromRoute] string type,
            [FromBody] house_for_rent _house_for_rent)
        {
            //var t = _teaching_fee_extra.ToArray();
            {
                var workbook = new XLWorkbook();
                var ws = workbook.Worksheets.Add("ค่าเช่าบ้าน");
                ws.Cell("F1").Value = "แบบ ง.1";
                ws.Cell("F1").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("F1").Style.Font.FontSize = 16;
                ws.Cell("F1").Style.Font.Bold = true;
                ws.Cell("F1").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Cell("F1").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Range("A2:F2").Merge().Value = "รายละเอียดประกอบการเสนอของบประมาณรายการ ค่าเช่าบ้าน ปีงบประมาณ" + _house_for_rent.budget_year;
                ws.Range("A2:F2").Style.Alignment.WrapText = true;
                ws.Range("A2:F2").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("A2:F2").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("A2").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("A2").Style.Font.FontSize = 16;
                ws.Range("A2:F2").Style.Font.Bold = true;
                ws.Range("A3:F3").Merge().Value = "แผน" + _house_for_rent.plan+" ผลผลิต "+_house_for_rent.product;
                ws.Range("A3:F3").Style.Alignment.WrapText = true;
                ws.Range("A3:F3").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("A3:F3").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("A3").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("A3").Style.Font.FontSize = 16;
                ws.Range("A3:F3").Style.Font.Bold = true;
                ws.Cell("A4").Value = "ลำดับ";
                ws.Cell("A4").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("A4").Style.Font.FontSize = 16;
                ws.Cell("A4").Style.Font.Bold = true;
                ws.Cell("A4").Style.Alignment.WrapText = true;
                ws.Cell("A5").Value = "ที่";
                ws.Cell("A5").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("A5").Style.Font.FontSize = 16;
                ws.Cell("A5").Style.Font.Bold = true;
                ws.Cell("A5").Style.Alignment.WrapText = true;
                ws.Range("B4:B5").Merge().Value = "ชื่อ - สกุล";
                ws.Cell("B4").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("B4").Style.Font.FontSize = 16;
                ws.Cell("B4").Style.Font.Bold = true;
                ws.Cell("B4").Style.Alignment.WrapText = true;
                ws.Cell("C4").Value = "ระดับ";
                ws.Cell("C4").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("C4").Style.Font.FontSize = 16;
                ws.Cell("C4").Style.Font.Bold = true;
                ws.Cell("C4").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("D4").Value = "อัตราเงินเดือน";
                ws.Cell("D4").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("D4").Style.Font.FontSize = 16;
                ws.Cell("D4").Style.Font.Bold = true;
                ws.Cell("D4").Style.Alignment.WrapText = true;
                ws.Cell("D5").Value = "ณ "+_house_for_rent.salary_date;
                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("E4").Value = "ค่าเช่าบ้าน";
                ws.Cell("E4").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("E4").Style.Font.FontSize = 16;
                ws.Cell("E4").Style.Font.Bold = true;
                ws.Cell("E4").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("F4").Value = "ค่าเช่าบ้าน";
                ws.Cell("F4").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("F4").Style.Font.FontSize = 16;
                ws.Cell("F4").Style.Font.Bold = true;
                ws.Cell("F4").Style.Alignment.WrapText = true;
                ws.Cell("F5").Value = "ต่อปี";
                ws.Cell("F5").Style.Font.FontName = "TH SarabunPSK";
                ws.Cell("F5").Style.Font.FontSize = 16;
                ws.Cell("F5").Style.Font.Bold = true;
                ws.Cell("F5").Style.Alignment.WrapText = true;
                ws.Range("A4:F5").Style.Fill.BackgroundColor = XLColor.SeaShell;
                ws.Range("A4:F5").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("A4:F5").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Range("A4:F5").Style.Border.OutsideBorder =
                    XLBorderStyleValues.Thin;
                ws.Range("A4:A5").Style.Border.RightBorder =
                    XLBorderStyleValues.Thin;
                ws.Range("B4:B5").Style.Border.RightBorder =
                    XLBorderStyleValues.Thin;
                ws.Range("C4:C5").Style.Border.RightBorder =
                    XLBorderStyleValues.Thin;
                ws.Range("D4:D5").Style.Border.RightBorder =
                    XLBorderStyleValues.Thin;
                ws.Range("E4:E5").Style.Border.RightBorder =
                    XLBorderStyleValues.Thin;
                ws.Column(1).Width = 10;
                ws.Column(2).Width = 40;
                ws.Column(3).Width = 25;
                ws.Column(4).Width = 15;
                ws.Column(5).Width = 15;
                ws.Column(6).Width = 15;
               
                int row = 6;
                //int row2 = 9;
                //ws.Cell(row, 3).SetDataType(XLDataType.Number);
                if (_house_for_rent != null)
                {
                     foreach (var house in _house_for_rent.data)
                    {
                            ws.Cell(row, 1).Value = house.no;
                            ws.Cell(row, 2).Value = house.display_name_th;
                            ws.Cell(row, 3).Value = house.level;
                            ws.Cell(row, 4).Value = house.salary_rate;
                            ws.Cell(row, 5).Value = house.per_month;
                            ws.Cell(row, 6).Value = house.per_year;
                          
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 6)).Style.Font.FontName =
                                "TH SarabunPSK";
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 6)).Style.Font.FontSize = 16;
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 6)).Style.Alignment.WrapText = true;
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 6)).Style.Alignment.Vertical =
                                XLAlignmentVerticalValues.Center;

                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 6)).Style.Border.OutsideBorder =
                                XLBorderStyleValues.Thin;
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 6)).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.Left;
                            ws.Cell(row, 4).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Cell(row, 5).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                            ws.Cell(row, 6).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                           
                            ws.Range(ws.Cell(row, 4), ws.Cell(row, 6)).SetDataType(XLDataType.Number);
                            ws.Range(ws.Cell(row, 4), ws.Cell(row, 6)).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",
                        "house_" + date + ".xlsx");
                }
            }
        }
    }
}