using RGD.DBUtility;
using System;
using System.Data;

namespace RGD.WCS
{
    public partial class FrmTaskList : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        #region
        private static FrmTaskList _formInstance;

        public static FrmTaskList FormInstance
        {
            get
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmTaskList();
                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }

        #endregion

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

        private void FrmTaskList_Load(object sender, EventArgs e)
        {
        }

        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 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;
        }
    }
}