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

namespace Data
{
    public partial class Frm_Visit : Form
    {
        public SqlCommand ObjCommand = new SqlCommand("select Code from Mariz where Code_Melli=@Code_Melli");
        public SqlConnection ObjConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Matab.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
        public SqlDataAdapter ObjDataAdapter = new SqlDataAdapter();
        public DataSet ObjDataSet = new DataSet();
        public DataView ObjDataView = new DataView();

        public DataSet ObjDataSet_Noskhe = new DataSet();
        public DataView ObjDataView_Noskhe = new DataView();

        public Frm_Visit()
        {
            InitializeComponent();
        }
        private void Txt_Code_Mariz_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyValue == 13)
                    if (Txt_Code_Mariz.Text != null)
                    {
                        if (Txt_Code_Mariz.Text != "")
                        {
                            ObjDataAdapter.SelectCommand = new SqlCommand();
                            ObjDataAdapter.SelectCommand.Connection = ObjConnection;
                            ObjDataAdapter.SelectCommand.CommandText = "Select Name,Lname from Mariz where Code=@Code";
                            ObjDataAdapter.SelectCommand.Parameters.AddWithValue("@Code", Txt_Code_Mariz.Text);
                            ObjConnection.Open();
                            object Search_Valid = ObjDataAdapter.SelectCommand.ExecuteScalar();
                            ObjConnection.Close();
                            if (Search_Valid != null)
                            {
                                ObjDataSet.Clear();
                                ObjDataAdapter.Fill(ObjDataSet, "Mariz");
                                ObjDataView = new DataView(ObjDataSet.Tables["Mariz"]);
                                Txt_Name.DataBindings.Clear();
                                Txt_Name.DataBindings.Add("Text", ObjDataView, "Name");
                                string Name = Txt_Name.Text;
                                Txt_Name.DataBindings.Clear();
                                Txt_Name.DataBindings.Add("Text", ObjDataView, "Lname");
                                Txt_Name.Text = Name + " " + Txt_Name.Text;
                                Txt_Detail.Enabled = true;
                                Btn_Noskhe.Enabled = true;
                                Btn_Save.Enabled = true;
                            }
                            else
                            {
                                MessageBox.Show("چنين فردي موجود نميباشد", "اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                                Txt_Code_Mariz.Text = "";
                                ObjDataSet.Clear();
                                ObjDataSet_Noskhe.Clear();
                                Txt_Detail.Enabled = false;
                                Btn_Noskhe.Enabled = false;
                                Btn_Save.Enabled = false;
                            }

                        }
                    }

                    else MessageBox.Show("كد بيمار را در محل مربوطه وارد نماييد", "اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("لطفا اطلاعات را به صورت صحيح وارد نماييد", "اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void Txt_Sabegeh_TextChanged(object sender, EventArgs e)
        {
            Btn_Save.Enabled = true;
        }
        private void Btn_Sabegeh_Click(object sender, EventArgs e)
        {
            if (Txt_Code_Mariz.Text != "")
            {
                ObjDataSet.Clear();
                ObjDataAdapter.SelectCommand = new SqlCommand();
                ObjDataAdapter.SelectCommand.Connection = ObjConnection;
                ObjDataAdapter.SelectCommand.CommandText = "Select Date,Detail from Sabegeh where Code_Mariz=@Code";
                ObjDataAdapter.SelectCommand.Parameters.AddWithValue("@Code", Txt_Code_Mariz.Text);
                ObjConnection.Open();
                object Search_Valid = ObjDataAdapter.SelectCommand.ExecuteScalar();
                ObjConnection.Close();
                if (Search_Valid != null)
                {
                    ObjDataAdapter.Fill(ObjDataSet, "Sabegeh");
                    DGView_Sabegeh.AutoGenerateColumns = true;
                    DGView_Sabegeh.DataSource = ObjDataSet;
                    DGView_Sabegeh.DataMember = "Sabegeh";
                    DGView_Sabegeh.Columns[0].HeaderText = "تاريخ";
                    DGView_Sabegeh.Columns[1].HeaderText = "جزِِييات";
                    Txt_Detail.Enabled = true;
                    Btn_Noskhe.Enabled = true;
                    Txt_Detail.Clear();
                    
                }
                else 
                { 
                    MessageBox.Show("چنين فردي موجود نميباشد", "اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    Txt_Code_Mariz.Text = ""; 
                    ObjDataSet.Clear();
                    ObjDataSet_Noskhe.Clear();
                    Txt_Detail.Enabled = false ;
                    Btn_Noskhe.Enabled = false ;
                    Btn_Save.Enabled = false;
                    

                }
            }
            else MessageBox.Show(".مشخصات ناقص ميباشد كد مريض بايد وارد شود", "اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void Btn_Save_Click(object sender, EventArgs e)
        {
            if (Txt_Detail.Text != "" && Txt_Date.Text != "")

            { 
                ObjDataAdapter.SelectCommand = new SqlCommand("Insert InTo Sabegeh(code_mariz,Date,Detail)values(@code_mariz,@Date,@Detail)", ObjConnection);
                ObjDataAdapter.SelectCommand.Parameters.AddWithValue("@code_mariz", Txt_Code_Mariz.Text);
                ObjDataAdapter.SelectCommand.Parameters.AddWithValue("@Date", Txt_Date.Text);
                ObjDataAdapter.SelectCommand.Parameters.AddWithValue("Detail", Txt_Detail.Text);
                ObjConnection.Open();
                ObjDataAdapter.SelectCommand.ExecuteScalar();
                ObjConnection.Close();
                Btn_Sabegeh_Click(null, null);
                Txt_Detail.Clear();
                MessageBox.Show("اطلاعات با موفقيت درج شد", "اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else MessageBox.Show(".مشخصات ناقص ميباشد تاريخ و جزييات بايد وارد شود", "اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void Btn_Noskhe_Click(object sender, EventArgs e)
        {
           
            DGView_Drog.Enabled = true; ;
            ObjDataAdapter.SelectCommand =new SqlCommand ( "select * from Drog",ObjConnection);
            ObjConnection.Open();
            ObjDataAdapter.SelectCommand.ExecuteScalar();
            ObjConnection.Close();
            ObjDataAdapter.Fill(ObjDataSet_Noskhe = new DataSet(), "Drog");
            DGView_Drog.DataSource = ObjDataSet_Noskhe.Tables["Drog"].DefaultView;

            DGView_Drog.AutoGenerateColumns = true;
            DGView_Drog.Columns[0].HeaderText = "كد دارو";
            DGView_Drog.Columns[1].HeaderText = "نام دارو";
            DGView_Drog.Columns[2].HeaderText = "نام فارسي دارو";
            DGView_Drog.Focus();
 
        }
        private void DGView_Drog_KeyDown(object sender, KeyEventArgs e)
        {
            ObjDataSet_Noskhe.Tables["Drog"].DefaultView.RowFilter = "Name Like '" + e.KeyData.ToString() + "%'";
            ObjDataSet_Noskhe.Clear();
            ObjDataAdapter.SelectCommand = new SqlCommand("select * from Drog", ObjConnection);
            ObjConnection.Open();
            ObjDataAdapter.SelectCommand.ExecuteScalar();
            ObjConnection.Close();
            ObjDataAdapter.Fill(ObjDataSet_Noskhe, "Drog");
        }
        private void DGView_Drog_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (Txt_Code_Mariz.Text != "" && Txt_Date.Text != "")
            {
                ObjCommand = new SqlCommand("Insert InTo Noskhe(Code_Mariz,Code_Drog,Date,Use_Way)Values(@Code_Mariz,@Code_Drog,@Date,@Use_Way)", ObjConnection);
                ObjCommand.Parameters.AddWithValue("@Code_Mariz", Txt_Code_Mariz.Text);
                ObjCommand.Parameters.AddWithValue("@Code_Drog", DGView_Drog.CurrentRow.Cells[0].Value);
                ObjCommand.Parameters.AddWithValue("@Date", Txt_Date.Text);
                ObjCommand.Parameters.AddWithValue("@Use_Way", InputBox.Show("لطفا طريقه مصرف يا تعداد دارو را وارد نماييد", "اطلاعات"));
                ObjConnection.Open();
                ObjCommand.ExecuteScalar();
                ObjConnection.Close();
            }
            else  MessageBox.Show(".مشخصات ناقص ميباشد كد و تاريخ  بايد وارد شود", "اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void Txt_Detail_TextChanged(object sender, EventArgs e)
        {
            Btn_Save.Enabled = true;
            if (Txt_Detail.Text.Length == 0)
                Btn_Save.Enabled = false;

        }

       
    }
}