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

namespace Mesnac.Controls.Feeding
{
    /// <summary>
    /// 小料列表
    /// </summary>
    [ToolboxBitmap(typeof(System.Windows.Forms.DataGridView))]
    public partial class SmallMaterialList2 : DataGridView
    {
        public SmallMaterialList2()
        {
            InitializeComponent();
            InitMethod();
            this.Resize += new EventHandler(SmallMaterialList_Resize);
        }

        public SmallMaterialList2(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
            InitMethod();
            this.Resize += new EventHandler(SmallMaterialList_Resize);
        }
        /// <summary>
        /// 初始化方法
        /// </summary>
        private void InitMethod()
        {
            this.SelectionMode = DataGridViewSelectionMode.FullRowSelect; //整行选择模式
            this.MultiSelect = false;       //禁用多行选项
            this.ReadOnly = true;    //禁止编辑
            this.RowTemplate.Height = 38;
            this.DefaultCellStyle.Font = new System.Drawing.Font("宋体", 28F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.ColumnHeadersHeight = 42;
            this.ColumnHeadersDefaultCellStyle.Font = new System.Drawing.Font("宋体", 25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;
            this.RowHeadersVisible = false;
            this.ScrollBars = ScrollBars.None;
        }

        protected void SmallMaterialList_Resize(object sender, EventArgs e)
        {
            this.SetColumnsStyle();
        }
        private void SetColumnsStyle()
        {
            lock (String.Empty)
            {
                try
                {
                    //设定初始列宽 //待调试
                    if (this.Columns.Contains("小料名称"))
                    {
                        this.Columns["小料名称"].Width = 200;
                    }
                    if (this.Columns.Contains("序号"))
                    {
                        this.Columns["序号"].Width = 50;
                    }
                    if (this.Columns.Contains("小料标重"))
                    {
                        this.Columns["小料标重"].Width = 100;
                    }
                    if (this.Columns.Contains("实重"))
                    {
                        this.Columns["实重"].Width = 100;
                    }
                    if (this.Columns.Contains("误差"))
                    {
                        this.Columns["误差"].Width = 100;
                    }
                }
                catch (Exception ex)
                {
                    ICSharpCode.Core.LoggingService.Error("胶料称监控画面小料列表控件设置列宽错误:" + ex.Message, ex);
                }
            }
        }

        #region 自定义属性

        private string _dataName;
        private DataTable _data;
        private DataTable _cloneData;
        private bool _bHaveAction;
        private string _smallMaterialName;
        private int _smallMaterial;
        private string _smallMaterialValueName;
        private double _smallMaterialValue;


        /// <summary>
        /// 数据源动画属性
        /// </summary>
        public string DataName
        {
            get { return _dataName; }
            set { _dataName = value; }
        }
        /// <summary>
        /// 正在称量名称
        /// </summary>
        public string SmallMaterialName
        {
            get { return this._smallMaterialName; }
            set { this._smallMaterialName = value; }
        }
        /// <summary>
        /// 正在称量画面
        /// </summary>
        public int SmallMaterial
        {
            get { return this._smallMaterial; }
            set
            {
                this._smallMaterial = value;
                UpdateCurrnetRow(1, value);
            }
        }


        /// <summary>
        /// 小料实际值动画属性
        /// </summary>
        public string SmallMaterialValueName
        {
            get { return this._smallMaterialValueName; }
            set { this._smallMaterialValueName = value; }
        }
        /// <summary>
        /// 正在称量小料
        /// </summary>
        public double SmallMaterialValue
        {
            get { return this._smallMaterialValue; }
            set
            {
                this._smallMaterialValue = value;
                this.UpdateCurrentValue(1, value);
            }
        }

        #endregion
        #region 数据属性
        /// <summary>
        /// 数据源
        /// </summary>
        public DataTable Data
        {
            get { return _data; }
            set
            {
                _data = value;
                if (this._cloneData == null || this._cloneData.Rows.Count == 0 || this._cloneData.TableName != this._data.TableName)
                {
                    InitData();
                }
            }
        }
        /// <summary>
        /// 数据初始化
        /// </summary>
        private void InitData()
        {
            try
            {
                if (this._cloneData != null)
                {
                    this.DataSource = null;
                    this._cloneData.Dispose();
                    this._cloneData = null;
                }
                if (this._data != null)
                {
                    this._cloneData = this._data.Copy();
                    this.DataSource = this._cloneData;
                    //禁止排序
                    for (int i = 0; i < this.ColumnCount; i++)
                    {
                        this.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
                    }
                    this.SetColumnsStyle();
                    this.ClearSelection();
                }
            }
            catch (Exception ex)
            {
                ICSharpCode.Core.LoggingService.Error("胶料称监控画面小料列表数据初始化错误:" + ex.Message, ex);
            }
        }
        #endregion
        /// <summary>
        /// 刷新当前行
        /// </summary>
        /// <param name="value"></param>
        private void UpdateCurrnetRow(int rowIndex,int value)
        {
            if (rowIndex < 1)
            {
                this.ClearSelection();
            }
            else
            {
                if (this._cloneData != null && this._cloneData.Columns.Contains("序号") && this._cloneData.Rows.Count > 0)
                {
                    if (rowIndex <= this._cloneData.Rows.Count)
                    {
                        DataRow row = this._cloneData.Rows[rowIndex - 1];
                        object val = row["序号"];
                        if (val != DBNull.Value)
                        {
                            this.ClearSelection();
                            if (value == 7 || value == 8)
                            {
                                this.Rows[rowIndex - 1].DefaultCellStyle.BackColor = Color.GreenYellow;
                            }
                            else
                            {
                                this.Rows[rowIndex - 1].DefaultCellStyle.BackColor = Color.White;
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 更新实际称量值
        /// </summary>
        private void UpdateCurrentValue(int rowIndex, double value)
        {
            try
            {
                if (rowIndex < 1)
                {
                    this.ClearSelection();
                }
                else
                {
                    if (this._cloneData != null)
                    {
                        if (this._cloneData.Columns.Contains("实重"))
                        {
                            if (rowIndex <= this._cloneData.Rows.Count)
                            {
                                DataRow row = this._cloneData.Rows[rowIndex - 1];
                                if (row != null)
                                {
                                    row["实重"] = value;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ICSharpCode.Core.LoggingService.Error("胶料称监控画面小料列表数据初始化错误:" + ex.Message, ex);
            }
        }
    }
}