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

namespace Mesnac.Controls.Feeding
{
    public partial class SmallMaterialItem : UserControl
    {
        public SmallMaterialItem()
        {
            InitializeComponent();
        }

        #region 自定义属性

        private string _dataName;
        private DataTable _data;
        private DataTable _cloneData;
        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(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(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._cloneData.Dispose();
                    this._cloneData = null;
                }
                if (this._data != null)
                {
                    this._cloneData = this._data.Copy();
                    if (this._cloneData.Rows.Count > 0)
                    {
                        DataRow dr = this._cloneData.Rows[0];
                        this.labMaterilName.Text = dr["小料名称"].ToString();
                        this.labSet.Text = dr["小料标重"].ToString();
                        this.labFact.Text = dr["实重"].ToString();
                        this.labError.Text = dr["误差"].ToString();
                    }
                    else
                    {
                        this.labMaterilName.Text = string.Empty;
                        this.labSet.Text = string.Empty;
                        this.labFact.Text = string.Empty;
                        this.labError.Text = string.Empty;
                    }
                }
            }
            catch (Exception ex)
            {
                ICSharpCode.Core.LoggingService.Error("胶料称监控画面小料列表数据初始化错误:" + ex.Message, ex);
            }
        }
        #endregion

        /// <summary>
        /// 更新实际称量值
        /// </summary>
        private void UpdateCurrentValue(double value)
        {
            try
            {
                if (this._cloneData != null)
                {
                    this.labFact.Text = value.ToString();
                }
            }
            catch (Exception ex)
            {
                ICSharpCode.Core.LoggingService.Error("胶料称监控画面小料列表数据初始化错误:" + ex.Message, ex);
            }
        }

        /// <summary>
        /// 刷新当前行
        /// </summary>
        /// <param name="value"></param>
        private void UpdateCurrnetRow(int value)
        {
            if (value == 7 || value == 8)
            {
                this.labState.Text = "称量正确,等待送料...";
            }
            else if(value==1)
            {
                this.labState.Text = "正在称量第一种物料";
            }
            else
                this.labState.Text = "";
        }
    }
}