
///////////////////////////////////////////////////////////////////////////////  
//文件名称: WinInsuerProjectInfo.xaml
//文件描述: 保险项目信息
//创 建 者: xls
//创建日期: 2018/5/24 21:58:36
//版 本 号：1.0.0.0
////////////////////////////////////////////////////////////////////////////////

using System;
using System.Windows;
using System.Windows.Input;
using Com.FirmLib.UI.BllCommon;
using Com.FirmLib.UI.Common;
using Com.FirmLib.UI.Common.Model;
using Com.FirmLib.UI.Insuer.Model;
using FWindSoft.Wpf;
using FWindSoft.Wpf.Controls;
using FWindSoft.Wpf.Selector;

namespace Com.FirmLib.UI.Insuer
{
    /// <summary>
    /// WinInsuerProjectInfo.xaml 的交互逻辑
    /// </summary>
    public partial class WinInsuerProjectInfo : NChildWindow
    {
        private VmInsuerProjectInfo m_Vm;
        public WinInsuerProjectInfo():this(new VmInsuerProjectInfo())
        {
        }
        public WinInsuerProjectInfo(VmInsuerProjectInfo 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)
        {
            if (parameter == null)
            {
                this.Title = "项目名称";
            }
            else
            {
                InsuerParameter currentParameter = parameter.Parameter as InsuerParameter;
                if (parameter != null)
                {
                    this.Title = string.Format("{0}", currentParameter.Project.Name);
                }
            }
             TryCatchWrapper.Handled(() => this.m_Vm.Init(parameter));
        }

       

        #region 界面关联事件
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (this.HasError())
                {
                    //MessageShow.Infomation("请修改错误输入项");
                    return;
                }          
            }
            catch (Exception ex)
            {
                //MessageShow.Show(ex);
            }
        }

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

        void SingleUpdate(object sender, ExecutedRoutedEventArgs e)
        {
            TextBoxEditor editor = e.Parameter as TextBoxEditor;
            if (editor == null)
                return;
            #region 验证信息
            var binding = editor.GetBindingExpression(TextBoxEditor.TextProperty);
            if (binding != null)
            {
                binding.UpdateSource();
            }
            if (editor.HasError())
            {
                editor.IsEditing = true;
                return;
            } 
            #endregion
            string field = string.Empty;
            if (editor.Tag != null)
            {
                field = editor.Tag.ToString();
            }
            bool result = this.m_Vm.SingleUpdateLinkerItem(field);
            if (!result)
            {
                //editor.Focus();
                editor.IsEditing = true;
            }
        }
    }



    #region 界面操作相关
    public class DownLoadTemplateChecker : IEmptyTemplateChecker
    {
        public bool IsEmpty(object item)
        {
            if (item == null)
            {
                return true;
            }

            if (item is AssetShowItem showItem)
            {
                return string.IsNullOrEmpty(showItem.PolicyFile);
            }

            if (item is PolicyShowItem policyItem)
            {
                  return string.IsNullOrEmpty(policyItem.PolicyFile);
            }

            return true;
        }
    }

    public class CopyHandle : ICopy
    {
        public void CopyValue(object baseItem, object targetItem)
        {
            var useBase = baseItem as AssetShowItem;
            var targetBase = targetItem as AssetShowItem;
            if (useBase == null || targetBase == null)
                return;
            targetBase.InsurancePolicyNo=useBase.InsurancePolicyNo;
            targetBase.PolicyFile=useBase.PolicyFile;
            targetBase.EndDate=useBase.EndDate;
        }
    }
    #endregion
}
