using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using ClosedXML.Excel;
using FastReport;
using FastReport.Export.Csv;
using FastReport.Export.Mht;
using FastReport.Export.OoXML;
using FastReport.Export.Pdf;
using Microsoft.AspNetCore.Mvc;
using rmutr_report.Models;
using rmutr_report.Models.Hr;
using rmutr_report.Models.Personnel;
using Swashbuckle.AspNetCore.Annotations;

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

        public PersonnelController(Setting setting)
        {
            this._setting = setting;
        }

        [HttpPost, Route("reports/personnel_summary/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetHrReport([FromRoute] string type, [FromBody] personnel_summary personnel_summarys)
        {
            foreach (var v in personnel_summarys.personnel_types)
            {
                if (v.count != null)
                {
                    var total = personnel_summarys.personnel_types.Select(r => r.count).Sum(t => t.Value);
                    personnel_summarys.total_pertype = total;
                }
                else
                {
                    return null;
                }
            }

            var personnel_summaryss = new List<personnel_summary>() {personnel_summarys};

            Report report = new Report();
            report.Load(_setting.report_path + "personnel_summary.frx");
            report.RegisterData(personnel_summaryss, "personnel_summary");
            report.Prepare();

            MemoryStream stream = new MemoryStream();
            switch (type)
            {
                case "pdf":
                    PDFExport pdf = new PDFExport();
                    report.Export(pdf, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/pdf");

                case "xls":
                case "xlsx":
                    Excel2007Export excel = new Excel2007Export();
                    report.Export(excel, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/vnd.ms-excel");
                    break;
                case "mht":
                    MHTExport mht = new MHTExport();
                    report.Export(mht, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "multipart/related");
                    break;
                case "csv":
                    CSVExport csv = new CSVExport();
                    report.Export(csv, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "text/csv");
                    break;
            }

            return Ok();
        }

        [HttpPost, Route("reports/man_power/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetManReport([FromRoute] string type, [FromBody] List<man_power> man_powers)
        {
            //var personnel_summaryss = new List<personnel_summary>() { personnel_summarys };

            Report report = new Report();
            report.Load(_setting.report_path + "man_power.frx");
            report.RegisterData(man_powers, "man_power");
            report.Prepare();

            MemoryStream stream = new MemoryStream();
            switch (type)
            {
                case "pdf":
                    PDFExport pdf = new PDFExport();
                    report.Export(pdf, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/pdf");

                case "xls":
                case "xlsx":
                    Excel2007Export excel = new Excel2007Export();
                    report.Export(excel, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/vnd.ms-excel");
                    break;
                case "mht":
                    MHTExport mht = new MHTExport();
                    report.Export(mht, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "multipart/related");
                    break;
                case "csv":
                    CSVExport csv = new CSVExport();
                    report.Export(csv, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "text/csv");
                    break;
            }

            return Ok();
        }

        [HttpPost, Route("reports/data_line_support/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetDataLineReport([FromRoute] string type, [FromBody] data_line_support data_line_supports)
        {
            var data_line_supportss = new List<data_line_support>() {data_line_supports};

            Report report = new Report();
            report.Load(_setting.report_path + "data_line_support.frx");
            report.RegisterData(data_line_supportss, "data_line_support");
            report.Prepare();

            MemoryStream stream = new MemoryStream();
            switch (type)
            {
                case "pdf":
                    PDFExport pdf = new PDFExport();
                    report.Export(pdf, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/pdf");

                case "xls":
                case "xlsx":
                    Excel2007Export excel = new Excel2007Export();
                    report.Export(excel, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/vnd.ms-excel");
                    break;
                case "mht":
                    MHTExport mht = new MHTExport();
                    report.Export(mht, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "multipart/related");
                    break;
                case "csv":
                    CSVExport csv = new CSVExport();
                    report.Export(csv, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "text/csv");
                    break;
            }

            return Ok();
        }

        [HttpPost, Route("reports/data_line_academic/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetDataLineAcaReport([FromRoute] string type,
            [FromBody] data_line_academic data_line_academicss)
        {
            var _data_line_academics = new List<data_line_academic>() {data_line_academicss};

            Report report = new Report();
            report.Load(_setting.report_path + "data_line_academic.frx");
            report.RegisterData(_data_line_academics, "data_line_academic");
            report.Prepare();

            MemoryStream stream = new MemoryStream();
            switch (type)
            {
                case "pdf":
                    PDFExport pdf = new PDFExport();
                    report.Export(pdf, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/pdf");

                case "xls":
                case "xlsx":
                    Excel2007Export excel = new Excel2007Export();
                    report.Export(excel, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/vnd.ms-excel");
                    break;
                case "mht":
                    MHTExport mht = new MHTExport();
                    report.Export(mht, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "multipart/related");
                    break;
                case "csv":
                    CSVExport csv = new CSVExport();
                    report.Export(csv, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "text/csv");
                    break;
            }

            return Ok();
        }

        [HttpPost, Route("reports/qualifications_teacher/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetQualificationsTeacherReport([FromRoute] string type,
            [FromBody] qualifications_teacher qualifications_teachers)
        {
            var qualifications_teacherss = new List<qualifications_teacher>() {qualifications_teachers};

            Report report = new Report();
            report.Load(_setting.report_path + "qualifications_teacher.frx");
            report.RegisterData(qualifications_teacherss, "qualifications_teacher");
            report.Prepare();

            MemoryStream stream = new MemoryStream();
            switch (type)
            {
                case "pdf":
                    PDFExport pdf = new PDFExport();
                    report.Export(pdf, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/pdf");

                case "xls":
                case "xlsx":
                    Excel2007Export excel = new Excel2007Export();
                    report.Export(excel, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/vnd.ms-excel");
                    break;
                case "mht":
                    MHTExport mht = new MHTExport();
                    report.Export(mht, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "multipart/related");
                    break;
                case "csv":
                    CSVExport csv = new CSVExport();
                    report.Export(csv, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "text/csv");
                    break;
            }

            return Ok();
        }

        [HttpPost, Route("reports/academic_position/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetAcademicPositionReport([FromRoute] string type,
            [FromBody] academic_position _academic_position)
        {
            var academic_positions = new List<academic_position>() {_academic_position};

            Report report = new Report();
            report.Load(_setting.report_path + "academic_position.frx");
            report.RegisterData(academic_positions, "academic_position");
            report.Prepare();

            MemoryStream stream = new MemoryStream();
            switch (type)
            {
                case "pdf":
                    PDFExport pdf = new PDFExport();
                    report.Export(pdf, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/pdf");

                case "xls":
                case "xlsx":
                    Excel2007Export excel = new Excel2007Export();
                    report.Export(excel, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/vnd.ms-excel");
                    break;
                case "mht":
                    MHTExport mht = new MHTExport();
                    report.Export(mht, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "multipart/related");
                    break;
                case "csv":
                    CSVExport csv = new CSVExport();
                    report.Export(csv, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "text/csv");
                    break;
            }

            return Ok();
        }

        [HttpPost, Route("reports/postponement_compensation/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetPostponementCompensationReport([FromRoute] string type,
            [FromBody] postponement_compensation postponement_compensations)
        {
            if (postponement_compensations.get_promoted == true)
            {
                postponement_compensations.check_get_promoted = "/";
                postponement_compensations.reason = "";
            }

            if (postponement_compensations.get_promoted == false)
            {
                postponement_compensations.check_get_promoted = "";
               
            }

            if (postponement_compensations.not_get_promoted == true)
            {
                postponement_compensations.check_not_get_promoted = "/";
            }

            if (postponement_compensations.not_get_promoted == false)
            {
                postponement_compensations.check_not_get_promoted = "";
            }

            string NumberText1 = postponement_compensations.data_date;
            var str1 =
                NumberText1.Replace('0', '๐').Replace('1', '๑').Replace('2', '๒').Replace('3', '๓')
                    .Replace('4', '๔').Replace('5', '๕').Replace('6', '๖').Replace('7', '๗').Replace('8', '๘')
                    .Replace('9', '๙');
            postponement_compensations.data_date = str1;
            string NumberText2 = postponement_compensations.start_date;
            var str2 =
                NumberText2.Replace('0', '๐').Replace('1', '๑').Replace('2', '๒').Replace('3', '๓')
                    .Replace('4', '๔').Replace('5', '๕').Replace('6', '๖').Replace('7', '๗').Replace('8', '๘')
                    .Replace('9', '๙');
            postponement_compensations.start_date = str2;
            string NumberText3 = postponement_compensations.end_date;
            var str3 =
                NumberText3.Replace('0', '๐').Replace('1', '๑').Replace('2', '๒').Replace('3', '๓')
                    .Replace('4', '๔').Replace('5', '๕').Replace('6', '๖').Replace('7', '๗').Replace('8', '๘')
                    .Replace('9', '๙');
            postponement_compensations.end_date = str3;
            string NumberText4 = postponement_compensations.base_calculation_date;
            var str4 =
                NumberText4.Replace('0', '๐').Replace('1', '๑').Replace('2', '๒').Replace('3', '๓')
                    .Replace('4', '๔').Replace('5', '๕').Replace('6', '๖').Replace('7', '๗').Replace('8', '๘')
                    .Replace('9', '๙');
            postponement_compensations.base_calculation_date = str4;
            string NumberText5 = postponement_compensations.director_date;
            var str5 =
                NumberText5.Replace('0', '๐').Replace('1', '๑').Replace('2', '๒').Replace('3', '๓')
                    .Replace('4', '๔').Replace('5', '๕').Replace('6', '๖').Replace('7', '๗').Replace('8', '๘')
                    .Replace('9', '๙');
            postponement_compensations.director_date = str5;
            foreach (var data in postponement_compensations.data)
            {
                if (data.base_calculation != null)
                {
                    string NumberText = data.base_calculation.ToString();
                    var str =
                        NumberText.Replace('0', '๐').Replace('1', '๑').Replace('2', '๒').Replace('3', '๓')
                            .Replace('4', '๔').Replace('5', '๕').Replace('6', '๖').Replace('7', '๗').Replace('8', '๘')
                            .Replace('9', '๙');
                    if (str.Length == 4)
                    {
                        for (int i = 1; i <= str.Length - 1; i += 4)
                        {
                            postponement_compensations.money1 = str.Insert(i, ",");
                        }
                    }
                    else if (str.Length == 5)
                    {
                        for (int i = 2; i <= str.Length - 1; i += 5)
                        {
                            postponement_compensations.money1 = str.Insert(i, ",");
                        }
                    }
                    else
                    {
                        postponement_compensations.money1 = str;
                    }
                }

                if (data.compensation_slide != null)
                {
                    string NumberText = data.compensation_slide.ToString();
                    var str =
                        NumberText.Replace('0', '๐').Replace('1', '๑').Replace('2', '๒').Replace('3', '๓')
                            .Replace('4', '๔').Replace('5', '๕').Replace('6', '๖').Replace('7', '๗').Replace('8', '๘')
                            .Replace('9', '๙');
                    if (str.Length == 4)
                    {
                        for (int i = 1; i <= str.Length - 1; i += 4)
                        {
                            postponement_compensations.money2 = str.Insert(i, ",");
                        }
                    }
                    else if (str.Length == 5)
                    {
                        for (int i = 2; i <= str.Length - 1; i += 5)
                        {
                            postponement_compensations.money2 = str.Insert(i, ",");
                        }
                    }
                    else
                    {
                        postponement_compensations.money2 = str;
                    }
                }

                if (data.compensation_receive != null)
                {
                    string NumberText = data.compensation_receive.ToString();
                    var str =
                        NumberText.Replace('0', '๐').Replace('1', '๑').Replace('2', '๒').Replace('3', '๓')
                            .Replace('4', '๔').Replace('5', '๕').Replace('6', '๖').Replace('7', '๗').Replace('8', '๘')
                            .Replace('9', '๙');
                    if (str.Length == 4)
                    {
                        for (int i = 1; i <= str.Length - 1; i += 4)
                        {
                            postponement_compensations.money3 = str.Insert(i, ",");
                        }
                    }
                    else if (str.Length == 5)
                    {
                        for (int i = 2; i <= str.Length - 1; i += 5)
                        {
                            postponement_compensations.money3 = str.Insert(i, ",");
                        }
                    }
                    else
                    {
                        postponement_compensations.money3 = str;
                    }
                }

                string NumberText6 = data.percentage.ToString();
                var str6 =
                    NumberText6.Replace('0', '๐').Replace('1', '๑').Replace('2', '๒').Replace('3', '๓')
                        .Replace('4', '๔').Replace('5', '๕').Replace('6', '๖').Replace('7', '๗').Replace('8', '๘')
                        .Replace('9', '๙');
                postponement_compensations.percentage = str6;
            }

            var _postponement_compensations = new List<postponement_compensation>() {postponement_compensations};

            Report report = new Report();
            report.Load(_setting.report_path + "postponement_compensation.frx");
            report.RegisterData(_postponement_compensations, "postponement_compensation");
            report.Prepare();

            MemoryStream stream = new MemoryStream();
            switch (type)
            {
                case "pdf":
                    PDFExport pdf = new PDFExport();
                    report.Export(pdf, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/pdf");

                case "xls":
                case "xlsx":
                    Excel2007Export excel = new Excel2007Export();
                    report.Export(excel, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "application/vnd.ms-excel");
                    break;
                case "mht":
                    MHTExport mht = new MHTExport();
                    report.Export(mht, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "multipart/related");
                    break;
                case "csv":
                    CSVExport csv = new CSVExport();
                    report.Export(csv, stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    return File(stream, "text/csv");
                    break;
            }

            return Ok();
        }
        [HttpPost, Route("reports/personnel_salary_permanent/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetPersonSalaryReport([FromRoute] string type,
            [FromBody] personnel_salary_permanent_root _personnel)
        {
            var workbook = new XLWorkbook();
            var ws = workbook.Worksheets.Add("พนักงานราชการ");
            
            ws.Range("A4:A6").Merge().Value = "ลำดับความสำคัญ(1)";
            ws.Cell("A4").Style.Alignment.WrapText = true;
            ws.Range("A4:A6").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range("A4:A6").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("A4:A6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Cell("A4").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("A4").Style.Font.FontSize = 14;
            ws.Range("B4:B6").Merge().Value = "รายการ";
            ws.Cell("B4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("B4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Cell("B4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Cell("B4").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("B4").Style.Font.FontSize = 14;
            ws.Range("C5:E5").Merge().Value = "ประเภทของครุภัณฑ์";
            ws.Range("C5:E5").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range("C5:E5").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("C5:E5").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Cell("C5").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("C5").Style.Font.FontSize = 11;
            ws.Range("C6").Value = "ทดแทนของเดิม";
            ws.Cell("C6").Style.Alignment.WrapText = true;
            ws.Cell("C6").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("C6").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Cell("C6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Cell("C6").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("C6").Style.Font.FontSize = 11;
            ws.Range("D6").Value = "เพิ่มประสิทธิภาพ";
            ws.Cell("D6").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("D6").Style.Font.FontSize = 11;
            ws.Cell("D6").Style.Alignment.WrapText = true;
            ws.Cell("D6").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("D6").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Cell("D6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("E6").Value = "ครุภัณฑ์ใหม่";
            ws.Cell("E6").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("E6").Style.Font.FontSize = 11;
            ws.Cell("E6").Style.Alignment.WrapText = true;
            ws.Cell("E6").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("E6").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Cell("E6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("F5:F6").Merge().Value = "หน่วยนับ";
            ws.Cell("F5").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("F5").Style.Font.FontSize = 12;
            //ws.Range("F3").Style.Font.Bold = true;
            ws.Range("F5:F6").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range("F5:F6").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("F5:F6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("G5:G6").Merge().Value = "จำนวน";
            ws.Cell("G5").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("G5").Style.Font.FontSize = 12;
            //ws.Range("G3").Style.Font.Bold = true;
            ws.Range("G5:G6").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range("G5:G6").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("G5:G6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("G5").Style.Alignment.SetTextRotation(90);
            ws.Range("H5:H6").Merge().Value = "ราคาต่อหน่วย";
            ws.Cell("H5").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("H5").Style.Font.FontSize = 12;
            //ws.Range("H3").Style.Font.Bold = true;
            ws.Range("H5:H6").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range("H5:H6").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("H5:H6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("I4:M4").Merge().Value = "แผนความต้องการครุภัณฑ์";
            ws.Cell("I4").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("I4").Style.Font.FontSize = 14;
            //ws.Range("I2").Style.Font.Bold = true;
            ws.Range("I4:M4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range("I4:M4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("I4:M4").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("I5:M5").Merge().Value = "วงเงิน";
            ws.Cell("I5").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("I5").Style.Font.FontSize = 14;
            ws.Range("I5:M5").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Range("I5:M5").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("I5:M5").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Range("N4:N6").Merge().Value = "คำอธิบายความพร้อม/ประโยชน์ที่จะได้รับ";
            ws.Cell("N4").Style.Font.FontName = "TH Sarabun New";
            ws.Cell("N4").Style.Font.FontSize = 14;
            ws.Cell("N4").Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
            ws.Cell("N4").Style.Alignment.Vertical = XLAlignmentVerticalValues.Center;
            ws.Range("N4:N6").Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
            ws.Row(4).Height = 30;
            ws.Row(5).Height = 30;
            ws.Row(6).Height = 30;
            ws.Column(1).Width = 9;
            ws.Column(2).Width = 40;
            ws.Column(3).Width = 5;
            ws.Column(4).Width = 5;
            ws.Column(5).Width = 5;
            ws.Column(6).Width = 10;
            ws.Column(7).Width = 10;
            ws.Column(8).Width = 10;
            ws.Column(9).Width = 10;
            ws.Column(10).Width = 10;
            ws.Column(11).Width = 10;
            ws.Column(12).Width = 10;
            ws.Column(13).Width = 10;
            ws.Column(14).Width = 40;
            int row = 7;
            int no = 1;

            if (_personnel != null)
            {
               
                foreach (var detail in _personnel.personnel_salary_permanent)
                {
                    
                    ws.Cell(row, 1).Value = no;
                    no++;
                    //ws.Cell(row, 2).Value = detail.manpower;
                    
                    ws.Cell(row, 1).Style.Border.OutsideBorder = 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.Range(ws.Cell(row, 1), ws.Cell(row, 14)).Style.Font.FontName =
                        "TH Sarabun New";
                    ws.Range(ws.Cell(row, 1), ws.Cell(row, 14)).Style.Font.FontSize = 11;
                    ws.Range(ws.Cell(row, 1), ws.Cell(row, 14)).Style.Alignment.WrapText = true;
                    ws.Range(ws.Cell(row, 1), ws.Cell(row, 14)).Style.Alignment.Vertical =
                        XLAlignmentVerticalValues.Center;
                    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, 6).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                    ws.Cell(row, 7).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
                    ws.Cell(row, 8).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                    ws.Cell(row, 9).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                    ws.Cell(row, 10).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                    ws.Cell(row, 11).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                    ws.Cell(row, 12).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                    ws.Cell(row, 13).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Right;
                    ws.Cell(row, 14).Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Left;

                    row++;
                }
            }

            using (var stream1 = new MemoryStream())
            {
                workbook.SaveAs(stream1);
                var content = stream1.ToArray();
                string date = DateTime.Now.ToString("yyyyMMddHHmmss");
                return File(
                    content,
                    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                    "personnel_" + date + ".xlsx");
            }
        }
    }
}