using ICSharpCode.Core;
using Mesnac.Action.Base;
using Mesnac.Action.ChemicalWeighing.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Mesnac.Action.ChemicalWeighing.BinManage
{
    class ModifyBinAction : ChemicalWeighingAction, IAction
    {
        #region 字段定义

        private RuntimeParameter _runtime;
        private DbMCControl _clientGridControl = null;      //料仓列表控件

        #endregion

        #region 事件定义

        /// <summary>
        /// 修改料仓信息事件定义
        /// </summary>
        public static event EventHandler OnModifyBin;

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

            #region  获取界面控件

            DbMCControl clientGridControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "Pmt_Bin").FirstOrDefault();
            if (clientGridControl == null)
            {
                ICSharpCode.Core.LoggingService<ModifyBinAction>.Error("缺少料仓信息列表控件...");
                return;
            }
            this._runtime = runtime;
            this._clientGridControl = clientGridControl;
            DataGridView clientGridView = this._clientGridControl.BaseControl as DataGridView;

            #endregion

            //验证是否选中某用户
            if (clientGridView.SelectedRows.Count != 1)
            {
                string msg1 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_BinManage_ModifyBinAction_msg1"));          //请选择一项要修改的料仓信息!
                MessageBox.Show(msg1, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                this._runtime.IsReturn = true;
                return;
            }
            //封装选中行对应的用户对象
            Pmt_Bin bin = new Pmt_Bin
            {
                Bin_Serial = Convert.ToInt32(clientGridView.SelectedRows[0].Cells["Bin_Serial"].Value),      //获取选中的用户ID
                Material_ID = clientGridView.SelectedRows[0].Cells["Material_ID"].Value as string
            };
            FrmBin frmBin = new FrmBin(ActionType.Modify, bin);
            DialogResult result = frmBin.ShowDialog();

            if (result == DialogResult.OK)
            {
                Pmt_Bin newBin = frmBin.Bin;
                frmBin.Dispose();

                string msg2 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_BinManage_ModifyBinAction_msg2"));      //确认修改{0}号料仓的物料信息?
                msg2 = String.Format(msg2, bin.Bin_Serial.ToString());
                if (MessageBox.Show(msg2, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    //更新数据库中的用户信息
                    BinHelper.updateBin(newBin);

                    #region 触发刷新料仓列表事件

                    if (OnModifyBin != null)
                    {
                        OnModifyBin(runtime, System.EventArgs.Empty);
                    }

                    #endregion
                }
            }
            else
            {
                frmBin.Dispose();
            }

        }
    }
}