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.Threading.Tasks;
using System.Windows.Forms;

namespace STS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            showAllInformation();
            //开始时不选中任何行
            dataGridView1.ClearSelection();
        }

        //展示数据库表中所有信息
        private void showAllInformation() 
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Server=localhost;Database=TaskManagementDB;Trusted_Connection=True;";
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "select * from STM";

            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = cmd;

            DataSet ds = new DataSet();
            adapter.Fill(ds, "STM");

            DataTable dt = ds.Tables["STM"];
            dataGridView1.DataSource = dt;
            conn.Close();
        }

        //单击“开始查询”按钮触发事件
        private void querybtn1_Click(object sender, EventArgs e)
        {
            string key = comboBox1.Text;
            string value = textBox1.Text;
            if (key == "条码")
            {
                key = "Barcode";
            }
            else if (key == "调度任务索引")
            {
                key = "ScheduledTaskIndex";
            }

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Server=localhost;Database=TaskManagementDB;Trusted_Connection=True;";
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;

            //如果有一项为空,则列出所有数据并提示
            if (key == "" || value == "")
            {
                cmd.CommandText = "select * from STM";
                //MessageBox.Show("缺少查询条件,展示全部数据!");
            }
            else
            {
                cmd.CommandText = $"select * from STM where {key}='{value}'";
            }
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = cmd;

            DataSet ds = new DataSet();
            adapter.Fill(ds, "STM");

            DataTable dt = ds.Tables["STM"];
            dataGridView1.DataSource = dt;
            conn.Close();
        }

        //鼠标右键单击某行,触发事件
        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && e.RowIndex > -1 && e.ColumnIndex > -1)
            {
                dataGridView1.CurrentRow.Selected = false;
                dataGridView1.Rows[e.RowIndex].Selected = true;
                dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
            }
        }

        //单击“任务删除”触发事件
        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            //确定该行条码值
            int crindex = dataGridView1.CurrentRow.Index;
            string barcode = dataGridView1.Rows[crindex].Cells["Barcode_DGV"].Value.ToString();
            //确认删除提示
            string message = $"确定要删除该条信息?\n条码:{barcode}";
            string title = "警告";
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            DialogResult result = MessageBox.Show(message, title, buttons, MessageBoxIcon.Warning);
            //选择“否”则结束方法
            if (result == DialogResult.No)
            {
                return;
            }
            //数据库连接
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Server=localhost;Database=TaskManagementDB;Trusted_Connection=True;";
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = $"delete from STM where Barcode='{barcode}'";
            //执行语句并记录受影响的行数
            int deletenum = cmd.ExecuteNonQuery();
            conn.Close();
            if (deletenum > 0)
            {
                MessageBox.Show("删除成功!");
                //刷新dataGridView
                showAllInformation();
            }
            else
            {
                MessageBox.Show("删除失败!");
            }
        }

        //单击“任务完成”触发事件
        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            //确定条码值
            int crindex = dataGridView1.CurrentRow.Index;
            string barcode = dataGridView1.Rows[crindex].Cells["Barcode_DGV"].Value.ToString();

            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Server=localhost;Database=TaskManagementDB;Trusted_Connection=True;";
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = $"update STM set TaskStatus='已完成' where Barcode='{barcode}'";
            //执行语句并记录受影响的行数
            int updatenum = cmd.ExecuteNonQuery();
            conn.Close();
            if (updatenum > 0)
            {
                MessageBox.Show("任务状态更新成功!");
                showAllInformation();
            }
            else
            {
                MessageBox.Show("任务状态更新失败!");
            }
        }
    }
}