
///////////////////////////////////////////////////////////////////////////////
//文件名称: VmSellerProjectInfo.cs
//文件描述: 供应商项目信息
//创 建 者: xls
//创建日期: 2018/5/24 21:29:08
//版 本 号：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 CEFSharpWpf;
using Com.FirmLib.Bll;
using Com.FirmLib.Entity;
using Com.FirmLib.UI.BllCommon;
using Com.FirmLib.UI.Common;
using Com.FirmLib.UI.Common.Model;
using Com.FirmLib.UI.Seller.Model;
using FWindSoft.DataFramework;
using FWindSoft.MVVM;
using FWindSoft.SystemExtensions;
using FWindSoft.Wpf;
using FWindSoft.Wpf.Common;

namespace Com.FirmLib.UI.Seller
{
    public class VmSellerProjectInfo : BaseViewModel
    {
        public VmSellerProjectInfo()
        {
            Init();
        }
        private void Init()
        {
            ContactItem = new ContactInformationItem();
        }
        public void Init(LoadParameter parameter)
        {
            var para = parameter.Parameter as SellerParameter;
            SellerId = para.Seller.Id;
            ProjectId = para.Project.Id;
            LoadSellerItem(SellerId);
            LoadProjectItem(ProjectId);
            LoadLinkerItem(SellerId, ProjectId);
            LoadAssetItems(SellerId, ProjectId);
        }
        #region 属性相关
        /// <summary>
        /// 供应商Id
        /// </summary>
        private string SellerId { get; set; }
        /// <summary>
        /// 项目Id
        /// </summary>
        public string ProjectId { get; private set; }
        /// <summary>
        /// 项目名称
        /// </summary>
        public string ProjectName { get; private set; }
        private string m_SellerName;
        /// <summary>
        /// 供应商名称
        /// </summary>
        public string SellerName
        {
            get { return this.m_SellerName; }
            set
            {
                this.m_SellerName = value;
                RaisePropertyChanged(() => this.SellerName);
            }
        }

        private string m_SellerUrl;
        /// <summary>
        /// 供应商Url
        /// </summary>
        public string SellerUrl
        {
            get { return this.m_SellerUrl; }
            set
            {
                this.m_SellerUrl = value;
                RaisePropertyChanged(() => this.SellerUrl);
            }
        }
        private ContactInformationItem m_ContactItem;
        /// <summary>
        /// 联系人信息
        /// </summary>
        public ContactInformationItem ContactItem
        {
            get { return this.m_ContactItem; }
            set
            {
                this.m_ContactItem = value;
                RaisePropertyChanged(() => this.ContactItem);
            }
        }


        private ObservableCollection<AssetShowItem> m_AssetItems;
        /// <summary>
        /// 资产项目
        /// </summary>
        public ObservableCollection<AssetShowItem> AssetItems
        {
            get { return this.m_AssetItems; }
            set
            {
                this.m_AssetItems = value;
                RaisePropertyChanged(() => this.AssetItems);
            }
        }
        #endregion

        #region 命令相关
        /// <summary>
        /// 浏览资产信息
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void ScanAssetInfo(object parameter)
        {

            try
            {
                AssetShowItem item = parameter as AssetShowItem;
                if (item == null)
                    return;
                AssetShowItemUtils.ShowAssetDetail(item);
            }
            catch (Exception ex)
            {
                MessageTipEx.Show(ex);
            }
        }
        public bool CanScanAssetInfo(object parameter)
        {
            return true;
        }
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void SaveCommand(object parameter)
        {

            try
            {
                bool flag = UpdateAsset();
                if (flag)
                {
                    AfterSuccessSave();
                    MessageTip.Show(this.GetWindow(), "保存成功");
                }
                else
                {
                    MessageTip.Show(this.GetWindow(), "保存失败");
                }
            }
            catch (Exception ex)
            {
                MessageTipEx.Show(ex);
            }
        }
        public bool CanSaveCommand(object parameter)
        {
            return true;
        }

        /// <summary>
        /// 选择合同编号
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void SelectContractCommand(object parameter)
        {
            AssetShowItem item = parameter as AssetShowItem;
            if (item == null)
                return;
            try
            {
                if (string.IsNullOrEmpty(ProjectId))
                {
                    MessageTip.Show(this.GetWindow(), "请先确定项目编号");
                    return;
                }
                WinSupplyContractNoSelector win = new WinSupplyContractNoSelector();
                win.Owner = this.GetWindow();
                ContractSelectorParameter par = new ContractSelectorParameter();
                par.ProjectId = ProjectId;
                par.VenderId = SellerId;
                par.ContractNo = item.SellerContractNo;
                LoadParameter.StartParameter(win, new LoadParameter(par));
                if (win.ShowDialog() == true)
                {
                    item.SellerContractNo = win.GetRealResult<string>() ?? string.Empty;
                }
            }
            catch (Exception ex)
            {

              MessageTipEx.Show(ex);
            }
        }
        public bool CanSelectContractCommand(object parameter)
        {
            return true;
        }
        /// <summary>
        /// 增加项目合同
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void AddSupplyContract(object parameter)
        {

            try
            {
                //添加供应合同
                WinSellerProjectContract win = new WinSellerProjectContract();
                LoadParameter.StartParameter(win, new LoadParameter(new VenderProjectParameter(SellerId, ProjectId)));
                win.Show(this.GetNavigationBar());
            }
            catch (Exception ex)
            {
                MessageTipEx.Show(ex);
            }
        }
        public bool CanAddSupplyContract(object parameter)
        {
            return true;
        }

