
///////////////////////////////////////////////////////////////////////////////
//文件名称: VmManufactureBaseInfoEditor.cs
//文件描述: 生产厂商基本信息维护
//创 建 者: xls
//创建日期: 2018/5/23 23:24:59
//版 本 号：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.Manufacturer
{
    public class VmManufactureBaseInfo : BaseViewModel
    {
        public VmManufactureBaseInfo()
        {
            Init();
        }

        private void Init()
        {
        }

        public void Init(LoadParameter parameter)
        {
            LoadItems();
        }

        #region 属性相关


        private ObservableCollection<ManufacturerShowItem> m_Manufacturers;

        /// <summary>
        /// 生产厂家明细
        /// </summary>
        public ObservableCollection<ManufacturerShowItem> Manufacturers
        {
            get { return this.m_Manufacturers; }
            set
            {
                this.m_Manufacturers = value;
                RaisePropertyChanged(() => this.Manufacturers);
            }
        }

        #endregion

        #region 命令相关

        /// <summary>
        /// 添加生产厂家
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void AddCommand(object parameter)
        {

            try
            {
                WinManufacturerEditor win = new WinManufacturerEditor();
                win.Owner = this.GetWindow();
                if (win.ShowDialog() == true)
                {
                    if (win.IsManage)
                    {
                        WinManufactureInfo winInfo = new WinManufactureInfo();
                        LoadParameter.StartParameter(winInfo, new LoadParameter(win.ManufacturerId));
                        winInfo.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 ManufacturerShowItem;
                if (item == null)
                    return;
                var result = MessageTip.Show(this.GetWindow(), "是否要删除", "提示", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                {
                    DeleteItem(item.ManufactureId);
                    //todo 刷新窗体
                    LoadItems();
                    // Manufacturers.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 manufacturer = parameter as ManufacturerShowItem;
                if (manufacturer == null)
                    return;
                WinManufactureInfo win = new WinManufactureInfo();
                LoadParameter.StartParameter(win, new LoadParameter(manufacturer.ManufactureId));
                win.Show(this.GetNavigationBar());
            }
            catch (Exception ex)
            {
                MessageTipEx.Show(ex);
            }
        }

        public bool CanManageCommand(object parameter)
        {
            return true;
        }

        #endregion

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadItems()
        {
            Manufacturers = new ObservableCollection<ManufacturerShowItem>();
            var items = BllFactory<ManufacturerBll>.Instance.GetAllItems();
            foreach (var manufacturerItem in items)
            {
                var tempItem = new ManufacturerShowItem();
                tempItem.ManufactureId = manufacturerItem.Id;
                tempItem.ManufacturerName = manufacturerItem.Name;
                tempItem.BrandNames = manufacturerItem.Brands.Select(b => b.Name).ToList();
                tempItem.FamilyNames = manufacturerItem.Familys
                    .Select(f => EquipmentFamilyManager.GetFamilyName(f.Code)).ToList();
                Manufacturers.Add(tempItem);
            }
        }

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

            return result;
        }
    }
}
