using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using DocumentFormat.OpenXml.Office2010.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.Hrregister;
using Swashbuckle.AspNetCore.Annotations;

namespace rmutr_report.Controllers
{
    [SwaggerTag("สำหรับรายงาน งานทะเบียนประวัติและค่าตอบแทน")]
    public class HRRegis : Controller
    {
        readonly Setting _setting;

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

        [HttpPost, Route("reports/hr_position_manage/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetHr1Report([FromRoute] string type, [FromBody] hr_position_manage _hr)
        {
            foreach (var hr in _hr.data)
            {
                if (hr.position_manage_salary != null)
                {
                    string NumberText = hr.position_manage_salary.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)
                        {
                            hr.money1 = str.Insert(i, ",");
                        }
                    }
                    else if (str.Length == 5)
                    {
                        for (int i = 2; i <= str.Length - 1; i += 5)
                        {
                            hr.money1 = str.Insert(i, ",");
                        }
                    }
                    else
                    {
                        hr.money1 = str;
                    }

                    var total1 = _hr.data.Select(t => t.position_manage_salary).Sum(h => h.Value);
                    var str3 =
                        total1.ToString().Replace('0', '๐').Replace('1', '๑').Replace('2', '๒').Replace('3', '๓')
                            .Replace('4', '๔').Replace('5', '๕').Replace('6', '๖').Replace('7', '๗').Replace('8', '๘')
                            .Replace('9', '๙');
                    if (str3.Length == 4)
                    {
                        for (int i = 1; i <= str3.Length - 1; i += 4)
                        {
                            _hr.total1 = str3.Insert(i, ",");
                        }
                    }
                    else if (str3.Length == 5)
                    {
                        for (int i = 2; i <= str3.Length - 1; i += 5)
                        {
                            _hr.total1 = str3.Insert(i, ",");
                        }
                    }
                    else
                    {
                        _hr.total1 = str3;
                    }

                    if (hr.position_manage_salary == null)
                    {
                        _hr.total1 = null;
                    }
                }

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

                    var total2 = _hr.data.Select(t => t.compensation).Sum(h => h.Value);
                    var str4 =
                        total2.ToString().Replace('0', '๐').Replace('1', '๑').Replace('2', '๒').Replace('3', '๓')
                            .Replace('4', '๔').Replace('5', '๕').Replace('6', '๖').Replace('7', '๗').Replace('8', '๘')
                            .Replace('9', '๙');
                    if (str4.Length == 4)
                    {
                        for (int i = 1; i <= str4.Length - 1; i += 4)
                        {
                            _hr.total2 = str4.Insert(i, ",");
                        }
                    }
                    else if (str4.Length == 5)
                    {
                        for (int i = 2; i <= str4.Length - 1; i += 5)
                        {
                            _hr.total2 = str4.Insert(i, ",");
                        }
                    }
                    else
                    {
                        _hr.total2 = str4;
                    }
                }

                if (hr.compensation == null)
                {
                    _hr.total2 = null;
                }
            }

            var hr1 = new List<hr_position_manage>() {_hr};