        /// <summary>
        /// 删除供应资产
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void DeleteCommand(object parameter)
        {

            try
            {
                AssetShowItem showItem = parameter as AssetShowItem;
                if (showItem == null)
                    return;

                var result = MessageTip.Show(this.GetWindow(), "是否要删除", "提示", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                {
                    bool flag = false;
                    if (showItem.SellerId == SellerId)
                    {
                        flag = AssetManager.DeleteSellerAssets(ProjectId, new List<string>() { showItem.Id });
                    }
                    else
                    {
                        flag = true;
                    }

                    if (flag)
                    {
                        this.AssetItems.Remove(showItem);
                    }
                    else
                    {
                        MessageTip.Show(this.GetWindow(), "删除失败");
                    }

                }
            }
            catch (Exception ex)
            {
                MessageTipEx.Show(ex);
            }
        }
        public bool CanDeleteCommand(object parameter)
        {
            return true;
        }
        /// <summary>
        /// 更新指定字段信息
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        public bool SingleUpdateLinkerItem(string field)
        {

            LinkerItem item = new LinkerItem();
            item.VenderId = this.SellerId;
            item.ProjectId = this.ProjectId;
            SetFieldValue(item, field);
            var result = BllFactory<SellerBll>.Instance.UpdateLinker(item);
            if (!result)
            {
                MessageTip.Show(this.GetWindow(), BllFactory<SellerBll>.Instance.ErrorMessage ?? string.Empty);
            }
            return result;
        }
        private void SetFieldValue(LinkerItem item, string field)
        {
            item.Name = ContactItem.People;
            item.Phone = this.ContactItem.Telephone;
            item.Email = this.ContactItem.Email;
            item.Fax = this.ContactItem.Fax;
            #region 修改相关
            //if (field == "People")
            //{
            //    item.Name = ContactItem.People;
            //    return;
            //}
            //if (field == "Telephone")
            //{
            //    item.Phone = this.ContactItem.Telephone;
            //    return;
            //}
            //if (field == "Email")
            //{
            //    item.Email = this.ContactItem.Email;
            //    return;
            //}
            //if (field == "Fax")
            //{
            //    item.Fax = this.ContactItem.Fax;
            //    return;
            //} 
            #endregion
        }
        #endregion
        #region 私有方法
        /// <summary>
        /// 加载供应商信息
        /// </summary>
        /// <param name="venderId">厂商id</param>
        private void LoadSellerItem(string venderId)
        {
            var seller = BllFactory<SellerBll>.Instance.GetAllItems().FirstOrDefault(c => c.Id == venderId);
            if (seller != null)
            {
                this.SellerName = seller.Name;
                this.SellerUrl = seller.Website;
            }

        } 
        /// <summary>
        /// 加载联系人信息
        /// </summary>
        /// <param name="venderId">厂商库id</param>
        /// <param name="projectId">项目Id</param>
        private void LoadLinkerItem(string venderId,string projectId)
        {
            var linkerItem = BllFactory<SellerBll>.Instance.GetLinker(venderId,projectId);
            if (linkerItem != null)
            {
                this.ContactItem.Telephone= linkerItem.Phone;
                this.ContactItem.People = linkerItem.Name;
                this.ContactItem.Fax = linkerItem.Fax;
                this.ContactItem.Email = linkerItem.Email;
            }
        }
        /// <summary>
        /// 获取相关资产信息
        /// </summary>
        /// <param name="venderId"></param>
        /// <param name="projectId"></param>
        private void LoadAssetItems(string venderId, string projectId)
        {
            AssetItems = new ObservableCollection<AssetShowItem>();
            var dataItems = BllFactory<SellerBll>.Instance.GetProjectAssetItems(venderId, projectId);
            foreach (var dataItem in dataItems)
            {
                var showItem= SellerDataConverter.ConvertShowItem(dataItem);
                showItem.ProjectId = projectId;
                showItem.SellerId = venderId;
                AssetItems.Add(showItem);
            }
          
        }

        private void LoadProjectItem(string projectId)
        {
            var projectItem = BllFactory<ProjectBll>.Instance.GetProjectItem(projectId);
            if (projectItem != null)
            {
                this.ProjectName = projectItem.Name;
            }

        }
        #endregion

        #region 保存资产
        #region 修改资产信息
        /// <summary>
        /// 更新资产
        /// </summary>
        /// <returns></returns>
        private bool UpdateAsset()
        {
            AssetItems.ToList().ForEach(assetItem =>
            {
                ///todo 历史值存储
                //assetItem.SellerId = SellerId;
                //assetItem.CacheParameters.Add((new WatchParameter(MBIBuiltInParameter.DPSellerID)));
                //暂时不修改资产类型
                //assetItem.CacheParameters.Add((new WatchParameter(MBIBuiltInParameter.DPSpecificationID)));
                assetItem.CacheParameters.CheckAdd((new WatchParameter(MBIBuiltInParameter.SupplierContractID)));
                assetItem.CacheParameters.CheckAdd((new WatchParameter(MBIBuiltInParameter.PurchasePrice)));
                assetItem.CacheParameters.CheckAdd((new WatchParameter(MBIBuiltInParameter.Warranty)));
                assetItem.CacheParameters.CheckAdd((new WatchParameter(MBIBuiltInParameter.ProductDate)));
            });
            return AssetManager.UpdateAssetItems(ProjectId, AssetItems.ToList());
        }


        #endregion
        #endregion
        /// <summary>
        /// 成功保存
        /// </summary>
        private void AfterSuccessSave()
        {
             LoadAssetItems(SellerId, ProjectId);
        }
       
    }
}
