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 frmServiceDef : Form
    {
        int int_ServiceDef = 0;
        public frmServiceDef()
        {
            InitializeComponent();
        }
        /// <summary>
        /// تابع به روز رسانی سرویس جدید 
        /// </summary>
        private void UDf_RefreshServiceDef()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            var Query = from row in db.tblServicesDefs
                        orderby row.ServiceID descending
                        select new
                        {
                            row.ServiceID,
                            row.ServiceDescript,
                            row.ServiceDate,
                            row.Quantity,
                            row.Str_ServiceDescript,
                            row.DefPrice,
                            row.ExpireBit
                        };
            Dgv_ServiceDef.DataSource = Query;
        }
        /// <summary>
        ///  تابع پر کردن کامو باکس نوع سرویس 
        /// </summary>
        private void UDF_FillCmbService()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            var Query = from row in db.tblServiceTypes
                        orderby row.ServiceTypeID descending
                        select new
                        {
                            row.ServiceTypeID,
                            row.ServiceDesCript
                        };
            Cmb_ServiceDefTypeSv.DisplayMember = "ServiceDesCript";
            Cmb_ServiceDefTypeSv.ValueMember = "ServiceTypeID";
            Cmb_ServiceDefTypeSv.DataSource = Query;
        }
        /// <summary>
        /// تابع مربوط به درج سرویس جدید 
        /// </summary>
        private void UDF_InsertServiceDef()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            tblServicesDef tblSDef = new tblServicesDef();
            tblSDef.ServiceDescript = Txt_ServiceDefSharh.Text.Trim();
            tblSDef.ServiceDate = Txt_ServiceDefDate.Text.Trim();
            tblSDef.Quantity = int.Parse(Txt_ServiceDefCapacity.Text.Trim());
            tblSDef.ServiceTypeID = int.Parse(Cmb_ServiceDefTypeSv.SelectedValue.ToString().Trim());
            tblSDef.ExpireBit = true;
            tblSDef.DefPrice =Decimal.Parse(Txt_ServiceDefPrice.Text.ToString());
            db.tblServicesDefs.InsertOnSubmit(tblSDef);
            db.SubmitChanges();
            Txt_ServiceDefCapacity.Text = "";
            Txt_ServiceDefDate.Text = "";
            Txt_ServiceDefSharh.Text = "";
            Txt_ServiceDefId.Text = "";
            Cmb_ServiceDefTypeSv.Text = "";
            Txt_ServiceDefPrice.Text = "";
        }
        /// <summary>
        ///   تابع مربوط به ویرایش تعریف سرویس
        /// </summary>
        private void UDF_UpdateServiceDef()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            tblServicesDef tblSDef = db.tblServicesDefs.Single(n => n.ServiceID == int.Parse(Txt_ServiceDefId.Text.Trim()));
            tblSDef.ServiceID = int.Parse(Txt_ServiceDefId.Text.Trim());
            tblSDef.ServiceDescript = Txt_ServiceDefSharh.Text.Trim();
            tblSDef.ServiceDate =Txt_ServiceDefDate.Text.Trim();
            tblSDef.Quantity = int.Parse(Txt_ServiceDefCapacity.Text.Trim());
            tblSDef.ServiceTypeID = int.Parse(Cmb_ServiceDefTypeSv.SelectedValue.ToString().Trim());
            tblSDef.DefPrice = Decimal.Parse(Txt_ServiceDefPrice.Text.ToString());
            Txt_ServiceDefCapacity.Text = "";
            Txt_ServiceDefDate.Text = "";
            Txt_ServiceDefId.Text = "";
            Txt_ServiceDefSharh.Text = "";
            Cmb_ServiceDefTypeSv.Text = "";
            Txt_ServiceDefPrice.Text = "";
            db.SubmitChanges();
            UDf_RefreshServiceDef();
        }
        /// <summary>
        /// تابع مربوط به حذف سرویس
        /// </summary>
        private void UDF_DeleteServiceDef()
        {
            LSC_TADataContext db = new LSC_TADataContext();
            try
            {
                tblServicesDef tblSDef = db.tblServicesDefs.Single(n => n.ServiceID == int.Parse(Txt_ServiceDefId.Text.Trim()));
                db.tblServicesDefs.DeleteOnSubmit(tblSDef);
                db.SubmitChanges();
                Txt_ServiceDefCapacity.Text = "";
                Txt_ServiceDefDate.Text = "";
                Txt_ServiceDefId.Text = "";
                Txt_ServiceDefSharh.Text = "";
                Cmb_ServiceDefTypeSv.Text = "";
                Txt_ServiceDefPrice.Text = "";
            }
            catch
            {
                MessageBox.Show("لطفا ابتدا رکورد مربوط به سرویس مورد نطر را از فرم رزرو بلیط حذف نمایید ، سپس به حذف سرویس بپردازید");
            }
        }
        /// <summary>
        /// تابع مربوط به جستجوی پیشرفته تعریف سرویس
        /// </summary>
        private void UDf_AdvancedSearchServiceDef()
        {
            int int_SearchFlg = 0;
            LSC_TADataContext db = new LSC_TADataContext();
            var Query = from Row in db.tblServicesDefs
                        select Row;
            try
            {
                if (Chk_ServiceDefId.Checked == true)
                {
                    var Query1 = from Row in Query
                                 where Row.ServiceID == int.Parse(Txt_ServiceDefCod.Text.Trim())
                                 select Row;
                    Query = Query1;
                    int_SearchFlg = 1;
                }
                if (Chk__ServiceDefSharh.Checked == true)
                {
                    var Query2 = from Row in Query
                                 where SqlMethods.Like(Row.ServiceDescript, "%" + Txt_ServiceDefSharh.Text.Trim() + "%")
                                 orderby Row.ServiceID descending
                                 select Row;
                    Query = Query2;
                    int_SearchFlg = 1;
                }
                if (Chk_ServiceDefDate.Checked == true)
                {
                    var Query3 = from Row in Query
                                 where Row.ServiceDate == Txt_ServiceDefDate.Text.Trim()
                                 select Row;
                    Query = Query3;
                    int_SearchFlg = 1;
                }
                if (Chk_ServiceDefCapacity.Checked == true)
                {
                    var Query4 = from Row in Query
                                 where Row.Quantity ==int.Parse(Txt_ServiceDefCapacity.Text.Trim())
                                 select Row;
                    Query = Query4;
                    int_SearchFlg = 1;
                }
                if (Chk_ServiceDefTypeSv.Checked == true)
                {
                    var Query5 = from Row in Query
                                 where Row.ServiceTypeID == int.Parse(Cmb_ServiceDefTypeSv.SelectedValue.ToString().Trim())
                                 select Row;
                    Query = Query5;
                    int_SearchFlg = 1;
                }
                if (Chk_ServiceDefPrice.Checked == true)
                {
                    var Query5 = from Row in Query
                                 where Row.DefPrice == decimal.Parse(Txt_ServiceDefPrice.Text.Trim())
                                 select Row;
                    Query = Query5;
                    int_SearchFlg = 1;
                }
                if (int_SearchFlg != 0)
                {
                    Dgv_ServiceDef.DataSource = Query;
                }
            }
            catch
            {
                MessageBox.Show("لطفا فیلد انتخاب شده را پر نمایید");
            }
        }
        private void frmServiceDef_Load(object sender, EventArgs e)
        { 
            UDf_RefreshServiceDef();
            UDF_FillCmbService();
            Pnl_ServiceDef2.Visible = false;
        }
        private void Btn_ServiceDefInsert_Click(object sender, EventArgs e)
        {
            if (Txt_ServiceDefCapacity.Text == "" && Txt_ServiceDefDate.Text == "" && Txt_ServiceDefSharh.Text == "")
            {
                MessageBox.Show("لطفا فیلد های خالی را پر نمایید");
            }
            else
            {
                Pnl_ServiceDef1.Visible = false;
                Pnl_ServiceDef2.Visible = true;
                int_ServiceDef = 1;
            }
        }
        private void Btn_ServiceDefUpdate_Click(object sender, EventArgs e)
        {
            if (Txt_ServiceDefCapacity.Text == "" && Txt_ServiceDefDate.Text == "" && Txt_ServiceDefId.Text == "" && Txt_ServiceDefSharh.Text == "")
            {
                MessageBox.Show("لطفا فیلد های خالی  را پر نمایید");
            }
            else
            {
                Pnl_ServiceDef1.Visible = false;
                Pnl_ServiceDef2.Visible = true;
                int_ServiceDef = 2;
            }
        }
        private void Btn_ServiceDefDelete_Click(object sender, EventArgs e)
        {
            if (Txt_ServiceDefId.Text == "")
            {
                MessageBox.Show("لطفا کد سرویس  را وارد نمایید");
            }
            else
            {
                Pnl_ServiceDef1.Visible = false;
                Pnl_ServiceDef2.Visible = true;
                int_ServiceDef = 3;
            }
        }
        private void Btn_ServiceDefSearch_Click(object sender, EventArgs e)
        {
            Btn_ServiceDefSearch.BringToFront();
            UDf_RefreshServiceDef();
        }
        private void Btn_ServiceDefTaeed_Click(object sender, EventArgs e)
        {
            try
            {
                Pnl_ServiceDef1.Visible = true;
                Pnl_ServiceDef2.Visible = false;
                if (int_ServiceDef == 1)
                {
                    UDF_InsertServiceDef();
                }
                else if (int_ServiceDef == 2)
                {
                    UDF_UpdateServiceDef();
                }
                else if (int_ServiceDef == 3)
                {
                    UDF_DeleteServiceDef();
                }
                UDf_RefreshServiceDef();
            }
            catch
            {
                MessageBox.Show("لطفا خطای به وجود آمده را برطرف نمایید");
            }
        }
        private void Btn_ServiceDefLaghv_Click(object sender, EventArgs e)
        {
            Pnl_ServiceDef1.Visible = true;
            Pnl_ServiceDef2.Visible = false;
        }

        int int_ServiceID1 = 0;
        private void Dgv_ServiceDef_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            LSC_TADataContext db = new LSC_TADataContext();
            Int32 selectedCellCount = Dgv_ServiceDef.GetCellCount(DataGridViewElementStates.Selected);
            int int_Counter = 0;
            if (selectedCellCount > 0)
            {
                if (Dgv_ServiceDef.AreAllCellsSelected(true))
                {
                    MessageBox.Show("لطفاسلول از سطر مورد نظر راانتخاب نماييد!", "Selected Cells");
                }
                else
                {
                    for (int i = 0; i < selectedCellCount; i++)
                    {
                        if (int_Counter == 0)
                        {
                            int_ServiceID1 = int.Parse(Dgv_ServiceDef.Rows[int.Parse(Dgv_ServiceDef.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString());
                            Cmb_ServiceDefTypeSv.SelectedValue = UDF_GetServiceTypeID(int_ServiceID1);
                        }
                        int_Counter++;
                        Txt_ServiceDefId.Text = Dgv_ServiceDef.Rows[int.Parse(Dgv_ServiceDef.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                        Txt_ServiceDefSharh.Text = Dgv_ServiceDef.Rows[int.Parse(Dgv_ServiceDef.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                        Txt_ServiceDefDate.Text = Dgv_ServiceDef.Rows[int.Parse(Dgv_ServiceDef.SelectedCells[i].RowIndex.ToString())].Cells[2].Value.ToString();
                        Txt_ServiceDefCapacity.Text = Dgv_ServiceDef.Rows[int.Parse(Dgv_ServiceDef.SelectedCells[i].RowIndex.ToString())].Cells[3].Value.ToString();
                        string str_CmbService = Dgv_ServiceDef.Rows[int.Parse(Dgv_ServiceDef.SelectedCells[i].RowIndex.ToString())].Cells[4].Value.ToString();
                        Txt_ServiceDefPrice.Text = Dgv_ServiceDef.Rows[int.Parse(Dgv_ServiceDef.SelectedCells[i].RowIndex.ToString())].Cells[5].Value.ToString();
                    }
                }
            }
        }
        private int UDF_GetServiceTypeID(int int_SereviceID)
        {
            int int_ServiceTypeID = 0;
            LSC_TADataContext db = new LSC_TADataContext();
            tblServicesDef tbSDef = db.tblServicesDefs.Single(n => n.ServiceID == int_SereviceID);
            try
            {
                int_ServiceTypeID = int.Parse(tbSDef.ServiceTypeID.ToString());
            }
            catch
            {
                int_ServiceTypeID = 0;
            }
            return int_ServiceTypeID;
        }
        private void Txt_ServiceDefCapacity_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_ServiceDefPrice_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_ServiceDefDate_KeyPress(object sender, KeyPressEventArgs e)
        {
            int int_Ascii = Convert.ToInt16(e.KeyChar);
            if (int_Ascii >= 47 && int_Ascii <= 57 || int_Ascii == 8 )
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        private void Btn_ServiceDefSearch_Click_1(object sender, EventArgs e)
        {
            UDf_AdvancedSearchServiceDef();
            Txt_ServiceDefSharh.Text = "";
            Txt_ServiceDefPrice.Text = "";
            Txt_ServiceDefDate.Text = "";
            Txt_ServiceDefCod.Text = "";
            Txt_ServiceDefCapacity.Text = "";
            Btn_ServiceDefSearch.SendToBack();
        }
    }
}