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.Product.PptPlan
{
    /// <summary>
    /// 删除计划业务
    /// </summary>
    public class DeleteAction : ChemicalWeighingAction, IAction
    {
        #region 事件定义

        /// <summary>
        /// 删除计划事件定义
        /// </summary>
        public static event EventHandler OnDeletePlan;

        #endregion

        #region 字段定义

        private RuntimeParameter _runtime;
        //private DbMCControl _planDateControl = null;        //计划日期
        //private DbMCControl _pptShiftControl = null;        //班次
        private DbMCControl _clientGridControl = null;      //网格计划控件

        #endregion

        #region IAction接口实现

        public void Run(RuntimeParameter runtime)
        {
            base.RunIni(runtime);
            this._runtime = runtime;

            ICSharpCode.Core.LoggingService<DeleteAction>.Debug("当班计划—删除计划业务...");

            //DbMCControl planDateControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "[RT_plan].[Plan_Date]").FirstOrDefault();
            //DbMCControl pptShiftControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "Pmt_Shiftime").FirstOrDefault();
            DbMCControl clientGridControl = this.GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType.Local, "Base_PlanInfo").FirstOrDefault();         //获取本机台计划网格控件

            //if (planDateControl == null)
            //{
            //    ICSharpCode.Core.LoggingService<InsertAction>.Error("{当班计划-删除计划} 缺少日期选择控件...");
            //    return;
            //}
            //if (pptShiftControl == null)
            //{
            //    ICSharpCode.Core.LoggingService<InsertAction>.Error("{当班计划-删除计划} 缺少班次组合框控件...");
            //    return;
            //}

            //this._planDateControl = planDateControl;
            //this._pptShiftControl = pptShiftControl;
            this._clientGridControl = clientGridControl;

            this.DoWork();
        }

        #endregion

        #region 方法定义

        /// <summary>
        /// 删除计划
        /// </summary>
        protected void DoWork()
        {
            this._runtime.BaseControl.MCEnabled = false;

            try
            {
                DataGridView clientGridView = this._clientGridControl.BaseControl as DataGridView;
                if (clientGridView.SelectedRows.Count == 1)
                {
                    string planID = clientGridView.SelectedRows[0].Cells["plan_Id"].Value as string;

                    PlanState planState = (PlanState)PlanHelper.StateToValue(clientGridView.SelectedRows[0].Cells["Plan_State"].Value.ToString());

                    if (planState == PlanState.Received)
                    {
                        string msg2 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_DeleteAction_msg20"));      //计划号:{0} 正在执行,不能删除,只能取消!
                        msg2 = String.Format(msg2, planID);
                        MessageBox.Show(msg2, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    if (planState == PlanState.Completed)
                    {
                        string msg2 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_DeleteAction_msg20"));      //计划号:{0} 已完成,不能删除!
                        msg2 = String.Format(msg2, planID);
                        MessageBox.Show(msg2, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }

                    //if (planState == PlanState.UnStart)
                    //{
                    //    string msg55 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_DeleteAction_msg55"));        //计划号:{0}已下传PLC,未启动状态,不能删除!
                    //    msg55 = String.Format(msg55, planID);
                    //    MessageBox.Show(msg55, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //    return;
                    //}
                    //if (planState == PlanState.Download)
                    //{
                    //    string msg6 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_DeleteAction_msg60"));      //计划号:{0} 已下传,不能删除!
                    //    msg6 = String.Format(msg6, planID);
                    //    MessageBox.Show(msg6, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //    return;
                    //}

                    string msg1 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_DeleteAction_msg1"));      //您确认要删除当前计划(计划号:{0})吗?
                    msg1 = String.Format(msg1, planID);
                    DialogResult result = MessageBox.Show(msg1, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (result == DialogResult.Yes)
                    {
                        //删除本地库计划
                        if (PlanHelper.DeletePlan(planID))
                        {
                            #region 触发事件

                            if (OnDeletePlan != null)
                            {
                                OnDeletePlan(null, System.EventArgs.Empty);
                            }
                            ICSharpCode.Core.LoggingService<DeleteAction>.Debug("删除计划成功!");
                            #endregion
                        }


                        #region 将等待计划数写入PLC

                        //ChemicalWeighingPlc.PlcPlanHelper.WaitPlanNumToPLC();

                        #endregion

                        #region 重新下传计划列表

                        //ChemicalWeighingPlc.PlcPlanHelper.PlanListReDown();

                        #endregion
                    }
                }
                else
                {
                    string msg4 = StringParser.Parse(ResourceService.GetString("Mesnac_Action_ChemicalWeighing_Product_PptPlan_DeleteAction_msg4"));      //请选择一条要删除的计划!
                    MessageBox.Show(msg4, Mesnac.Basic.LanguageHelper.Caption, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                ICSharpCode.Core.LoggingService<DeleteAction>.Error("删除计划异常:" + ex.Message, ex);

                #region 记录操作日志

                //base.DBLog(ex.Message);

                #endregion

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

        #endregion
    }
}