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.Navigation;
using System.Windows.Shapes;

namespace SiaSun.LMS.WPFClient.SYS
{
    /// <summary>
    /// RECORD_MES_LOG.xaml 的交互逻辑
    /// </summary>
    public partial class RECORD_MES_LOG : AvalonDock.DocumentContent
    {
        public RECORD_MES_LOG()
        {
            InitializeComponent();

            //ucLogQuery控件委托
            this.ucLogQuery.U_Query += new UC.ucQuickQuery.U_QueryEventHandler
                                            ((QueryWhere) =>
                                            {
                                                try
                                                {
                                                    QueryWhere = string.IsNullOrEmpty(QueryWhere) ? "1=1" : QueryWhere;
                                                    string strDateTime = this.GetDateStr();
                                                    QueryWhere = string.IsNullOrEmpty(strDateTime) ? QueryWhere : string.Format("{0} and {1}", QueryWhere, strDateTime);
                                                    this.gridLog_Bind(QueryWhere);
                                                }
                                                catch (Exception ex)
                                                {
                                                    MainApp._MessageDialog.ShowException(ex);
                                                }
                                            }
                                            );
        }
        /// <summary>加载窗体
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DocumentContent_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                //默认勾选时间
                this.chkLogCreateTime.IsChecked = true;
                //默认日期
                this.dtpStart.SelectedDate = DateTime.Now;
                this.dtpEnd.SelectedDate = DateTime.Now;

                //绑定ucLogQuery
                this.ucLogQuery_Bind();

                //绑定gridLog
                this.gridLog_Bind(this.GetDateStr());
            }
            catch (Exception ex)
            {
                MainApp._MessageDialog.ShowException(ex);
            }
        }

        /// <summary>绑定ucLogQuery
        /// 
        /// </summary>
        private void ucLogQuery_Bind()
        {
            try
            {
                this.ucLogQuery.U_WindowName = this.GetType().Name;
                this.ucLogQuery.U_XmlTableName = "MES_INTERFACE_LOG";
                this.ucLogQuery.U_InitControl();
            }
            catch (Exception ex)
            {
                MainApp._MessageDialog.ShowException(ex);
            }
        }

        /// <summary>绑定gridLog
        /// 
        /// </summary>
        private void gridLog_Bind(string strWhere)
        {
            try
            {
                this.gridLog.U_WindowName = this.GetType().Name;
                this.gridLog.U_TableName = "MES_INTERFACE_LOG";
                this.gridLog.U_XmlTableName = "MES_INTERFACE_LOG";
                this.gridLog.U_OrderField = "TIME_STAMP DESC";
                this.gridLog.U_Where = strWhere;
                this.gridLog.U_AllowOperatData = false;
                this.gridLog.U_AllowChecked = false;
                this.gridLog.U_AllowPage = true;
                this.gridLog.U_AllowSave = Visibility.Visible;
                this.gridLog.U_AllowEdit = Visibility.Collapsed;
                this.gridLog.U_AllowCancel = Visibility.Collapsed;
                this.gridLog.U_AllowAdd = Visibility.Visible;
                this.gridLog.U_AllowDelete = Visibility.Visible;
                this.gridLog.U_InitControl();
            }
            catch (Exception ex)
            {
                MainApp._MessageDialog.ShowException(ex);
            }
        }

        /// <summary>获得时间查询条件
        /// 
        /// </summary>
        private string GetDateStr()
        {
            string strDateWhere = string.Empty;
            string strDateTimeColumn = "TIME_STAMP";

            //判断是否添加时间查询
            if (this.chkLogCreateTime.IsChecked == true)
            {
                //判断选择的值
                if (this.dtpStart.SelectedDate.HasValue && !this.dtpEnd.SelectedDate.HasValue)
                {
                    strDateWhere = string.Format("({0}>='{1} 00:00:00')",
                                                                               strDateTimeColumn,
                                                                               this.dtpStart.SelectedDate.Value.ToString("yyyy-MM-dd"));
                }
                else if (!this.dtpStart.SelectedDate.HasValue && this.dtpEnd.SelectedDate.HasValue)
                {
                    strDateWhere = string.Format("({0}<='{1} 23:59:59')",
                                                                              strDateTimeColumn,
                                                                              this.dtpEnd.SelectedDate.Value.ToString("yyyy-MM-dd"));
                }
                else if (this.dtpStart.SelectedDate.HasValue && this.dtpEnd.SelectedDate.HasValue)
                {
                    strDateWhere = string.Format("({0}>='{1} 00:00:00' AND {2}<='{3} 23:59:59')",
                                                                            strDateTimeColumn,
                                                                            this.dtpStart.SelectedDate.Value.ToString("yyyy-MM-dd"),
                                                                            strDateTimeColumn,
                                                                            this.dtpEnd.SelectedDate.Value.ToString("yyyy-MM-dd"));

                }
            }
            return strDateWhere;
        }
    }
}