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 frmServiceTypes : Form
    {
        int int_ServiceType = 0;
        public frmServiceTypes()
        {
            InitializeComponent();
        }
        /// <summary>
        /// تابع مربوط به به روز رسانی نوع سرویس
        /// </summary>
        private void UDf_RefreshServiceType()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            var Query = from row in db.tblServiceTypes
                        orderby row.ServiceTypeID descending
                        select new
                        {
                            row.ServiceTypeID,
                            row.ServiceDesCript
                        };
            Dgv_ServiceTypes.DataSource = Query;
        }
        /// <summary>
        /// تابع مربوط به درج نوع سرویس
        /// </summary>
        private void UDF_InsertServiceType()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            tblServiceType tblStype = new tblServiceType();
            tblStype.ServiceDesCript = Txt_ServiceTypesSharhHarekat.Text.Trim();
            db.tblServiceTypes.InsertOnSubmit(tblStype);
            db.SubmitChanges();
            Txt_ServiceTypesSharhHarekat.Text = "";
            Txt_ServiceTypesSearch.Text = "";
        }
        /// <summary>
        /// تابع مربوط به ویرایش نوع سرویس
        /// </summary>
        private void UDF_UpdateServiceType()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            tblServiceType tblStype = db.tblServiceTypes.Single(n => n.ServiceTypeID == int.Parse(Txt_ServiceTypesSearch.Text.Trim()));
            tblStype.ServiceDesCript = Txt_ServiceTypesSharhHarekat.Text.Trim();
            Txt_ServiceTypesSharhHarekat.Text = "";
            Txt_ServiceTypesSearch.Text = "";
            db.SubmitChanges();
            UDf_RefreshServiceType();
        }
        /// <summary>
        /// تابع مربوط به حذف نوع سرویس
        /// </summary>
        private void UDF_DeleteServiceType()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            try
            {
                tblServiceType tblStype = db.tblServiceTypes.Single(n => n.ServiceTypeID == int.Parse(Txt_ServiceTypesSearch.Text.Trim()));
                db.tblServiceTypes.DeleteOnSubmit(tblStype);
                db.SubmitChanges();
                Txt_ServiceTypesSearch.Text = "";
                Txt_ServiceTypesSharhHarekat.Text = "";
            }
            catch
            {
                MessageBox.Show("لطفا ابتدا رکورد مربوط به نوع سرویس را از فرم سرویس های تعریف شده حذف نمایید  ، سپس به حذف نوع سرویس بپردازید");
            }
        }
        /// <summary>
        /// تابع مربوط به جستجوی پیشرفته نوع سرویس
        /// </summary>
        private void UDf_SearchAdvancedServiceType()
        {
              int int_SearchFlg = 0;
                LSC_TADataContext db = new LSC_TADataContext();
                var Query = from Row in db.tblServiceTypes
                            select Row;
                try
                {
                    if (Chk_ServiceTypesId.Checked == true)
                    {
                        var Query1 = from Row in Query
                                     where Row.ServiceTypeID == int.Parse(Txt_ServiceTypesSearch.Text.Trim())
                                     orderby Row.ServiceTypeID descending
                                     select Row;
                        Query = Query1;
                        int_SearchFlg = 1;
                    }
                    if (Chk_ServiceTypesSharhHarekat.Checked == true)
                    {
                        var Query2 = from Row in Query
                                     where SqlMethods.Like(Row.ServiceDesCript, "%" + Txt_ServiceTypesSharhHarekat.Text.Trim() + "%")
                                     orderby Row.ServiceTypeID descending
                                     select Row;
                        Query = Query2;
                        int_SearchFlg = 1;
                    }
                    if (int_SearchFlg != 0)
                    {
                        Dgv_ServiceTypes.DataSource = Query;
                    }
                }
                catch
                {
                    MessageBox.Show("لطفا فیلد انتخاب شده را پر نمایید");
                }
        }
        private void frmServiceTypes_Load(object sender, EventArgs e)
        {
            UDf_RefreshServiceType();
            Pnl_ServiceTypes2.Visible = false;  
        }
        private void Btn_ServiceTypesUpdate_Click(object sender, EventArgs e)
        {
            if (Txt_ServiceTypesId.Text == "" && Txt_ServiceTypesSharhHarekat.Text == "")
            {
                MessageBox.Show("لطفا کد سرویس و  شرح حرکت را وارد نمایید");
            }
            else
            {
                Pnl_ServiceTypes1.Visible = false;
                Pnl_ServiceTypes2.Visible = true;
                int_ServiceType = 2;
            }
        }
        private void Btn_ServiceTypesSearch_Click(object sender, EventArgs e)
        {
            Btn_ServiceTypesSearch.BringToFront();
            UDf_RefreshServiceType();
        }
        private void Btn_ServiceTypesTaeed_Click(object sender, EventArgs e)
        {
            try
            {
                Pnl_ServiceTypes2.Visible = false;
                Pnl_ServiceTypes1.Visible = true;
                if (int_ServiceType == 1)
                {
                    UDF_InsertServiceType();
                }
                else if (int_ServiceType == 2)
                {
                    UDF_UpdateServiceType();
                }
                else
                {
                    UDF_DeleteServiceType();
                }
                UDf_RefreshServiceType();
            }
            catch
            {
                MessageBox.Show("لطفا خطای به وجود آمده را برطرف نمایید");
            }
        }
        private void Btn_ServiceTypesLaghv_Click(object sender, EventArgs e)
        {
            try
            {
                Pnl_ServiceTypes2.Visible = false;
                Pnl_ServiceTypes1.Visible = true;
                UDf_RefreshServiceType();
                Txt_ServiceTypesSharhHarekat.Text = "";
                Txt_ServiceTypesId.Text = "";
            }
            catch
            {
                MessageBox.Show("لطفا خطای به وجود آمده را بر طرف نمایید");
            }
        }
        private void Btn_ServiceTypesInsert_Click(object sender, EventArgs e)
        {
            if (Txt_ServiceTypesSharhHarekat.Text == "")
            {
                MessageBox.Show("لطفا فیلد مورد نظر را پر نمایید");
            }
            else
            {
                int_ServiceType = 1;
                Pnl_ServiceTypes1.Visible = false;
                Pnl_ServiceTypes2.Visible = true;
            }
               UDf_RefreshServiceType();
        }
        private void Btn_ServiceTypesDelete_Click(object sender, EventArgs e)
        {
            if (Txt_ServiceTypesSearch.Text == "")
            {
                MessageBox.Show("لطفا کد سرویس را وارد نمایید");
            }
            else
            {
                Pnl_ServiceTypes1.Visible = false;
                Pnl_ServiceTypes2.Visible = true;
                int_ServiceType = 3;
            }
        }
        private void Dgv_ServiceTypes_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            LSC_TADataContext db = new LSC_TADataContext();
            Int32 selectedCellCount = Dgv_ServiceTypes.GetCellCount(DataGridViewElementStates.Selected);
            if (selectedCellCount > 0)
            {
                if (Dgv_ServiceTypes.AreAllCellsSelected(true))
                {
                    MessageBox.Show("لطفاسلول از سطر مورد نظر راانتخاب نماييد!", "Selected Cells");
                }
                else
                {
                    for (int i = 0; i < selectedCellCount; i++)
                    {
                        Txt_ServiceTypesSearch.Text = Dgv_ServiceTypes.Rows[int.Parse(Dgv_ServiceTypes.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                        Txt_ServiceTypesSharhHarekat.Text = Dgv_ServiceTypes.Rows[int.Parse(Dgv_ServiceTypes.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();   
                    }
                }
            }
        }
        private void Btn_ServiceTypesSearch_Click_1(object sender, EventArgs e)
        {
            UDf_SearchAdvancedServiceType();
            Txt_ServiceTypesSearch.Text = "";
            Txt_ServiceTypesSharhHarekat.Text = "";
            Btn_ServiceTypesSearch.SendToBack();
        }
    }
}