using ExcelExample.Entites;
using ExcelExample.Context;
using Microsoft.EntityFrameworkCore.Storage;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ExcelExample;
using ClosedXML.Excel;

namespace LoadingExample
{
    public partial class DataForm : Form
    {


        public DataForm()
        {

            InitializeComponent();

        }

        private void DataForm_Load(object sender, EventArgs e)
        {
            using (var db = new MyDB())
            {
                dataGridView1.DataSource = db.users.ToList();
            }


        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void BtnLoadDb_Click(object sender, EventArgs e)
        {

        }

        private void BtnAddExcel_Click(object sender, EventArgs e)
        {
            AddExcelForm addExcelForm = new AddExcelForm();
            if (addExcelForm.ShowDialog() == DialogResult.OK)
            {
                using (var db = new MyDB())
                {
                    dataGridView1.DataSource = db.users.ToList();
                }
            }
        }
       async Task GetExcel(string Path)
        {
            await Task.Run(() =>
            {
                DataTable dataTable = new DataTable();  
                foreach(DataGridViewColumn column in dataGridView1.Columns)
                {
                    dataTable.Columns.Add(column.HeaderText, typeof(string));
                }
                foreach(DataGridViewRow row in dataGridView1.Rows)
                {
                    DataRow dataRow = dataTable.NewRow();
                    for(int i=0;i<dataGridView1.Columns.Count;i++)
                    {
                        dataRow[i] = row.Cells[i].Value;
                    }
                    dataTable.Rows.Add(dataRow);
                }
                using (XLWorkbook workbook = new XLWorkbook())
                {
                    workbook.Worksheets.Add(dataTable, "Sheet1");
                    workbook.SaveAs(Path);
                }
            });
        }
        private async void BtnExportExcel_Click(object sender, EventArgs e)
        {
            try
            {
                using (SaveFileDialog saveFileDialog =new SaveFileDialog())
                {
                    saveFileDialog.Filter = "Excel files (*.xlsx) | *.xlsx|All files(*.*) |*.*";
                    saveFileDialog.RestoreDirectory = true;
                    string FilePath = "";
                    if(saveFileDialog.ShowDialog()==DialogResult.OK)
                    {
                        FilePath = saveFileDialog.FileName;
                        await GetExcel(FilePath);
                        MessageBox.Show("عملیات با موفقیت انجام شد");
                    }
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }
    }
}
