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

namespace rmutr_report.Controllers
{
    [SwaggerTag("สำหรับรายงานค่าใช้จ่ายในการดำเนินงาน")]
    public class Expenses : Controller
    {
        readonly Setting _setting;

        public Expenses(Setting setting)
        {
            this._setting = setting;
        }
        [HttpPost, Route("reports/operating_expenses/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetExpensesReport([FromRoute] string type,
            [FromBody] operating_expenses _operating_expenses)
        {
            {
                var workbook = new XLWorkbook();
                var ws = workbook.Worksheets.Add("ค่าใช้จ่ายดำเนินงาน");
                ws.Range("A1:D1").Merge().Value = "ผลผลิต " + _operating_expenses.product;
                ws.Range("A1:D1").Style.Alignment.WrapText = true;
                ws.Range("A1:D1").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Range("A1:D1").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("A1").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("A1").Style.Font.FontSize = 14;
                ws.Range("A1:D1").Style.Font.Bold = true;
                ws.Cell("A2").Value = "(1 ชุด ต่อ 1 ผลผลิต)";
                ws.Cell("A2").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("A2").Style.Font.FontSize = 14;
                ws.Cell("A2").Style.Font.Bold = true;
                ws.Cell("A2").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left;
                ws.Cell("A2").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("D2").Value = "หน่วย : บาท";
                ws.Cell("D2").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("D2").Style.Font.FontSize = 14;
                ws.Cell("D2").Style.Font.Bold = true;
                ws.Cell("D2").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                ws.Cell("D2").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("A3").Value = "ลำดับที่";
                ws.Cell("A3").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("A3").Style.Font.FontSize = 14;
                ws.Cell("A3").Style.Font.Bold = true;
                ws.Cell("A3").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Cell("A3").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("A3").Style.Border.TopBorder = XLBorderStyleValues.Thin;
                ws.Cell("A3").Style.Border.RightBorder = XLBorderStyleValues.Thin;
                ws.Cell("A4").Style.Border.TopBorder = XLBorderStyleValues.Thin;
                ws.Cell("A4").Style.Border.RightBorder = XLBorderStyleValues.Thin;
                ws.Cell("B3").Value = "งบรายจ่าย";
                ws.Cell("B3").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("B3").Style.Font.FontSize = 14;
                ws.Cell("B3").Style.Font.Bold = true;
                ws.Cell("B3").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Cell("B3").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("B3").Style.Border.TopBorder = XLBorderStyleValues.Thin;
                ws.Cell("B3").Style.Border.RightBorder = XLBorderStyleValues.Thin;
                ws.Cell("B4").Style.Border.TopBorder = XLBorderStyleValues.Thin;
                ws.Cell("B4").Style.Border.RightBorder = XLBorderStyleValues.Thin;
                ws.Cell("C3").Value = "คำขอตั้ง";
                ws.Cell("C3").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("C3").Style.Font.FontSize = 14;
                ws.Cell("C3").Style.Font.Bold = true;
                ws.Cell("C3").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Cell("C3").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("C3").Style.Border.TopBorder = XLBorderStyleValues.Thin;
                ws.Cell("C3").Style.Border.RightBorder = XLBorderStyleValues.Thin;
                ws.Cell("C4").Style.Border.TopBorder = XLBorderStyleValues.Thin;
                ws.Cell("C4").Style.Border.RightBorder = XLBorderStyleValues.Thin;
                ws.Cell("D3").Value = "สรุปคำชี้แจง";
                ws.Cell("D3").Style.Font.FontName = "TH Sarabun New";
                ws.Cell("D3").Style.Font.FontSize = 14;
                ws.Cell("D3").Style.Font.Bold = true;
                ws.Cell("D3").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                ws.Cell("D3").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                ws.Cell("D3").Style.Border.TopBorder = XLBorderStyleValues.Thin;
                ws.Cell("D3").Style.Border.RightBorder = XLBorderStyleValues.Thin;
                ws.Cell("D4").Style.Border.TopBorder = XLBorderStyleValues.Thin;
                ws.Cell("D4").Style.Border.RightBorder = XLBorderStyleValues.Thin;
                ws.Column(1).Width = 10;
                ws.Column(2).Width = 40;
                ws.Column(3).Width = 30;
                ws.Column(4).Width = 50;
                int row = 5;
                ws.Cell(row, 3).SetDataType(XLDataType.Number);
                    if (_operating_expenses != null)
                    {
                        ws.Cell("C4").Value = "ปี "+_operating_expenses.request_year;
                        ws.Cell("C4").Style.Alignment.WrapText = true;
                        ws.Cell("C4").Style.Font.FontName = "TH Sarabun New";
                        ws.Cell("C4").Style.Font.FontSize = 14;
                        ws.Cell("C4").Style.Font.Bold = true;
                        ws.Cell("C4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                        ws.Cell("C4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
                       foreach (var expenses in _operating_expenses.data)
                        {
                            ws.Cell(row, 1).Value = "'"+expenses.no;
                            ws.Cell(row, 2).Value = expenses.expenditure_budget;
                            ws.Cell(row, 3).Value = expenses.amount;
                            ws.Cell(row, 4).Value = expenses.clarification_summary;
                           
                           ws.Range(ws.Cell(row, 1), ws.Cell(row, 4)).Style.Font.FontName =
                                "TH Sarabun New";
                            ws.Range(ws.Cell(row, 1), ws.Cell(row, 4)).Style.Font.FontSize = 14;
                            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.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.Right;
                            ws.Cell(row, 4).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left;
                            ws.Range(ws.Cell(5, 1), ws.Cell(5, 4)).Style.Font.Bold = true;
                            ws.Range(ws.Cell(5, 1), ws.Cell(5, 4)).Style.Border.BottomBorder = XLBorderStyleValues.Double;
                            ws.Range(ws.Cell(6, 1), ws.Cell(6, 4)).Style.Font.Bold = true;
                            //ws.Cell(row, 3).Style.NumberFormat.NumberFormatId = 2;
                            //ws.Cell(row, 3).DataType = XLDataType.Number;
                            //ws.Cell(row,3).SetDataType(XLDataType.Number);
                            ws.Cell(row,3).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",
                        "operatingexpenses_" + date + ".xlsx");
                }
            }
        }
    }
}