using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VideoCloop
{
    public partial class frmCustomer : Form
    {
        public frmCustomer()
        {
            InitializeComponent();
        }

        private void frmCustomer_Load(object sender, EventArgs e)
        {
            Udf_FillGrid();
        }
        private void Udf_FillGrid()
        {
            Lsc_VideoClubDataContext db = new Lsc_VideoClubDataContext();
            var Query = from Row in db.tblCustomers 
                        select Row;
            Dg_Customer.DataSource = Query;
            Dg_Customer.Columns[0].HeaderText = "شماره مشتری";
            Dg_Customer.Columns[1].HeaderText = "نام";
            Dg_Customer.Columns[2].HeaderText = "نام خانوادگی";
            Dg_Customer.Columns[3].HeaderText = "تاریخ تولد";
            Dg_Customer.Columns[4].HeaderText = "کدملی";
            Dg_Customer.Columns[5].HeaderText = "آدرس";
        }

        private void Btn_Insert_Click(object sender, EventArgs e)
        {
            try
            {
                if (Txt_BirthDate.Text != "    /  /" && Txt_Address.Text != "" && Txt_BirthDate.Text != "" && Txt_MeliCode.Text != "" && Txt_Name.Text != "" && Txt_SName.Text != "")
                {
                    Lsc_VideoClubDataContext db = new Lsc_VideoClubDataContext();
                    tblCustomer add = new tblCustomer();
                    add.Address = Txt_Address.Text;
                    add.DateBirth = Txt_BirthDate.Text;
                    add.MeliCode = Txt_MeliCode.Text;
                    add.Name = Txt_Name.Text;
                    add.SName = Txt_SName.Text;
                    db.tblCustomers.InsertOnSubmit(add);
                    db.SubmitChanges();
                    MessageBox.Show("عملیات درج انجام شد!");
                    Udf_FillGrid();
                    foreach (Control ctl in this.Controls)
                    {
                        if (ctl is TextBox) ctl.Enabled = true;
                        if (ctl is MaskedTextBox) ctl.Enabled = true;
                        if (ctl is Button) { ctl.Enabled = true; ctl.Visible = true; }
                        btnCancle.Visible = false;
                    }
                }
                else MessageBox.Show("لطفا اطلاعات راوارد نمایید!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("عملیات درج انجام نشد!");
            }
        }

        private void Dg_TypeProduct_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Lsc_VideoClubDataContext db = new Lsc_VideoClubDataContext();

            Int32 selectedCellCount = Dg_Customer.GetCellCount(DataGridViewElementStates.Selected);

            if (selectedCellCount > 0)
            {
                if (Dg_Customer.AreAllCellsSelected(true))
                {
                    MessageBox.Show("لطفاسلول از سطر مورد نظر راانتخاب نماييد!", "Selected Cells");
                }
                else
                {

                    for (int i = 0; i < selectedCellCount; i++)
                    {

                        Txt_Address.Text = Dg_Customer.Rows[int.Parse(Dg_Customer.SelectedCells[i].RowIndex.ToString())].Cells[5].Value.ToString();
                        Txt_BirthDate.Text = Dg_Customer.Rows[int.Parse(Dg_Customer.SelectedCells[i].RowIndex.ToString())].Cells[3].Value.ToString();
                        Txt_Name.Text = Dg_Customer.Rows[int.Parse(Dg_Customer.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                        Txt_SName.Text = Dg_Customer.Rows[int.Parse(Dg_Customer.SelectedCells[i].RowIndex.ToString())].Cells[2].Value.ToString();
                        Txt_MeliCode.Text = Dg_Customer.Rows[int.Parse(Dg_Customer.SelectedCells[i].RowIndex.ToString())].Cells[4].Value.ToString();

                    }
                }
            }
        }

        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            try
            {
                Lsc_VideoClubDataContext db = new Lsc_VideoClubDataContext();
                tblCustomer DelTbl = db.tblCustomers.SingleOrDefault(n => n.customerId == Decimal.Parse(Dg_Customer.CurrentRow.Cells[0].Value.ToString()));
                DialogResult result = MessageBox.Show("آیا شما می خواهیدمشتری باشماره" + DelTbl.customerId  + "راحذف نمایید", "اخطار", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
                if (result == DialogResult.OK)
                {
                    db.tblCustomers .DeleteOnSubmit(DelTbl);
                    db.SubmitChanges();
                    MessageBox.Show("عملیات  حذف انجام شد!");
                }
                Udf_FillGrid();
            }
            catch
            {
                MessageBox.Show("اشکالی در حذف رکورد پیش آمده است");
            }
        }

        private void Btn_Update_Click(object sender, EventArgs e)
        {
            try
            {
                Lsc_VideoClubDataContext db = new Lsc_VideoClubDataContext();
                tblCustomer UpdateTbl = db.tblCustomers.Single(n => n.customerId == int.Parse(Dg_Customer.CurrentRow.Cells[0].Value.ToString()));
                UpdateTbl.Address  = Txt_Address.Text;
                UpdateTbl.DateBirth  = Txt_BirthDate.Text;
                UpdateTbl.MeliCode  = Txt_MeliCode.Text;
                UpdateTbl.Name  = Txt_Name.Text;
                UpdateTbl.SName  = Txt_SName.Text;
                db.SubmitChanges();
                MessageBox.Show("رکورد مورد نظر ویرایش شد");
                Udf_FillGrid();

            }
            catch (Exception ex)
            {
                MessageBox.Show("اشکالی در ویرایش رکورد پیش آمده است");
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            foreach (Control ctl in this.Controls)
            {
                if (ctl is TextBox) { ctl.Enabled = false; ctl.Text = ""; }
                if (ctl is Button) ctl.Enabled = false;
                if (ctl is MaskedTextBox) ctl.Enabled = false;
            }
           DialogResult result = MessageBox.Show("آیا شما می خواهیدعمل درج راانجام دهید", "اخطار", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
           if (result == DialogResult.OK)
           {
               foreach (Control ctl in this.Controls)
               {
                   if (ctl is TextBox) ctl.Enabled = true;
                   if (ctl is MaskedTextBox) ctl.Enabled =true ;
                   if (ctl is Button) ctl.Enabled = true ;
                   btnCancle.Visible = true;
                   btnAdd.Visible = false;
                   Btn_Delete.Visible =false;
                   Btn_Update .Visible = false;
               } 
           }
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            foreach (Control ctl in this.Controls)
            {
                if (ctl is TextBox) ctl.Enabled = true;
                if (ctl is MaskedTextBox) ctl.Enabled = true;
                if (ctl is Button) { ctl.Enabled = true; ctl.Visible = true; }
                btnCancle.Visible = false;
            }
        }
       
    }
}
