using System;
using System.Linq;
using System.Windows.Forms;
using Mesnac.Action.Base;
using Mesnac.Action.ChemicalWeighing.LjMaterial;

namespace Mesnac.Action.ChemicalWeighing.LjDevice
{
    public class UpdateAction : ChemicalWeighingAction, IAction
    {
        private DbMCControl _clientGridControl = null; //用户列表控件

        /// <summary>
        /// 新增物料事件定义
        /// </summary>
        public static event EventHandler OnUpdateHandler;

        private RuntimeParameter _runtime;

        public void Run(RuntimeParameter runtime)
        {
            base.RunIni(runtime); //必须调用
            this._runtime = runtime;
            DoWork();
        }

        private void DoWork()
        {



            DbMCControl clientGridControl =
                this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "Device").FirstOrDefault();

            if (clientGridControl == null)
            {
                ICSharpCode.Core.LoggingService<UpdateAction>.Error("缺少物料信息列表控件...");
                return;
            }

            this._clientGridControl = clientGridControl;
            DataGridView clientGridView = this._clientGridControl.BaseControl as DataGridView;


            //验证是否选中某物料
            if (clientGridView.SelectedRows.Count != 1)
            {
                string msg1 = "请选择一项要修改的设备!"; //请选择一项要修改的物料!
                MessageBox.Show(msg1, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
                this._runtime.IsReturn = true;
                return;
            }

            int id = (int)clientGridView.SelectedRows[0].Cells[0].Value;
            var name = clientGridView.SelectedRows[0].Cells["name"].Value as string;

            this._runtime.BaseControl.MCEnabled = false;
            FrmDevice frmInsertMaterial = new FrmDevice(new DeviceModel()
            {
                ID = id,
                Name = name
            });
            frmInsertMaterial.ShowDialog(this._runtime.BaseControl.MCRoot as Control);
            if (frmInsertMaterial.DialogResult == DialogResult.OK)
            {
                if (OnUpdateHandler != null)
                {
                    OnUpdateHandler(this._runtime.BaseControl.MCRoot, System.EventArgs.Empty);
                }

                frmInsertMaterial.Dispose();
              
            }
            
            this._runtime.BaseControl.MCEnabled = true;


        }
    }
}