
///////////////////////////////////////////////////////////////////////////////
//文件名称: VmUpdateProductType.cs
//文件描述: VmUpdateProductType.cs
//创 建 者: xls
//创建日期: 2018/9/18 16:59:34
//版 本 号：1.0.0.0
////////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Com.FirmLib.Bll;
using Com.FirmLib.Entity;
using Com.FirmLib.UI.Common;
using Com.FirmLib.UI.Maintainer.Mode;
using FWindSoft.Data;
using FWindSoft.DataFramework;
using FWindSoft.MVVM;
using FWindSoft.Wpf;

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

        private void Init()
        {

        }
        public void Init(LoadParameter parameter)
        {
            EditParameter tempParameter = parameter.Parameter as EditParameter;
            Id = tempParameter.VenderId;
            BrandId = tempParameter.BrandId;
            IsEdit = tempParameter.IsEdit;
            LoadProductTypeItems();
        }
        #region 属性相关
        /// <summary>
        /// 关联厂商Id[可能添加功能需要]
        /// </summary>
        public string Id { get; set; }
        /// <summary>
        /// 品牌Id信息
        /// </summary>
        public string BrandId { get; set; }
        private bool m_IsEdit;
        /// <summary>
        /// 是否是编辑
        /// </summary>
        public bool IsEdit
        {
            get { return this.m_IsEdit; }
            set
            {
                this.m_IsEdit = value;
                RaisePropertyChanged(nameof(this.IsEdit));
            }
        }

        private TreeNavigation<ShowDataItem> m_Root;
        /// <summary>
        /// 根元素
        /// </summary>
        public TreeNavigation<ShowDataItem> Root
        {
            get { return this.m_Root; }
            set
            {
                this.m_Root = value;
                RaisePropertyChanged(nameof(this.Root));
            }
        }

        #endregion

        #region 命令相关
        /// <summary>
        /// 更新关联产品类型信息
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void UpdateProductTypeCommand(object parameter)
        {
            try
            {
                if (Root == null)
                    return;
                #region 获取需要删除的类型id
                List<string> productTypeIds = new List<string>();
                var showItems = Root?.GetLastLevelData();
                productTypeIds.AddRange(showItems.Where(s => s.IsChecked != true).Select(s => s.RefItem.Id));
                if (!productTypeIds.Any())
                {
                    MessageTip.Show(this.GetWindow(), "没有需要删除的型号");
                    return;
                }
                #endregion
                //删除相关类型
                #region 删除产品类型
                var result = MessageTip.Show(this.GetWindow(), "是否要删除", "提示", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                {
                    var flag = BllFactory<ProductDirectoryBll>.Instance.Delete(Id, new List<string>(productTypeIds));
                    if (flag)
                    {
                        //删除完毕关闭窗口
                        #region 关闭窗口
                        Window window = parameter as Window;
                        if (window != null)
                            window.DialogResult = true;
                        #endregion
                    }
                    else
                    {
                        MessageTip.Show(this.GetWindow(), BllFactory<ProductDirectoryBll>.Instance.ErrorMessage, "失败");
                    }

                } 
                #endregion
            }
            catch (Exception ex)
            {
                MessageTipEx.Show(this.GetWindow(), ex);
            }

        }
        public bool CanUpdateProductTypeCommand(object parameter)
        {
            return IsEdit;
        }

        #endregion

        #region 加载数据
        private void LoadProductTypeItems()
        {
            Root = new TreeNavigation<ShowDataItem>();
            Root.CurrentItemChanged = ChangeSelectedItem;
            Root.Items.AddRange(LoadFamilys(Id,new ProdcutTypeCondition() { BrandId = BrandId}));
            Root.CurrentItem = Root.Items.FirstOrDefault();
        }
        #endregion

        #region 加载类型相关
        private void ChangeSelectedItem(TreeNavigationItem<ShowDataItem> item)
        {
            if (item == null)
                return;
            if (item.Level > 3)
                return;
            if (item.Nodes.Any())
                return;
            item.Nodes.Clear();
            if (item.Level == 1)
            {
                item.Nodes.AddRange(GetBrands(new ProdcutTypeCondition(Id) { BrandId = BrandId }));
            }
            else if (item.Level == 2)
            {
                item.Nodes.AddRange(GetProducts(new ProdcutTypeCondition(Id) { BrandId = BrandId }));
            }
            else if (item.Level == 3)
            {
                item.Nodes.AddRange(GetProductTypes(new ProdcutTypeCondition(Id) {ProductId=item.RefItem.Id}));
                item.UpdateCheckedState();
            }
            item.CurrentItem = item.Nodes.FirstOrDefault();
        }
        /// <summary>
        /// 产品信息数据源
        /// </summary>
        private List<ProductTypeDetailItem> DataSource { get; set; }
        /// <summary>
        /// 查询设备族信息
        /// </summary>
        ///  <param name="venderId"></param>
        /// <param name="conditon"></param>
        /// <returns></returns>
        private List<TreeNavigationItem<ShowDataItem>> LoadFamilys(string venderId,ProdcutTypeCondition conditon)
        {
            List<TreeNavigationItem<ShowDataItem>> results = new List<TreeNavigationItem<ShowDataItem>>();
            var source = BllFactory<ProductDirectoryBll>.Instance.GetAllItems(venderId).Where(p => p.BrandId == conditon.BrandId).ToList();
            DataSource = source ?? new List<ProductTypeDetailItem>();
            var familys = ProductTypeManager.GetFamilys(new ProdcutTypeCondition(source));
            familys.ForEach(f => results.Add(new TreeNavigationItem<ShowDataItem>() { RefItem = f }));
            return results;
        }
        /// <summary>
        /// 查询品牌信息
        /// </summary>
        /// <param name="conditon"></param>
        /// <returns></returns>
        private List<TreeNavigationItem<ShowDataItem>> GetBrands(ProdcutTypeCondition conditon)
        {
            List<TreeNavigationItem<ShowDataItem>> results = new List<TreeNavigationItem<ShowDataItem>>();
            if (DataSource == null)
                return results;
            var showItems = ProductTypeManager.GetBrands(new ProdcutTypeCondition(DataSource) { BrandId = conditon.BrandId });
            showItems.ForEach(r => results.Add(new TreeNavigationItem<ShowDataItem>() { RefItem = r }));
            return results;
        }
        private List<TreeNavigationItem<ShowDataItem>> GetProducts(ProdcutTypeCondition conditon)
        {
            List<TreeNavigationItem<ShowDataItem>> results = new List<TreeNavigationItem<ShowDataItem>>();
            if (DataSource == null)
                return results;
            var products = ProductTypeManager.GetProducts(new ProdcutTypeCondition(DataSource) {BrandId = conditon.BrandId});
            products.ForEach(p => results.Add(new TreeNavigationItem<ShowDataItem>() { RefItem = p }));
            return results;
        }
        private List<TreeNavigationItem<ShowDataItem>> GetProductTypes(ProdcutTypeCondition conditon)
        {
            List<TreeNavigationItem<ShowDataItem>> results = new List<TreeNavigationItem<ShowDataItem>>();
            if (DataSource == null)
                return results;
            var products = ProductTypeManager.GetProductTypes(new ProdcutTypeCondition(DataSource) { ProductId = conditon.ProductId });
            products.ForEach(p => results.Add(new TreeNavigationItem<ShowDataItem>() { RefItem = p,IsChecked=true }));   
            return results;
        }
        #endregion
    }
}
