//修改上级单位
function updateEnterPriseEmployee(id,regNoType,regNo){
	sendlistpage = $('#superiorUnitList').getGridParam('page');
	var custId='';
	if( $("#companySocialCreditCode").val()=='' ){
		custId=enterPriseBusinessQuery.enterPriseDomesticCompanyId
	}else{
		custId=$("#companySocialCreditCode").val()
	};
	$(".enterpriseDialog").load('/orgbaseInfo/parentbody/update?custId='+custId+'&regNoType='+regNoType+'&regNo='+regNo,function(){
		$("#myModal-updateSuperiorUnitInfor").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 = $('#superiorUnitList').getGridParam('page');
				$.ajax({
					url:'/orgbaseInfo/parentbody/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();
								//刷新表格(当前页)
								 $('#superiorUnitList').jqGrid('setGridParam',{ 
						            mtype:'GET',
						            url : '/orgbaseInfo/parentbody/doList',
						            postData: {
						            	'entBusinessId':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;
		};
		$('#superiorUnitList').setGridParam({
			mtype:'GET',
			url : '/orgbaseInfo/parentbody/doList',
			datatype : 'json',
			postData: postdata,
			page : 1
		}).trigger('reloadGrid');
	};
	
	function operateBusinessUser(cellValue,options,rowObject){
		var btnstr = '';
		
		if( $('#companySocialCreditCode').val()=='' ){// 查看进入时不显示
			if( $('#updateOrgParentbody').length>0 ){
				btnstr += '<p><span>修改</span><i class="operateIconfont operateIcon-bianji" onclick="updateEnterPriseEmployee('+rowObject.id+',\''+rowObject.regNoType+'\',\''+rowObject.regNo+'\')"></i></p>';
			};
			if( $("#deleteOrgParentbody").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('#superiorUnitList',{
		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: 'parentName', label: '上级机构名称',sortable:false,width:150,align:'left',title:false},
            {name: 'regNoTypeCN', label: '登记注册号类型',sortable:false,width:100,align:'left',title:false},
            {name: 'regNo', label: '登记注册号码',sortable:false,align:'left',width:100,title:false},
            {name: 'operate', label: '操作',sortable:false,align:'center',width:120,title:false,formatter:operateBusinessUser}
		], 
		height:'auto',
		mtype:'GET',
		url : '/orgbaseInfo/parentbody/doList',
		pager : "#superiorUnitListPage",
		multiselect: false,
		rownumbers:false,
		postData : tablepostdata,
		page:sendlistpage,
		loadError:function(){
			new $.zui.Messager('上级单位列表加载失败', {
			    type: 'warning',
			    time:1500
			}).show();
		}
	},true);
	
	//刷新
	$('#refresh').click(loadSendData);
	//新增
	$("#addSuperiorUnit").click(function(){
		sendlistpage = $('#superiorUnitList').getGridParam('page');
		$('.enterpriseDialog').load('/orgbaseInfo/parentbody/add?custId='+custId,function(){
			$("#myModal-addSuperiorUnitInfor").modal("show");
		})
	})
})