using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using Muchinfo.MTPClient.Bank.Views;
using Muchinfo.MTPClient.Data.Model.Bank;
using Muchinfo.MTPClient.Infrastructure.Utilities;
using Muchinfo.MTPClient.IService;
namespace Muchinfo.MTPClient.Bank.ViewModels
{
public class BranchViewModel : ViewModelBase
{
#region 变量成员
private IBankService _bankService;
private string _searchText;
///
///搜索内容
///
public string SearchText
{
get { return _searchText; }
set { Set(() => SearchText, ref _searchText, value); }
}
///
/// 省份列表
///
private List> _provinceList;
public List> ProvinceList
{
get { return _provinceList; }
set { _provinceList = value; }
}
private string _selectProvinceId;
///
/// 选择省份Id
///
public string SelectProvinceId
{
get
{
return _selectProvinceId;
}
set
{
_selectProvinceId = value;
CityList = _bankService.GetCitys(value, UserManager.CurrentTradeAccount);
SelectCityName = CityList[0];
}
}
///
/// 城市列表
///
private List _cityList;
public List CityList
{
get { return _cityList; }
set { Set(() => CityList, ref _cityList, value); }
}
private string _selectCityName;
///
/// 选择城市Id
///
public string SelectCityName
{
get
{
return _selectCityName;
}
set
{
Set(() => SelectCityName, ref _selectCityName, value);
_selectCityName = value;
LoadBranchBanks();
}
}
private List _branchList;
///
/// 支行列表
///
public List BranchList
{
get
{
return _branchList;
}
set
{
_branchList = value;
BranchSource = value;
}
}
private List _branchSource;
///
/// 支行绑定数据源
///
/// The bank branch dictionary.
public List BranchSource
{
get { return _branchSource; }
set { Set(() => BranchSource, ref _branchSource, value); }
}
private Branchs _selectBranch = new Branchs();
///
/// 当前选择支行
///
public Branchs SelectBranch
{
get { return _selectBranch; }
set { _selectBranch = value; }
}
///
/// 当前签约银行
///
private SigningBank CurrentSigningBank;
#endregion
#region 构造函数
public BranchViewModel(BranchView win_BranchView, SigningBank currentSigningBank, string ProvinceName, string CityName)
{
win_BranchView.Closing += win_BranchView_Closing;
this.CurrentSigningBank = currentSigningBank;
_bankService = SimpleIoc.Default.GetInstance();
ProvinceList = _bankService.GetProvinces(UserManager.CurrentTradeAccount);
if (!string.IsNullOrWhiteSpace(ProvinceName))
{
SelectProvinceId = ProvinceList.Where(x => x.Value == ProvinceName).First().Key;
}
if (!string.IsNullOrWhiteSpace(CityName))
{
SelectCityName = CityList.Where(x => x == "CityName").First();
}
}
#endregion
#region 公共方法
///
/// 查询支行
///
public void LoadBranchBanks()
{
// this.BranchList = _bankService.GetBranchBanks(this.CurrentSigningBank.Id, this.SelectCityName, UserManager.CurrentTradeAccount);
}
///
/// 搜索
///
public RelayCommand SearchCommand
{
get
{
return new RelayCommand(() =>
{
ShowBranchBanks();
});
}
}
///
/// 点击RadioButton
///
public RelayCommand SelectionChangeCommand
{
get
{
return new RelayCommand((dg_Branch) =>
{
if (dg_Branch != null && dg_Branch.Count > 0)
{
SelectBranch = dg_Branch[0] as Branchs;
SelectBranch.IsSelected = true;
}
});
}
}
///
/// 完成
///
public RelayCommand OkCommand
{
get
{
return new RelayCommand((win_BranchView) =>
{
this.CurrentSigningBank.BranchId = SelectBranch.FullBankId;
this.CurrentSigningBank.BranchBankName = SelectBranch.BankName;
win_BranchView.Visibility = System.Windows.Visibility.Collapsed;
});
}
}
///
/// 取消
///
public RelayCommand CancelCommand
{
get
{
return new RelayCommand((win_BranchView) =>
{
win_BranchView.Visibility = System.Windows.Visibility.Collapsed;
});
}
}
#endregion
#region 私有方法
///
/// 显示列表
///
private void ShowBranchBanks()
{
try
{
//省份,城市,搜索关键字
if (string.IsNullOrEmpty(SearchText))
{
this.BranchSource = this.BranchList;
}
else
{
this.BranchSource = this.BranchList.Where(p => p.BankName.Contains(SearchText.Trim())).ToList();
}
Branchs branchs = this.BranchSource.FirstOrDefault(c => c.FullBankId == this.SelectBranch.FullBankId);
if (branchs != null)
{
branchs.IsSelected = true;
}
}
catch (Exception ex)
{
}
}
//点击窗体自带关闭按钮时使用
void win_BranchView_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
BranchView branchView = sender as BranchView;
branchView.Visibility = System.Windows.Visibility.Collapsed;
e.Cancel = true;
}
#endregion
}
}