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.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class FormSickAddBox : Form
    {
        private const string _CommandingText = "SELECT SickMeliCode,SickFName,SickLName,SickSex,SickYear,SickPolicyType,SickPolicyNum,AccessCodeDrug,SickNumDrug FROM DrugSick";
        private const string _ConnectioningString = "server=(local);database=Drug;Integrated Security=True";
        // Declare global objects...
        SqlConnection objConnection;
        SqlConnection objConnecting;
        SqlDataAdapter objDataAdapting;
        SqlDataAdapter objDataAdapter;
        DataSet objDataSet;
        DataSet objDataSeting;
        DataView objDataViewing;
        DataView objDataView;
        CurrencyManager objCurrencyManagering;
        CurrencyManager objCurrencyManager;
 private void FillDataSetAndViewing()
 {
            // Initialize a new instance of the DataSet object...
            objDataSeting = new DataSet();
            // Fill the DataSet object with data...
            objDataAdapting.Fill(objDataSeting, "DrugSick");
            // Set the DataView object to the DataSet object...
            objDataViewing = new DataView();
            objDataViewing = new DataView(
            objDataSeting.Tables["DrugSick"]);
            // Set our CurrencyManager object
            // to the DataView object...
            objCurrencyManagering = (CurrencyManager)(
            this.BindingContext[objDataViewing]);
        }
        private void BindFielding()
        {
            // Clear any previous bindings...
            txtMeliCode.DataBindings.Clear();
            txtNameSick.DataBindings.Clear();
            txtFamilSick.DataBindings.Clear();
            cmbSickType.DataBindings.Clear();
            txtSickYear.DataBindings.Clear();
            cmbTypePolicy.DataBindings.Clear();
            txtPolicyNum.DataBindings.Clear();
            txtAccessCodeDrug.DataBindings.Clear();
            txtNumberDrug.DataBindings.Clear();
            // Add new bindings to the DataView object...
            txtMeliCode.DataBindings.Add("Text",
            objDataViewing, "SickMeliCode");
            txtNameSick.DataBindings.Add("Text",
            objDataViewing, "SickFName");
            txtFamilSick.DataBindings.Add("Text",
            objDataViewing, "SickLName");
            cmbSickType.DataBindings.Add("Text",
            objDataViewing, "SickSex");
            txtSickYear.DataBindings.Add("Text",
            objDataViewing, "SickYear");
            cmbTypePolicy.DataBindings.Add("Text",
            objDataViewing, "SickPolicyType");
            txtPolicyNum.DataBindings.Add("Text",
            objDataViewing, "SickPolicyNum");
            txtAccessCodeDrug.DataBindings.Add("Text",
                objDataViewing, "AccessCodeDrug");
            txtNumberDrug.DataBindings.Add("Text",
                objDataViewing, "SickNumDrug");
            // Display a ready status...

        }


        public FormSickAddBox()
        {
            objConnecting = new SqlConnection(_ConnectioningString);
            objDataAdapting = new SqlDataAdapter(_CommandingText, objConnecting);
            objConnection = new SqlConnection("server=(local);database=Drug;Integrated Security=True");
            objDataAdapter = new SqlDataAdapter("SELECT LatinDrug,ShortDrug,PersionDrug,AccessCodeDrug,ExchangeUnit,Price,Drugform,Stock FROM DrugDefinition", objConnection);


            InitializeComponent();
        }
        private void label3_Click(object sender, EventArgs e)
        {

        }

        DrugSick sick = new DrugSick();
        private void button1_Click(object sender, EventArgs e)
        {
            Cleantxt();
            btnSaveSick.Enabled = true;

        }
        public void Cleantxt()
        {
            txtMeliCode.Text = "";
            txtNameSick.Text = "";
            txtFamilSick.Text = "";
            cmbSickType.Text = "";
            txtSickYear.Text = "";
            cmbTypePolicy.Text = "";
            txtPolicyNum.Text = "";
            txtAccessCodeDrug.Text = "";
            txtNumberDrug.Text = "";

        }

        private void cmbSickType_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void cmbTypePolicy_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnSaveSick_Click(object sender, EventArgs e)
        {
            string SickMeliCodeV, SickFNameV, SickLNameV, SickSexV, SickYearV, SickPolicyTypeV, SickPolicyNumV, AccessCodeDrugV, SickNumDrugV;
            SickMeliCodeV = txtMeliCode.Text;
            SickFNameV = txtNameSick.Text;
            SickLNameV = txtFamilSick.Text;
            SickSexV = cmbSickType.Text;
            SickYearV = txtSickYear.Text;
            SickPolicyTypeV = cmbTypePolicy.Text;
            SickPolicyNumV = txtPolicyNum.Text;
            AccessCodeDrugV = txtAccessCodeDrug.Text;
            SickNumDrugV = txtNumberDrug.Text;
            UpdateDrugDefinition();
            sick.AddSick(SickMeliCodeV, SickFNameV, SickLNameV, SickSexV, SickYearV, SickPolicyTypeV, SickPolicyNumV, AccessCodeDrugV, SickNumDrugV);

            btnSaveSick.Enabled = false;

        }

        private void FormSickAddBox_Load(object sender, EventArgs e)
        {
            btnSaveSick.Enabled = false;
            FillDataSetAndViewing();
            BindFielding();
            btnUpdteSick.Enabled = false;
        }

        private void btnSearchSick_Click(object sender, EventArgs e)
        {
            // Declare local variables...
            int intPosition;
            // Determine the appropriate item selected and set the
            // Sort property of the DataView object...
            switch (cmbSearchSick.SelectedIndex)
            {
                case 0://LatinDrug
                    objDataViewing.Sort = "SickMeliCode";
                    break;
                case 1://ShortDrug
                    objDataViewing.Sort = "SickFname";
                    break;
                case 2://PersionDrug
                    objDataViewing.Sort = "SickLname";
                    break;
                case 3://AccessCodedrug
                    objDataViewing.Sort = "SickSex";
                    break;
                case 4://ExchangeUnit
                    objDataViewing.Sort = "SickYear";
                    break;
                case 5://price
                    objDataViewing.Sort = "SickPolicyType";
                    break;
                case 6://DrugForm
                    objDataViewing.Sort = "SickPolicyNum";
                    break;
                case 7://AccessCodeDrug
                    objDataViewing.Sort = "AccessCodeDrug";
                    break;
                case 8://SickNumDrug
                    objDataViewing.Sort = "SickNumDrug";
                    break;

            }
            // Find the search...
            intPosition = objDataViewing.Find(txtSearchSick.Text);
            if (intPosition == -1)
            {
                // Display a message
                // that the record was not found...
                MessageBox.Show("رکوردی با مشخصات فوق پیدا نشد", "جستجو");
            }
            else
            {
                // Otherwise display a message that the record
                // was found and reposition the CurrencyManager
                // to that record...
                MessageBox.Show("رکورد مورد نطر پیدا شد", "جستجو");
                objCurrencyManagering.Position = intPosition;
                cmbSearchSick.Text = "";
                txtSearchSick.Text = "";
                btnUpdteSick.Enabled = true;
            }

        }

        private void btnUpdteSick_Click(object sender, EventArgs e)
        {
            // Declare local variables and objects...
            int intPosition;
            SqlCommand objCommanding = new SqlCommand();
            // Save the current record position...
            intPosition = objCurrencyManagering.Position;
            // Set the SqlCommand object properties...
            objCommanding.Connection = objConnecting;
            objCommanding.CommandText = "UPDATE DrugSick SET SickMeliCode=@SickMeliCode ,SickFName=@SickFName,SickLName=@SickLName,SickSex=@SickSex,SickYear=@SickYear,SickPolicyType=@SickPolicyType,SickPolicyNum=@SickPolicyNum,AccessCodeDrug=@AccessCodeDrug,SickNumDrug=@SickNumDrug  WHERE SickMeliCode=@SickMeliCode ";
            objCommanding.CommandType = CommandType.Text;
            // Add parameters for the placeholders in the SQL in
            // the CommandText property...
            FillDataSetAndViewing();
            // Parameter for the title_id field...
            objCommanding.Parameters.AddWithValue("@SickMeliCode", txtMeliCode.Text);
            objCommanding.Parameters.AddWithValue("@SickFname", txtNameSick.Text);
            objCommanding.Parameters.AddWithValue("@SickLname", txtFamilSick.Text);
            objCommanding.Parameters.AddWithValue("@SickSex", cmbSickType.Text);
            objCommanding.Parameters.AddWithValue("@SickYear", txtSickYear.Text);
            objCommanding.Parameters.AddWithValue("@SickPolicyType", cmbTypePolicy.Text);
            objCommanding.Parameters.AddWithValue("@SickPolicyNum", txtPolicyNum.Text);
            objCommanding.Parameters.AddWithValue("@AccessCodeDrug", txtAccessCodeDrug.Text);
            objCommanding.Parameters.AddWithValue("@SickNumDrug", txtNumberDrug.Text);
            // Open the connection...
            objConnecting.Open();
            // Execute the SqlCommand object to update the data...
            objCommanding.ExecuteNonQuery();
            // Close the connection...
            objConnecting.Close();
            // Fill the DataSet and bind the fields...
            FillDataSetAndViewing();
            BindFielding();
            // Set the record position
            // to the one that you saved...
            objCurrencyManagering.Position = intPosition;
            // Display a message that the record was updated...
            MessageBox.Show("مشخصات  رکورد به هنگام سازی شد", "ویرایش");



        }
        public void UpdateDrugDefinition()
        {
            SqlCommand objCommand = new SqlCommand();
            // Set the SqlCommand object properties...
            objCommand.Connection = objConnection;
            objCommand.CommandText = "UPDATE DrugDefinition SET AccessCodeDrug=@AccessCodeDrug , Stock=@Stock WHERE AccessCodeDrug = @AccessCodeDrug ";
            objCommand.CommandType = CommandType.Text;
            // Add parameters for the placeholders in the SQL in
            // the CommandText property...
            // Parameter for the title field...
            string TempStock1 = txtNumberDrug.Text;
            txtNumberDrug.Text = "";
            FillDataSetAndView();
            txtNumberDrug.DataBindings.Clear();
            txtNumberDrug.DataBindings.Add("Text", objDataView, "Stock");
            int TempStock2 = (int.Parse(TempStock1)) - (int.Parse(txtNumberDrug.Text));
            if (TempStock2 <= 0)
            {
                txtNumberDrug.Text = "0";
                MessageBox.Show("دارویی با این شماره دسترسی در دپارتمان داروخانه موجودی ندارد", "داروخانه");
            }
            txtNumberDrug.Text = TempStock1;
            objCommand.Parameters.AddWithValue("@Stock", TempStock2);
            // Parameter for the title_id field...
            objCommand.Parameters.AddWithValue("@AccesscodeDrug", txtAccessCodeDrug.Text);
            // Open the connection...
            objConnection.Open();
            // Execute the SqlCommand object to update the data...
            objCommand.ExecuteNonQuery();
            // Close the connection...
            objConnection.Close();



        }
        private void FillDataSetAndView()
        {
            // Initialize a new instance of the dataset object 
            objDataSet = new DataSet();
            // fill the dataset objectwith data 
            objDataAdapter.Fill(objDataSet, "DrugDefinition");
            // Set the DataView object to the DataSet object...
            objDataView = new DataView(objDataSet.Tables["DrugDefinition"]);
            // Set our CurrencyManager object
            // to the DataView object...
            objCurrencyManager = (CurrencyManager)(this.BindingContext[objDataView]);


        }
    }
}