            Report report = new Report();
            report.Load(_setting.report_path + "hr_position_manage.frx");
            report.RegisterData(hr1, "hr_position_manage");
            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");
                    break;
                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/hr_position_academic/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetHr2Report([FromRoute] string type, [FromBody] hr_position_manage _hr)
        {
            // var sum = _hr.summary.ToArray();
            // if (sum.GetLength(0)>1)
            //     {
            //         _hr.text = "สรุปจำนวน"; 
            //     }
            //     else if (sum.GetLength(0)<=1)
            //     {
            //         _hr.text = ""; 
            //     }
     foreach (var hr in _hr.data)
            {
                if (hr.position_academic_salary != null)
                {
                    string NumberText = hr.position_academic_salary.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)
                        {
                            hr.money1 = str.Insert(i, ",");
                        }
                    }
                    else if (str.Length == 5)
                    {
                        for (int i = 2; i <= str.Length - 1; i += 5)
                        {
                            hr.money1 = str.Insert(i, ",");
                        }
                    }
                    else
                    {
                        hr.money1 = str;
                    }

                    var total1 = _hr.data.Select(t => t.position_academic_salary).Sum(h => h.Value);
                    var str3 =
                        total1.ToString().Replace('0', '๐').Replace('1', '๑').Replace('2', '๒').Replace('3', '๓')
                            .Replace('4', '๔').Replace('5', '๕').Replace('6', '๖').Replace('7', '๗').Replace('8', '๘')
                            .Replace('9', '๙');
                    if (str3.Length == 4)
                    {
                        for (int i = 1; i <= str3.Length - 1; i += 4)
                        {
                            _hr.total1 = str3.Insert(i, ",");
                        }
                    }
                    else if (str3.Length == 5)
                    {
                        for (int i = 2; i <= str3.Length - 1; i += 5)
                        {
                            _hr.total1 = str3.Insert(i, ",");
                        }
                    }
                    else
                    {
                        _hr.total1 = str3;
                    }

                    if (hr.position_academic_salary == null)
                    {
                        _hr.total1 = null;
                    }
                }

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

                    var total2 = _hr.data.Select(t => t.compensation).Sum(h => h.Value);
                    var str4 =
                        total2.ToString().Replace('0', '๐').Replace('1', '๑').Replace('2', '๒').Replace('3', '๓')
                            .Replace('4', '๔').Replace('5', '๕').Replace('6', '๖').Replace('7', '๗').Replace('8', '๘')
                            .Replace('9', '๙');
                    if (str4.Length == 4)
                    {
                        for (int i = 1; i <= str4.Length - 1; i += 4)
                        {
                            _hr.total2 = str4.Insert(i, ",");
                        }
                    }
                    else if (str4.Length == 5)
                    {
                        for (int i = 2; i <= str4.Length - 1; i += 5)
                        {
                            _hr.total2 = str4.Insert(i, ",");
                        }
                    }
                    else
                    {
                        _hr.total2 = str4;
                    }
                }

