using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms; 
// ref https://www.c-sharpcorner.com/article/read-excel-file-in-c-sharp-winform/
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using System.IO;

namespace solarApp
{
    public partial class fmExcel : Form
    {
        System.Data.DataTable dt_cap1 = new System.Data.DataTable();

        public fmExcel()
        {
            InitializeComponent();
        }

        private void bt_inv_hour_hj_Click(object sender, EventArgs e)
        {
            string fname = "";
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "Excel File Dialog";
            fdlg.InitialDirectory = @"c:\";
            fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
            fdlg.FilterIndex = 2;
            fdlg.RestoreDirectory = true;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                fname = fdlg.FileName;
            }
            else return;
            //取得選取檔案的路徑
            string dir = Path.GetDirectoryName(fname);
            // 取得路徑下所有檔案
            string[] fileEntries = System.IO.Directory.GetFiles(dir); 

            Service.operateCSV csvSvc = new Service.operateCSV(); // readCsvTxt
            System.Data.DataTable dt = new System.Data.DataTable();
            bool isFirst = true;
            foreach (string fileName in fileEntries)
            {
                #region  取得 filename 中的 InvID 
                string fName = Path.GetFileName(fileName);
                string[] ss = fName.Split("_");
                string InvID = ss[0];
                #endregion

                if (isFirst)
                {
                    csvSvc.clear_inv("hour");
                    csvSvc.createColumnHour(ref dt, fileName);
                    csvSvc.readCsvFile(ref dt, fileName, InvID, dt.Columns.Count, isFirst);
                    isFirst = false;
                }
                else
                    csvSvc.readCsvFile(ref dt, fileName, InvID, dt.Columns.Count, isFirst);
            }
            MessageBox.Show(" 共 "+ dt.Rows.Count.ToString());

            //System.Data.DataTable dt = solarApp.Service.csvHelper.OpenCSV(fname); 
            dataGridView1.DataSource = csvSvc.insertHour2DB(ref dt);
            MessageBox.Show("OK");
        }
        protected void ImportFile()
        {
            string fname = "";
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "Excel File Dialog";
            fdlg.InitialDirectory = @"c:\";
            fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
            fdlg.FilterIndex = 2;
            fdlg.RestoreDirectory = true;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                fname = fdlg.FileName;
            }
            else return; 

            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            Microsoft.Office.Interop.Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(fname);
            Microsoft.Office.Interop.Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
            Microsoft.Office.Interop.Excel.Range xlRange = xlWorksheet.UsedRange;

            int rowCount = xlRange.Rows.Count;
            int colCount = xlRange.Columns.Count;

            // dt.Column = colCount;  
            dataGridView1.ColumnCount = colCount;
            dataGridView1.RowCount = rowCount;

            for (int i = 1; i <= rowCount; i++)
            {
                for (int j = 1; j <= colCount; j++)
                { 
                    //write the value to the Grid   
                    if (xlRange.Cells[i, j] != null && xlRange.Cells[i, j].Value2 != null)
                    {
                        dataGridView1.Rows[i - 1].Cells[j - 1].Value = xlRange.Cells[i, j].Value2.ToString();
                    }
                    // Console.Write(xlRange.Cells[i, j].Value2.ToString() + "\t");   
                    //add useful things here!     
                }
            }

            //cleanup  
            GC.Collect();
            GC.WaitForPendingFinalizers();

            //rule of thumb for releasing com objects:  
            //  never use two dots, all COM objects must be referenced and released individually  
            //  ex: [somthing].[something].[something] is bad  

            //release com objects to fully kill excel process from running in the background  
            Marshal.ReleaseComObject(xlRange);
            Marshal.ReleaseComObject(xlWorksheet);

            //close and release  
            xlWorkbook.Close();
            Marshal.ReleaseComObject(xlWorkbook);

            //quit and release  
            xlApp.Quit();
            Marshal.ReleaseComObject(xlApp);

        }

        private void bt_inv_day_hj_Click(object sender, EventArgs e)
        {
            string fname = "";
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "Excel File Dialog";
            fdlg.InitialDirectory = @"c:\";
            fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
            fdlg.FilterIndex = 2;
            fdlg.RestoreDirectory = true;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                fname = fdlg.FileName;
            }
            else return;
            //取得選取檔案的路徑
            string dir = Path.GetDirectoryName(fname);
            // 取得路徑下所有檔案
            string[] fileEntries = System.IO.Directory.GetFiles(dir);

            Service.operateCSV csvSvc = new Service.operateCSV(); // readCsvTxt
            System.Data.DataTable dt = new System.Data.DataTable();
            bool isFirst = true;
            foreach (string fileName in fileEntries)
            {
                #region  取得 filename 中的 InvID 
                string fName = Path.GetFileName(fileName);
                string[] ss = fName.Split("_");
                string InvID = ss[0];
                #endregion

                if (isFirst)
                {
                    csvSvc.clear_inv("day");
                    csvSvc.createColumnDay(ref dt, fileName);
                    csvSvc.readCsvFile(ref dt, fileName, "", dt.Columns.Count, isFirst);
                    isFirst = false;
                }
                else
                    csvSvc.readCsvFile(ref dt, fileName, "", dt.Columns.Count, isFirst);
            }
            MessageBox.Show(" 共 " + dt.Rows.Count.ToString());

            //System.Data.DataTable dt = solarApp.Service.csvHelper.OpenCSV(fname);
            dataGridView1.DataSource = dt;
            csvSvc.insertDay2DB(ref dt);
            MessageBox.Show("OK");
        }

        private void bt_day_archive_hj_Click(object sender, EventArgs e)
        {
            Service.operateCSV csvSvc = new Service.operateCSV(); // readCsvTxt
            csvSvc.archive_data("day");
            MessageBox.Show("ok");
        }

        private void bt_hour_archive_hj_Click(object sender, EventArgs e)
        {
            Service.operateCSV csvSvc = new Service.operateCSV(); // readCsvTxt
            csvSvc.archive_data("hour");
            MessageBox.Show("ok");
        }
    }
}
