using DynStatDisk.App.Com;
using DynStatDisk.App.Model;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace DynStatDisk.App
{
    public partial class frmHistoryData : Form
    {
        public frmHistoryData()
        {
            InitializeComponent();
        }

        //单例模式
        private static frmHistoryData frm = null;
        public static frmHistoryData CreateInstrance()
        {
            if (frm == null || frm.IsDisposed)
            {
                frm = new frmHistoryData();
            }
            return frm;
        }

        /// <summary>
        /// 电器盒安装集合数据
        /// </summary>
        private List<KPDynStaticMach> LisDynStatDiskInstall = new List<KPDynStaticMach>();

        private void frmHistoryData_Load(object sender, EventArgs e)
        {
            LoadListViewColumn();
            //获取程序的配置信息
            var SqlCon = ConfigHelper.GetValue("connecting");

            

        }



        private void LoadListViewColumn()
        {
            ColumnHeader h0 = new ColumnHeader();
            ColumnHeader h1 = new ColumnHeader();
            ColumnHeader h2 = new ColumnHeader();
            ColumnHeader h3 = new ColumnHeader();
            ColumnHeader h4 = new ColumnHeader();
            ColumnHeader h5 = new ColumnHeader();
            ColumnHeader h6 = new ColumnHeader();
            ColumnHeader h7 = new ColumnHeader();
            ColumnHeader h8 = new ColumnHeader();
            ColumnHeader h9 = new ColumnHeader();
            ColumnHeader h10 = new ColumnHeader();
            ColumnHeader h11 = new ColumnHeader();
            ColumnHeader h12 = new ColumnHeader();
            ColumnHeader h13 = new ColumnHeader();
            ColumnHeader h14 = new ColumnHeader();
            ColumnHeader h15 = new ColumnHeader();
            ColumnHeader h16 = new ColumnHeader();
            ColumnHeader h17 = new ColumnHeader();
            ColumnHeader h18 = new ColumnHeader();
            ColumnHeader h19 = new ColumnHeader();
            ColumnHeader h20 = new ColumnHeader();
            ColumnHeader h21 = new ColumnHeader();
            ColumnHeader h22 = new ColumnHeader();
            ColumnHeader h23 = new ColumnHeader();
            ColumnHeader h24 = new ColumnHeader();
            ColumnHeader h25 = new ColumnHeader();
            ColumnHeader h26 = new ColumnHeader();


            h0.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h4.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h5.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h6.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h7.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h8.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h9.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h10.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h11.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h12.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h13.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h14.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h15.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h16.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h17.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h18.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h19.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h20.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h21.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h22.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h23.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h24.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h25.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            h26.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;


            h0.Width = 20;
            h1.Width = 100;
            h2.Width = 100;
            h3.Width = 100;
            h4.Width = 150;
            h5.Width = 150;
            h6.Width = 0;
            h7.Width = 100;
            h8.Width = 100;
            h9.Width = 100;
            h10.Width = 100;
            h11.Width = 100;
            h12.Width = 100;
            h13.Width = 140;
            h14.Width = 100;
            h15.Width = 100;
            h16.Width = 120;
            h17.Width = 120;
            h18.Width = 120;
            h19.Width = 120;
            h20.Width = 120;
            h21.Width = 120;
            h22.Width = 120;
            h23.Width = 120;
            h24.Width = 160;
            h25.Width = 160;
            h26.Width = 0;


            h0.Text = "";
            h1.Text = "压缩机铭板号";
            //h2.Text = "节拍";
            h3.Text = "结果";
            //h4.Text = "开始时间";
            h5.Text = "结束时间";
            h6.Text = "";
            h7.Text = "上活塞油沟";
            h8.Text = "工号";
            h9.Text = "创建时间";
            h10.Text = "";
            h11.Text = "xxx";
            h12.Text = "xxx";
            h13.Text = "xxx";
            h14.Text = "xxx";
            h15.Text = "xxx";
            h16.Text = "xxx";
            h17.Text = "xxx";
            h18.Text = "xxx";
            h19.Text = "xxx";
            h20.Text = "xxx";
            h21.Text = "xxx";
            h22.Text = "xxx";
            h23.Text = "xxx";
            h24.Text = "xxx";
            h25.Text = "xxx";
            h26.Text = "";


            //h16.Text = "Id";
            listView1.Columns.Clear();
            listView1.Columns.AddRange(new ColumnHeader[] { h0, h1,h3, h5, h6 });
            listView1.View = View.Details;
        }


        private void InsertListViewData(long Id,
         string PlaqueNo,
         string Result,
         DateTime CreateTime)
        {
            //var EndTime = CreateTime.AddMinutes(Cycle);
            ListViewItem lvi = new ListViewItem();
            lvi.ImageIndex = 0;
            lvi.Text = "";
            lvi.SubItems.Add(PlaqueNo);
            //lvi.SubItems.Add(Cycle.ToString());
            lvi.SubItems.Add(Result);
            lvi.SubItems.Add(CreateTime.ToString("yyyy-MM-dd HH:mm:ss"));
            //lvi.SubItems.Add(EndTime.ToString("yyyy-MM-dd HH:mm:ss"));
            lvi.SubItems.Add(Id.ToString());
            this.listView1.Items.Insert(0, lvi);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();

            var start = dtpStart.Value;
            var end = dtpEnd.Value;
            var data = FSqlContext.FDb.Select<KPDynStaticMach>().Where(a => a.CreateTime >= start && a.CreateTime <= end).OrderByDescending(a => a.CreateTime).ToList();
            if (data != null && data.Count > 0)
            {
                LisDynStatDiskInstall = data;
                foreach (var item in data)
                {
                    InsertListViewData(item.Id,
                        item.StaDskCastNo,
                        item.EuropMacine,
                        item.CreateTime
                        );
                }
            }

        }

        private void btnReport_Click(object sender, EventArgs e)
        {
            //UpperSdReportFile();


        }

        private void ReportFile()
        {
            var start = dtpStart.Value.ToString("yyyy-MM-dd HH");
            var end = dtpEnd.Value.ToString("yyyy-MM-dd HH");

            //try
            //{
            /***********************
          1.读取excel到workbook中
          ***********************/
            string TemplateFilePath = System.Environment.CurrentDirectory;
            string tPath = TemplateFilePath + @"\ReportFile\GroupStandSdReportTemplate.xlsx";
            XSSFWorkbook wk = null;
            using (FileStream fs = File.Open(tPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                wk = new XSSFWorkbook(fs);
                fs.Close();   //把excel里的内容保存到workbook中之后，就可以关闭了
            }
            NPOI.SS.UserModel.ISheet sheetSourceData = wk.GetSheetAt(0);   //获得wk中第一个sheet,保存到sheet中
                                                                           //NPOI.SS.UserModel.ISheet sheetshift = wk.GetSheetAt(2);   //获得wk中第一个sheet,保存到sheet中
                                                                           //NPOI.SS.UserModel.ISheet sheetDt = wk.GetSheetAt(3);   //获得wk中第一个sheet,保存到sheet中
                                                                           //NPOI.SS.UserModel.ISheet sheetCount = wk.GetSheetAt(4);   //获得wk中第一个sheet,保存到sheet中
                                                                           //NPOI.SS.UserModel.ISheet sheetCycle = wk.GetSheetAt(5);   //获得wk中第一个sheet,保存到sheet中

            /***********************
            2.操作数据
            ***********************/
            //2行  5行
            IDataFormat dataformat = wk.CreateDataFormat();
            ICellStyle style0 = wk.CreateCellStyle();
            for (int i = 0; i < LisDynStatDiskInstall.Count; i++)// (int i = 2; i < 6; i += 3)
            {
                IRow row;
                row = sheetSourceData.CreateRow(i + 1);
                //row.CreateCell(0).SetCellValue(LisDynStatDiskInstall[i].Id.ToString());
                //row.CreateCell(1).SetCellValue(LisDynStatDiskInstall[i].SerialNo.ToString());
                //row.CreateCell(2).SetCellValue(LisDynStatDiskInstall[i].ModelName.ToString());
                //row.CreateCell(3).SetCellValue(LisDynStatDiskInstall[i].ModelType.ToString());
                //row.CreateCell(4).SetCellValue(LisDynStatDiskInstall[i].MiddlePlateNo.ToString());
                //row.CreateCell(5).SetCellValue(LisDynStatDiskInstall[i].UpperGroupStandDiff.ToString("f2"));
                //row.CreateCell(6).SetCellValue(LisDynStatDiskInstall[i].UpperGroupStandDiffResult.ToString());
                //row.CreateCell(7).SetCellValue(LisDynStatDiskInstall[i].LowerGroupStandDiff.ToString("f2"));
                //row.CreateCell(8).SetCellValue(LisDynStatDiskInstall[i].LowerGroupStandDiffResult.ToString());
                //row.CreateCell(9).SetCellValue(LisDynStatDiskInstall[i].GroupStandDiff.ToString("f2"));
                //row.CreateCell(10).SetCellValue(LisDynStatDiskInstall[i].GroupStandDiffResult.ToString());
                //row.CreateCell(11).SetCellValue(LisDynStatDiskInstall[i].OperationNo.ToString());
                //row.CreateCell(12).SetCellValue(LisDynStatDiskInstall[i].CreateTime.ToString("yyyy-MM-dd HH:mm"));
                //row.GetCell(12).CellStyle = style0;
                //row.GetCell(8).CellStyle = style0;
                //row.GetCell(10).CellStyle = style0;
            }


            //DayDailyParameterInfo = null;
            //NightDailyParameterInfo = null;


            string FilePath = "";
            FolderBrowserDialog folder = new FolderBrowserDialog();
            folder.Description = "选择文件所在文件夹目录";  //定义在对话框上显示的文本

            if (folder.ShowDialog() == DialogResult.OK)
            {
                FilePath = folder.SelectedPath;

                /***********************
                3.修改完成，写入到客户端 
                ***********************/
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                {
                    //tPath = @"D:\日立报表\";
                    //判断文件夹是否存在，不存在就创建
                    if (!Directory.Exists(FilePath))
                    {
                        Directory.CreateDirectory(FilePath);
                    }
                    FilePath = FilePath + @"\" + start + "-" + end + "报表数据.xlsx";
                    wk.Write(ms);

                    using (FileStream fs = new FileStream(FilePath, FileMode.Create, FileAccess.Write))
                    {
                        byte[] data = ms.ToArray();
                        fs.Write(data, 0, data.Length);
                        fs.Flush();
                    }
                    wk = null;

                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
