using SSWMS.Common;
using System;
using System.Data;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace SSWMS.Client
{
    public partial class QueryDialog : Window
    {
        public string U_Where = string.Empty;

        public QueryDialog()
        {
            InitializeComponent();
        }

        public void U_InitWindow(DataTable dt)
        {
            try
            {
                int iRow = 0;
                foreach (DataRow rowField in dt.Select("Header<>''"))
                {
                    this.gridControls.RowDefinitions.Add(new RowDefinition
                    {
                        Height = GridLength.Auto
                    });
                    TextBlock tbTitle = new TextBlock
                    {
                        MinWidth = 65,
                        Margin = new Thickness(5, 0, 2, 0),
                        VerticalAlignment = VerticalAlignment.Center,
                        Text = rowField["Header"].ToString()
                    };
                    Grid.SetColumn(tbTitle, 0);
                    Grid.SetRow(tbTitle, iRow);
                    this.gridControls.Children.Add(tbTitle);

                    StackPanel panelItem = new StackPanel
                    {
                        Tag = rowField["Column"].ToString(),
                        Margin = new Thickness(10, 2, 10, 2),
                        Orientation = Orientation.Horizontal
                    };
                    panelItem.Children.Add(new ComboBox
                    {
                        Width = 65,
                        Height = 21,
                        Tag = "CompareOp",
                        Margin = new Thickness(1),
                        DisplayMemberPath = "NAME",
                        SelectedValuePath = "VALUE",
                        ItemsSource = SystemCodeData.DicDataView["OPERATOR_COMPARE"],
                        SelectedIndex = 0
                    });
                    string sControlType = rowField["Control"].ToString();
                    switch (sControlType)
                    {
                        case "CheckBox":
                            panelItem.Children.Add(new CheckBox
                            {
                                VerticalAlignment = VerticalAlignment.Center,
                                Width = 150,
                                IsThreeState = false,
                                Margin = new Thickness(10, 2, 5, 2)
                            });
                            break;
                        case "TextBox":
                            panelItem.Children.Add(new TextBox
                            {
                                Style = Application.Current.Resources["styleDefaultTextBox"] as Style,
                                Width = 150,
                                Margin = new Thickness(10, 2, 5, 2)
                            });
                            break;
                        default:
                            panelItem.Children.Add(new ComboBox
                            {
                                Width = 150,
                                Height = 21,
                                Margin = new Thickness(10, 2, 5, 2),
                                DisplayMemberPath = "NAME",
                                SelectedValuePath = "VALUE",
                                ItemsSource = SystemCodeData.DicDataView[sControlType],
                                SelectedIndex = -1
                            });
                            break;
                    }
                    panelItem.Children.Add(new ComboBox
                    {
                        Width = 60,
                        Height = 21,
                        Tag = "LogicOp",
                        Margin = new Thickness(1),
                        DisplayMemberPath = "NAME",
                        SelectedValuePath = "VALUE",
                        ItemsSource = SystemCodeData.DicDataView["OPERATOR_LOGICAL"],
                        SelectedIndex = 0
                    });
                    Grid.SetColumn(panelItem, 1);
                    Grid.SetRow(panelItem, iRow);
                    this.gridControls.Children.Add(panelItem);
                    ++iRow;
                }
            }
            catch (Exception ex)
            {
                MessageDialog.ShowException(ex);
            }
        }

        private void WrapPanel_Click(object sender, RoutedEventArgs e)
        {
            Button b = e.OriginalSource as Button;
            switch (b.Name)
            {
                case "bOK":
                    StringBuilder sbAnd = new StringBuilder();
                    StringBuilder sbOr = new StringBuilder();
                    sbAnd.Append("1=1");
                    sbOr.Append("1<>1");
                    foreach (UIElement uie in this.gridControls.Children)
                    {
                        StackPanel sp = uie as StackPanel;
                        if (sp != null)
                        {
                            string sValue = string.Empty;
                            if (sp.Children[1] is CheckBox)
                            {
                                CheckBox cb = sp.Children[1] as CheckBox;
                                sValue = Convert.ToInt32(cb.IsChecked).ToString();
                            }
                            else if (sp.Children[1] is ComboBox)
                            {
                                ComboBox cb = sp.Children[1] as ComboBox;
                                if (cb.SelectedIndex > -1)
                                {
                                    sValue = cb.SelectedValue.ToString();
                                }
                            }
                            else if (sp.Children[1] is TextBox)
                            {
                                TextBox tb = sp.Children[1] as TextBox;
                                if (!string.IsNullOrEmpty(tb.Text))
                                {
                                    sValue = tb.Text;
                                }
                            }
                            string sColumn = sp.Tag as string;
                            string sCompare = (sp.Children[0] as ComboBox).SelectedValue.ToString();
                            string sExpression = string.Empty;
                            if (sCompare == SystemCode.OPERATOR.IsNull ||
                                sCompare == SystemCode.OPERATOR.IsNotNull)
                            {
                                sExpression = string.Format("{0} {1}", sColumn, sCompare);
                            }
                            else if (sCompare == SystemCode.OPERATOR.Between)
                            {
                                string[] asValue = sValue.Split('-');
                                if (asValue.Length != 2)
                                {
                                    continue;
                                }
                                sExpression = string.Format("{0} between '{1}' and '{2}'", sColumn, asValue[0], asValue[1]);
                            }
                            else
                            {
                                if (string.IsNullOrWhiteSpace(sValue))
                                {
                                    continue;
                                }
                                sExpression = string.Format("{0} {1} '{2}'", sColumn, sCompare,
                                    sCompare == SystemCode.OPERATOR.Like ||
                                    sCompare == SystemCode.OPERATOR.NotLike ? "%" + sValue + "%" : sValue);
                            }
                            if ((sp.Children[2] as ComboBox).SelectedValue.ToString() == SystemCode.OPERATOR.And)
                            {
                                sbAnd.Append(string.Format(" and {0}", sExpression));
                            }
                            else
                            {
                                sbOr.Append(string.Format(" or {0}", sExpression));
                            }
                        }
                    }
                    if (sbOr.Length > 5)
                    {
                        sbAnd.Append(" and (" + sbOr.ToString() + ")");
                    }
                    this.U_Where = sbAnd.ToString();
                    this.DialogResult = true;
                    break;
            }
            this.Close();
        }
    }
}