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

namespace projectcsharp
{
    public partial class frm_f_com : Form
    {
        public frm_f_com()
        {
            InitializeComponent();
        }
        byte case_save = 0;
        public SqlConnection con = new SqlConnection("server=.;database=resturan;Integrated Security=True");
        private void show_info()
        {
            SqlDataAdapter da = new SqlDataAdapter("select id as 'کد',id_food as [نام غذا],component1 as 'جزء1',component2 as 'جزء2',component3 as 'جزء3',component4 as 'جزء4',component5 as 'جزء5',component6 as 'جزء6',component7 as 'جزء7',component8 as 'جزء8',component9 as 'جزء9',component10 as 'جزء10' from tbl_component", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            
            SqlDataAdapter da1 = new SqlDataAdapter("select * from tbl_food", con);
            DataTable dt1 = new DataTable();
            da1.Fill(dt1);
            comboBox1.DataSource = dt1;
            comboBox1.DisplayMember = "name";
        }
        private void Enabled_true()
        {
            btn_new.Enabled = true;
            btn_save.Enabled = false;
            btn_edit.Enabled = true;
            btn_del.Enabled = true;
            btn_back.Enabled = true;
            btn_cancel.Enabled = false;
        }
        private void Enabled_false()
        {
            btn_new.Enabled = false;
            btn_save.Enabled = true;
            btn_edit.Enabled = false;
            btn_del.Enabled = false;
            btn_back.Enabled = false;
            btn_cancel.Enabled = true;
        }

        private void frm_f_com_Load(object sender, EventArgs e)
        {
            show_info();
            Enabled_true();
        }


        private void btn_back_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            Enabled_true();
        }

        private void btn_new_Click(object sender, EventArgs e)
        {
            case_save = 0;

            Enabled_false();
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult res = MessageBox.Show(" حذف شود؟   " + dataGridView1[1, dataGridView1.CurrentRow.Index].Value.ToString() + " " + dataGridView1[2, dataGridView1.CurrentRow.Index].Value.ToString() + " ", "حذف شود؟", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (res == DialogResult.Yes)
                {
                    SqlCommand cmd = new SqlCommand("delete from tbl_component where id='" + dataGridView1[0, dataGridView1.CurrentRow.Index].Value.ToString() + "'", con);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    show_info();
                    MessageBox.Show("اطلاعات غذا با موفقیت حذف شد", ".حذف شد", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Enabled_true();
                    listBox1.ClearSelected();
                }
            }
            catch
            {
            }

            Enabled_true();
        }

        private void btn_edit_Click(object sender, EventArgs e)
        {
            case_save = 1;
            Enabled_false();
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            string[] com = { "", "", "", "", "", "", "", "", "", "" };
            for (int i = 0; i < listBox1.SelectedItems.Count; i++)
                com[i] = listBox1.SelectedItems[i].ToString();
            if (case_save == 0)
            {
                SqlCommand cmd = new SqlCommand("insert into tbl_component (id_food , component1 , component2 , component3 , component4 , component5 , component6 , component7  ,component8 , component9 , component10) values(N'" + comboBox1.Text + "' ,N'" + com[0] + "' ,N'" + com[1] + "' ,N'" + com[2] + "' , N'" + com[3] + "' ,N'" + com[4] + "' ,N'" + com[5] + "' , N'" + com[6] + "' ,N'" + com[7] + "' ,N'" + com[8] + "' , N'" + com[9] + "')", con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                show_info();
                listBox1.ClearSelected();
            }
            if (case_save == 1)
            {
                int id = int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString());
                SqlCommand cmd = new SqlCommand("update tbl_component set component1 =N'" + com[0] + "' , component2 =N'" + com[1] + "' , component3 =N'" + com[2] + "' , component4 = N'" + com[3] + "', component5 =N'" + com[4] + "' , component6 =N'" + com[5] + "' , component7 = N'" + com[6] + "', component8 =N'" + com[7] + "' , component9 = N'" + com[8] + "', component10 =N'" + com[9] + "' where id = '" + id + "'", con);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                show_info();
                listBox1.ClearSelected();
            }
            Enabled_true();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            listBox1.ClearSelected();
            comboBox1.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            string[] com = { "", "", "", "", "", "", "", "", "", "" };
            for (int i = 0, j = 2; i < 10; i++, j++)
                com[i] = dataGridView1.CurrentRow.Cells[j].Value.ToString();

            for (int c = 0; c < 10; c++)
                for (int x = 0; x < listBox1.Items.Count; x++)
                    if (listBox1.Items[x].ToString() == com[c])
                        listBox1.SetSelected(x, true);
        }

     
    }
}
