using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using SiaSun.LMS.Model;
using SiaSun.LMS.Common;
using System.Net;

namespace SiaSun.LMS.WPFClient.FLOW_ACTION
{
    /// <summary>
    /// FLOW_ACTION.xaml 的交互逻辑
    /// </summary>
    public partial class LOG_ACTION : AvalonDock.DocumentContent
    {
        string strExceptPlanStatus = string.Empty;

        private string[] _aFLOW_PARA;

        private bool bLoad = true;

        /// <summary>
        /// 构造函数
        /// </summary>
        public LOG_ACTION()
        {
            InitializeComponent();

            this.ucQueryPlan.U_Query += new UC.ucQuickQuery.U_QueryEventHandler
                                       ((QueryWhere) =>
                                       {
                                           try
                                           {
                                               this.ucLogGrid.U_AllowPage = true;
                                               this.ucLogGrid.U_AppendWhere = QueryWhere;
                                               this.ucLogGrid.U_InitControl();
                                               //this.SelectPlanDefault();
                                           }
                                           catch (Exception ex)
                                           {
                                               MainApp._MessageDialog.ShowException(ex);
                                           }
                                       }
                                       );
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public LOG_ACTION(string EXCEPT_STATUS)
            : this()
        {
            strExceptPlanStatus = EXCEPT_STATUS;
        }

        /// <summary>
        /// 加载窗体
        /// </summary>
        private void DocumentContent_Loaded(object sender, RoutedEventArgs e)
        {
            //this._lsFLOW_PARA = MainApp._I_FlowService.FlowGetParameters("FLOW_PLAN");
            //this._aFLOW_PARA = new string[this._lsFLOW_PARA.Count];

            this.PLAN_Query_Bind();
            this.PLAN_Bind();
            this.ucLogGrid.gridApp.GotFocus += new RoutedEventHandler(gridApp_GotFocus);
        }
        void gridApp_GotFocus(object sender, RoutedEventArgs e)
        {
            this.ucLogGrid.gridApp.SelectionChanged -= new SelectionChangedEventHandler(gridApp_SelectionChanged);
            this.ucLogGrid.gridApp.SelectionChanged += new SelectionChangedEventHandler(gridApp_SelectionChanged);
        }
        /// <summary>
        /// 
        /// </summary>
        void gridApp_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.ucLogGrid.gridApp.SelectedItem == null)
            {

                return;
            }

            DataRowView drvLog = this.ucLogGrid.gridApp.SelectedItem as DataRowView;

            string name = drvLog["NAME"].ToString();
            this.txtParameter.Text = drvLog["PARAMETER"].ToString();
            //this.txtParameter.Text = drvLog["result"].ToString();

            this.lbName.Text = drvLog["NAME"].ToString();
        }
        /// <summary>
        /// 
        /// </summary>
        private void PLAN_Query_Bind()
        {
            try
            {

                this.ucQueryPlan.U_XmlTableName = "INTERFACE_LOG";
                this.ucQueryPlan.U_WindowName = this.GetType().Name;
                this.ucQueryPlan.U_InitControl();
            }
            catch (Exception ex)
            {
                MainApp._MessageDialog.ShowException(ex);
            }
        }

        //查询
        void ucQueryPlan_U_Query(string QueryWhere)
        {
            this.ucLogGrid.U_AllowPage = true;
            this.ucLogGrid.U_AppendWhere = QueryWhere;
            this.ucLogGrid.U_InitControl();
        }

        /// <summary>
        /// 
        /// </summary>
        private void PLAN_Bind()
        {
            try
            {
                this.ucLogGrid.gridApp.RowHeight = 25;

                //设置数据源
                this.ucLogGrid.U_WindowName = this.GetType().Name;
                this.ucLogGrid.U_TableName = "INTERFACE_LOG";
                this.ucLogGrid.U_OrderField = "INVOKE_TIME DESC";
                //设置筛选条件
                //this.ucLogGrid.U_Where = string.Format(@"INVOKE_TIME LIKE '%{0}%'", DateTime.Now.ToString("yyyy-MM-dd"));// strExceptPlanStatus.Length > 0 ? "PLAN_STATUS NOT IN('" + strExceptPlanStatus.Replace("|", "','") + "')" : string.Empty;

                this.ucLogGrid.U_AllowChecked = false;
                this.ucLogGrid.U_AllowOperatData = false;
                this.ucLogGrid.U_AllowPage = true;

                //初始化控件
                this.ucLogGrid.U_InitControl();

                //加载默认数据
                this.SelectPlanDefault();
            }
            catch (Exception ex)
            {
                MainApp._MessageDialog.ShowException(ex);
            }
        }

        /// <summary>
        /// 加载默认数据
        /// </summary>
        private void SelectPlanDefault()
        {
            //if (this.gridPlan.gridApp.SelectedItem != null)
            //{
            //    DataRowView drvPlan = this.gridPlan.gridApp.SelectedItem as DataRowView;
            //    int intPlanID = drvPlan == null ? 0 : Convert.ToInt32(drvPlan["PLAN_ID"].ToString());

            //    //加载计划清单
            //    this.PLAN_LIST_Bind(intPlanID);

            //    //流程行为按钮绑定
            //    this.PLAN_ACTION_Bind(drvPlan.Row);
            //}
        }


        /// <summary>
        /// 加载
        /// </summary>
        private void PLAN_LIST_Bind(int PLAN_ID)
        {
            //数据源属性
            //明细属性
            //this.gridPlanList.U_DetailTableName = "PLAN_DETAIL";
            //this.gridPlanList.U_DetailRelatvieColumn = "PLAN_LIST_ID";

            try
            {
                //初始化控件

            }
            catch (Exception ex)
            {
                MainApp._MessageDialog.ShowException(ex);
            }
        }

        /// <summary>
        /// 状态流按钮加载
        /// </summary>
        private void PLAN_ACTION_Bind(DataRow rowPlan)
        {

        }

        private void btnInvoke_Click(object sender, RoutedEventArgs e)
        {
            bool bResult = true;
            string sResult = string.Empty;

            DataRowView drvLog = this.ucLogGrid.gridApp.SelectedItem as DataRowView;
            string stock_barcode = drvLog["KEY_WORD"].ToString();
            switch (lbName.Text)
            {
                case "Send_FG_Putawway_update"://FG入库在次更新SAP
                    //bResult = MainApp._I_SAPService.Send_FG_Putaway_Update(stock_barcode, this.txtParameter.Text, out sResult);
                    if (bResult)
                    {
                        sResult = "Send_FG_Putawway_update在次调用成功";
                        MainApp._MessageDialog.ShowResult(bResult, sResult);
                    }
                    else
                    {
                        MainApp._MessageDialog.ShowResult(bResult, sResult);
                    }
                    break;
                default:
                    break;
            }
            ucLogGrid.U_Update();
        }
    }
}