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

using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using Com.FirmLib.Bll;
using Com.FirmLib.UI.Seller.Model;
using FWindSoft.DataFramework;
using FWindSoft.MVVM;
using FWindSoft.Wpf;
using FWindSoft.Wpf.Common;

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

        private void Init()
        {

        }
        public void Init(LoadParameter parameter)
        {
            //需要项目Id,厂商Id,当前型号
            ContractSelectorParameter useParameter = parameter.Parameter as ContractSelectorParameter;
            string projectId = useParameter?.ProjectId;//关联项目Id
            string venderId = useParameter?.VenderId;
            string contractNo = useParameter?.ContractNo;
            //todo 加载项目中的合同
            LoadContracts(projectId, venderId);
            UseNo = contractNo;           
        }
        #region 属性相关

        private string m_UseNo;
        /// <summary>
        /// 使用合同号
        /// </summary>
        public string UseNo
        {
            get { return this.m_UseNo; }
            set
            {
                this.m_UseNo = value;
               
;               try
                {
                    BeginInEdit();
                    CurrentNo = this.ExistContracts.FirstOrDefault(c=>c==value);
                }
                finally
                {
                    EndInEdit();
                }
                RaisePropertyChanged(nameof(this.UseNo));
            }
        }


        private string m_CurrentNo;
        /// <summary>
        /// 当前合同号
        /// </summary>
        public string CurrentNo
        {
            get { return this.m_CurrentNo; }
            set
            {
                this.m_CurrentNo = value;
                if (!IsInEditing)
                {
                    if (!string.IsNullOrEmpty(value))
                    {
                        this.UseNo = value;
                    }
                }
                
                RaisePropertyChanged(() => this.CurrentNo);
            }
        }



        private ObservableCollection<string> m_ExistContracts;
        /// <summary>
        /// 存在的合同
        /// </summary>
        public ObservableCollection<string> ExistContracts
        {
            get { return this.m_ExistContracts; }
            set
            {
                this.m_ExistContracts = value;
                RaisePropertyChanged(() => this.ExistContracts);
            }
        }

        #endregion

        #region 命令相关

        /// <summary>
        /// 确认命令
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void GetContractNo(object parameter)
        {

            try
            {
                Window window = parameter as Window;
                if (window != null)
                {
                    window.SetRealResult<string>(UseNo);
                    window.DialogResult = true;
                }
            }
            catch (System.Exception ex)
            {
                MessageTipEx.Show(ex);
            }
                
        }
        public bool CanGetContractNo(object parameter)
        {
            return !string.IsNullOrEmpty(UseNo);
        }


        /// <summary>
        /// 选择命令
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void SelectCommand(object parameter)
        {
            //string contractNo = parameter as string;
            //if (!string.IsNullOrEmpty(contractNo))
            //{
            //    this.UseNo = contractNo;
            //}

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

        #endregion

        #region 私有方法
        private void LoadContracts(string projectId,string venderId)
        {
            var items = BllFactory<SellerBll>.Instance.GetProjectContracts(venderId,projectId);
            ExistContracts = new ObservableCollection<string>(items??new System.Collections.Generic.List<string>());
        }
        #endregion
    }
}
