//修改关联企业
function updateEnterPriseEmployee(id,regNoType,regNo){
	sendlistpage = $('#relationEnterpriseList').getGridParam('page');
	var custId='';
	if( $("#companySocialCreditCode").val()=='' ){
		custId=enterPriseBusinessQuery.enterPriseDomesticCompanyId
	}else{
		custId=$("#companySocialCreditCode").val()
	};
	$(".enterpriseDialog").load('/orgbaseInfo/affiliated/update?custId='+custId+'&regNoType='+regNoType+'&regNo='+regNo,function(){
		$("#myModal-updateRelationEnterpriseInfor").modal("show");
	})
};

//删除关联企业
function deleteEnterPriseEmployee(id,regNoType,regNo){
	var custId='';
	if( $("#companySocialCreditCode").val()=='' ){
		custId=enterPriseBusinessQuery.enterPriseDomesticCompanyId
	}else{
		custId=$("#companySocialCreditCode").val()
	};
	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) {
				sendlistpage = $('#relationEnterpriseList').getGridParam('page');
				$.ajax({
					url:'/orgbaseInfo/affiliated/doDelete',
					type:'get',
					data:{
						'custId':custId,
						'regNoType':regNoType,
						'regNo':regNo
					},
					success:function(data){
						if( data.success ){
							if( data.data ){
								new $.zui.Messager('删除关联企业成功', {
								    type: 'success'
								}).show();
								//刷新表格(当前页)
								 $('#relationEnterpriseList').jqGrid('setGridParam',{ 
						            mtype:'GET',
						            url : '/orgbaseInfo/affiliated/doList',
						            postData: {
						            	'fastSearch':custId
									},
						            page:sendlistpage 
						        }).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(){
	var custId='';
	if( $("#companySocialCreditCode").val()=='' ){
		custId=enterPriseBusinessQuery.enterPriseDomesticCompanyId
	}else{
		custId=$("#companySocialCreditCode").val()
	};
	function loadSendData(data){
		sendlistpage = 1;
		var postdata = {};
		if( data.data == null ){
			postdata = {'fastSearch':custId};
		}else{
			postdata = data.data;
		};
		$('#relationEnterpriseList').setGridParam({
			mtype:'GET',
			url : '/orgbaseInfo/affiliated/doList',
			datatype : 'json',
			postData: postdata,
			page : 1
		}).trigger('reloadGrid');
	};
	
	function operateBusinessUser(cellValue,options,rowObject){
		var btnstr = '';
		
		if( $('#companySocialCreditCode').val()=='' ){// 查看进入时不显示
			if( $('#updateOrgAffiliated').length>0 ){
				btnstr += '<p><span>修改</span><i class="operateIconfont operateIcon-bianji" onclick="updateEnterPriseEmployee('+rowObject.id+',\''+rowObject.regNoType+'\',\''+rowObject.regNo+'\')"></i></p>';
			};
			if( $("#deleteOrgAffiliated").length>0 ){
				btnstr += '<p><span>删除</span><i class="operateIconfont operateIcon-shanchu" onclick="deleteEnterPriseEmployee('+rowObject.id+',\''+rowObject.regNoType+'\',\''+rowObject.regNo+'\')"></i></p>';
			};
		}
		
		return '<div class="operateIcon noHover">'+btnstr+'</div>'
	};
	
	var tablepostdata = {'fastSearch':custId};
	
	sxUtil.jqGrid('#relationEnterpriseList',{
		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: 'affiliatedType', label: '关联类型',sortable:false,width:100,align:'left',title:false},
            {name: 'affiliatedEntName', label: '关联企业名称',sortable:false,width:100,align:'left',title:false},
            {name: 'regNoTypeCN', label: '登记注册号类型',sortable:false,align:'left',width:100,title:false},
            {name: 'regNo', label: '登记注册号码',sortable:false,align:'left',width:150,title:false},
            {name: 'operate', label: '操作',sortable:false,align:'center',width:150,title:false,formatter:operateBusinessUser}
		], 
		height:'auto',
		mtype:'GET',
		url : '/orgbaseInfo/affiliated/doList',
		pager : "#relationEnterpriseListPage",
		multiselect: false,
		rownumbers:false,
		postData : tablepostdata,
		page:sendlistpage,
		loadError:function(){
			new $.zui.Messager('关联企业列表加载失败', {
			    type: 'warning',
			    time:1500
			}).show();
		}
	},true);
	
	//刷新
	$('#refresh').click(loadSendData);
	//新增
	$("#addRelationEnterprise").click(function(){
		sendlistpage = $('#relationEnterpriseList').getGridParam('page');
		$('.enterpriseDialog').load('/orgbaseInfo/affiliated/add?custId='+custId,function(){
			$("#myModal-addRelationEnterpriseInfor").modal("show");
		})
	})
})