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

namespace ControlSystem
{
    public partial class FrmTaskList : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        #region Modified by DingXiaoxu for Dock TaskList on 20101219

        #region Functions
        public FrmTaskList()
        {
            InitializeComponent();
            _formInstance = this;
            this.dgvManager.DataSource = dbo.ExceSQL("select * from V_Manage_Task where " + CStaticClass.Manstatus).Tables[0].DefaultView;
            this.dgvMonitor.DataSource = dbo.ExceSQL("select * from V_Monitor_Task where " + CStaticClass.Monstatus + " order by 设备指令索引 asc ").Tables[0].DefaultView;
        }

        public void ManagerRefresh(object dataSource)
        {
            if (this.dgvManager == null)
            {
                return;
            }

            if (this.dgvManager.DataSource == null || dataSource == null)
            {
                return;
            }
            if (!this.CompareDataTable((dataSource as DataView).Table, (this.dgvManager.DataSource as DataView).Table))
            {
                if (this.dgvManager == null)
                {
                    return;
                }
                if (this.dgvManager.DataSource == null || dataSource==null)
                {
                    return;
                }
                this.dgvManager.DataSource = dataSource;

            }
            //20100108
            dataSource = null;

        }

        public void MonitorRefresh(object dataSource)
        {
            if (this.dgvMonitor == null)
            {
                return;
            }
            
            if (this.dgvMonitor.DataSource == null || dataSource == null)
            {
                return;
            }
            if (!this.CompareDataTable((dataSource as DataView).Table, (this.dgvMonitor.DataSource as DataView).Table))
            {
                if (this.dgvMonitor == null)
                {
                    return;
                }
            
                if (this.dgvMonitor.DataSource == null || dataSource == null)
                {
                    return;
                }
                this.dgvMonitor.DataSource = dataSource;
            }

            //20100108
            dataSource = null;
        }

        private void rbmanAll_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            if ((rb != null) && (rb.Checked == true))
            {
                CStaticClass.Manstatus = " F_Status<>-1 ";
            }
            this.ManageTaskFilte();
        }

        private void rbmanRun_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            if ((rb != null) && (rb.Checked == true))
            {
                CStaticClass.Manstatus = " (F_Status>=1) ";
            }
            this.ManageTaskFilte();
        }

        private void rbmanWait_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            if ((rb != null) && (rb.Checked == true))
            {
                CStaticClass.Manstatus = " F_Status=0 ";
            }
            this.ManageTaskFilte();
        }

        private bool CompareDataTable(DataTable newTable, DataTable oldTable)//Added by on 20101218 by DingXiaoxu for List refresh
        {
            if (newTable == null || oldTable == null)
            {
                return true;//20110513
            }
            if (newTable.Rows.Count != oldTable.Rows.Count)
            {
                return false;
            }
            if (newTable.Columns.Count != oldTable.Columns.Count)
            {
                return false;
            }
            for (int i = 0; i < newTable.Rows.Count; i++)
            {
                for (int j = 0; j < newTable.Columns.Count; j++)
                {
                    if (!newTable.Rows[i][j].ToString().Equals(oldTable.Rows[i][j].ToString()))
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        //private void comboBox_Kind_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        string df = "";

        //        switch (this.comboBox_Kind.Text)
        //        {
        //            case "表箱条码":
        //                df = "表箱条码";
        //                break;
        //            case "调度任务索引":
        //                df = "调度任务索引";
        //                break;
        //            case "设备指令索引":
        //                df = "设备指令索引";
        //                break;


        //            default:
        //                return;
        //        }
        //        //20100208
        //        string sql = "select distinct " + df + " from V_Monitor_Task ";
        //        DataView dv = dbo.ExceSQL(sql).Tables[0].DefaultView;
        //        if (dv.Count > 0)
        //        {
        //            this.textBox_KeyWord.DataSource = null;
        //            this.textBox_KeyWord.DisplayMember = df;
        //            this.textBox_KeyWord.ValueMember = df;
        //            this.textBox_KeyWord.DataSource = dv;

        //            //this.comboBox_Kind.DataSource = null;
        //            //this.comboBox_Kind.DisplayMember = df;
        //            //this.comboBox_Kind.ValueMember = df;
        //            //this.comboBox_Kind.DataSource = dv;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}

        private void button_StartSearch_Click(object sender, EventArgs e)
        {
            if (this.textBox_KeyWord.Text.Trim().Length == 0) return;
            int index = 0;
            bool isint = int.TryParse(this.textBox_KeyWord.Text.Trim(), out index);

            if (this.comboBox_Kind.Text == "表箱条码")
            {
                CStaticClass.Monstatus = " 表箱条码 like'%" + this.textBox_KeyWord.Text.Trim() + "%' ";
            }
            if (this.comboBox_Kind.Text == "调度任务索引")
            {
                if (isint == false)
                {
                    //MessageBox.Show("请输入数字！", "误操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBox_KeyWord.Text = string.Empty;
                    return;
                }
                CStaticClass.Monstatus = " 调度任务索引='" + this.textBox_KeyWord.Text.Trim() + "' ";
            }
            if (this.comboBox_Kind.Text == "设备指令索引")
            {
                if (isint == false)
                {
                    //MessageBox.Show("请输入数字！", "误操作提示：", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    textBox_KeyWord.Text = string.Empty;
                    return;
                }
                CStaticClass.Monstatus = " 设备指令索引='" + this.textBox_KeyWord.Text.Trim() + "' ";
            }

            this.MonitorTaskFilte();
        }

        private void MonitorTaskFilte()
        {
            CStaticClass.RealRefresh = true;
            MonitorRefresh(dbo.ExceSQL("select * from V_Monitor_Task where " + CStaticClass.Monstatus + " order by 设备指令索引 asc ").Tables[0].DefaultView);
        }

        private void ManageTaskFilte()
        {
            CStaticClass.RealRefresh = true;
            ManagerRefresh(dbo.ExceSQL("select * from V_Manage_Task where " + CStaticClass.Manstatus).Tables[0].DefaultView);
        } 
        #endregion

        #region Properties and Members
        private DBOperator dbo = CStaticClass.dbo;

        private static FrmTaskList _formInstance;
        public static FrmTaskList FormInstance
        {
            get
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmTaskList();

                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }
        #endregion 

        private void button_DisplayAll_Click(object sender, EventArgs e)
        {
            CStaticClass.Monstatus = " F_Status<>-1 ";
            MonitorTaskFilte();
        }


        #endregion




    }
}