//查看银行账户信息详情
function viewBankAccountInfo(t){
	var id = $(t).parents('tr').attr('id');
	$('#bankAccountInfoDialog').load('/bankCardInfoSet/view?id='+id,function(){
		$("#myModal-viewBankAccountInfo").modal("show")
	})
};

//修改按钮打开修改弹框
function updateBankAccountInfoBtn(t){
	var id = $(t).parents('tr').attr('id');
	bankNewsQuery.listPage = $('#bankAccountInfoList').getGridParam('page');
	$('#bankAccountInfoDialog2').load("/bankCardInfoSet/update?id="+id,function(){
		$("#myModal-updatebankCardInfoSetInfo").modal("show");
	})
};

//删除银行账户信息
function deleteBankAccountInfo(t){
	bootbox.confirm({
		title : '删除',
		message : '<div style="text-align:left;font-size:0.12rem">确认删除该银行账户吗？</div>',
		size : 'small',
		buttons : {
			cancel : {
				label : '取消',
				className:'btn-white btn-info'
			},
			confirm : {
				label : '确定',
				className:'btn-white btn-info colorbtn'
			}
		},
		callback:function(result){
			if (result) {
				var id = $(t).parents('tr').attr('id');
				bankNewsQuery.listPage = $('#bankAccountInfoList').getGridParam('page');
				$.ajax({
					url:'/bankCardInfoSet/detete',
					type:'post',
					data:{
						'id':id
					},
					success:function(data){
						if( data.success ){
							if( data.data ){
								new $.zui.Messager('删除银行账户信息成功', {
								    type: 'success'
								}).show();
								//刷新表格(当前页)
								 $('#bankAccountInfoList').jqGrid('setGridParam',{
						            mtype:'GET',
						            url : '/bankCardInfoSet/select',
						            postData: {
						            	'companyBelong':bankNewsQuery.bankcompanyownerid,
										'fastSearch':bankNewsQuery.bankKeyWord
									},
						            page:bankNewsQuery.listPage
						        }).trigger('reloadGrid');
							}else{
								new $.zui.Messager('删除银行账户信息失败', {
								    type: 'danger'
								}).show();
							}
						}else{
							new $.zui.Messager('删除银行账户信息请求失败:'+data.msg, {
							    type: 'danger'
							}).show();
						}
					},
					error:function(status){
						new $.zui.Messager('删除银行账户信息时发生错误:'+status, {
						    type: 'danger'
						}).show();
					}
				})
			}
		}
	})
};

//禁用启用账户
function disabledBankAccountInfo(t){
	bankNewsQuery.listPage = $('#bankAccountInfoList').getGridParam('page');
	var errortip = '',status = true,id = $(t).parents('tr').attr('id');
	if( $(t).hasClass('operateIcon-jinyong') ){
		errortip = '禁用';
		status = 0;
	}else{
		errortip = '启用';
		status = 1;
	};
	$.ajax({
		url:'/bankCardInfoSet/enable',
		type:'post',
		data:{
			'id':id,
			'flag':status
		},
		success:function(data){
			if( data.success ){
				 if( !data.data ){
					 new $.zui.Messager(errortip+'设置失败', {
						    type: 'danger'
					 }).show();
				 }else{
					 new $.zui.Messager(errortip+'设置成功', {
						    type: 'success'
					 }).show();
					//刷新表格(当前页)
					 $('#bankAccountInfoList').jqGrid('setGridParam',{
			            mtype:'GET',
			            url : '/bankCardInfoSet/select',
			            postData: {
							'companyBelong':bankNewsQuery.bankcompanyownerid,
							'fastSearch':bankNewsQuery.bankKeyWord
						},
			            page:bankNewsQuery.listPage
			         }).trigger('reloadGrid');
				 }
			 }else{
				 new $.zui.Messager(errortip+'设置请求失败:'+data.msg, {
					    type: 'danger'
				 }).show();
			 }
		},
		error:function(status){

		}
	})
}

