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

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

        private void UDF_GrdFill()
        {
            LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
            var Query = from Row in db.tblBimars
                        orderby Row.BimarID descending
                        select new
                        {
                            Row.BimarID,
                            Row.BimarName,
                            Row.BimarFamily,
                            Row.DateOzviatSH,
                            Row.BimarTell,
                            Row.BimarMobail,
                            Row.BimarAddress,
                            Row.BimarIsActive

                        };
            Grd_BimarInfo.DataSource = Query;

        }

        private void UDF_ClearTextBox()
        {
            Txt_Address.Clear();
            Txt_Family.Clear();
            Txt_Mobail.Clear();
            Txt_Name.Clear();
            Txt_Tell.Clear();
            Chb_IsActive.Checked = false;
        }

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

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

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            if (Txt_Address.Text == "" || Txt_Family.Text == "" || Txt_Mobail.Text == "" || Txt_Name.Text == "" || Txt_Tell.Text == "")
            {
                MessageBox.Show("ورود نام ، نام خانوادگی ،تلفن ، تلفن همراه ، آدرس و وضعیت فعالیت بیمار الزامی است");
                return;
            }
            LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
            tblBimar bimar = new tblBimar();
            bimar.BimarName = Txt_Name.Text.Trim();
            bimar.BimarFamily = Txt_Family.Text.Trim();
            Class_ShamsiTools shamsi = new Class_ShamsiTools();
            shamsi.UDF_GetCurrentShamsiDate();
            bimar.DateOzviatSH = shamsi.CurrentShmasiDate;
            bimar.BimarTell = Txt_Tell.Text.Trim();
            bimar.BimarMobail = Txt_Mobail.Text.Trim();
            bimar.BimarAddress = Txt_Address.Text.Trim();
            bimar.BimarIsActive = Chb_IsActive.Checked;
            db.tblBimars.InsertOnSubmit(bimar);
            db.SubmitChanges();
            UDF_GrdFill();
            btnAdd.BringToFront();
            Btn_Save.SendToBack();
            UDF_ClearTextBox();
            MessageBox.Show("رکورد جدید درج شد.");

        }

        private void Grd_BimarInfo_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Int32 selectedCellCount = Grd_BimarInfo.GetCellCount(DataGridViewElementStates.Selected);
            if (selectedCellCount > 0)
            {
                if (Grd_BimarInfo.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++)
                    {
                        Boolean Bln_Check;
                        Txt_IDBimar.Text = Grd_BimarInfo.Rows[int.Parse(Grd_BimarInfo.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                        Txt_Name.Text = Grd_BimarInfo.Rows[int.Parse(Grd_BimarInfo.SelectedCells[i].RowIndex.ToString())].Cells[1].Value.ToString();
                        Txt_Family.Text = Grd_BimarInfo.Rows[int.Parse(Grd_BimarInfo.SelectedCells[i].RowIndex.ToString())].Cells[2].Value.ToString();
                        Txt_Tell.Text = Grd_BimarInfo.Rows[int.Parse(Grd_BimarInfo.SelectedCells[i].RowIndex.ToString())].Cells[4].Value.ToString();
                        Txt_Mobail.Text = Grd_BimarInfo.Rows[int.Parse(Grd_BimarInfo.SelectedCells[i].RowIndex.ToString())].Cells[5].Value.ToString();
                        Txt_Address.Text = Grd_BimarInfo.Rows[int.Parse(Grd_BimarInfo.SelectedCells[i].RowIndex.ToString())].Cells[6].Value.ToString();
                        Bln_Check = Boolean.Parse(Grd_BimarInfo.Rows[int.Parse(Grd_BimarInfo.SelectedCells[i].RowIndex.ToString())].Cells[7].Value.ToString());
                        if (Bln_Check == true)
                        {
                            Chb_IsActive.Checked = true;
                        }
                        else if (Bln_Check == false)
                        {
                            Chb_IsActive.Checked = false;
                        }

                    }
                }
            }
        }

        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            if (Txt_Name.Text == "")
            {
                MessageBox.Show("انتخاب بیمار مورد نظر برای حذف الزامی است");
                return;
            }
            DialogResult dr = MessageBox.Show("آیا نسبت به عمل حذف رکورد اطمینان دارید", "اخطار حذف", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
                tblBimar bimar = db.tblBimars.Single(n => n.BimarID == Decimal.Parse(Txt_IDBimar.Text.Trim()));

                db.tblBimars.DeleteOnSubmit(bimar);
                db.SubmitChanges();
                UDF_ClearTextBox();
                UDF_GrdFill();
            }
        }

        private void Btn_Edit_Click(object sender, EventArgs e)
        {
            if (Txt_IDBimar.Text == "") 
            {
                MessageBox.Show("انتخاب بیمار مورد نظر برای حذف الزامی است");
                return;
            }
            DialogResult dr = MessageBox.Show("آیا نسبت به عمل ویرایش اطمینان دارید" , "اخطار ویرایش" , MessageBoxButtons.OKCancel , MessageBoxIcon.Warning );
            if (dr == DialogResult.OK)
            {
                LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
                tblBimar bimar = db.tblBimars.Single(n => n.BimarID == Decimal.Parse(Txt_IDBimar.Text.Trim()));
                bimar.BimarName = Txt_Name.Text.Trim();
                bimar.BimarFamily = Txt_Family.Text.Trim();
                bimar.BimarTell = Txt_Tell.Text.Trim();
                bimar.BimarMobail = Txt_Mobail.Text.Trim();
                bimar.BimarAddress = Txt_Address.Text.Trim();
                bimar.BimarIsActive = Chb_IsActive.Checked;
                db.SubmitChanges();
                UDF_GrdFill();
                UDF_ClearTextBox();
            }
        }

        private void Btn_Search_Click(object sender, EventArgs e)
        {
            if (Txt_Search.Text == "")
            {
                MessageBox.Show("لطفا فیلد جستجو را وارد کنید");
                return;
            }
            if (Rdo_BimarID.Checked == true)
            {
                LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
                var Query = from Row in db.tblBimars
                            where Row.BimarID == Decimal.Parse(Txt_Search.Text.Trim())
                            select new
                            {
                                Row.BimarID,
                                Row.BimarName,
                                Row.BimarFamily,
                                Row.DateOzviatSH,
                                Row.BimarTell,
                                Row.BimarMobail,
                                Row.BimarAddress,
                                Row.BimarIsActive

                            };
                Grd_BimarInfo.DataSource = Query;
                if (Query.Count() <= 0)
                {
                    Txt_Search.Text = "";
                    Btn_Refresh.BringToFront();
                    Btn_Search.SendToBack();
                    MessageBox.Show("رکوردی یافته نشد");
                }
            }
            else if (Rdo_Name.Checked == true)
            {
                LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
                var Query = from Row in db.tblBimars
                            where SqlMethods.Like(Row.BimarName , "%" + Txt_Search.Text.Trim() + "%")
                            select new
                            {
                                Row.BimarID,
                                Row.BimarName,
                                Row.BimarFamily,
                                Row.DateOzviatSH,
                                Row.BimarTell,
                                Row.BimarMobail,
                                Row.BimarAddress,
                                Row.BimarIsActive

                            };
                Grd_BimarInfo.DataSource = Query;
                if (Query.Count() <= 0)
                {
                    Txt_Search.Text = "";
                    Btn_Refresh.BringToFront();
                    Btn_Search.SendToBack();
                    MessageBox.Show("رکوردی یافته نشد");
                }
            }
            else if (Rdo_Family.Checked == true)
            {
                LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
                var Query = from Row in db.tblBimars
                            where SqlMethods.Like(Row.BimarFamily, "%" + Txt_Search.Text.Trim() + "%")
                            select new
                            {
                                Row.BimarID,
                                Row.BimarName,
                                Row.BimarFamily,
                                Row.DateOzviatSH,
                                Row.BimarTell,
                                Row.BimarMobail,
                                Row.BimarAddress,
                                Row.BimarIsActive
                            };
                Grd_BimarInfo.DataSource = Query;
                if (Query.Count() <= 0)
                {
                    Txt_Search.Text = "";
                    Btn_Refresh.BringToFront();
                    Btn_Search.SendToBack();
                    MessageBox.Show("رکوردی یافته نشد.");
                }
            }
        }

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

       
    }
}