
///////////////////////////////////////////////////////////////////////////////  
//文件名称: WinSeller.xaml
//文件描述: 供应商信息
//创 建 者: xls
//创建日期: 2018/5/24 21:14:59
//版 本 号：1.0.0.0
////////////////////////////////////////////////////////////////////////////////

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Com.FirmLib.UI.BllCommon;
using Com.FirmLib.UI.Common;
using FWindSoft.Data;
using FWindSoft.Wpf;
using FWindSoft.Wpf.Controls;

namespace Com.FirmLib.UI.Seller
{
    /// <summary>
    /// WinSeller.xaml 的交互逻辑
    /// </summary>
    public partial class WinSellerInfo : NChildWindow
    {
        private VmSellerInfo m_Vm;
        public WinSellerInfo():this(new VmSellerInfo())
        {
          
        }
        public WinSellerInfo(VmSellerInfo vm)
        {
            InitializeComponent();
            EveryTimeLoad = true;
            this.m_Vm = vm;
            this.m_Vm.SetRefView(this);
            this.DataContext = this.m_Vm;
            this.CommandBindings.Add(new CommandBinding(EditCommands.SingleUpdate, SingleUpdate));
        }
        protected override void LoadData(LoadParameter parameter)
        {
            //初始化列表数据
           TryCatchWrapper.Handled(() => this.m_Vm.Init(parameter));
            this.Title = this.m_Vm.Name;
        }
        #region 界面关联事件
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.HasError())
                {
                    //MessageShow.Infomation("请修改错误输入项");
                    return;
                }
                //if (Execute())
                //{
                //    MessageShow.Infomation("");
                //    DialogResult = true;
                //}
            }
            catch (Exception ex)
            {
                //MessageShow.Show(ex);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
        #endregion

        #region 命令绑定相关
        void SingleUpdate(object sender, ExecutedRoutedEventArgs e)
        {
            TextBoxEditor editor = e.Parameter as TextBoxEditor;
            if (editor == null)
                return;
            string field = string.Empty;
            if (editor.Tag != null)
            {
                field = editor.Tag.ToString();
            }
            bool result = this.m_Vm.SingleUpdateVenderItem(field);
            if (!result)
            {
                //editor.Focus();
                editor.IsEditing = true;
            }
        }
        #endregion

        #region 动态更新列
        private void ListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            var list = sender as ListBox;
            if (list == null)
            {
                return;
            }
               
            var current = (list.SelectedItem as TreeNavigationItem<BasePropertyChanged>)?.RefItem as ShowDataItem;
            if (current == null)
            {
                return;
            }
               
            CreateColumns(current.FamilyCode);
        }
        public void CreateColumns(string code)
        {
            if (string.IsNullOrEmpty(code))
            {
                return;
            }
            for (int i = ProductTypeGrid.Columns.Count - 1; i >1; i--)
            {
                ProductTypeGrid.Columns.RemoveAt(i);
            }
            var definitions = ProductTypeManager.GetInfoPointDefinitions(code);
            definitions.ForEach(c => c.IsReadOnly = false);
            var columns = ProductTypeGrid.CreateColumns(definitions, "RefItem.Parameters", InfoPointTemplateParse.CreateTemplateManager());
            columns.ForEach(c =>
            {
                //DataGridDragCopyOptions.SetCanColumnDragCopy(c, true);
                c.Width = c.Header.ToString().Length * 16+40;
                c.IsReadOnly = true;
                c.MinWidth = 80;
            });
        }
        #endregion

       
    }
}
