using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using ICSharpCode.Core;
using Mesnac.Controls.Base;
using Mesnac.Action.Base;
using Mesnac.Codd.Session;
using Mesnac.Action.ChemicalWeighing.Entity;

namespace Mesnac.Action.ChemicalWeighing.Technical.PmtRecipe
{
    /// <summary>
    /// 添加配方业务
    /// </summary>
    public class InsertAction : ChemicalWeighingAction, IAction
    {
        #region 事件定义

        /// <summary>
        /// 添加配方事件定义
        /// </summary>
        public static event EventHandler OnInsertRecipe;

        #endregion

        #region 字段定义

        private RuntimeParameter _runtime;
        private DbMCControl _clientGridControl = null;

        #endregion

        #region IAction接口实现
        public void Run(RuntimeParameter runtime)
        {
            base.RunIni(runtime);
            this._runtime = runtime;

            ICSharpCode.Core.LoggingService<InsertAction>.Debug("配方管理—添加配方业务...");

            List<DbMCControl> recipeControlList = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "Base_RecipeInfo");

            DbMCControl clientGridControl = recipeControlList.Where(x => x.BaseControl is TreeView).FirstOrDefault();

            if (clientGridControl == null || !(clientGridControl.BaseControl is TreeView))
            {
                ICSharpCode.Core.LoggingService<RefreshAction>.Error("{配方管理—添加配方}缺少配方管理控件...");
                return;
            }
            this._clientGridControl = clientGridControl;
            this.DoWork();
        }

        #endregion

        #region 方法定义

        /// <summary>
        /// 添加配方
        /// </summary>
        protected void DoWork()
        {
            this._runtime.BaseControl.MCEnabled = false;
            try
            {   
                FrmRecipe frmNewRecipe = new FrmRecipe(ActionType.Add);
                frmNewRecipe.ShowDialog(this._runtime.BaseControl.MCRoot as Control);
                if (frmNewRecipe.DialogResult == DialogResult.OK)
                {
                    frmNewRecipe.Dispose();

                    #region 触发事件

                    if (OnInsertRecipe != null)
                    {
                        OnInsertRecipe(this._runtime.BaseControl.MCRoot, System.EventArgs.Empty);
                    }
                    MessageBox.Show("新配方添加成功!");
                    #endregion
                }
                else
                {
                    frmNewRecipe.Dispose();
                }
            }
            catch (Exception ex)
            {
                ICSharpCode.Core.LoggingService<InsertAction>.Error("添加新配方异常:" + ex.Message, ex);

                #region 记录操作日志

                base.DBLog(ex.Message);

                #endregion

                MessageBox.Show(ex.Message, Mesnac.Basic.LanguageHelper.WarnCaption, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            this._runtime.BaseControl.MCEnabled = true;
        }

        #endregion
    }
}