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;

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

        private void UDF_GrdFill()
        {
            LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
            var Query = from Row in db.tblRadioLogyPics
                        orderby Row.RadPicID descending
                        select new
                        {
                            Row.RadPicID ,
                            Row.PazireshID ,
                            Row.RadPicDescription ,
                            Row.RadiologOperator ,
                            Row.DateOfActPicSH 

                        };
            Grd_Picture.DataSource = Query;
        }

        private void UDF_CmbFill()
        {
            LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
            var Query = from Row in db.xtblPazireshes 
                        orderby Row.PazireshID descending
                        select new
                        {
                            Row.PazireshID,
                            Row.Expr1 
                        };
            Cmb_PazireshID.DisplayMember = "Expr1";
            Cmb_PazireshID.ValueMember = "PazireshID";
            Cmb_PazireshID.DataSource = Query;

            var QueryOperator = from Row in db.tblUsers
                                orderby Row.UsersID descending
                                select new
                                {
                                    Row.UsersID ,
                                    Row.UsersName 
                                };
            Cmb_Operator.DisplayMember = "UsersName";
            Cmb_Operator.ValueMember = "UsersID";
            Cmb_Operator.DataSource = QueryOperator;

            var QuerySearch = from Row in db.xtblnumberPazireshes
                              orderby Row.PazireshID descending
                              select new
                              {
                                  Row.PazireshID  ,
                                  Row.NameSname 
                              };
            Cmb_Search.DisplayMember = "NameSname";
            Cmb_Search.ValueMember = "PazireshID";
            Cmb_Search.DataSource = QuerySearch;
        }
        private void frmPicture_Load(object sender, EventArgs e)
        {
            UDF_CmbFill();
            UDF_GrdFill();
        }

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

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            if (Txt_PicDesc.Text == "")
            {
                MessageBox.Show("ورود اطلاعات الزامی می باشد");
                return;
            }
            LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
            tblRadioLogyPic pic = new tblRadioLogyPic();
            Class_ShamsiTools shamsi = new Class_ShamsiTools();
            shamsi.UDF_GetCurrentShamsiDate();
            pic.PazireshID = Decimal.Parse(Cmb_PazireshID.SelectedValue.ToString());
            pic.RadPicDescription=Txt_PicDesc.Text.Trim();
            pic.RadiologOperator = Cmb_Operator.Text.Trim();
            pic.DateOfActPicSH = shamsi.CurrentShmasiDate;
            db.tblRadioLogyPics.InsertOnSubmit(pic);
            db.SubmitChanges();
            UDF_CmbFill();
            UDF_GrdFill();
            btnAdd.BringToFront();
            Btn_Save.SendToBack();
            Txt_PicDesc.Clear();
            MessageBox.Show("رکورد جدید درج شد");
        }

        private void Grd_Picture_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            Int32 selectedCellCount =Grd_Picture.GetCellCount(DataGridViewElementStates.Selected);
            if (selectedCellCount > 0)
            {
                if (Grd_Picture.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_PicID.Text = Grd_Picture.Rows[int.Parse(Grd_Picture.SelectedCells[i].RowIndex.ToString())].Cells[0].Value.ToString();
                        Txt_PicDesc.Text = Grd_Picture.Rows[int.Parse(Grd_Picture.SelectedCells[i].RowIndex.ToString())].Cells[2].Value.ToString();

                    }
                    UDF_SelectCmbValue(Decimal.Parse(Txt_PicID.Text.Trim()));
                }
            }
        }
        private void UDF_SelectCmbValue(Decimal Dec_Pic)
        {
            LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
            tblRadioLogyPic pic = db.tblRadioLogyPics.Single(n => n.RadPicID == Dec_Pic );
           
            Cmb_PazireshID.SelectedValue = pic.PazireshID;
            Cmb_Operator.Text  = pic.RadiologOperator;
        }

        private void Btn_Delete_Click(object sender, EventArgs e)
        {
            if (Txt_PicDesc.Text == "")
            {
                MessageBox.Show("انتخاب عکس برای حذف الزامی است");
                return;
            }
            DialogResult dr = MessageBox.Show( "آیا نسبت به عمل حذف اطمینان دارید" ,"اخطاریه حذف" ,MessageBoxButtons.OKCancel , MessageBoxIcon.Warning );
            if (dr == DialogResult.OK)
            {
                LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
                tblRadioLogyPic pic = db.tblRadioLogyPics.Single(n => n.RadPicID == Decimal.Parse(Txt_PicID.Text.Trim()));
                db.tblRadioLogyPics.DeleteOnSubmit(pic);
                db.SubmitChanges();
                Txt_PicDesc.Clear();
                UDF_CmbFill();
                UDF_GrdFill();
                
            }
        }

        private void Btn_Edit_Click(object sender, EventArgs e)
        {
            if (Txt_PicID.Text == "")
            {
                MessageBox.Show("انتخاب عکس مورد نظر برای ویرایش الزامی است");
                return;
            }
            DialogResult dr = MessageBox.Show("آیا نسبت به عمل ویرایش اطمینان دارید؟", "اخطاریه ویرایش", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (dr == DialogResult.OK)
            {
                LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
                tblRadioLogyPic pic = db.tblRadioLogyPics.Single(n => n.RadPicID == Decimal.Parse(Txt_PicID.Text.Trim()));

                pic.PazireshID = Decimal.Parse(Cmb_PazireshID.SelectedValue.ToString());
                pic.RadPicDescription = Txt_PicDesc.Text.Trim();
                pic.RadiologOperator = Cmb_Operator.Text.Trim();
                db.SubmitChanges();

                UDF_CmbFill();
                UDF_GrdFill();
                Txt_PicDesc.Clear();
            }
        }

        private void Btn_Search_Click(object sender, EventArgs e)
        {
            if (Cmb_Search.Text == "")
            {
                MessageBox.Show("انتخاب فیلد جستجو الزامی می باشد");
                return;
            }
            LSC_RadioLogyDataContext db = new LSC_RadioLogyDataContext();
            var Query = from Row in db.tblRadioLogyPics 
                        where Row.PazireshID == Decimal.Parse(Cmb_Search.SelectedValue.ToString())
                        select new
                        {
                            Row.RadPicID,
                            Row.PazireshID,
                            Row.RadPicDescription,
                            Row.RadiologOperator,
                            Row.DateOfActPicSH 
                        };
            Grd_Picture.DataSource = Query;
            if (Query.Count() <= 0)
            {
                Cmb_Search.Text = "";
                Btn_Search.SendToBack();
                Btn_Refresh.BringToFront();
                MessageBox.Show("رکوردی یافته نشد");
            }
          
        }

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


    }
}
