
///////////////////////////////////////////////////////////////////////////////
//文件名称: VmInsuerBaseInfo.cs
//文件描述: 保险商基本信息
//创 建 者: xls
//创建日期: 2018/5/24 21:41:29
//版 本 号：1.0.0.0
////////////////////////////////////////////////////////////////////////////////

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

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

        private void Init()
        {
            #region 测试数据
            //Insuers = new ObservableCollection<InsuerShowItem>();
            //for (int i = 0; i < 10; i++)
            //{
            //    var tempItem = new InsuerShowItem() { Name = "保险商" + (i + 1) };
            //    tempItem.Id = (i + 1).ToString();
            //    tempItem.Url = "baidu.com";
            //    tempItem.RefProjectCount = 1;
            //    Insuers.Add(tempItem);
            //} 
            #endregion
            //LoadItems();
        }
        public void Init(LoadParameter parameter)
        {
            LoadItems();

        }
        #region 属性相关



        private ObservableCollection<InsuerShowItem> m_Insuers;
        /// <summary>
        /// 保险商信息
        /// </summary>
        public ObservableCollection<InsuerShowItem> Insuers
        {
            get { return this.m_Insuers; }
            set
            {
                this.m_Insuers = value;
                RaisePropertyChanged(() => this.Insuers);
            }
        }
        #endregion

        #region 命令相关
        /// <summary>
        /// 添加保险商
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void AddCommand(object parameter)
        {
            try
            {
                WinInsuerEditor win = new WinInsuerEditor();
                win.Owner = this.GetWindow();
                if (win.ShowDialog() == true)
                {
                    if (win.IsManage)
                    {
                        WinInsuerInfo winInsuerInfo = new WinInsuerInfo();
                        LoadParameter.StartParameter(winInsuerInfo, new LoadParameter(win.NewId));
                        winInsuerInfo.Show(this.GetNavigationBar());
                    }
                    else
                    {
                        //todo 刷新界面
                        LoadItems();
                    }
                }
            }
            catch (Exception ex)
            {

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

        /// <summary>
        /// 删除生产厂家
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void DeleteCommand(object parameter)
        {
            try
            {
                var item = parameter as InsuerShowItem;
                if (item == null)
                    return;
                var result = MessageTip.Show(this.GetWindow(), "是否要删除", "提示", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                {
                    //todo 网络api处理
                    DeleteItem(item.Id);
                    //todo 刷新窗体
                    LoadItems();
                    //todo 删除操作
                    //Sellers.Remove(item);
                }
            }
            catch (Exception ex)
            {
                MessageTipEx.Show(ex);
            }
        }
        public bool CanDeleteCommand(object parameter)
        {
            return true;
        }

        /// <summary>
        /// 管理命令
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void ManageCommand(object parameter)
        {
            try
            {
                var item = parameter as InsuerShowItem;
                WinInsuerInfo win = new WinInsuerInfo();
                LoadParameter.StartParameter(win, new LoadParameter(item.Id));
                win.Show(this.GetNavigationBar());
            }
            catch (Exception ex)
            {
                MessageTipEx.Show(ex);
            }
        }
        public bool CanManageCommand(object parameter)
        {
            return true;
        }
        #endregion

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadItems()
        {
            Insuers = new ObservableCollection<InsuerShowItem>();
            var items = BllFactory<InsuerBll>.Instance.GetAllItems();
            foreach (var sellerItem in items)
            {
                var tempItem = new InsuerShowItem();
                tempItem.Id = sellerItem.Id;
                tempItem.Name = sellerItem.Name;
                tempItem.Url = sellerItem.Website;
                //tempItem.BrandNames = maintainerItem..Select(b => b.Name).ToList();
                //tempItem.FamilyNames = maintainerItem.Familys.Select(f => f.Name).ToList();
                tempItem.RefProjectCount = sellerItem.ProjectCount;
                Insuers.Add(tempItem);
            }
        }

        /// <summary>
        /// 返回删除厂商信信息函数
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private bool DeleteItem(string id)
        {
            var result = BllFactory<InsuerBll>.Instance.Delete(id);
            if (!result)
            {
                MessageTip.Show(this.GetWindow(), BllFactory<InsuerBll>.Instance.ErrorMessage ?? string.Empty);
            }
            return result;
        }
    }
}
