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

namespace _01调度窗口
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //全部任务
        private void showitem_Click(object sender, EventArgs e)
        {
            string constr = @"Server=DESKTOP-PE4T4D8;Database=Test;Trusted_Connection=true;";
            SqlConnection con = new SqlConnection(constr);
            con.Open();

            string sql = "select * from Table_1";
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            con.Close();
        }

        //按照下拉栏开始查询
        private void btsearch_Click(object sender, EventArgs e)
        {
            string constr = @"Server=DESKTOP-PE4T4D8;Database=Test;Trusted_Connection=true;";
            SqlConnection con = new SqlConnection(constr);
            con.Open();

            string sql = null;
            if(comboBox1.Text == "条码")
            {
                sql = $"select * from Table_1 where 条码 = '" + textBox1.Text + "'";
            }
            else if(comboBox1.Text == "调度任务索引")
            {
                sql = $"select * from Table_1 where 调度任务索引 ='" + textBox1.Text + "' ";
            }
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            con.Close();
        }

        //右键显示菜单
        private void dataGridView1_MouseUp(object sender, MouseEventArgs e)
        {

            if (e.Button == MouseButtons.Right)
            {
                if (dataGridView1.SelectedRows.Count > 0)
                {
                    this.contextMenuStrip1.Show(dataGridView1, e.Location);
                }
                else
                {
                    //MessageBox.Show("当前没有选中行");
                }
            }
        }

        //任务完成按键
        private void 任务完成ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string constr = @"Server=DESKTOP-PE4T4D8;Database=Test;Trusted_Connection=true;";
            SqlConnection con = new SqlConnection(constr);
            con.Open();

            if(dataGridView1.CurrentRow != null)
            {
                int selectrow = dataGridView1.CurrentRow.Index; //得到选中行的索引
                DataGridViewColumn dataGridViewColumn = dataGridView1.Columns["条码"]; //得到指定列
                int selectcol = dataGridView1.Columns.IndexOf(dataGridViewColumn);  //得到指定列的索引
                string selectvalue = dataGridView1.Rows[selectrow].Cells["条码"].Value.ToString(); //得到指定行 列索引的内容

                string finsql = $"update Table_1 set 任务状态 = '已完成' where 条码 = '{selectvalue}'";
                SqlDataAdapter da = new SqlDataAdapter(finsql, con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
                MessageBox.Show("更改状态成功,请刷新界面!");
                con.Close();
            }
        }

        //任务删除按键
        private void 任务删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string constr = @"Server=DESKTOP-PE4T4D8;Database=Test;Trusted_Connection=true;";
            SqlConnection con = new SqlConnection(constr);
            con.Open();

            if (dataGridView1.CurrentRow != null)
            {
                int selectrow = dataGridView1.CurrentRow.Index;
                DataGridViewColumn dataGridViewColumn = dataGridView1.Columns["条码"];
                int selectcol = dataGridView1.Columns.IndexOf(dataGridViewColumn);
                string selectvalue = dataGridView1.Rows[selectrow].Cells["条码"].Value.ToString();

                string delsql = $"delete from Table_1 where 条码 = '{selectvalue}'";
                SqlDataAdapter da = new SqlDataAdapter(delsql, con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
                MessageBox.Show("删除成功,请刷新界面!");
                con.Close();
            }
        }

        //打开子窗口->实现增加任务行  实例化子窗口!!!
        private void btadd_Click(object sender, EventArgs e)
        {
            //实例化子窗口!!!!
            addmenu menu = new addmenu();
            menu.Show();
        }
    }
}