using ExcelExample.Context;
using ExcelExample.Entites;
using ExcelExample.Excel;
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;

namespace ExcelExample
{
    public partial class AddExcelForm : Form
    {
        public AddExcelForm()
        {
            InitializeComponent();
        }

        private void BtnChoseExcel_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Title = "فایل اکسل مورد نظر را انتخاب نمایید";
                openFileDialog.Filter = "Excel Sheet(*.xlsx)|*xlsx|All Files(*.*)|*.*";
                openFileDialog.Multiselect = false;
                openFileDialog.RestoreDirectory = true;
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    TxtPath.Text = openFileDialog.FileName;

                    AddExcelClass addExcelClass = new AddExcelClass();
                    dataGridView1.DataSource = addExcelClass.GetDataView(openFileDialog.FileName);
                    BtnSave.Enabled = true;
                    dataGridView1.Columns[0].HeaderText = "نام";
                    dataGridView1.Columns[1].HeaderText = "نام خانوادگی";
                    dataGridView1.Columns[2].HeaderText = "ایمیل";
                    dataGridView1.Columns[3].HeaderText = "شماره تماس";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
                {
                    var user = new User
                    {
                        Name = dataGridView1.Rows[i].Cells[0].Value.ToString(),
                        family = dataGridView1.Rows[i].Cells[1].Value.ToString(),
                        Email = dataGridView1.Rows[i].Cells[2].Value.ToString(),
                        number = dataGridView1.Rows[i].Cells[3].Value.ToString(),
                    };
                    var db = new MyDB();
                 bool ExsitUser =  db.users.Any(x => x.family == dataGridView1.Rows[i].Cells[1].Value.ToString());
                   
                    if(!ExsitUser)
                    {
                        using (var mydb = new MyDB())
                        {
                            mydb.users.Add(user);
                            mydb.SaveChanges();
                        }
                    }
                  
                }
                if (MessageBox.Show("اطلاعات با موفقیت به پایگاه داده افزوده شد") == DialogResult.OK)
                {
                    DialogResult = DialogResult.OK;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }

        }
    }
}
