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

namespace RezerveBeliteHavaPeimaCS
{
    public partial class frmCustumers : Form
    {
        int int_Customer = 1;
        public frmCustumers()
        {
            InitializeComponent();
        }
        /// <summary>
        /// تابع مربوط به درج مشتری
        /// </summary>
        private void UDF_InsertCustumers()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            tblCustumer tblCm = new tblCustumer();
            tblCm.NameSname = Txt_CustumersName.Text.Trim();
            tblCm.PhoneNum = Txt_CustumersTel.Text.Trim();
            tblCm.FaxNum = Txt_CustumersFax.Text.Trim();
            tblCm.Notes = Txt_CustumersMolahezat.Text.Trim();
            db.tblCustumers.InsertOnSubmit(tblCm);
            db.SubmitChanges();
            Txt_CustumersFax.Text = "";
            Txt_CustumersMolahezat.Text = "";
            Txt_CustumersName.Text = "";
            Txt_CustumersTel.Text = "";
        }
        /// <summary>
        /// تابع مربوط به به روز رسانی مشتری
        /// </summary>
        private void UDf_RefreshCustumurs()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            var Query = from row in db.tblCustumers
                        orderby row.CustomerID descending
                        select new
                        {
                            row.CustomerID,
                            row.NameSname,
                            row.PhoneNum,
                            row.Notes,
                            row.FaxNum
                        };
            Dgv_Custumers.DataSource = Query;
        }
        /// <summary>
        /// تابع مربوط به ویرایش مشتری
        /// </summary>
        private void UDF_UpdateCustumers()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            tblCustumer tblCm=db.tblCustumers.Single(n=> n.CustomerID==int.Parse(Txt_CustumersId.Text.Trim()));
            tblCm.NameSname = Txt_CustumersName.Text.Trim();
            tblCm.PhoneNum = Txt_CustumersTel.Text.Trim();
            tblCm.FaxNum = Txt_CustumersFax.Text.Trim();
            tblCm.Notes = Txt_CustumersMolahezat.Text.Trim();
            Txt_CustumersFax.Text = "";
            Txt_CustumersMolahezat.Text = "";
            Txt_CustumersName.Text = "";
            Txt_CustumersTel.Text = "";
            Txt_CustumersId.Text = "";
            db.SubmitChanges();
            UDf_RefreshCustumurs();
        }
        /// <summary>
        /// تابع مربوط به حذف مشتری
        /// </summary>
        private void UDF_DeleteCustumers()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            try
            {
                tblCustumer tblCm = db.tblCustumers.Single(n => n.CustomerID == int.Parse(Txt_CustumersId.Text.Trim()));
                db.tblCustumers.DeleteOnSubmit(tblCm);
                db.SubmitChanges();
                Txt_CustumersId.Text = "";
                Txt_CustumersFax.Text = "";
                Txt_CustumersMolahezat.Text = "";
                Txt_CustumersName.Text = "";
                Txt_CustumersTel.Text = "";
            }
            catch
            {
                MessageBox.Show("لطفا ابتدا بلیط مربوط به مشتری را کنسل کرده سپس مشتری را حذف نمایید");
            }
        }
        /// <summary>
        /// تابع مربوط به جستجوی پیشرفته مشتری
        /// </summary>
        private void UDf_SearchAdvancedCustumurs()
        {
                int int_SearchFlg = 0;
                LSC_TADataContext db = new LSC_TADataContext();
                var Query = from Row in db.tblCustumers
                            select Row;
          try
            {
                if (Chk_CustumersId.Checked == true)
                {
                        var Query1 = from Row in Query
                                     where Row.CustomerID == int.Parse(Txt_CustumersSearch.Text.Trim())
                                     select Row;
                        Query = Query1;
                        int_SearchFlg = 1;
                }
                if (Chk_CustumersName.Checked == true)
                {
                    var Query2 = from Row in Query
                                 where SqlMethods.Like(Row.NameSname, "%" + Txt_CustumersName.Text.Trim() + "%")
                                 orderby Row.CustomerID descending
                                 select Row;
                    Query = Query2;
                    int_SearchFlg = 1;
                }
                if (Chk_CustumersTel.Checked == true)
                {
                    var Query3 = from Row in Query
                                 where Row.PhoneNum == Txt_CustumersTel.Text.Trim()
                                 select Row;
                    Query = Query3;
                    int_SearchFlg = 1;
                }
                if (Chk_CustumersFax.Checked == true)
                {
                    var Query4 = from Row in Query
                                 where Row.FaxNum == Txt_CustumersFax.Text.Trim()
                                 select Row;
                    Query = Query4;
                    int_SearchFlg = 1;
                }
                if (Chk_CustumersMolahezat.Checked == true)
                {
                    var Query5 = from Row in Query
                                 where Row.Notes == Txt_CustumersMolahezat.Text.Trim()
                                 select Row;
                    Query = Query5;
                    int_SearchFlg = 1;
                }
                if (int_SearchFlg != 0)
                {
                    Dgv_Custumers.DataSource = Query;
                }
            }
            catch
            {
                MessageBox.Show("لطفا فیلد انتخاب شده را پر نمایید");
            }
        }
        private void frmCostumers_Load(object sender, EventArgs e)
        {
            Pnl_Custumers2.Visible = false;
            UDf_RefreshCustumurs();
        }
        private void Btn_InsertHotel1_Click(object sender, EventArgs e)
        {
            Pnl_Custumers1.Visible = false;
            Pnl_Custumers2.Visible = true;
        }
        private void Btn_DeleteHotel_Click(object sender, EventArgs e)
        {
            Pnl_Custumers1.Visible = false;
            Pnl_Custumers2.Visible = true;
        }
        private void Btn_CustumersUpdate_Click(object sender, EventArgs e)
        {
            Txt_CustumersId.Enabled = true;

            if (Txt_CustumersName.Text == "" && Txt_CustumersId.Text == "")
            {
                MessageBox.Show("لطفا کد مشتری و  نام و نام خانوادگی را وارد نمایید");
            }
            else
            {
                Pnl_Custumers1.Visible = false;
                Pnl_Custumers2.Visible = true;
              int_Customer = 2;
            }
        }
        private void Btn_CustumersInsert_Click(object sender, EventArgs e)
        {
            if (Txt_CustumersName.Text == "")
            {
                MessageBox.Show("لطفا نام و نام خانوادگی را وارد نمایید");
            }
            else
            {
                Pnl_Custumers1.Visible = false;
                Pnl_Custumers2.Visible = true;
                int_Customer = 1;
            } 
        }
        private void Btn_CustumersDelete_Click(object sender, EventArgs e)
        {
            Txt_CustumersId.Enabled = true;
            if ( Txt_CustumersId.Text == "")
            {
                MessageBox.Show("لطفا  مشتری مورد نظرتان را از جدول انتخاب کنید");
            }
            else
            {
                Pnl_Custumers1.Visible = false;
                Pnl_Custumers2.Visible = true;
                int_Customer = 3;
            }
        }
        private void Btn_CustumersSearch_Click(object sender, EventArgs e)
        {
            Btn_CustumersSearch.BringToFront();
            UDf_RefreshCustumurs();
        }
        private void Btn_CustumersTaeed_Click(object sender, EventArgs e)
        {
            try
            {
                Pnl_Custumers2.Visible = false;
                Pnl_Custumers1.Visible = true;
                if (int_Customer == 1)
                {
                    UDF_InsertCustumers();
                }
                else if (int_Customer == 2)
                {
                    UDF_UpdateCustumers();
                }
                else if (int_Customer == 3)
                {
                    UDF_DeleteCustumers();
                }
                UDf_RefreshCustumurs();
            }
            catch
            {
                MessageBox.Show("لطفا خطای به وجود آمده را برطرف نمایید");
            }
        }
        private void Btn_CustumersLaghv_Click(object sender, EventArgs e)
        {
            try
            {
                Pnl_Custumers2.Visible = false;
                Pnl_Custumers1.Visible = true;
                UDf_RefreshCustumurs();
                Txt_CustumersFax.Text = "";
                Txt_CustumersMolahezat.Text = "";
                Txt_CustumersName.Text = "";
                Txt_CustumersTel.Text = "";
            }
            catch
            {
                MessageBox.Show("لطفا خطای به وجود آمده را برطرف نمایید");
            }
        }
        private void Dgv_Custumers_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            LSC_TADataContext db = new LSC_TADataContext();
            Int32 selectedCellCount = Dgv_Custumers.GetCellCount(DataGridViewElementStates.Selected);
            if (selectedCellCount > 0)
            {
                if (Dgv_Custumers.AreAllCellsSelected(true))
                {
                    MessageBox.Show("لطفاسلول از سطر مورد نظر راانتخاب نماييد!", "Selected Cells");
                }
                else
                {
                    for (int i = 0; i < selectedCellCount; i++)
                    {
                        Txt_CustumersId.Text = Dgv_Custumers.Rows[int.Parse(Dgv_Custumers.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                        Txt_CustumersName.Text = Dgv_Custumers.Rows[int.Parse(Dgv_Custumers.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                        Txt_CustumersTel.Text = Dgv_Custumers.Rows[int.Parse(Dgv_Custumers.SelectedCells[i].RowIndex.ToString())].Cells[2].Value.ToString();
                        Txt_CustumersFax.Text = Dgv_Custumers.Rows[int.Parse(Dgv_Custumers.SelectedCells[i].RowIndex.ToString())].Cells[4].Value.ToString();
                        Txt_CustumersMolahezat.Text = Dgv_Custumers.Rows[int.Parse(Dgv_Custumers.SelectedCells[i].RowIndex.ToString())].Cells[3].Value.ToString();
                    }
                }
            }
        }
        private void Txt_CustumersTel_KeyPress(object sender, KeyPressEventArgs e)
        {
            int int_Ascii = Convert.ToInt16(e.KeyChar);
            if (int_Ascii >= 48 && int_Ascii <= 57 || int_Ascii == 8)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        private void Txt_CustumersFax_KeyPress(object sender, KeyPressEventArgs e)
        {
            int int_Ascii = Convert.ToInt16(e.KeyChar);
            if (int_Ascii >= 48 && int_Ascii <= 57 || int_Ascii == 8)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        private void Btn_CustumersSearch_Click_1(object sender, EventArgs e)
        {
           UDf_SearchAdvancedCustumurs();
           Txt_CustumersSearch.Text = "";
           Txt_CustumersName.Text = "";
           Txt_CustumersTel.Text = "";
           Txt_CustumersFax.Text = "";
           Txt_CustumersMolahezat.Text = "";
           Btn_CustumersSearch.SendToBack();
        }

        private void Dgv_Custumers_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}