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.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Net;
using System.Net.Sockets;

namespace GcDevicePc
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        private void Form3_Load(object sender, EventArgs e)
        {

        }

        DriveInfo out_drive;
        int data_out_kind = 1;
        DataSet AAA = new DataSet();
        DataTable data_total = new DataTable("总平均值");
        DataTable value_chroma0 = new DataTable("浓度值");
        DataTable value_chroma1 = new DataTable("浓度值");
        DataTable value_gongkuang = new DataTable("工况值");
        DataTable select_table = new DataTable();
        DataTable select_operate_table = new DataTable();
        DataTable select_warn_table = new DataTable();
        DataTable signal_light = new DataTable("信号图");
        DateTime t1 = new DateTime();
        DateTime t2 = new DateTime();
        string QueryString = "";
        int data_select_index = -1;

        private void chart_show_button_Click(object sender, EventArgs e)
        {
            if (chart_show_button.Text == "折线图开")
            {
                chart_show_panel.Show();
                search_date_show_panel.Hide();
                row_data_out_show_panel.Hide();
                chart_show_button.Text = "折线图关";
            }
            else if (chart_show_button.Text == "折线图关")
            {
                chart_show_panel.Hide();
                chart_show_button.Text = "折线图开";
            }
        }
        private void all_data_out_bt_Click(object sender, EventArgs e)
        {
            row_data_out_show_panel.Hide();
            search_date_show_panel.Hide();
            try
            {
                data_out_listbox.Items.Clear();
                string[] pan = System.IO.Directory.GetLogicalDrives();
                for (int i = 0; i < pan.Length; i++)
                {
                    out_drive = new DriveInfo(pan[i]);
                    data_out_listbox.Items.Add(pan[i] + "    (" + out_drive.TotalSize / (1024 * 1024 * 1024) + "GB)");
                }
            }
            catch (Exception)
            {

            }
            data_out_kind = 1;
            data_out_panel.Show();
        }
        private void data_delete_run_bt_Click(object sender, EventArgs e)
        {
            if (day_radio.Checked == true)
            {
                t1 = new DateTime(dateTime_day.Value.Year, dateTime_day.Value.Month, dateTime_day.Value.Day, 0, 0, 0);
                t2 = new DateTime(dateTime_day.Value.Year, dateTime_day.Value.Month, dateTime_day.Value.Day, 23, 59, 59);
            }
            else if (date_radio.Checked == true)
            {
                t1 = new DateTime(dateTime_date_s.Value.Year, dateTime_date_s.Value.Month, dateTime_date_s.Value.Day, 0, 0, 0);
                t2 = new DateTime(dateTime_date_e.Value.Year, dateTime_date_e.Value.Month, dateTime_date_e.Value.Day, 23, 59, 59);
            }
            if (history_data_see_tab.SelectedIndex == 0)
            {
                if (yin_search_rBt.Checked)
                {
                    QueryString = "delete from save_data0 where v_date between '" + t1.ToString() + "' and '" + t2.ToString() + "' ";
                }
                else if (yan_search_rBt.Checked)
                {
                    QueryString = "delete from save_data where v_date between '" + t1.ToString() + "' and '" + t2.ToString() + "' ";
                }
                sql_Dataclass.Dataclass.Query(QueryString);
                sql_Dataclass.Dataclass.write_operation_data("系统操作", "历史测量浓度删除！");
            }
            else if (history_data_see_tab.SelectedIndex == 1)
            {
                QueryString = "delete from operate_data where data_time between '" + t1.ToString() + "' and '" + t2.ToString() + "' ";
                sql_Dataclass.Dataclass.Query(QueryString);
                QueryString = "delete from warn_data where data_time between '" + t1.ToString() + "' and '" + t2.ToString() + "' ";
                sql_Dataclass.Dataclass.Query(QueryString);
                sql_Dataclass.Dataclass.write_operation_data("系统操作", "历史操作记录删除！");
                sql_Dataclass.Dataclass.write_operation_data("系统操作", "历史报警记录删除！");
            }
            search_date_show_panel.Hide();
        }
        private void data_search_run_bt_Click(object sender, EventArgs e)
        {
            string search_history_st = "";
            if (day_radio.Checked == true)
            {
                t1 = new DateTime(dateTime_day.Value.Year, dateTime_day.Value.Month, dateTime_day.Value.Day, 0, 0, 0);
                t2 = new DateTime(dateTime_day.Value.Year, dateTime_day.Value.Month, dateTime_day.Value.Day, 23, 59, 59);
            }
            else if (date_radio.Checked == true)
            {
                t1 = new DateTime(dateTime_date_s.Value.Year, dateTime_date_s.Value.Month, dateTime_date_s.Value.Day, 0, 0, 0);
                t2 = new DateTime(dateTime_date_e.Value.Year, dateTime_date_e.Value.Month, dateTime_date_e.Value.Day, 23, 59, 59);
            }
            string select_v_list = "";
            if (history_data_see_tab.SelectedIndex == 0)
            {
                if (algorithm_W.algorithm.calculate_tab == null)
                {
                    MessageBox.Show("请先载入配置信息！");
                    return;
                }
                //////for (int ColumnIndex = 0; ColumnIndex < algorithm_W.algorithm.calculate_tab.Rows.Count; ColumnIndex++)
                //////{
                //////    if (algorithm_W.algorithm.calculate_tab.Rows[ColumnIndex][1].ToString().Trim() == name)
                //////    {
                //////        MessageBox.Show("此组分已经存在！");
                //////        return;
                //////    }
                //////}

                select_v_list = "";
                if (yin_search_rBt.Checked)
                {
                    ////search_history_st = system_Settings.Default.tongdao_A_sql;
                    //////select_v_list =
                    ////string[] value_txt = search_history_st.Split(',');
                    select_v_list = "ID as [编号] , v_date as [日期] ,v_kind_t as [类型] ,";
                    for (int i = 0; i < algorithm_W.algorithm.calculate_tab.Rows.Count; i++)
                    {
                        select_v_list += "v_data" + (i + 1).ToString() + " as [" + algorithm_W.algorithm.calculate_tab.Rows[i][1] + "] , ";
                    }
                    select_v_list += "v_air_volume as [大气体积] ,v_ring_volume as [定量环体积] , v_liuliang as [流量] , v_yali as [压力] , v_dianya as [电压] ";
                    QueryString = "select " + select_v_list + " from save_data0 where v_date between '" + t1.ToString() + "' and '" + t2.ToString() + "' " + "order by ID asc";
                }
                else if (yan_search_rBt.Checked)
                {
                    ////search_history_st = system_Settings.Default.tongdao_B_sql;
                    ////string[] value_txt = search_history_st.Split(',');
                    select_v_list = "ID as [编号] , v_date as [日期] ,v_kind_t as [类型] ,";
                    for (int i = 0; i < algorithm_W.algorithm.calculate_tab.Rows.Count; i++)
                    {
                        select_v_list += "v_data" + (i + 1).ToString() + " as [" + algorithm_W.algorithm.calculate_tab.Rows[i][1] + "] , ";
                    }
                    select_v_list += "v_air_volume as [大气体积] ,v_ring_volume as [定量环体积] , v_liuliang as [流量] , v_yali as [压力] , v_dianya as [电压] ";
                    QueryString = "select " + select_v_list + " from save_data where v_date between '" + t1.ToString() + "' and '" + t2.ToString() + "' " + "order by ID asc";
                }

                select_table = sql_Dataclass.Dataclass.Query(QueryString);
                search_data_dataGridView.DataSource = select_table;
                //更新折线选择列表
                search_line_cBox1.Items.Clear();
                search_line_cBox2.Items.Clear();
                print_out_list.Items.Clear();
                for (int i = 3; i < search_data_dataGridView.Columns.Count; i++)
                {
                    search_data_dataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
                    search_line_cBox1.Items.Add(search_data_dataGridView.Columns[i].HeaderText);
                    search_line_cBox2.Items.Add(search_data_dataGridView.Columns[i].HeaderText);
                    print_out_list.Items.Add(search_data_dataGridView.Columns[i].HeaderText);
                }
                sql_Dataclass.Dataclass.write_operation_data("系统操作", "查询历史测量数据！");
            }
            else if (history_data_see_tab.SelectedIndex == 1)
            {
                select_v_list = " ID AS [编号], data_time AS [日期], data_kind AS [类型], data_infor AS [信息]";// sample_air_show_name.Default.operate_data_seach;
                QueryString = "select " + select_v_list + " from operate_data where data_time between '" + t1.ToString() + "' and '" + t2.ToString() + "' " + search_history_st + "order by ID asc";
                select_operate_table = sql_Dataclass.Dataclass.Query(QueryString);

                select_v_list = " ID AS [编号], data_time AS [日期], data_kind AS [类型], data_infor AS [信息]";//sample_air_show_name.Default.warn_data_seach;
                QueryString = "select " + select_v_list + " from warn_data where data_time between '" + t1.ToString() + "' and '" + t2.ToString() + "' " + search_history_st + "order by ID asc";
                select_warn_table = sql_Dataclass.Dataclass.Query(QueryString);
                if (operation_rBut.Checked)
                {
                    sql_Dataclass.Dataclass.write_operation_data("系统操作", "查询历史操作记录！");
                    work_data_dataGridView.DataSource = select_operate_table;
                }
                else if (warn_rBut.Checked)
                {
                    work_data_dataGridView.DataSource = select_warn_table;
                    sql_Dataclass.Dataclass.write_operation_data("系统操作", "查询历史报警记录！");
                }
            }
            search_date_show_panel.Hide();
        }
        private void search_date_close_button_Click(object sender, EventArgs e)
        {
            search_date_show_panel.Hide();
        }
        private void data_out_list_fresh_Click(object sender, EventArgs e)
        {
            try
            {
                data_out_listbox.Items.Clear();
                string[] pan = System.IO.Directory.GetLogicalDrives();
                for (int i = 0; i < pan.Length; i++)
                {
                    out_drive = new DriveInfo(pan[i]);
                    data_out_listbox.Items.Add(pan[i] + "    (" + out_drive.TotalSize / (1024 * 1024 * 1024) + "GB)");
                }
            }
            catch (Exception)
            {

            }
        }
        private void data_out_list_ok_Click(object sender, EventArgs e)
        {
            if (history_data_see_tab.SelectedIndex == 0)
            {
                if (data_out_listbox.SelectedIndex < 0) { MessageBox.Show("请先选择盘符！"); return; }
                if (data_out_kind == 1)
                {
                    string path = data_out_listbox.SelectedItem.ToString().Split('(')[0].Trim();
                    algorithm_W.algorithm.DataToExcel(search_data_dataGridView, path, DateTime.Now.ToFileTime().ToString());
                    sql_Dataclass.Dataclass.write_operation_data("系统操作", "查询历史测量数据表格导出！");
                    MessageBox.Show("测量浓度表格导出成功！");
                }
                else if (data_out_kind == 2)
                {
                    if (data_select_index < 0) { MessageBox.Show("请先选择行！"); return; }
                    string path = data_out_listbox.SelectedItem.ToString().Split('(')[0].Trim();
                    DateTime in_date = Convert.ToDateTime(search_data_dataGridView.Rows[data_select_index].Cells[3].Value.ToString());
                    string name = search_data_dataGridView.Rows[data_select_index].Cells[1].Value.ToString() + in_date.ToFileTime();
                    string data_txt = "";
                    for (int i = 0; i < search_data_dataGridView.Columns.Count; i++)
                    {
                        data_txt += search_data_dataGridView.Columns[i].HeaderText.Trim() + "    " + search_data_dataGridView.Rows[data_select_index].Cells[i].Value.ToString().Trim() + Environment.NewLine;
                    }
                    algorithm_W.algorithm.DataTotxt(data_txt, path, name);
                    sql_Dataclass.Dataclass.write_operation_data("系统操作", "查询历史测量数据TXT导出！");
                    MessageBox.Show("测量浓度TXT导出成功！  (" + name + ".txt" + ")");
                }
            }
            else if (history_data_see_tab.SelectedIndex == 1)
            {
                if (data_out_listbox.SelectedIndex < 0) { MessageBox.Show("请先选择盘符！"); return; }
                string path = data_out_listbox.SelectedItem.ToString().Split('(')[0].Trim();
                algorithm_W.algorithm.DataToExcel(work_data_dataGridView, path, DateTime.Now.ToFileTime().ToString());
                if (operation_rBut.Checked)
                {
                    sql_Dataclass.Dataclass.write_operation_data("系统操作", "查询历史操作记录表格导出！");
                    MessageBox.Show("历史操作记录表格导出成功！");
                }
                else if (warn_rBut.Checked)
                {
                    sql_Dataclass.Dataclass.write_operation_data("系统操作", "查询历史报警记录表格导出！");
                    MessageBox.Show("历史报警记录表格导出成功！");
                }
            }
            data_out_panel.Hide();
        }
        private void search_line_cBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (select_table == null) { MessageBox.Show("请先搜索！"); return; }
            chart1.DataSource = select_table;
            chart1.ChartAreas[0].AxisX.IntervalAutoMode = System.Windows.Forms.DataVisualization.Charting.IntervalAutoMode.VariableCount;

            chart1.Series[0].YValueMembers = search_line_cBox1.Text;
            chart1.Series[0].XValueMember = "编号";
            chart1.Series[0].LegendText = search_line_cBox1.Text;
            chart1.DataBind();
            sql_Dataclass.Dataclass.write_operation_data("系统操作", "查询历史测量数据折线显示1！");
        }
        private void search_line_cBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (select_table == null) { MessageBox.Show("请先搜索！"); return; }
            chart1.DataSource = select_table;
            chart1.ChartAreas[0].AxisX.IntervalAutoMode = System.Windows.Forms.DataVisualization.Charting.IntervalAutoMode.VariableCount;

            chart1.Series[1].YValueMembers = search_line_cBox2.Text;
            chart1.Series[1].XValueMember = "编号";
            chart1.Series[1].LegendText = search_line_cBox2.Text;
            chart1.DataBind();
            sql_Dataclass.Dataclass.write_operation_data("系统操作", "查询历史测量数据折线显示2！");
        }
        private void data_out_panel_close_Click(object sender, EventArgs e)
        {
            data_out_panel.Hide();
        }

        private void history_data_see_tab_DrawItem(object sender, DrawItemEventArgs e)
        {
            Font fntTab;
            Brush bshBack;
            Brush bshFore;
            Rectangle recTab = e.Bounds;
            recTab = new Rectangle(recTab.X, recTab.Y - 4, recTab.Width, recTab.Height + 4);
            if (e.Index == ((TabControl)sender).SelectedIndex)
            {
                fntTab = new Font(e.Font, FontStyle.Bold);
                bshBack = new SolidBrush(Color.WhiteSmoke);
                bshFore = Brushes.Black;
            }
            else
            {
                fntTab = e.Font;
                bshBack = new System.Drawing.Drawing2D.LinearGradientBrush(recTab, Color.FromArgb(240, 240, 240), Color.FromArgb(60, 60, 60), System.Drawing.Drawing2D.LinearGradientMode.Vertical);
                bshFore = new SolidBrush(Color.Black);
            }
            string tabName = ((TabControl)sender).TabPages[e.Index].Text;
            StringFormat sftTab = new StringFormat();
            sftTab.LineAlignment = StringAlignment.Center;
            sftTab.Alignment = StringAlignment.Center;
            e.Graphics.FillRectangle(bshBack, recTab);
            e.Graphics.DrawString(tabName, fntTab, bshFore, recTab, sftTab);
        }

        private void history_data_see_tab_MouseClick(object sender, MouseEventArgs e)
        {
            if (history_data_see_tab.SelectedIndex == 0)
            {
                yin_yang_search_cb_panel2.Show();
                search_date_show_panel.Show();

                row_data_out_show_panel.Hide();
                data_out_panel.Hide();
                chart_show_panel.Hide();
                chart_show_button.Text = "折线图开";
                row_data_out_show_panel.Hide();
                work_data_check_panel.Hide();
                chart_show_button.Show();
            }
            else if (history_data_see_tab.SelectedIndex == 1)
            {
                yin_yang_search_cb_panel2.Hide();
                work_data_check_panel.Show();
                if (operation_rBut.Checked)
                {
                    data_out_panel.Hide();
                    search_date_show_panel.Show();

                }
                else if (warn_rBut.Checked)
                {
                    data_out_panel.Hide();
                    search_date_show_panel.Show();

                }
                chart_show_button.Hide();
                data_out_panel.Hide();
            }
            else if (history_data_see_tab.SelectedIndex == 2)
            {
                data_out_panel.Hide();
                search_date_show_panel.Show();
                work_data_check_panel.Hide();
                chart_show_button.Hide();
                data_out_panel.Hide();
            }
        }

        private void Form3_FormClosed(object sender, FormClosedEventArgs e)
        {
            //////this.Close();         
        }


    }
}