                if (hr.compensation == null)
                {
                    _hr.total2 = null;
                }
            }

            var hr1 = new List<hr_position_manage>() {_hr};

            Report report = new Report();
            report.Load(_setting.report_path + "hr_position_academic.frx");
            report.RegisterData(hr1, "hr_position_manage");
            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");
                    break;
                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/hr_position_profession/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetHr3Report([FromRoute] string type, [FromBody] hr_position_manage _hr)
        {
            foreach (var hr in _hr.data)
            {
                if (hr.position_profession_salary != null)
                {
                    string NumberText = hr.position_profession_salary.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)
                        {
                            hr.money1 = str.Insert(i, ",");
                        }
                    }
                    else if (str.Length == 5)
                    {
                        for (int i = 2; i <= str.Length - 1; i += 5)
                        {
                            hr.money1 = str.Insert(i, ",");
                        }
                    }
                    else
                    {
                        hr.money1 = str;
                    }

                    var total1 = _hr.data.Select(t => t.position_profession_salary).Sum(h => h.Value);
                    var str3 =
                        total1.ToString().Replace('0', '๐').Replace('1', '๑').Replace('2', '๒').Replace('3', '๓')
                            .Replace('4', '๔').Replace('5', '๕').Replace('6', '๖').Replace('7', '๗').Replace('8', '๘')
                            .Replace('9', '๙');
                    if (str3.Length == 4)
                    {
                        for (int i = 1; i <= str3.Length - 1; i += 4)
                        {
                            _hr.total1 = str3.Insert(i, ",");
                        }
                    }
                    else if (str3.Length == 5)
                    {
                        for (int i = 2; i <= str3.Length - 1; i += 5)
                        {
                            _hr.total1 = str3.Insert(i, ",");
                        }
                    }
                    else
                    {
                        _hr.total1 = str3;
                    }

                    if (hr.position_profession_salary == null)
                    {
                        _hr.total1 = null;
                    }
                }

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

                    var total2 = _hr.data.Select(t => t.compensation).Sum(h => h.Value);
                    var str4 =
                        total2.ToString().Replace('0', '๐').Replace('1', '๑').Replace('2', '๒').Replace('3', '๓')
                            .Replace('4', '๔').Replace('5', '๕').Replace('6', '๖').Replace('7', '๗').Replace('8', '๘')
                            .Replace('9', '๙');
                    if (str4.Length == 4)
                    {
                        for (int i = 1; i <= str4.Length - 1; i += 4)
                        {
                            _hr.total2 = str4.Insert(i, ",");
                        }
                    }
                    else if (str4.Length == 5)
                    {
                        for (int i = 2; i <= str4.Length - 1; i += 5)
                        {
                            _hr.total2 = str4.Insert(i, ",");
                        }
                    }
                    else
                    {
                        _hr.total2 = str4;
                    }
                }

                if (hr.compensation == null)
                {
                    _hr.total2 = null;
                }
            }

            var hr1 = new List<hr_position_manage>() {_hr};

            Report report = new Report();
            report.Load(_setting.report_path + "hr_position_profession.frx");
            report.RegisterData(hr1, "hr_position_manage");
            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");
                    break;
                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/hr_discipline/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetHr4Report([FromRoute] string type, [FromBody] hr_discipline _hr)
        {

            var hr1 = new List<hr_discipline>() {_hr};

            Report report = new Report();
            report.Load(_setting.report_path + "discipline.frx");
            report.RegisterData(hr1, "hr_discipline");
            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");
                    break;
                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/hr_attend/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetHr5Report([FromRoute] string type, [FromBody] hr_attend _hr)
        {

            var hr1 = new List<hr_attend>() {_hr};

            Report report = new Report();
            report.Load(_setting.report_path + "hr_attend.frx");
            report.RegisterData(hr1, "hr_attend");
            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");
                    break;
                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/hr_retire/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetHr6Report([FromRoute] string type, [FromBody] hr_attend _hr)
        {

            var hr1 = new List<hr_attend>() {_hr};

            Report report = new Report();
            report.Load(_setting.report_path + "hr_retire.frx");
            report.RegisterData(hr1, "hr_attend");
            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");
                    break;
                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/hr_contract_history/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetHr7Report([FromRoute] string type, [FromBody] List<hr_contract_history> _hr)
        {
            
            Report report = new Report();
            report.Load(_setting.report_path + "hr_contract_history.frx");
            report.RegisterData(_hr, "hr_contract_history");
            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");
                    break;
                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/hr_insignia/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetHr8Report([FromRoute] string type, [FromBody] List<hr_insignia> _hr)
        {
            
            Report report = new Report();
            report.Load(_setting.report_path + "hr_insignia.frx");
            report.RegisterData(_hr, "hr_insignia");
            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");
                    break;
                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/hr_askfor_insignia/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetHr9Report([FromRoute] string type, [FromBody] hr_askfor_insignia _hr)
        {
            
            var text1 =
                _hr.insignia_date.Replace('0', '๐').Replace('1', '๑').Replace('2', '๒').Replace('3', '๓')
                    .Replace('4', '๔').Replace('5', '๕').Replace('6', '๖').Replace('7', '๗').Replace('8', '๘')
                    .Replace('9', '๙');
            _hr.insignia_date = text1;
            var text2 =
                _hr.note_insignia_date.Replace('0', '๐').Replace('1', '๑').Replace('2', '๒').Replace('3', '๓')
                    .Replace('4', '๔').Replace('5', '๕').Replace('6', '๖').Replace('7', '๗').Replace('8', '๘')
                    .Replace('9', '๙');
            _hr.note_insignia_date = text2;
            var text7 =
                _hr.year.Replace('0', '๐').Replace('1', '๑').Replace('2', '๒').Replace('3', '๓')
                    .Replace('4', '๔').Replace('5', '๕').Replace('6', '๖').Replace('7', '๗').Replace('8', '๘')
                    .Replace('9', '๙');
            _hr.year = text7;
            foreach (var hr in _hr.data)
            {
                foreach (var detail in hr.data_detail)
                {
                   var text =
                       detail.id_card.Replace('0', '๐').Replace('1', '๑').Replace('2', '๒').Replace('3', '๓')
                        .Replace('4', '๔').Replace('5', '๕').Replace('6', '๖').Replace('7', '๗').Replace('8', '๘')
                        .Replace('9', '๙');
                   detail.id_card = text;
                var text3 =
                    detail.insignia_start_date.Replace('0', '๐').Replace('1', '๑').Replace('2', '๒').Replace('3', '๓')
                        .Replace('4', '๔').Replace('5', '๕').Replace('6', '๖').Replace('7', '๗').Replace('8', '๘')
                        .Replace('9', '๙');
                detail.insignia_start_date = text3;
                var text4 =
                    detail.insignia_receive_date.Replace('0', '๐').Replace('1', '๑').Replace('2', '๒').Replace('3', '๓')
                        .Replace('4', '๔').Replace('5', '๕').Replace('6', '๖').Replace('7', '๗').Replace('8', '๘')
                        .Replace('9', '๙');
                detail.insignia_receive_date = text4;
                var text5 =
                    detail.note.Replace('0', '๐').Replace('1', '๑').Replace('2', '๒').Replace('3', '๓')
                        .Replace('4', '๔').Replace('5', '๕').Replace('6', '๖').Replace('7', '๗').Replace('8', '๘')
                        .Replace('9', '๙');
                detail.note = text5;
                var text6 =
                    detail.position_current_past.Replace('0', '๐').Replace('1', '๑').Replace('2', '๒').Replace('3', '๓')
                        .Replace('4', '๔').Replace('5', '๕').Replace('6', '๖').Replace('7', '๗').Replace('8', '๘')
                        .Replace('9', '๙');
                detail.position_current_past = text6;
                if (detail.salary != null)
                {
                    string NumberText = detail.salary.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)
                        {
                            detail.money = str.Insert(i, ",");
                        }
                    }
                    else if (str.Length == 5)
                    {
                        for (int i = 2; i <= str.Length - 1; i += 5)
                        {
                            detail.money = str.Insert(i, ",");
                        }
                    }
                    else if (str.Length == 6)
                    {
                        for (int i = 3; i <= str.Length - 1; i += 6)
                        {
                            detail.money = str.Insert(i, ",");
                        }
                    }
                    else
                    {
                        detail.money = str;
                    }
                } 
                }
                
            }

            var hr1 = new List<hr_askfor_insignia>() {_hr};
                Report report = new Report();
            report.Load(_setting.report_path + "hr_askfor_insignia.frx");
            report.RegisterData(hr1, "hr_askfor_insignia");
            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");
                    break;
                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/hr_summary_leave/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetHr9Report([FromRoute] string type, [FromBody] List<hr_summary_leave> _hr)
        {
            
            Report report = new Report();
            report.Load(_setting.report_path + "hr_summary_leave.frx");
            report.RegisterData(_hr, "hr_summary_leave");
            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");
                    break;
                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/hr_leave_statistics/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetHr10Report([FromRoute] string type, [FromBody] List<hr_summary_leave> _hr)
        {
            
            Report report = new Report();
            report.Load(_setting.report_path + "hr_leave_statistics.frx");
            report.RegisterData(_hr, "hr_summary_leave");
            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");
                    break;
                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/hr_summary_leave_agency/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetHr11Report([FromRoute] string type, [FromBody]hr_summary_leave_agency _hr)
        {
            var hr1 = new List<hr_summary_leave_agency>() {_hr};
            Report report = new Report();
            report.Load(_setting.report_path + "hr_summary_leave_agency.frx");
            report.RegisterData(hr1, "hr_summary_leave_agency");
            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");
                    break;
                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/hr_summary_study_leave/{type}")]
        [ApiExplorerSettings(GroupName = "reports")]
        public IActionResult GetHr12Report([FromRoute] string type, [FromBody]List<hr_summary_study_leave> _hr)
        {
            Report report = new Report();
            report.Load(_setting.report_path + "hr_summary_study_leave.frx");
            report.RegisterData(_hr, "hr_summary_study_leave");
            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");
                    break;
                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();
        }
    }
}