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_Paziresh : 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 Frm_Paziresh()
        {
            InitializeComponent();
        }
        private void Btn_Save_Click(object sender, EventArgs e)
        {
            if (Txt_Name.Text != "" && Txt_Lname.Text != "" && Txt_Code_Melli.Text != "" &&
                 Txt_Tel.Text != "")
            {
                ObjCommand = new SqlCommand();
                ObjCommand.Connection = ObjConnection;
                ObjCommand.CommandText = "select Code from Mariz where Code_Melli=@Code_Melli_";
                ObjCommand.Parameters.AddWithValue("@Code_Melli_", Txt_Code_Melli.Text);
                ObjConnection.Open();
                object Code_Melli_Valid = ObjCommand.ExecuteScalar();
                if (Code_Melli_Valid == null)
                {
                    ObjCommand = new SqlCommand();
                    ObjCommand.Connection = ObjConnection;
                    ObjCommand.CommandText = "select max(code) from mariz";
                    object MaxId = ObjCommand.ExecuteScalar();
                    int Id = Int32.Parse(MaxId.ToString());
                    Id++;
                    string StrId = Id.ToString();
                    ObjCommand.CommandText = "INSERT INTO Mariz" +
                         "(Code,Name,Lname,Age,Sex,Code_Melli,Tell,Mobile,Adress)" +
                         "VALUES(@Code,@Name,@Lname,@Age,@Sex,@Code_Melli,@Tell,@Mobile,@Adress)";
                    ObjCommand.Parameters.AddWithValue("@Code", StrId);
                    ObjCommand.Parameters.AddWithValue("@Name", Txt_Name.Text);
                    ObjCommand.Parameters.AddWithValue("@Lname", Txt_Lname.Text);
                    ObjCommand.Parameters.AddWithValue("@Age", Txt_Age.Text);
                    ObjCommand.Parameters.AddWithValue("@Sex", Cmb_Sex.Text);
                    ObjCommand.Parameters.AddWithValue("@Code_Melli", Txt_Code_Melli.Text);
                    ObjCommand.Parameters.AddWithValue("@Adress", Txt_Adreess.Text);
                    ObjCommand.Parameters.AddWithValue("@Tell", Txt_Tel.Text);
                    ObjCommand.Parameters.AddWithValue("@Mobile", Txt_Mobile.Text);
                    ObjCommand.Connection = ObjConnection;
                    ObjCommand.ExecuteScalar();
                    ObjConnection.Close();
                    Fill_Grp_Doctor_Name();
                    //ObjCommand.ExecuteNonQuery();
                    Btn_Taeen_Vagt.Enabled = true;
                    Grp_Taeen_Vagt.Enabled = true;
                    MessageBox.Show("اطلاعات با موفقيت درج شد", "اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("فردي با اين مشخصات موجود ميباشد", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    ObjConnection.Close();
                }

            }
            else
                MessageBox.Show(".مشخصات ناقص ميباشد نام  شهرت  كد ملي و شماره تلفن بايد وارد شود", "اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        private void Chk_Search_CheckStateChanged(object sender, EventArgs e)
        {
            if (Chk_Search.Checked == true)
            { Grp_Search.Visible = true; }
            else
                Grp_Search.Visible = false;
        }
        private void Btn_Go_Search_Click(object sender, EventArgs e)
        {
            ObjDataAdapter.SelectCommand = new SqlCommand();
            ObjDataAdapter.SelectCommand.CommandType = CommandType.Text;
            ObjDataAdapter.SelectCommand.Connection = ObjConnection;
            ObjDataSet = new DataSet();
            string Value = InputBox.Show(" لطفا" + Cmb_Search.SelectedItem.ToString() + "     را وارد كنيد  ", "ورود اطلاعات");
            Value = Value.Trim();
            switch (Cmb_Search.SelectedIndex)
            {
                case 0:
                    {
                        ObjDataAdapter.SelectCommand.CommandText = "Select * from Mariz where Code_Melli=@Value";
                        break;
                    }
                case 1:
                    {
                        ObjDataAdapter.SelectCommand.CommandText = "Select * from Mariz where Tell=@Value";
                        break;
                    }
                case 2:
                    {
                        ObjDataAdapter.SelectCommand.CommandText = "Select * from Mariz where Mobile=@Value";
                        break;
                    }
            }
            ObjDataAdapter.SelectCommand.Parameters.AddWithValue("@Value", Value);
            ObjConnection.Open();
            object Search_Valid = ObjDataAdapter.SelectCommand.ExecuteScalar();
            ObjConnection.Close();
            ObjDataAdapter.Fill(ObjDataSet, "Mariz");
            ObjDataView = new DataView(ObjDataSet.Tables["Mariz"]);

            if (Search_Valid == null) MessageBox.Show("چنين فردي موجود نميباشد", "اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            else
            {
                Set_Binding_Data();
                Btn_Save.Enabled = false;
                Btn_Taeen_Vagt.Enabled = true;
                Grp_Taeen_Vagt.Enabled = true;
                Btn_New.Enabled = true;
                //////////////////////////////////////////   Set Cmb_Doctor Certificate //////////////
                ObjDataAdapter.SelectCommand.CommandText = "select distinct Certificate from Doctor";
                ObjConnection.Open();
                ObjDataAdapter.SelectCommand.ExecuteScalar();
                ObjConnection.Close();
                ObjDataAdapter.Fill(ObjDataSet, "Doctor");
                ObjDataView = new DataView(ObjDataSet.Tables["Doctor"]);

                Cmb_Doctor_Name.DataSource = ObjDataView;
                Cmb_Doctor_Name.DisplayMember = "certificate";
                ObjDataSet = null;
                /////////////////////////////////////////////////////////////////////////////
            }
        }
        public void Set_Binding_Data()
        {
            Txt_Name.DataBindings.Clear();
            Txt_Lname.DataBindings.Clear();
            Txt_Age.DataBindings.Clear();
            Cmb_Sex.DataBindings.Clear();
            Txt_Code_Melli.DataBindings.Clear();
            Txt_Adreess.DataBindings.Clear();
            Txt_Tel.DataBindings.Clear();
            Txt_Mobile.DataBindings.Clear();
            Txt_Code_Mariz.DataBindings.Clear();

            Txt_Name.DataBindings.Add("Text", ObjDataView, "Name");
            Txt_Lname.DataBindings.Add("Text", ObjDataView, "LName");
            Txt_Age.DataBindings.Add("Text", ObjDataView, "Age");
            Cmb_Sex.DataBindings.Add("Text", ObjDataView, "Sex");
            Txt_Code_Melli.DataBindings.Add("Text", ObjDataView, "Code_Melli");
            Txt_Adreess.DataBindings.Add("Text", ObjDataView, "Adress");
            Txt_Tel.DataBindings.Add("Text", ObjDataView, "Tell");
            Txt_Mobile.DataBindings.Add("Text", ObjDataView, "Mobile");
            Txt_Code_Mariz.DataBindings.Add("Text", ObjDataView, "Code");
        }
        private void Cmb_Search_DropDownClosed(object sender, EventArgs e)
        {
            Btn_Go_Search.Enabled = true;
            Btn_Go_Search.Focus();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Txt_Name.Text = "";
            Txt_Lname.Text = "";
            Txt_Age.Text = "";
            Cmb_Sex.Text = "";
            Txt_Code_Melli.Text = "";
            Txt_Adreess.Text = "";
            Txt_Tel.Text = "";
            Txt_Mobile.Text = "";
            Txt_Code_Mariz.Text = "";
            Txt_Code_Doctor.Text = "";
            Grp_Taeen_Vagt.Enabled = false;
            Btn_Save.Enabled = true;
            Btn_Taeen_Vagt.Enabled = false;
            Data_Grid_Doctor.DataSource = null;
        }
        private void Cmb_Doctor_Name_SelectedIndexChanged(object sender, EventArgs e)
        {////////////////////////////       Ste DataGrid View Info
            ObjDataAdapter.SelectCommand.CommandText = "Select Code,Name,Lname from Doctor where Certificate='" + Cmb_Doctor_Name.Text.Trim() + "'";
            ObjConnection.Open();
            ObjDataAdapter.SelectCommand.ExecuteScalar();
            ObjConnection.Close();
            ObjDataSet = new DataSet();
            ObjDataAdapter.Fill(ObjDataSet, "Doctor");
            Data_Grid_Doctor.AutoGenerateColumns = true;
            Data_Grid_Doctor.DataSource = ObjDataSet;
            Data_Grid_Doctor.DataMember = "Doctor";
            Data_Grid_Doctor.Columns[0].HeaderText = "كد";
            Data_Grid_Doctor.Columns[1].HeaderText = "نام";
            Data_Grid_Doctor.Columns[2].HeaderText = "فاميلي";
            ObjDataSet = null;

        }
        private void Data_Grid_Doctor_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {////////////////////////   Set Txt_Doctor_Code Info
            Txt_Code_Doctor.Text = Data_Grid_Doctor.CurrentRow.Cells[0].Value.ToString();
        }
        private void Btn_Taeen_Vagt_Click(object sender, EventArgs e)
        {
            DialogResult Result;
            if (Txt_Time.Text != "" && Txt_Date.Text != "" && Txt_Code_Doctor.Text != "")
            {
                ObjDataAdapter.SelectCommand = new SqlCommand();
                ObjDataAdapter.SelectCommand.CommandText = "Select date from Reserve where date=@date and Code_Doctor=@Code_Doctor";
                ObjDataAdapter.SelectCommand.Parameters.AddWithValue("@date", Txt_Date.Text);
                ObjDataAdapter.SelectCommand.Parameters.AddWithValue("@Code_Doctor", Txt_Code_Doctor.Text);
                ObjDataAdapter.SelectCommand.Connection = ObjConnection;
                ObjConnection.Open();
                ObjDataAdapter.SelectCommand.ExecuteScalar();
                ObjConnection.Close();
                ObjDataAdapter.Fill(ObjDataSet = new DataSet(), "Reserve");
                ObjDataView = new DataView(ObjDataSet.Tables["Reserve"]);
                if (ObjDataView.Count <= 40)
                {
                    if ((Result = MessageBox.Show("آيا ميخواهيد ثبت شود؟", "اطلاعات", MessageBoxButtons.YesNo, MessageBoxIcon.Information)) == DialogResult.Yes)
                    {
                        ObjCommand = new SqlCommand();
                        ObjCommand.Connection = ObjConnection;
                        ObjCommand.CommandText = "Select * from Reserve where Code_Doctor=@Code_Doctor and Code_Mariz=@Code_Mariz and Date=@Date and Time=@Time";
                        ObjCommand.Parameters.AddWithValue("@Code_Doctor", Txt_Code_Doctor.Text);
                        ObjCommand.Parameters.AddWithValue("@Code_Mariz", Txt_Code_Mariz.Text);
                        ObjCommand.Parameters.AddWithValue("@Date", Txt_Date.Text);
                        ObjCommand.Parameters.AddWithValue("@Time", Txt_Time.Text);
                        ObjConnection.Open();
                        object Search_Valid = ObjCommand.ExecuteScalar();
                        ObjConnection.Close();
                        if (Search_Valid == null)
                        {
                            ObjCommand.CommandText = "insert into Reserve(Code_Doctor,Code_Mariz,Date,Time)" +
                                                                 "values(@Code_Doctor,@Code_Mariz,@Date,@Time)";
                            ObjConnection.Open();
                            ObjCommand.ExecuteScalar();
                            ObjConnection.Close();
                            MessageBox.Show("اطلاعات با موفقيت ثبت شد", "اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            ObjCommand = null;
                        }
                        else MessageBox.Show("اطلاعات ذكر شده تكراري ميباشد.لطفا مجددا اطلاعات را بررسي نماييد", "اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else if (Result != DialogResult.No) MessageBox.Show("ظرفيت تكميل ميباشد. لطفا روز ديگري را امتحان كنيد", "اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else MessageBox.Show("مشخصات ناقص ميباشد لطفا كد بيمار كد دكتر تاريخ و زمان را بررسي نماييد", "اطلاعات", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        private void Fill_Grp_Doctor_Name()
        {
            ObjDataAdapter.SelectCommand = new SqlCommand();
            ObjDataAdapter.SelectCommand.Connection = ObjConnection;
            ObjDataAdapter.SelectCommand.CommandText = "select distinct Certificate from Doctor";
            ObjConnection.Open();
            ObjDataAdapter.SelectCommand.ExecuteScalar();
            ObjConnection.Close();
            ObjDataSet = new DataSet();
            ObjDataAdapter.Fill(ObjDataSet, "Doctor");
            ObjDataView = new DataView(ObjDataSet.Tables["Doctor"]);

            Cmb_Doctor_Name.DataSource = ObjDataView;
            Cmb_Doctor_Name.DisplayMember = "certificate";

            ObjDataAdapter.SelectCommand = new SqlCommand("select code from mariz where Code_Melli=@Code_Melli", ObjConnection);
            ObjDataAdapter.SelectCommand.Parameters.Add("@Code_Melli", Txt_Code_Melli.Text);
            ObjConnection.Open();
            ObjDataAdapter.SelectCommand.ExecuteScalar();
            ObjConnection.Close();
            ObjDataSet = new DataSet();
            ObjDataAdapter.Fill(ObjDataSet, "Mariz");
            ObjDataView = new DataView(ObjDataSet.Tables["Mariz"]);
            Txt_Code_Mariz.DataBindings.Clear();
            Txt_Code_Mariz.DataBindings.Add("Text", ObjDataView, "Code");
        }
    }
}