﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace SGLibrary.Framework.Criterion
{
    /// <summary>
    /// 查询条件基类
    /// </summary>
    [Browsable(false)]
    [DefaultEvent("WhereChanged")]
    [System.ComponentModel.ToolboxItem(false)]
    public partial class WhereCtlBase : UserControl
    {
        public const Int32 cComboBoxWidth = 80;

        public WhereCtlBase()
        {
            InitializeComponent();
        }

        public virtual void Clear()
        {
            CompareCb.SelectedIndex = -1;
        }

        [Browsable(true), Description("显示的文本"),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]   //是否自动生成设计时相关代码（设计时序列化）
        public override String Text
        {
            get
            {
                return label1.Text;
            }
            set
            {
                label1.Text = value;
            }
        }

        [Description("文本预留的空间")]
        public Int32 TextSectionLength
        {
            get
            {
                return label1.Width;
            }
            set
            {
                label1.Width = value;
                _Resize();
            }
        }

        [Description("文本对齐方式")]
        public WhereContentAlignment TextAlign
        {
            get
            {
                return (WhereContentAlignment)label1.TextAlign;
            }
            set
            {
                label1.TextAlign = (ContentAlignment)value;
            }
        }

        [Description("绑定的属性")]
        public String BindProperty{ get; set; }

        private void WhereControlBase_Resize(object sender, EventArgs e)
        {
            _Resize();
        }

        protected virtual void _Resize()
        {
            if (CompareCb == null)
                return;

            CompareCb.Width = cComboBoxWidth;
            CompareCb.Left = label1.Width + 5;
            CompareCb.Top = (Height - CompareCb.Height) / 2;
        }

        private void WhereControlBase_Load(object sender, EventArgs e)
        {
            if (this.DesignMode)
                return;

            this._Resize();
        }

        /// <summary>
        /// 查询条件改变
        /// </summary>
        public event EventHandler<WhereNodeEventArgs> WhereChanged;

        protected void OnWhereChanged(IWhereNode whereNode)
        {
            if (WhereChanged != null)
                WhereChanged(this, new WhereNodeEventArgs { Value = whereNode });
        }
    }
}
