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;
using System.Data.Linq.SqlClient;

namespace RadioLogy
{
    public partial class frmService : Form
    {
        public frmService()
        {
            InitializeComponent();
        }

        private void UDF_GrdFill()
        {
            LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
            var Query = from Row in db.tblRadioLogyServiceTypes 
                        orderby Row.ServiceTypeID  descending
                        select new
                        {
                            Row.ServiceTypeID,
                            Row.ServiceTypeDescription 
                            
                        };
            Grd_Service.DataSource = Query;


        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            Txt_ServiceDesc.Text = String.Empty;
            btnAdd.SendToBack();
            Btn_Save.BringToFront();
            MessageBox.Show("لطفا اطلاعات را وارد کنید.");
        }

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            if (Txt_ServiceDesc.Text == "")
            {
                MessageBox.Show("لطفا شرح خدمت را وارد کنید");
                return;
            }
            LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
            tblRadioLogyServiceType service = new tblRadioLogyServiceType();
            service.ServiceTypeDescription = Txt_ServiceDesc.Text.Trim();
            db.tblRadioLogyServiceTypes.InsertOnSubmit(service);
            db.SubmitChanges();
            UDF_GrdFill();
            btnAdd.BringToFront();
            Btn_Save.SendToBack();
            Txt_ServiceDesc.Clear();
        }

        private void Grd_Service_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Int32 selectedCellCount = Grd_Service.GetCellCount(DataGridViewElementStates.Selected);
            if (selectedCellCount > 0)
            {
                if (Grd_Service.AreAllCellsSelected(true))
                {
                    MessageBox.Show("All cells are selected", "Selected Cells");
                }
                else
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    for (int i = 0; i < selectedCellCount; i++)
                    {
                        Txt_ServiceID.Text = Grd_Service.Rows[int.Parse(Grd_Service.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                        Txt_ServiceDesc.Text = Grd_Service.Rows[int.Parse(Grd_Service.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                    }
                }
            }
        }

        private void frmService_Load(object sender, EventArgs e)
        {
            UDF_GrdFill();
        }

        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            if (Txt_ServiceDesc.Text == "")
            {
                MessageBox.Show("انتخاب خدمت مورد نظر برای حذف الزامی است");
                return;
            }
            DialogResult dr = MessageBox.Show("آیا نسبت به عمل حذف اطمینان دارید", "اخطار حذف", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
                tblRadioLogyServiceType service = db.tblRadioLogyServiceTypes.Single(n => n.ServiceTypeID == int.Parse(Txt_ServiceID.Text.Trim()));
                db.tblRadioLogyServiceTypes.DeleteOnSubmit(service);
                db.SubmitChanges();
                Txt_ServiceDesc.Clear();
                UDF_GrdFill();
            }
        }

        private void Btn_Edit_Click(object sender, EventArgs e)
        {
            if (Txt_ServiceID.Text == "")
            {
                MessageBox.Show("انتخاب خدمت مورد نظر برای ویرایش الزامی است");
                return;
            }
            DialogResult dr = MessageBox.Show("آیا نسبت به عمل ویرایش اطمینان دارید" , "اخطار ویرایش" , MessageBoxButtons.OKCancel , MessageBoxIcon.Warning );
            if (dr == DialogResult.OK)
            {
                LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
                tblRadioLogyServiceType service = db.tblRadioLogyServiceTypes.Single(n => n.ServiceTypeID == int.Parse(Txt_ServiceID.Text.Trim()));
                service.ServiceTypeDescription = Txt_ServiceDesc.Text.Trim();
                db.SubmitChanges();
                Txt_ServiceDesc.Clear();
                UDF_GrdFill();
            }
        }

        private void Btn_Search_Click(object sender, EventArgs e)
        {
            if (Txt_ServiceDesc.Text == "")
            {
                MessageBox.Show("لطفا شرح ویا بخشی از شرح خدمت را وارد کنید.");
                return;
            }
            LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
            var Query = from Row in db.tblRadioLogyServiceTypes 
                        where SqlMethods.Like(Row.ServiceTypeDescription , "%" + Txt_ServiceDesc.Text.Trim() + "%")
                        select new
                        {
                            Row.ServiceTypeID ,
                            Row.ServiceTypeDescription 
                           
                        };
            Grd_Service.DataSource = Query;
            if (Query.Count() <= 0)
            {
                Txt_ServiceDesc.Text = "";
                Btn_Refresh.BringToFront();
                Btn_Search.SendToBack();
                MessageBox.Show("رکوردی یافته نشد.");
            }
        }

        private void Btn_Refresh_Click(object sender, EventArgs e)
        {
            UDF_GrdFill();
            Btn_Refresh.SendToBack();
            Btn_Search.BringToFront();
        }
    }
}
