
///////////////////////////////////////////////////////////////////////////////
//文件名称: VmProductEditor.cs
//文件描述: 产品编辑
//创 建 者: xls
//创建日期: 2018/5/23 23:27:03
//版 本 号：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.Entity;
using Com.FirmLib.UI.BllCommon;
using Com.FirmLib.UI.Manufacturer.Model;
using FWindSoft;
using FWindSoft.DataFramework;
using FWindSoft.MVVM;
using FWindSoft.Wpf;

namespace Com.FirmLib.UI.Manufacturer
{
    public class VmProductEditor : BaseViewModel
    {
        public VmProductEditor()
        {
            Init();
        }

        private void Init()
        {

        }
        public void Init(LoadParameter parameter)
        {
            ManufactureParameter param = parameter.Parameter as ManufactureParameter;
            if (param == null)
                return;
            VenderId = param.Manufacture.Id;
            //todo 根据参数加载项目信息
            #region 加载品牌信息
            if (string.IsNullOrEmpty(param.Branch.Id))
            {
                //todo 根据厂家加载combox列表信息
                //指定厂商Id
                LoadBrandItems(param.Manufacture.Id);
            }
            else
            {
                Brands = new List<TComboxItem>() { new TComboxItem(GetBrandName(param.Manufacture.Id, param.Branch.Id), param.Branch.Id) };
                this.CurrentBrandId = param.Branch.Id;
            }

            if (Brands.Any())
            {
                CurrentBrandId = Brands.FirstOrDefault().Value.ToString();
            }
            #endregion

            #region 加载设备信息
            LoadFamilyItems();
            if (Families.Any())
            {
                CurrentFamily = Families.FirstOrDefault().Value.ToString();
            }
          
            #endregion
        }
        #region 属性相关
        /// <summary>
        /// 关联生产厂家Id
        /// </summary>
        public string VenderId { get;private set; }
        /// <summary>
        /// 产品Id
        /// </summary>
        public string ProductId { get; set; }
        /// <summary>
        /// 管理类型
        /// </summary>
        public bool ManageType { get; set; }



        private string m_CurrentBrandId;
        /// <summary>
        /// 当前品牌Id
        /// </summary>
        public string CurrentBrandId
        {
            get { return this.m_CurrentBrandId; }
            set
            {
                this.m_CurrentBrandId = value;
                RaisePropertyChanged(() => this.CurrentBrandId);
            }
        }


        private List<TComboxItem> m_Brands;

        /// <summary>
        /// 品牌列表
        /// </summary>
        public List<TComboxItem> Brands
        {
            get { return this.m_Brands; }
            set
            {
                this.m_Brands = value;
                RaisePropertyChanged(() => this.Brands);
            }
        }

        private string m_ProductName;
        /// <summary>
        /// 产品名称
        /// </summary>
        public string ProductName
        {
            get { return this.m_ProductName; }
            set
            {
                this.m_ProductName = value;
                RaisePropertyChanged(() => this.ProductName);
            }
        }


        private string m_CurrentFamily;
        /// <summary>
        /// 当前选中设备族
        /// </summary>
        public string CurrentFamily
        {
            get { return this.m_CurrentFamily; }
            set
            {
                this.m_CurrentFamily = value;
                RaisePropertyChanged(() => this.CurrentFamily);
            }
        }


        private List<TComboxItem> m_Families;
        /// <summary>
        /// 设备族信息
        /// </summary>
        public List<TComboxItem> Families
        {
            get { return this.m_Families; }
            set
            {
                this.m_Families = value;
                RaisePropertyChanged(() => this.Families);
            }
        }
        #endregion

        #region 命令相关
        /// <summary>
        /// 添加产品完成
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void AddCommand(object parameter)
        {

            try
            {
                if (!CreateProduct())
                    return;
                Window window = parameter as Window;
                if (window != null)
                    window.DialogResult = true;
            }
            catch (Exception ex)
            {
                MessageTipEx.Show(ex);
            }
        }
        public bool CanAddCommand(object parameter)
        {
            return IsValidated;
        }


        /// <summary>
        /// 添加产品并管理类型
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void AddAndManageCommand(object parameter)
        {

            try
            {
                if (!CreateProduct())
                    return;
                this.ManageType = true;
                Window window = parameter as Window;
                if (window != null)
                    window.DialogResult = true;
            }
            catch (Exception ex)
            {
                MessageTipEx.Show(ex);
            }
        }
        public bool CanAddAndManageCommand(object parameter)
        {
            return IsValidated;
        }
        #endregion

        #region 私有函数
        /// <summary>
        /// 根据厂商库Id,加载品牌信息
        /// </summary>
        /// <param name="venderId"></param>
        private void LoadBrandItems(string venderId)
        {
            var brands = BllFactory<BrandBll>.Instance.GetAllItems(venderId);
            Brands = brands.Select(b => new TComboxItem(b.Name, b.Id)).ToList();
        }
        private string GetBrandName(string venderId,string brandId)
        {
            var brands = BllFactory<BrandBll>.Instance.GetAllItems(venderId);
            var item=brands.FirstOrDefault(c=>c.Id==brandId);
            string name = string.Empty;
            if (item != null)
            {
                name = item.Name;
            }
            return name;
        }

        /// <summary>
        /// 加载设备族信息
        /// </summary>
        private void LoadFamilyItems()
        {
            var familys=EquipmentFamilyManager.GetFamilys();
            Families = familys.Select(b => new TComboxItem(b.Name, b.Code)).ToList();
        }

        private bool CreateProduct()
        {
            if (string.IsNullOrWhiteSpace(this.CurrentBrandId))
            {
                MessageTip.Show(this.GetWindow(), "品牌不能为空");
                return false;
            }
            ProductItem item = new ProductItem();
            item.Id = ProductId ?? string.Empty;
            item.Name = this.ProductName;
            item.BrandId = this.CurrentBrandId;
            item.FamilyCode = CurrentFamily;
            item.ManufacturerId = this.VenderId;
            var result = false;
            if (string.IsNullOrEmpty(ProductId))
            {
                result = BllFactory<ProductBll>.Instance.Add(item);

                ProductId = item.Id;
            }
            if (result)
            {
                ProductId = item.Id;
            }
            else
            {
                MessageTip.Show(this.GetWindow(), BllFactory<ProductBll>.Instance.ErrorMessage ?? string.Empty);
            }
            return result;
        } 
        #endregion
    }
}
