;(function($){
	$.fn.createTabDialog = function(obj,submiturl,judgeadd){//创建主体
		if(!submiturl){
			submiturl="";
		}
		var num = 0;
		var numconstant = 0;
		var tags = 0;
		var objwidth = 850;
		var objheight = 600;
		var objtitle = "标题";
		var nextstep = "<input type='button' class='nextstep btn btn-default btn-primary btn-sm' value='下一步'>";
		var prevstep = "<input type='button' class='prevstep btn btn-default btn-primary btn-sm' value='上一步'>";
		var savestep = "<input type='button' class='savestep btn btn-default btn-primary btn-sm' value='保存'>";
		var closestep = "<input type='button' class='savestep btn btn-default btn-primary btn-sm' value='关闭'>";
		/*$.closedialog = function(){//关闭
			$("#next_menuboxdialog").fadeOut("slow",function(){
				$("#next_menuboxdialog").find(".next_stepbox ul").empty();
				$("#next_menuboxdialog").find(".next_buttonbox").empty();
				$(".next_stepbox .next_stepbarhover").width(0);
				num = obj.steps.length;numconstant = obj.steps.length;tags = 1;objwidth = 850;objheight = 600;objtitle = "标题";
			});
		};*/
		$.buttonshow = function(sizes){//初始buttonshow
			if( sizes<=0 ){
				layer.msg('无数据');
			}
			if(sizes==1){
				if(judgeadd=="add"||judgeadd=="update"){
					$(".next_buttonbox").empty().append(savestep);
				}else if(judgeadd=="view"){
					$(".next_buttonbox").empty().append(closestep);
				}
			}
			if(sizes>1){
				$(".next_buttonbox").empty().append(nextstep);
			}
		};
		$.clickbuttonshow = function(n){//点击按钮后的buttonshow
			$(".next_stepbox .next_stepbarhover").animate({width:$(".next_stepbox li").width()*(n-1)},"fast","linear",function(){
					$(".next_stepbox li a").removeClass("next_stepahover");
					for(var j=0;j<=(n-1);j++){
						$(".next_stepbox li a").eq(j).addClass("next_stepahover");
					}	
			})
			if( parseInt(n)>1&&parseInt(n)<numconstant ){
				$(".next_buttonbox").empty().append(prevstep,nextstep);
			}
			if( parseInt(n)==1&&numconstant==1 ){
				if(judgeadd=="add"||judgeadd=="update"){
					$(".next_buttonbox").empty().append(savestep);
				}else if(judgeadd=="view"){
					$(".next_buttonbox").empty().append(closestep);
				}
			}
			if( parseInt(n)==1&&numconstant>1 ){
				$(".next_buttonbox").empty().append(nextstep);
			}
			if(parseInt(n)>1&&parseInt(n)==numconstant){
				if(judgeadd=="add"||judgeadd=="update"){
					$(".next_buttonbox").empty().append(prevstep,savestep);
				}else if(judgeadd=="view"){
					$(".next_buttonbox").empty().append(prevstep,closestep);
				}
				
			}
		};
		if(obj){
			var $self = $(this);
			
			//if(obj.width){objwidth=obj.width};
			//if(obj.height){objheight=obj.height};
			//if(obj.title){objtitle=obj.title};
			//$self.width(objwidth).height(objheight).css({"margin-left":-objwidth/2+"px"}).fadeIn();//出现
			$self.show();
			$self.find(".next_titlep").text( objtitle );//标题
			var stepsli;
			for(var i=0;i<obj.steps.length;i++){//步骤li
				stepsli = "<li><a class='next_stepa'>"+(i+1)+"</a><p class='next_stepp'>"+obj.steps[i].title+"</p></li>";
				$self.find(".next_stepbox ul").append(stepsli);
				stepsli = null;
			}
			$(".next_stepbox li").css({width:(100/i)+"%"});//步骤均等分以及样式操作
			$(".next_stepbox li a:first").addClass("next_stepahover");
			$(".next_stepbox .next_stepbar").css({width:(100-100/i)+"%","margin-left":(100/i/2)+"%"} );
			$(".next_stepbox .next_stepbarhover").css({"margin-left":(100/i/2)+"%"} );
			
			
			
			//$(".next_formbox").css({height:($self.height()-$('.next_stepbox').height()-80)+"px","overflow":"auto"});
			//$(".next_buttonbox").css({height:($self.height()-$('.next_stepbox').height()-$(".next_formbox").height()-60)+"px"});

			$.buttonshow(obj.steps.length);//初始加载时显示的按钮
			$(".next_formbox").load(obj.steps[0].url,{"random":Math.random()},function(){
				tags = $(".next_formbox form").find(".hidden").val() ;
			});//jquery load加载表单页面
			num = obj.steps.length;
			numconstant = obj.steps.length;

			$(".next_stepbox li a").each(function(i,ele){//点击头部步骤跳转
				$(ele).click(function(){
				   // if(submiturl==""){
						submiturl=$(".next_formbox form").attr("action");
					//}
					if( Math.abs(parseInt($(ele).text())-parseInt($(".next_formbox form").find(".hidden").val())) ==1  ){//判断当前的和点击的标识差,等于1标识可点击
						if( $(".next_formbox form").valid() ){//验证
							$(".next_formbox form").ajaxSubmit({//提交
								url:submiturl,
								type:"POST",
								data: $(".next_formbox form").formSerialize(),
								beforeSend:function(){
									$(".next_loding").show();//等待效果
								},
								error:function(){//失败
									layer.alert('保存失败',{"icon":"5"});
								},
								success:function(){//成功
									$(".next_loding").hide();
									btnshowtags = $(".next_formbox form").find(".hidden").val();
									$(".next_formbox").load(obj.steps[i].url,{"random":Math.random()},function(){
										num = numconstant - i;
										$.clickbuttonshow($(".next_formbox form").find(".hidden").val());
										//layer.alert( "第"+(btnshowtags)+"步成功",{"icon":"6"});
										$(".next_stepbox .next_stepbarhover").animate({width:$(".next_stepbox li").width()*i},"slow","linear",function(){
											$(".next_stepbox li a").removeClass("next_stepahover");
											for(var j=0;j<=i;j++){
												$(".next_stepbox li a").eq(j).addClass("next_stepahover");
											}	
										})
									});
								}
							});
						}
					}else{
						layer.alert('不可跨步骤点击');
						return;
					}
					
				})
			});
			
			$.clickbutton = function(){//上一步和下一步和保存
				var $that = $(this);
				//if(submiturl==""){
					submiturl=$(".next_formbox form").attr("action");
				//}
				
				if( $(".next_formbox form").valid() ){
					$(".next_formbox form").ajaxSubmit({
						url:submiturl,
						type:"POST",
						data: $(".next_formbox form").formSerialize(),
						beforeSend:function(){
							$(".next_loding").show();//等待效果
						},
						error:function(){
							layer.alert('保存失败',{"icon":"5"});
							return;
						},
						success:function(data){
							$(".next_loding").hide();
							if( $that.hasClass("nextstep")||$that.hasClass("savestep")||$that.hasClass("closestep") ){//点击按钮是下一步或保存
								num<=0?num=0:num=num-1;
								steps=numconstant-num;
							}else if( $that.hasClass("prevstep") ){//点击按钮是上一步
								num==0?num=2:num=num+1;
								steps=numconstant-num+2;
							}
							if( (numconstant-num)<numconstant ){
								var jumpUrl =obj.steps[numconstant-num].url;
								if(judgeadd=="add"){
									jumpUrl+="&dueDiligenceId="+data.id;
								}
								$(".next_formbox").load(jumpUrl,{"random":Math.random()},function(){
									tags = $(".next_formbox form").find(".hidden").val() ;
									//$(".next_formbox").find("form").append("<input type='hidden' name='balanceSheetId' value='"+balanceSheetId+"'>");
									$.clickbuttonshow(tags);
									//layer.alert( "第"+(steps)+'步成功',{"icon":"6"});
								});
							}else{//已经是最后一步了
								if(judgeadd=="add"||judgeadd=="update"){
									sxUtil.msgbox.success('操作成功！');
								}
								
								//刷新数据
								$("#balanceSheetList").trigger("reloadGrid");
								$("#cashFlowtList").trigger("reloadGrid"); 
								$("#dueDiligenceList").trigger("reloadGrid"); 
								$("#supplierDueDiligenceList").trigger("reloadGrid");
								$(".layui-layer.layui-layer-page,.layui-layer-shade").hide();
								$(".layui-layer.layui-layer-page").empty();
							}
						}
					});
				}
			};
		}else{
			layer.alert('请传入参数');
		}
	};
})(jQuery);