
///////////////////////////////////////////////////////////////////////////////
//文件名称: VmInsuerInfo.cs
//文件描述: 保险商项目信息维护
//创 建 者: xls
//创建日期: 2018/5/24 21:55:24
//版 本 号：1.0.0.0
////////////////////////////////////////////////////////////////////////////////

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

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

        private void Init()
        {
           
        }

        public  void Init(LoadParameter parameter)
        {
            var id = parameter.Parameter as string;
            Id = id;
            //加载数据
            LoadVenderItem(Id);
            LoadProjectRelationItems(Id);
        }
        /// <summary>
        /// 保险商id
        /// </summary>
        public string Id { get; set; }
        #region 属性相关


        private string m_Name;
        /// <summary>
        /// 保险商名称
        /// </summary>
        public string Name
        {
            get { return this.m_Name; }
            set
            {
                this.m_Name = value;
                RaisePropertyChanged(() => this.Name);
            }
        }
        private string m_Url;
        /// <summary>
        /// 保险商网址
        /// </summary>
        public string Url
        {
            get { return this.m_Url; }
            set
            {
                this.m_Url = value;
                RaisePropertyChanged(() => this.Url);
            }
        }


        private ObservableCollection<ProjectRelationShowItem> m_ProjectItems;
        /// <summary>
        /// 关联项目信息
        /// </summary>
        public ObservableCollection<ProjectRelationShowItem> ProjectItems
        {
            get { return this.m_ProjectItems; }
            set
            {
                this.m_ProjectItems = value;
                RaisePropertyChanged(() => this.ProjectItems);
            }
        }
        #endregion

        #region 命令相关

        /// <summary>
        /// 增加保单
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void AddInsuracncePolicy(object parameter)
        {
            try
            {
                WinInsurancePolicyEditor win = new WinInsurancePolicyEditor();
                var f = new InsuerParameter();
                f.Insuer.Id = Id;
                LoadParameter.StartParameter(win, new LoadParameter(f));
                win.Show(this.GetNavigationBar());
            }
            catch (Exception ex)
            {

                MessageTipEx.Show(ex);
            }
        }
        public bool CanAddInsuracncePolicy(object parameter)
        {
            return true;
        }

        /// <summary>
        /// 进入项目信息
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void GoToProject(object parameter)
        {
            try
            {
                ProjectRelationShowItem showItem = parameter as ProjectRelationShowItem;
                var pa = new InsuerParameter();
                pa.Insuer.Id = Id;
                pa.Project.Id = showItem.ProjectId;
                pa.Project.Name = showItem.ProjectLocalName;
                LoadParameter loadParameter = new LoadParameter(pa);
                WinInsuerProjectInfo win = new WinInsuerProjectInfo();
                LoadParameter.StartParameter(win, loadParameter);
                win.Show(this.GetNavigationBar());
            }
            catch (Exception ex)
            {
                MessageTipEx.Show(ex);
            }
        }
        public bool CanGoToProject(object parameter)
        {
            return true;
        }


        /// <summary>
        /// 保存功能
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void SaveCommand(object parameter)
        {
            //MessageTip.Show(this.GetWindow(), "保存");
        }
        public bool CanSaveCommand(object parameter)
        {
            return true;
        }
        /// <summary>
        /// 刷新动画
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void RefreshCommand(object parameter)
        {
            try
            {
                var item = parameter as ProjectRelationShowItem;
                if (item != null)
                {
                    item.IsLoading = true;
                    TaskUtil.StartSTATask((obj) =>
                  {
                      var useItem = obj as ProjectRelationShowItem;
                      if (useItem == null)
                          return;
                      try
                      {
                          BllFactory<ProjectBll>.Instance.ProjectSync(useItem.ProjectId);
                          Thread.Sleep(3000);//立即执行偶尔导致连接取消
                         var projects = BllFactory<InsuerBll>.Instance.GetProjectItems(Id);
                          foreach (var project in projects)
                          {
                              if (project.Id == useItem.ProjectId)
                              {
                                  useItem.AssetCount = (int)project.TotalCount.ToDouble();
                                  useItem.InsurancePolicyCount = (int)project.ContractCount.ToDouble();
                              }

                          }
                      }
                      catch (Exception ex)
                      {
                          MessageTipEx.Show(ex);
                      }

                      finally
                      {

                          useItem.IsLoading = false;
                      }
                  }, item);

                }

            }
            catch (Exception ex)
            {
                MessageTipEx.Show(ex);
            }

        }
        public bool CanRefreshCommand(object parameter)
        {
            return true;
        }
        #endregion

        #region 公开方法
        /// <summary>
        /// 更新指定字段信息
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        public bool SingleUpdateVenderItem(string field)
        {

            InsuerItem item = new InsuerItem();
            item.Id = this.Id;
            SetFieldValue(item, field);
            var result = BllFactory<InsuerBll>.Instance.Update(item);
            if (!result)
            {
                MessageTip.Show(this.GetWindow(), BllFactory<InsuerBll>.Instance.ErrorMessage ?? string.Empty);
            }
            return result;
        }

        private void SetFieldValue(InsuerItem item, string field)
        {
            if (field == "Name")
            {
                item.Name = this.Name;
                return;
            }
            if (field == "Url")
            {
                item.Website = this.Url;
                return;
            }
          
        }
        #endregion

        #region 私有方法
        /// <summary>
        /// 加载厂商信息
        /// </summary>
        /// <param name="venderId">厂商Id</param>
        private void LoadVenderItem(string venderId)
        {
            #region 加载厂商信息
            var venderItem = BllFactory<InsuerBll>.Instance.GetAllItems().FirstOrDefault(c => c.Id == venderId);
            Name = venderItem.Name;
            Url = venderItem.Website;
            #endregion
        }
        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="venderId"></param>
        private void LoadProjectRelationItems(string venderId)
        {
            ProjectItems = new ObservableCollection<ProjectRelationShowItem>();
            var projects = BllFactory<InsuerBll>.Instance.GetProjectItems(venderId);
            foreach (var project in projects)
            {
                var showItem = new ProjectRelationShowItem();
                showItem.ProjectId = project.Id;
                showItem.ProjectLocalName = project.Name;
                showItem.AssetCount = (int)project.TotalCount.ToDouble();
                showItem.InsurancePolicyCount = (int)project.ContractCount.ToDouble();
                ProjectItems.Add(showItem);
            }
        }

        #endregion
    }

    /// <summary>
    /// 项目关联信息
    /// </summary>
    public class ProjectRelationShowItem : BasePropertyChanged
    {

        private string m_ProjectId;
        /// <summary>
        /// 项目ID
        /// </summary>
        public string ProjectId
        {
            get { return this.m_ProjectId; }
            set
            {
                this.m_ProjectId = value;
                RaisePropertyChanged(() => this.ProjectId);
            }
        }

        private string m_ProjectLocalName;
        /// <summary>
        /// 项目本地名称
        /// </summary>
        public string ProjectLocalName
        {
            get { return this.m_ProjectLocalName; }
            set
            {
                this.m_ProjectLocalName = value;
                RaisePropertyChanged(() => this.ProjectLocalName);
            }
        }



        private int m_InsurancePolicyCount;
        /// <summary>
        /// 涉及保单数量
        /// </summary>
        public int InsurancePolicyCount
        {
            get { return this.m_InsurancePolicyCount; }
            set
            {
                this.m_InsurancePolicyCount = value;
                RaisePropertyChanged(() => this.InsurancePolicyCount);
            }
        }

        private int m_AssetCount;
        /// <summary>
        /// 资产数量
        /// </summary>
        public int AssetCount
        {
            get { return this.m_AssetCount; }
            set
            {
                this.m_AssetCount = value;
                RaisePropertyChanged(() => this.AssetCount);
            }
        }


        private bool m_IsLoading;
        /// <summary>
        /// 加载数据
        /// </summary>
        public bool IsLoading
        {
            get { return this.m_IsLoading; }
            set
            {
                this.m_IsLoading = value;
                RaisePropertyChanged(nameof(this.IsLoading));
            }
        }
    }
}
