
///////////////////////////////////////////////////////////////////////////////
//文件名称: VmMaintainerBaseInfo.cs
//文件描述: 维修商信息列表
//创 建 者: xls
//创建日期: 2018/5/24 22:04:55
//版 本 号：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.BllCommon;
using FWindSoft.DataFramework;
using FWindSoft.MVVM;
using FWindSoft.Wpf;

namespace Com.FirmLib.UI.Maintainer
{
    public class VmMaintainerBaseInfo : BaseViewModel
    {
        public VmMaintainerBaseInfo()
        {
            Init();
        }

        private void Init()
        {
            #region 测试数据
            //Manitainers = new ObservableCollection<MaintainerShowItem>();
            //for (int i = 0; i < 10; i++)
            //{
            //    var tempItem = new MaintainerShowItem() { Name = "维修商" + (i + 1) };
            //    tempItem.Id = (i + 1).ToString();
            //    tempItem.Url = "baidu.com";
            //    tempItem.BrandNames = new List<string>();
            //    tempItem.BrandNames.Add("品牌1");
            //    tempItem.BrandNames.Add("品牌2");
            //    tempItem.FamilyNames = new List<string>();
            //    tempItem.FamilyNames.Add("设备族1");
            //    tempItem.FamilyNames.Add("设备族2");
            //    tempItem.RefProjectCount = 39;
            //    Manitainers.Add(tempItem);
            //} 
            #endregion
            //LoadItems();
        }
        public void Init(LoadParameter parameter)
        {
            //todo 加载数据
            LoadItems();

        }
        #region 属性相关


        private ObservableCollection<MaintainerShowItem> m_Manitainers;
        /// <summary>
        /// 维修商集合
        /// </summary>
        public ObservableCollection<MaintainerShowItem> Manitainers
        {
            get { return this.m_Manitainers; }
            set
            {
                this.m_Manitainers = value;
                RaisePropertyChanged(() => this.Manitainers);
            }
        }

        #endregion

        #region 命令相关

        /// <summary>
        /// 增加维修商
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void AddCommand(object parameter)
        {

            try
            {
                WinMaintainerEditor win = new WinMaintainerEditor();
                win.Owner = this.GetWindow();
                if (win.ShowDialog() == true)
                {
                    if (win.IsManage)
                    {
                        WinMaintainerInfo winMaintrainerInfo = new WinMaintainerInfo();
                        LoadParameter.StartParameter(winMaintrainerInfo, new LoadParameter(win.NewId));
                        winMaintrainerInfo.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
            {
                MaintainerShowItem item = parameter as MaintainerShowItem;
                if (item == null)
                    return;
                var result = MessageTip.Show(this.GetWindow(), "是否要删除", "提示", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                {
                    //todo 网络api处理
                    DeleteItem(item.Id);
                    //todo 刷新窗体
                    LoadItems();
                    // Manitainers.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
            {
                MaintainerShowItem item = parameter as MaintainerShowItem;
                if (item == null)
                    return;
                WinMaintainerInfo win = new WinMaintainerInfo();
                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()
        {
            Manitainers = new ObservableCollection<MaintainerShowItem>();
            var items = BllFactory<MaintainerBll>.Instance.GetAllItems();
            foreach (var maintainerItem in items)
            {
                var tempItem = new MaintainerShowItem();
                tempItem.Id = maintainerItem.Id;
                tempItem.Name = maintainerItem.Name;
                tempItem.Url = maintainerItem.Website;
                tempItem.BrandNames = maintainerItem.Brands.Select(b => b).ToList();
                tempItem.FamilyNames = maintainerItem.Familys.Select(f => EquipmentFamilyManager.GetFamilyName(f)).ToList();
                tempItem.RefProjectCount = maintainerItem.ProjectCount;
                Manitainers.Add(tempItem);
            }
        }

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