
///////////////////////////////////////////////////////////////////////////////
//文件名称: VmMultiProductTypeSelecter.cs
//文件描述: VmMultiProductTypeSelecter.cs
//创 建 者: xls
//创建日期: 2018/9/12 22:29:02
//版 本 号：1.0.0.0
////////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Com.FirmLib.Bll;
using Com.FirmLib.Entity;
using Com.FirmLib.UI.Common;
using FWindSoft.Data;
using FWindSoft.DataFramework;
using FWindSoft.MVVM;
using FWindSoft.Wpf;

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

        private void Init()
        {

        }
        public void Init(LoadParameter parameter)
        {
            Id = parameter.Parameter as string;
            ExistProductTypeIds = GetProductIds(Id) ?? new List<string>();
            Root = new TreeNavigation<ShowDataItem>();
            Root.CurrentItemChanged = ChangeSelectedItem;
            Root.Items.AddRange(GetBrands());
            Root.CurrentItem = Root.Items.FirstOrDefault();
        }
        /// <summary>
        /// 关联厂商Id[可能添加功能需要]
        /// </summary>
        public string Id { get; set; }
        #region 属性相关
        /// <summary>
        /// 存在的产品型号Id集合
        /// </summary>
        public List<string> ExistProductTypeIds { get; set; }

        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 SelectedCommand(object parameter)
        {

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

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

            try
            {
                AddTypeList(parameter as Window);
            }
            catch (Exception ex)
            {
                MessageTipEx.Show(ex);
            }
            
        }
        public bool CanAddCommand(object parameter)
        {
            return true;
        }

        #endregion

        #region 私有方法
        /// <summary>
        /// 增加类型信息
        /// </summary>
        /// <param name="window"></param>
        private void AddTypeList(Window window)
        {
            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&&!ExistProductTypeIds.Contains(s.RefItem.Id)).Select(s => s.RefItem.Id));
                #endregion

                #region 增加维护类型信息
                if (productTypeIds.Count == 0)
                {
                    MessageTip.Show(this.GetWindow(), "没有需要增加的产品类型");
                    return;
                }
                var flag = BllFactory<ProductDirectoryBll>.Instance.Add(Id, new List<string>(productTypeIds));
                if (flag)
                {
                    ExistProductTypeIds.AddRange(productTypeIds);
                    MessageTip.Show(this.GetWindow(), "添加成功");
                    if (window != null)
                    {
                        //window.DialogResult = true;
                    }
                    
                }
                else
                {
                    MessageTip.Show(this.GetWindow(), BllFactory<ProductDirectoryBll>.Instance.ErrorMessage, "失败");
                } 
                #endregion
            }
            catch (Exception ex)
            {

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

        #region 关联类型相关
        private void ChangeSelectedItem(TreeNavigationItem<ShowDataItem> item)
        {
            if (item == null)
                return;
            if (item.Level > 2)
                return;
            item.Nodes.Clear();
            if (item.Level == 1)
            {
                item.Nodes.AddRange(GetProducts(item.RefItem.Id));
            }
            else if (item.Level == 2)
            {
                //可以将选中信息处理掉
                item.Nodes.AddRange(GetProductTypes(item.RefItem.Id));
                item.UpdateCheckedState();
            }
            item.CurrentItem = item.Nodes.FirstOrDefault();
        }
        /// <summary>
        /// 当前型号数据
        /// </summary>
        private List<ProductTypeDetailItem> DataSource { get; set; }     
        /// <summary>
        /// 获取品牌信息
        /// </summary>
        /// <returns></returns>
        private List<TreeNavigationItem<ShowDataItem>> GetBrands()
        {
            List<TreeNavigationItem<ShowDataItem>> results = new List<TreeNavigationItem<ShowDataItem>>();
            #region 数据查询
            if (DataSource == null)
            {
                DataSource =ProductTypeManager.ConverToList(BllFactory<GeneralBll>.Instance.GetProductTypeItems());
            }
            #endregion
            var brands = ProductTypeManager.GetBrands(new ProdcutTypeCondition(DataSource));
            brands.ForEach(b => results.Add(new TreeNavigationItem<ShowDataItem>() { RefItem = b }));          
            return results;
        }
        /// <summary>
        /// 获取产品信息
        /// </summary>
        /// <param name="brandId"></param>
        /// <returns></returns>
        private List<TreeNavigationItem<ShowDataItem>> GetProducts(string brandId)
        {
            List<TreeNavigationItem<ShowDataItem>> results = new List<TreeNavigationItem<ShowDataItem>>();
            var products = ProductTypeManager.GetProducts(new ProdcutTypeCondition(DataSource) { BrandId = brandId });
            products.ForEach(b => results.Add(new TreeNavigationItem<ShowDataItem>() { RefItem = b }));

            return results;
        }

        /// <summary>
        /// 获取产品信息
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        private List<TreeNavigationItem<ShowDataItem>> GetProductTypes(string productId)
        {
            List<TreeNavigationItem<ShowDataItem>> results = new List<TreeNavigationItem<ShowDataItem>>();
            var productTypes = ProductTypeManager.GetProductTypes(new ProdcutTypeCondition(DataSource) { ProductId = productId });
          
            #region 转换数据
            List<string> useList = ExistProductTypeIds ?? new List<string>();
            foreach (var productType in productTypes)
            {
                var useData = new TreeNavigationItem<ShowDataItem>()
                {
                    RefItem = productType
                };
                useData.IsChecked = useList.Any(id => id == useData.RefItem?.Id);
                //这两个暂时一样，因为分页取消选中
                useData.IsEnabled = !(useData.IsChecked==true);

                results.Add(useData);
            }
            #endregion

            return results;
        }

        /// <summary>
        /// 获取厂商关联的型号信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<string> GetProductIds(string venderId)
        {
            var types = BllFactory<ProductDirectoryBll>.Instance.GetAllItems(venderId);
            var ids = types.Select(t => t.ProductTypeId).ToList();
            return ids;
        }
        #endregion
    }
}
