
///////////////////////////////////////////////////////////////////////////////
//文件名称: VmSellerBaseInfoEditor.cs
//文件描述: 经销商信息编辑
//创 建 者: xls
//创建日期: 2018/5/24 21:22:10
//版 本 号：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 Com.FirmLib.UI.Seller.Model;
using FWindSoft.DataFramework;
using FWindSoft.MVVM;
using FWindSoft.Wpf;

namespace Com.FirmLib.UI.Seller
{
    public class VmSellerBaseInfo : BaseViewModel
    {
        public VmSellerBaseInfo()
        {
            Init();
        }

        private void Init()
        {
            #region 测试数据注释
            //Sellers = new ObservableCollection<SellerShowItem>();
            //for (int i = 0; i < 10; i++)
            //{
            //    var tempItem = new SellerShowItem() { 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;
            //    Sellers.Add(tempItem);
            //} 
            #endregion
            //LoadItems();
        }
        public void Init(LoadParameter parameter)
        {
            //todo 加载数据

            LoadItems();
        }
        #region 属性相关


        private ObservableCollection<SellerShowItem> m_Sellers;
        /// <summary>
        /// 供应商集合
        /// </summary>
        public ObservableCollection<SellerShowItem> Sellers
        {
            get { return this.m_Sellers; }
            set
            {
                this.m_Sellers = value;
                RaisePropertyChanged(() => this.Sellers);
            }
        }

        #endregion

        #region 命令相关

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

            try
            {
                WinSellerEditor win = new WinSellerEditor();
                win.Owner = this.GetWindow();
                if (win.ShowDialog() == true)
                {
                    if (win.IsManage)
                    {
                        WinSellerInfo winSellerInfo = new WinSellerInfo();
                        LoadParameter.StartParameter(winSellerInfo, new LoadParameter(win.SellerId));
                        winSellerInfo.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
            {
                SellerShowItem item = parameter as SellerShowItem;
                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
            {
                SellerShowItem item = parameter as SellerShowItem;
                if (item == null)
                    return;
                WinSellerInfo win = new WinSellerInfo();
                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()
        {
            Sellers = new ObservableCollection<SellerShowItem>();
            var items = BllFactory<SellerBll>.Instance.GetAllItems();
            foreach (var sellerItem in items)
            {
                var tempItem = new SellerShowItem();
                tempItem.Id = sellerItem.Id;
                tempItem.Name = sellerItem.Name;
                tempItem.Url = sellerItem.Website;
                tempItem.BrandNames = sellerItem.Brands.Select(b => b).ToList();
                tempItem.FamilyNames = sellerItem.Familys.Select(f => EquipmentFamilyManager.GetFamilyName(f)).ToList();
                tempItem.RefProjectCount = sellerItem.ProjectCount;
                Sellers.Add(tempItem);
            }
        }

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