
///////////////////////////////////////////////////////////////////////////////
//文件名称: VmPolicySelector.cs
//文件描述: VmPolicySelector.cs
//创 建 者: xls
//创建日期: 2018/9/18 0:07:27
//版 本 号：1.0.0.0
////////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Com.FirmLib.Bll;
using Com.FirmLib.Entity;
using Com.FirmLib.UI.Insuer.Model;
using FWindSoft.DataFramework;
using FWindSoft.MVVM;
using FWindSoft.Wpf;
using FWindSoft.Wpf.Common;

namespace Com.FirmLib.UI.Insuer
{
    public class VmPolicySelector : BaseViewModel
    {
        public VmPolicySelector()
        {
            Init();
        }

        private void Init()
        {

        }
        public void Init(LoadParameter parameter)
        {
            InsuerParameter insuerParameter = parameter.Parameter as InsuerParameter;
            if (insuerParameter == null)
                return;
            //需要厂商id,和项目Id
            LoadPolicyItems(insuerParameter.Project.Id, insuerParameter.Insuer.Id);
            if (insuerParameter != null)
            {
                this.CurrentPolicyItem = this.PolicyItems.FirstOrDefault(c => c.PolicyNo == insuerParameter.Policy.Id);
            }
        }
        #region 属性相关



        private ObservableCollection<PolicyShowItem> m_PolicyItems;
        /// <summary>
        /// 保单信息
        /// </summary>
        public ObservableCollection<PolicyShowItem> PolicyItems
        {
            get { return this.m_PolicyItems; }
            set
            {
                this.m_PolicyItems = value;
                RaisePropertyChanged(nameof(this.PolicyItems));
            }
        }


        private PolicyShowItem m_CurrentPolicyItem;
        /// <summary>
        /// 当前选中保单
        /// </summary>
        public PolicyShowItem CurrentPolicyItem
        {
            get { return this.m_CurrentPolicyItem; }
            set
            {
                this.m_CurrentPolicyItem = value;
                RaisePropertyChanged(nameof(this.CurrentPolicyItem));
            }
        }
        #endregion

        #region 命令相关

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void EnsureCommand(object parameter)
        {

            try
            {
                Window window = parameter as Window;
                if (window != null)
                {
                    window.SetRealResult(m_CurrentPolicyItem.PolicyNo);
                    window.DialogResult = true;

                }
            }
            catch (Exception ex)
            {
                MessageTipEx.Show(ex);
            }
        }
        public bool CanEnsureCommand(object parameter)
        {
            return m_CurrentPolicyItem!=null;
        }
        /// <summary>
        /// 单击列表框按钮
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void SelectCommand(object parameter)
        {

            try
            {
                PolicyShowItem item = parameter as PolicyShowItem;
                if (item != null)
                {
                    this.CurrentPolicyItem = item;
                }
            }
            catch (Exception ex)
            {
                MessageTipEx.Show(ex);
            }
        }
        public bool CanSelectCommand(object parameter)
        {
            return true;
        }
        #endregion

        #region 私有方法
        private void LoadPolicyItems(string projectId,string insuerId)
        {
            PolicyItems = new ObservableCollection<PolicyShowItem>();
            var policyItems = BllFactory<InsuerBll>.Instance.GetAllPolicyItems(insuerId, projectId);
            foreach (var dataItem in policyItems)
            {
                var item = ConvertPolicyShowItem(dataItem);
                item.InsuerId = insuerId;
                item.ProjectId = projectId;
                PolicyItems.Add(item);
            }
        }
        private PolicyShowItem ConvertPolicyShowItem(InsuerContractStatisticsItem dataItem)
        {
            var showItem = new PolicyShowItem();
            showItem.PolicyNo = dataItem.InsuranceNo;
            //showItem.RefAssetCount = (int)dataItem.PropertyCount.ToDouble();
            showItem.EndDate = dataItem.ExpireDate;
            return showItem;
        }
        #endregion
    }
}
