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 团团圆圆饭店管理系统
{
    public partial class 点餐界面 : Form
    {
        SqlDataAdapter ada;
        DataTable table;
        string cname;
        int cid;
        public 点餐界面(int cid, string cname):base()
        {
            InitializeComponent();
            this.cid = cid;
            this.cname = cname;
        }
        private void frmselcai_Load(object sender, EventArgs e)
        {
            label1.Text = "菜名:" + cname;
            string strcon = "Data Source=(local);Initial Catalog=团团圆圆饭店管理系统;Integrated Security=True";
            SqlConnection sqlcon = new SqlConnection(strcon);
            ada = new SqlDataAdapter("select * from 菜单表", sqlcon);
            SqlCommandBuilder builder = new SqlCommandBuilder(ada);
            table = new DataTable();
            ada.Fill(table);
            dataGridView1.DataSource = table;
            dataGridView1.Refresh();

            txtCname.DataBindings.Add("Text", table, "cname");
            txtCprice.DataBindings.Add("Text", table, "cprice");
            txtIsselected.DataBindings.Add("Text", table, "isselected");
            comboCtype.DataBindings.Add("Text", table, "ctype");
            setComboItems();
            DataGridViewCheckBoxColumn check = new DataGridViewCheckBoxColumn();
            dataGridView1.Columns.Add(check);
            dataGridView1.Columns[dataGridView1.Columns.Count - 1].HeaderText = "选择";
            dataGridView1.Refresh();
            checkCai();

        }
        void checkCai()
        {
            string sql = "select * from 菜单表 where id=" + cid;
            string strcon ="Data Source=(local);Initial Catalog=团团圆圆饭店管理系统;Integrated Security=True";
            SqlConnection sqlcon = new SqlConnection(strcon);
            SqlDataAdapter ada2 = new SqlDataAdapter(sql, sqlcon);
            DataTable table2 = new DataTable();
            ada2.Fill(table2);

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                int Cid = (int)dataGridView1.Rows[i].Cells["Cid"].Value;
                DataRow[] rows = table2.Select("Cid=" + Cid);
                if (rows.Length > 0)
                {
                    dataGridView1.Rows[i].Cells[dataGridView1.Columns.Count - 1].Value = true;
                }
            }
        }
        void setComboItems()
        {
            string strcon = "Data Source=(local);Initial Catalog=团团圆圆饭店管理系统;Integrated Security=True";
            SqlConnection sqlcon = new SqlConnection(strcon);
            SqlDataAdapter adapter = new SqlDataAdapter("select id,ctype from ctype", sqlcon);
            DataTable table = new DataTable();
            adapter.Fill(table);
            comboCtype.DataSource = table;
            comboCtype.DisplayMember = "ctype";
            comboCtype.ValueMember = "id";
        }
        private void btnOk_Click(object sender, EventArgs e)
        {
            string strcon = @"Data Source=(local);Initial Catalog=团团圆圆饭店管理系统;Integrated Security=True";
            SqlConnection sqlcon = new SqlConnection(strcon);
            sqlcon.Open();
            string sql = "delete from 菜单表 where cid=" + cid;
            SqlCommand command = new SqlCommand(sql, sqlcon);
            command.ExecuteNonQuery();

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells[dataGridView1.Columns.Count - 1].Value != null)
                {
                    bool sel = (bool)dataGridView1.Rows[i].Cells[dataGridView1.Columns.Count - 1].Value;
                    if (sel == true)
                    {
                        sql = "insert into 顾客消费信息表(cid,zid) values(" + cid + "," + dataGridView1.Rows[i].Cells["cid"].Value + ")";
                        command = new SqlCommand(sql, sqlcon);
                        command.ExecuteNonQuery();
                    }
                }
                //sqlcon.Close();
                //MessageBox.Show("ok");
            }
            消费信息界面 f4 = new 消费信息界面();
            f4.Show();
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.RemoveAt(dataGridView1.SelectedCells[0].RowIndex);
        }


        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                //if ((bool)dataGridView1.Rows[i].Cells["isSelect"].Value == true)
                if (dataGridView1.Rows[i].Cells[dataGridView1.Columns.Count - 1].Value.ToString() == "True")
                {
                    //bool sel = (bool)dataGridView1.Rows[i].Cells[dataGridView1.Columns.Count - 1].Value;
                    MessageBox.Show(dataGridView1.Rows[i].Cells["zid"].Value.ToString());
                    object cname = dataGridView1.Rows[i].Cells["cname"].Value;
                    object cprice = dataGridView1.Rows[i].Cells["cprice"].Value;
                    object zid = dataGridView1.Rows[i].Cells["zid"].Value;
                    string sql = "insert into 顾客消费信息表 (zid,cname,cprice) values('" + Program.id + "','" + cname.ToString().Trim() + "','" + cprice.ToString().Trim() + "')";
                    execCommand(sql);
                    return;
                }
            }
            MessageBox.Show("点餐成功.");
        }
        void execCommand(string sql)
        {
            string strConn = "Data Source=(local);Initial Catalog=团团圆圆饭店管理系统;Integrated Security=True";
            SqlConnection conn = new SqlConnection(strConn);
            SqlCommand command = new SqlCommand(sql, conn);
            conn.Open();
            command.ExecuteNonQuery();
            conn.Close();
        }
    }
}