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 Javaher
{
    public partial class Form5 : Form
    {
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=javaher;Integrated Security=True");
        SqlCommand cmd = new SqlCommand();
        DataTable dt = new DataTable();
        DataTable dtfill = new DataTable();
        DataTable dtcustomer = new DataTable();
        public Form5()
        {
            InitializeComponent();
        }

        void showgrid()
        {
            dt.Clear();
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = "select * from [factor]";
            cmd.ExecuteNonQuery();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            con.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Form5_Load(object sender, EventArgs e)
        {
            showgrid();
            /////////////////////////////////////////////////
            dataGridView1.Columns[0].HeaderText = "کد فروش";
            dataGridView1.Columns[1].HeaderText = "نام کالا";
            dataGridView1.Columns[2].HeaderText = "قیمت";
            dataGridView1.Columns[3].HeaderText = "نام مشتری";
            dataGridView1.Columns[4].HeaderText = "تعداد";
            dataGridView1.Columns[5].HeaderText = "تاریخ";
            /////////////////////////////////////////////////
            // ComBoBox1//
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = "select name from [kala]";
            SqlDataReader dr = cmd.ExecuteReader();
            dtfill.Columns.Add("name");
            dtfill.Load(dr);
            comboBox1.DataSource = dtfill;
            comboBox1.ValueMember = "name";
            comboBox1.DisplayMember = "name";
            con.Close();
            // ComBoBox2//
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = "select name from [customer]";
            SqlDataReader drcustomer = cmd.ExecuteReader();
            dtcustomer.Columns.Add("name");
            dtcustomer.Load(drcustomer);
            comboBox2.DataSource = dtcustomer;
            comboBox2.ValueMember = "name";
            comboBox2.DisplayMember = "name";
            con.Close();
            ///
            comboBox1_SelectedIndexChanged(null, null);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtcount.Text == "")
            {
                MessageBox.Show("لطفا فیلد تعداد مورد درخواست را پر نمایید", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtcount.Focus();
            }
            else
            {
                con.Open();
                cmd.Connection = con;
                cmd.CommandText = "insert into [factor] (kalaname,price,customername,count,date) values ('" + comboBox1.SelectedValue.ToString() + "','" + txtprice.Text.Trim() + "','" + comboBox2.SelectedValue.ToString() + "','" + txtcount.Text.Trim() + "','" + persianDateTimePicker1.Value.ToString("yyyy/MM/dd") + "')";
                cmd.ExecuteNonQuery();
                MessageBox.Show("ثبت با موفقیت انجام شد", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                con.Close();

                showgrid();
                txtcount.Text = "";
                txtcount.Focus();
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }

            con.Open();
            cmd.Connection = con;
            cmd.CommandText = "select * from kala where name='"+comboBox1.SelectedValue.ToString()+"'";
            SqlDataReader drread = cmd.ExecuteReader();
            while (drread.Read())
            {
                txtcompany.Text = drread["company"].ToString();
                txtprice.Text = drread["price"].ToString();
            }

            con.Close();
        }
    }
}
