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 frmPaziresh : Form
    {
        public frmPaziresh()
        {
            InitializeComponent();
        }

        private void UDF_CmbFill()
        {
            LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
            var QueryBimar = from Row in db.xtblBimars
                             orderby Row.BimarID descending
                             select new
                             {
                                 Row.BimarID,
                                 Row.Expr1
                             };
            Cmb_NameAndFamilyBimar.DisplayMember = "Expr1";
            Cmb_NameAndFamilyBimar.ValueMember = "BimarID";
            Cmb_NameAndFamilyBimar.DataSource = QueryBimar;

            var QueryTypeBime = from Row in db.tblBimehTypes
                                orderby Row.BimehTypeID descending
                                select new
                                {
                                    Row.BimehTypeID,
                                    Row.BimehDescription
                                };
            Cmb_TypeBime.DisplayMember = "BimehDescription";
            Cmb_TypeBime.ValueMember = "BimehTypeID";
            Cmb_TypeBime.DataSource = QueryTypeBime;

            var QueryTypeService = from Row in db.tblRadioLogyServiceTypes
                                   orderby Row.ServiceTypeID descending
                                   select new
                                   {
                                       Row.ServiceTypeID,
                                       Row.ServiceTypeDescription
                                   };
            Cmb_TypeService.DisplayMember = "ServiceTypeDescription";
            Cmb_TypeService.ValueMember = "ServiceTypeID";
            Cmb_TypeService.DataSource = QueryTypeService;
        }

        private void UDF_GrdFill()
        {
            LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
            var Query = from Row in db.tblPazireshes
                        orderby Row.PazireshID descending
                        select new
                        {
                            Row.PazireshID,
                            Row.BimarID,
                            Row.BimaryDescription,
                            Row.PazireshDateSh,
                            Row.BimehTypeID,
                            Row.ServiceTypeID,
                            Row.HaggeVisit
                        };
            Grd_Paziresh.DataSource = Query;
        }

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

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

        }

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            if (Txt_BimariDesc.Text == "" || Txt_HaghVisit.Text == "")
            {
                MessageBox.Show("ورود موارد الزامی است");
                return;
            }
            LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
            tblPaziresh paziresh = new tblPaziresh();
            Class_ShamsiTools shamsi = new Class_ShamsiTools();
            paziresh.BimarID = Decimal.Parse(Cmb_NameAndFamilyBimar.SelectedValue.ToString());
            paziresh.BimaryDescription = Txt_BimariDesc.Text.Trim();
            shamsi.UDF_GetCurrentShamsiDate();
            paziresh.PazireshDateSh = shamsi.CurrentShmasiDate;
            paziresh.BimehTypeID = int.Parse(Cmb_TypeBime.SelectedValue.ToString());
            paziresh.ServiceTypeID = int.Parse(Cmb_TypeService.SelectedValue.ToString());
            paziresh.HaggeVisit = Decimal.Parse(Txt_HaghVisit.Text.Trim());
            db.tblPazireshes.InsertOnSubmit(paziresh);
            db.SubmitChanges();
            UDF_CmbFill();
            UDF_GrdFill();
            btnAdd.BringToFront();
            Btn_Save.SendToBack();
            Txt_BimariDesc.Clear();
            Txt_HaghVisit.Clear();
            MessageBox.Show("رکورد جدید درج شد");
        }

        private void Grd_Paziresh_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Int32 selectedCellCount = Grd_Paziresh.GetCellCount(DataGridViewElementStates.Selected);
            if (selectedCellCount > 0)
            {
                if (Grd_Paziresh.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_PazireshID.Text = Grd_Paziresh.Rows[int.Parse(Grd_Paziresh.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                        Txt_BimariDesc.Text = Grd_Paziresh.Rows[int.Parse(Grd_Paziresh.SelectedCells[i].RowIndex.ToString())].Cells[2].Value.ToString();
                        Txt_HaghVisit.Text = Grd_Paziresh.Rows[int.Parse(Grd_Paziresh.SelectedCells[i].RowIndex.ToString())].Cells[6].Value.ToString();
                    }
                    UDF_SelectCmbValue(Decimal.Parse(Txt_PazireshID.Text.Trim()));
                }
            }
        }

        private void UDF_SelectCmbValue(Decimal Dec_PazireshID)
        {
            LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
            tblPaziresh paziresh = db.tblPazireshes.Single(n => n.PazireshID == Dec_PazireshID );
            Cmb_NameAndFamilyBimar.SelectedValue = paziresh.BimarID;
            Cmb_TypeBime.SelectedValue = paziresh.BimehTypeID;
            Cmb_TypeService.SelectedValue = paziresh.ServiceTypeID;
        }

        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            if (Txt_BimariDesc.Text == "")
            {
                MessageBox.Show("انتخاب پذیرش مورد نظر برای حذف الزامی است");
                return;
            }
            DialogResult dr = MessageBox.Show( "آیا نسبت به عمل حذف اطمینان دارید؟" , "اخطاریه حذف" , MessageBoxButtons.OKCancel ,MessageBoxIcon.Warning );
            if (dr == DialogResult.OK)
            {
                LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
                tblPaziresh paziresh = db.tblPazireshes.Single(n => n.PazireshID == Decimal.Parse(Txt_PazireshID.Text.Trim()));
                db.tblPazireshes.DeleteOnSubmit(paziresh);
                db.SubmitChanges();
                Txt_BimariDesc.Clear();
                Txt_HaghVisit.Clear();
                UDF_CmbFill();
                UDF_GrdFill();
            }
        }

        private void Btn_Edit_Click(object sender, EventArgs e)
        {
            if (Txt_PazireshID.Text == "")
            {
                MessageBox.Show("انتخاب پذیرش مورد نظر برای ویرایش الزامی است");
                return;
            }
            DialogResult dr = MessageBox.Show( "آیا نسبت به عمل ویرایش اطمینان دارید" , "اخطاریه ویرایش" ,MessageBoxButtons.OKCancel , MessageBoxIcon.Warning );
            if (dr == DialogResult.OK)
            {
                LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
                tblPaziresh paziresh = db.tblPazireshes.Single(n => n.PazireshID == Decimal.Parse(Txt_PazireshID.Text.Trim()));
                //Class_ShamsiTools shamsi = new Class_ShamsiTools();
                paziresh.BimarID = Decimal.Parse(Cmb_NameAndFamilyBimar.SelectedValue.ToString());
                paziresh.BimaryDescription = Txt_BimariDesc.Text.Trim();
                //shamsi.UDF_GetCurrentShamsiDate();
                //paziresh.PazireshDateSh = shamsi.CurrentShmasiDate;
                paziresh.BimehTypeID = int.Parse(Cmb_TypeBime.SelectedValue.ToString());
                paziresh.ServiceTypeID = int.Parse(Cmb_TypeService.SelectedValue.ToString());
                paziresh.HaggeVisit = Decimal.Parse(Txt_HaghVisit.Text.Trim());
                
                db.SubmitChanges();
                UDF_CmbFill();
                UDF_GrdFill();
                Txt_BimariDesc.Clear();
                Txt_HaghVisit.Clear();
            }
        }

        private void Btn_Search_Click(object sender, EventArgs e)
        {
            if (Txt_Search.Text == "")
            {
                MessageBox.Show("لطفا کد بیمار را وارد کنید");
                return;
            }
            LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
            var Query = from Row in db.tblPazireshes
                        where Row.BimarID == Decimal.Parse(Txt_Search.Text.Trim())
                        select new
                        {
                            Row.PazireshID,
                            Row.BimarID,
                            Row.BimaryDescription,
                            Row.PazireshDateSh,
                            Row.BimehTypeID,
                            Row.ServiceTypeID,
                            Row.HaggeVisit
                        };
            Grd_Paziresh.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();
            UDF_CmbFill();
            Btn_Search.BringToFront();
            Btn_Refresh.SendToBack();
        }
    }
}