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 frmStaffs : Form
    {
        int int_staffs = 0;

        public frmStaffs()
        {
            InitializeComponent();
        }
        /// <summary>
        /// تابع مربوط به به روز رسانی پرسنل
        /// </summary>
        private void UDf_RefreshStaffs()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            var Query = from row in db.tblStaffsTables
                        orderby row.StaffID descending
                        select new
                        {
                            row.StaffID,
                            row.NameSname,
                            row.PhoneNum,
                            row.Notes,
                            row.FaxNum
                        };
            Dgv_Staffs.DataSource = Query;
        }
        /// <summary>
        /// تابع مربوط به درج پرسنل
        /// </summary>
        private void UDF_InsertStaffs()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            tblStaffsTable tblStaffs = new tblStaffsTable();
            tblStaffs.NameSname = Txt_StaffsName.Text.Trim();
            tblStaffs.PhoneNum = Txt_StaffsTel.Text.Trim();
            tblStaffs.FaxNum = Txt_StaffsFax.Text.Trim();
            tblStaffs.Notes = Txt_StaffsMolahezat.Text.Trim();
            db.tblStaffsTables.InsertOnSubmit(tblStaffs);
            db.SubmitChanges();
            Txt_StaffsFax.Text = "";
            Txt_StaffsMolahezat.Text = "";
            Txt_StaffsTel.Text = "";
            Txt_StaffsName.Text = "";
        }
        /// <summary>
        /// نابع مربوط به ویرایش پرسنل
        /// </summary>
        private void UDF_UpdateStaffs()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            tblStaffsTable tblStaffs= db.tblStaffsTables.Single(n => n.StaffID == int.Parse(Txt_StaffsId.Text.Trim()));
            tblStaffs.NameSname = Txt_StaffsName.Text.Trim();
            tblStaffs.PhoneNum = Txt_StaffsTel.Text.Trim();
            tblStaffs.FaxNum = Txt_StaffsFax.Text.Trim();
            tblStaffs.Notes = Txt_StaffsMolahezat.Text.Trim();
            Txt_StaffsFax.Text = "";
            Txt_StaffsId.Text = "";
            Txt_StaffsName.Text = "";
            Txt_StaffsTel.Text = "";
            Txt_StaffsMolahezat.Text = "";
            db.SubmitChanges();
            UDf_RefreshStaffs();
        }
        /// <summary>
        /// تابع مربوط به حذف پرسنل
        /// </summary>
        private void UDF_DeleteStaffs()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            try
            {
                tblStaffsTable tblStaffs = db.tblStaffsTables.Single(n => n.StaffID == int.Parse(Txt_StaffsId.Text.Trim()));
                db.tblStaffsTables.DeleteOnSubmit(tblStaffs);
                db.SubmitChanges();
                Txt_StaffsId.Text = "";
                Txt_StaffsMolahezat.Text = "";
                Txt_StaffsTel.Text = "";
                Txt_StaffsName.Text = "";
                Txt_StaffsFax.Text = "";
            }
            catch
            {
                MessageBox.Show("لطفا ابتدا تمام بلیط های رزرو شده توسط این کارمند را کنسل،سپس کارمند مورد نظر را حذف نمایید");
            }
        }
        /// <summary>
        /// تابع مربوط به جستجوی پیشرفته پرسنل
        /// </summary>
        private void UDf_AdvancedSearchStaffs()
        {
            int int_SearchFlg = 0;
            LSC_TADataContext db = new LSC_TADataContext();
            var Query = from Row in db.tblStaffsTables
                        select Row;
            try
            {
                if (Chk_StaffsId.Checked == true)
                {
                    var Query1 = from Row in Query
                                 where Row.StaffID == int.Parse(Txt_StaffsCod.Text.Trim())
                                 select Row;
                    Query = Query1;
                    int_SearchFlg = 1;
                }
                if (Chk_StaffsName.Checked == true)
                {
                    var Query2 = from Row in Query
                                 where SqlMethods.Like(Row.NameSname, "%" + Txt_StaffsName.Text.Trim() + "%")
                                 orderby Row.StaffID descending
                                 select Row;
                    Query = Query2;
                    int_SearchFlg = 1;
                }
                if (Chk_StaffsTel.Checked == true)
                {
                    var Query2 = from Row in Query
                                 where Row.PhoneNum == Txt_StaffsTel.Text.Trim()
                                 orderby Row.StaffID descending
                                 select Row;
                    Query = Query2;
                    int_SearchFlg = 1;
                }
                if (Chk_StaffsFax.Checked == true)
                {
                    var Query2 = from Row in Query
                                 where Row.FaxNum == Txt_StaffsFax.Text.Trim()
                                 orderby Row.StaffID descending
                                 select Row;
                    Query = Query2;
                    int_SearchFlg = 1;
                }
                if (Chk_StaffsMolahezat.Checked == true)
                {
                    var Query2 = from Row in Query
                                 where Row.Notes == Txt_StaffsMolahezat.Text.Trim()
                                 orderby Row.StaffID descending
                                 select Row;
                    Query = Query2;
                    int_SearchFlg = 1;
                }
                if (int_SearchFlg != 0)
                {
                    Dgv_Staffs.DataSource = Query;
                }
            }
            catch
            {
                MessageBox.Show("لطفا فیلد انتخاب شده را پر نمایید");
            }
        }
        private void Btn_StaffsTaeed_Click(object sender, EventArgs e)
        {
            try
            {
                Pnl_Staffs2.Visible = false;
                Pnl_Staffs1.Visible = true;
                if (int_staffs == 1)
                {
                    UDF_InsertStaffs();
                }
                else if (int_staffs == 2)
                {
                    UDF_UpdateStaffs();
                }
                else if (int_staffs == 3)
                {
                    UDF_DeleteStaffs();
                }
                UDf_RefreshStaffs();
            }
            catch
            {
                MessageBox.Show("لطفا خطای به وجود آمده را برطرف نمایید");
            }
        }
        private void Btn_StaffsLaghv_Click(object sender, EventArgs e)
        {
            try
            {
                Pnl_Staffs2.Visible = false;
                Pnl_Staffs1.Visible = true;
                UDf_RefreshStaffs();
                Txt_StaffsFax.Text = "";
                Txt_StaffsMolahezat.Text = "";
                Txt_StaffsName.Text = "";
                Txt_StaffsTel.Text = "";
            }
            catch
            {
                MessageBox.Show("لطفا خطای به وجود آمده را برطرف نمایید");
            }
        }
        private void Btn_StaffsInsert_Click(object sender, EventArgs e)
        {
            if (Txt_StaffsName.Text == "")
            {
                MessageBox.Show("لطفا نام و نام خانوادگی را وارد نمایید");
            }
            else
            {
                Pnl_Staffs2.Visible = true;
                Pnl_Staffs1.Visible = false;
                int_staffs = 1;
            }
        }
        private void Btn_StaffsUpdate_Click(object sender, EventArgs e)
        {
            if (Txt_StaffsName.Text == "")
            {
                MessageBox.Show("لطفا نام و نام خانوادگی را وارد نمایید");
            }
            else
            { 
            Pnl_Staffs2.Visible = true;
            Pnl_Staffs1.Visible = false;
            int_staffs = 2;
            }
        }
        private void Btn_StaffsDelete_Click(object sender, EventArgs e)
        {
              Txt_StaffsId.Enabled = true;
              if (Txt_StaffsId.Text == "")
              {
                  MessageBox.Show("لطفا کارمند مورد نظرتان را از جدول انتخاب کنید");
              }
              else
              {
                  Pnl_Staffs2.Visible = true;
                  Pnl_Staffs1.Visible = false;
                  int_staffs = 3;
              } 
        }
        private void Btn_StaffsSearch_Click(object sender, EventArgs e)
        {
            Btn_StaffsSearch.BringToFront();
            UDf_RefreshStaffs();
        }
        private void Dgv_Staffs_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            LSC_TADataContext db = new LSC_TADataContext();
            Int32 selectedCellCount = Dgv_Staffs.GetCellCount(DataGridViewElementStates.Selected);
            if (selectedCellCount > 0)
            {
                if (Dgv_Staffs.AreAllCellsSelected(true))
                {
                    MessageBox.Show("لطفاسلول از سطر مورد نظر راانتخاب نماييد!", "Selected Cells");
                }
                else
                {
                    for (int i = 0; i < selectedCellCount; i++)
                    {
                        Txt_StaffsId.Text = Dgv_Staffs.Rows[int.Parse(Dgv_Staffs.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                        Txt_StaffsName.Text = Dgv_Staffs.Rows[int.Parse(Dgv_Staffs.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                        Txt_StaffsTel.Text = Dgv_Staffs.Rows[int.Parse(Dgv_Staffs.SelectedCells[i].RowIndex.ToString())].Cells[2].Value.ToString();
                        Txt_StaffsFax.Text = Dgv_Staffs.Rows[int.Parse(Dgv_Staffs.SelectedCells[i].RowIndex.ToString())].Cells[4].Value.ToString();
                        Txt_StaffsMolahezat.Text = Dgv_Staffs.Rows[int.Parse(Dgv_Staffs.SelectedCells[i].RowIndex.ToString())].Cells[3].Value.ToString();
                    }
                }
            }
        }
        private void Txt_StaffsTel_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_StaffsFax_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_StaffsSearch_Click_1(object sender, EventArgs e)
        {
            UDf_AdvancedSearchStaffs();
            Txt_StaffsTel.Text = "";
            Txt_StaffsFax.Text = "";
            Txt_StaffsSearch.Text = "";
            Txt_StaffsName.Text = "";
            Txt_StaffsMolahezat.Text = "";
            Txt_StaffsId.Text = "";
            Txt_StaffsCod.Text = "";
            Btn_StaffsSearch.SendToBack();
        }

        private void frmStaffs_Load(object sender, EventArgs e)
        {
            Pnl_Staffs2.Visible = false;
            UDf_RefreshStaffs();
        }
    }
}