$(function(){
	// function getCompany() {
	// 	$.ajax({
	// 		url: '/platform/list' ,
	// 		type: 'get',
	// 		data: {},
	// 		async: false,
	// 		success: function (data) {
	// 			if (data.success) {
	// 				$("[name=companyBelong]").empty();
	// 				for (var i = 0; i < data.data.length; i++) {
	// 					$('.companySelect').append('<li value="'+data.data[i].id+'" class="companyOwner"><a style="padding-left:0.08rem;" javascript:void(0)>'+data.data[i].platformName+'</a></li>')
	// 				}
	// 				;
	// 				i = null;
	// 			} else {
	// 				new $.zui.Messager('获取选项失败:' + data.msg, {
	// 					type: 'danger'
	// 				}).show();
	// 			}
	// 			;
	// 		},
	// 		error: function (status) {
	// 			new $.zui.Messager('获取选项出错:' + status, {
	// 				type: 'danger'
	// 			}).show();
	// 		}
	// 	});
	// }
	// getCompany()

	function loadSendData(){
		var postdata = {
			'companyBelong':bankNewsQuery.bankcompanyownerid,
			'fastSearch':bankNewsQuery.bankKeyWord
		};
		$('#bankAccountInfoList').setGridParam({
			mtype:'GET',
			url : '/bankCardInfoSet/select',
			datatype : 'json',
			postData: postdata,
			page : 1
		}).trigger('reloadGrid');
	};

	function operate(cellValue,options,rowObject){
		if( rowObject.status == '1'){
			return $("#disableBankAccountInfo").html();
		}else{
			return $("#enableBankAccountInfo").html();
		}
	};

	function showstatus(cellValue,options,rowObject){
		if( rowObject.status == '1'){
			return '启用'
		}else{
			return '禁用'
		}
	};
	function showAmount(cellValue,options,rowObject) {
		return commaDelimiter(cellValue)
	}

	sxUtil.jqGrid('#bankAccountInfoList',{
		colNames:['银行账户信息ID','序号','公司归属','银行名称','卡号','期初数值','币种','状态','操作'],
		colModel:[
			{name: 'id', label: '银行账户信息ID', hidedlg:true, hidden:true,sortable:false,title:false},
			{name: 'orderNumber', label: '序号', sortable:false,title:false,width:70},
			{name: 'companyBelongDict.name', label: '公司归属',sortable:false,width:100,align:'center',title:false},
            {name: 'bankName', label: '银行名称',sortable:false,width:120,align:'left',title:false},
            {name: 'bankAccount', label: '卡号',sortable:false,align:'center',width:180,title:false},
            {name: 'beginValue', label: '期初数值',sortable:false,align:'right',width:180,title:false,formatter:showAmount},
			{name: 'currencyDict.code', label: '币种',sortable:false,align:'center',width:180,title:false,},
            {name: 'status', label: '状态',sortable:false,align:'center',width:90,title:false,formatter:showstatus},
            {name: 'operate', label: '操作',sortable:false,align:'center',width:180,title:false,formatter:operate}
		],
		height:'auto',
		mtype:'GET',
		url : '/bankCardInfoSet/select',
		pager : "#bankAccountListPage",
		rownumbers:false,
		multiselect: false,
		postData : {
			'companyBelong':bankNewsQuery.bankcompanyownerid,
			'fastSearch':bankNewsQuery.bankKeyWord
		},
		page:bankNewsQuery.listPage,
		loadError:function(){
			new $.zui.Messager('银行账户信息设置列表加载失败', {
			    type: 'warning',
			    time:1500
			}).show();
		}
	},true);

	//刷新
	$('#refresh').click(function(){
		$("#bankCardNoName").val('');//清空
		$("#companyOwnerId i").text('请选择公司归属');
		$("#companyOwnerId").data('companyownerid','');
		$('#bankAccountInfoList').jqGrid('setGridParam',{
            mtype:'GET',
            url : '/bankCardInfoSet/select',
            postData: {
				'companyBelong':'',
				'fastSearch':''
				},
            page:1
        }).trigger('reloadGrid'); //重新载入
	});

	//公司归属
	$(".companyOwner").click(function(){
		$("#companyOwnerId i").text( $(this).text() )
		$("#companyOwnerId").data('companyownerid',$(this).attr('value'));
		bankNewsQuery.bankcompanyownerid = $("#companyOwnerId").data('companyownerid');
		loadSendData();
	})
	//“查询按钮点击”
	$("#query").click(function(){
		bankNewsQuery.bankKeyWord = $("#bankCardNoName").val();
		loadSendData();
	});

	//新增银行信息按钮点击
	$("#bankAccountInfoAdd").click(function(){
		mylistpage = $('#bankAccountInfoList').getGridParam('page');
		$('#bankAccountInfoDialog').load('/bankCardInfoSet/add',function(){
			$("#myModal-addBankAccountInfo").modal("show")
		})
	});

	//切换表格
	$(".projectListTab li").click(function () {

		if ($(this).text() == '现金流管理') {
			$(".yk_rightcontent").load('/cashFlow/list');
		} else if ($(this).text() == '银行信息设置') {
			$(".yk_rightcontent").load('/bankCardInfoSet/list');
		}
	});
})
