<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
	<head>
		<link rel="stylesheet" type="text/css"
			href="${base_url}css/themes/default/easyui.css">
		<script src="${base_url}js/jquery-1.8.0.min.js" type="text/javascript"></script>
		<script src="${base_url}js/jquery.easyui.min.js"
			type="text/javascript"></script>
		<title>铭飞内容管理系统安装程序</title>
		<link href="${base_url}install/style.css" rel="stylesheet"
			type="text/css" />
	</head>

	<body>
		<div class="t">
			<div class="top-version">
				<!-- 版本信息 -->
				<img alt="logo" src="install/images/logo.jpg">
				<span class="huan">欢迎使用铭飞网站内容管理系统</span>
			</div>

			<div class="main">
				<div class="pleft">
					<dl class="setpbox t1">
						<dt>
							<h3>
								安装步骤
							</h3>
						</dt>
						<dd>
							<ul>
								<li class="succeed">
									许可协议
								</li>
								<li class="now">
									参数配置
								</li>
								<li>
									正在安装
								</li>
								<li>
									安装完成
								</li>
							</ul>
						</dd>
					</dl>
				</div>
				<form action="alterDbServlet" method="post" name="form1">
					<div class="pr-title">
						<h3>
							数据库设定
						</h3>
					</div>
					<table width="726" border="0" align="center" cellpadding="0"
						cellspacing="0" class="twbox">
						<tr>
							<td class="onetd">
								<strong>数据库主机：</strong>
							</td>
							<td>
								<input name="dbhost" id="dbhost" type="text" value="localhost"
									class="input-txt" onblur="ajaxSubmit();"/><small>一般为localhost </small>
									<div id="dbhost_err" style="display:none; color: red">数据库主机名称不能为空</div>
									<div id="dbhost_ajax_err" style="display:none; color: red">数据库主机名称错误</div>
							</td>
						</tr>
						<tr>
							<td class="onetd">
								<strong>数据库名称：</strong>
							</td>
							<td>
								<input name="dbname" id="dbname" type="text" value=""
									class="input-txt" onblur="ajaxSubmit();"/>
									<div id="dbname_err" style="display:none; color: red">数据库名称不能为空</div>
									<div id="dbname_ajax_err" style="display:none; color: red">数据库未找到</div>
									<div id="dbname_ajax_cover" style="display:none; color: red">数据库已存在将进行覆盖</div>
								<br/>
								<p class="zhu">
									请先确保本地的数据库有已经创建好的数据库，并将此数据库名填写进输入框。
								</p>
							</td>
						</tr>
						<tr>
							<td class="onetd">
								<strong>数据库用户：</strong>
							</td>
							<td>
								<input name="dbuser" id="dbuser" type="text" value=""
									class="input-txt" onblur="ajaxSubmit();"/>
									<div id="dbuser_err" style="display:none; color: red">数据库用户不能为空</div>
							</td>
						</tr>
						<tr>
							<td class="onetd">
								<strong>数据库密码：</strong>
							</td>
							<td>
								<div style='float: left; margin-right: 3px;'>
									<input name="dbpwd" id="dbpwd" type="text" class="input-txt"
										onblur="ajaxSubmit(1);"/>
								</div><div id="dbuser_ajax_err" style="display:none; color: red">数据库用户名或密码错误</div>
								<br />
								<br />
						<!-- 		<p class="zhu">
									注意：修改完成后必须要重启一下服务器！
								</p> -->
							</td>
							<div style='float: left' id='dbpwdsta'></div>
						</tr>
					</table>
					<div class="pr-title">
						<h3>
							管理员初始密码
						</h3>
					</div>
								
					<table width="720" border="0" align="center" cellpadding="0"
						cellspacing="0" class="twbox">
						<p class="zhu" >
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;不填写或只填写其中一条信息无效，则默认用户帐号为：admin 密码为：admin
						</p>		
						<tr>
							<td class="onetd">
								<strong>用户名：</strong>
							</td>
							<td>
								<input id="adminuser" name="adminuser" type="text" value=""
									class="input-txt" />
								<p>
									<small>只能用'0-9'、'a-z'、'A-Z'、'.'、'@'、'_'、'-'、'!'以内范围的字符</small>
								</p>
							</td>
						</tr>
						<tr>
							<td class="onetd">
								<strong>密 码：</strong>
							</td>
							<td>
								<input id="adminpwd" name="adminpwd" type="text" value=""
									class="input-txt" />
							</td>
						</tr>
					</table> 
					<div class="btn-box">
						<div style="width: 700px; text-align: center;">
							<input type="button" class="btn-back" value="后退"
								onclick="window.location.href='${base_url}installStepServlet?step=1';" />
							<input type="button" class="btn-next" value="开始安装"
								onclick="doInstall();" />
						</div>
					</div>
			</div>
		</div>
		<div class="foot"></div>
		</form>
		</div>
		<script type="text/javascript">
		//进行安装 
	function doInstall() {
		if(temp==108){
			$(":input").attr("disabled","disabled");
			$.ajax({
			type : "post",
			url : "${base_url}alterDbServlet",
			dataType:'json',
			data : "dbhost="+$('#dbhost').val()+"&dbname="+$('#dbname').val()+"&dbuser="+$('#dbuser').val()+"&dbpwd="+$('#dbpwd').val(),
		    success: function(data){
		    	if(data.code==1){
		    		showFormMsg("铭飞文章管理系统","您已经安装过如需重新安装请先删除WEB-INF文件夹下面的cms_lock文件!");
		    	}else if(data.code==2){
		    		setTimeout("forward()",3000);
		    	}else if(data.code==3){
					showFormMsg("铭飞文章管理系统",data.content);	
				}
		    } 
		});
		}else{
			showFormMsg("铭飞文章管理系统","请先填写相应的内容！");
		}
	}
	//数据库主机不为空提示
	function showDbhostErr(){	
		var dbhost = $('#dbhost').val().replace(/[ ]/g,""); 
		if(dbhost==""){
			$('#dbhost_err').show("slow");
			return false;
		}else{
			$('#dbhost_err').hide("slow");
			return true;
		}
	}
	//数据库名提示不为空
	function showDbnameErr(){	
		var dbname = $('#dbname').val().replace(/[ ]/g,""); 
		if(dbname==""){
			$('#dbname_err').show("slow");
			return false;
		}else{
			$('#dbname_err').hide("slow");
			return true;
		}
	}
	//数据库用户名
	function showDbuserErr(){	
		var dbname = $('#dbuser').val().replace(/[ ]/g,""); 
		if(dbname==""){
			$('#dbuser_err').show("slow");
			return false;
		}else{
			$('#dbuser_err').hide("slow");
			return true;
		}
	}
	
	//标记四个输入框是否都走过
	var tem = 0;
	var temp = 0;
	function ajaxSubmit(data){
		if(data==1){
			tem = 1;
		}
		if(showDbhostErr()&&showDbnameErr()&&showDbuserErr()&&tem==1){
			$('#dbhost_ajax_err').hide("slow");
			$('#dbname_ajax_err').hide("slow");	
			$('#dbuser_ajax_err').hide("slow");
			$('#dbname_ajax_cover').hide("slow");
			$.ajax({
				type : "post",
				url : "${base_url}installJudgeServlet",
				dataType:'json',
				data : "dbhost="+$('#dbhost').val()+"&dbname="+$('#dbname').val()+"&dbuser="+$('#dbuser').val()+"&dbpwd="+$('#dbpwd').val(),
			    success: function(data){
			    	if(data.code==101){
			    		temp = 101;
			    		showFormMsg("铭飞文章管理系统",data.content);
			    	}else if(data.code==102){
			    		temp = 102;
			    		showFormMsg("铭飞文章管理系统",data.content);	
			    	}else if(data.code==103){
			    		temp = 103;
						showFormMsg("铭飞文章管理系统",data.content);	
					}else if(data.code==104){
						temp = 104;
						showFormMsg("铭飞文章管理系统",data.content);	
					}else if(data.code==105){
						temp = 105;
						$('#dbname_ajax_err').show("slow");	
					}else if(data.code==106){
						temp = 106;
						$('#dbuser_ajax_err').show("slow");	
					}else if(data.code==107){
						temp = 107;
						$('#dbhost_ajax_err').show("slow");
					}else if(data.code==0){
						temp = 108;
						$('#dbname_ajax_cover').show("slow");
					}
			    } 
			}); 
		}
	}
	
	/**
	*显示提示信息，应用与表单操作后成功或失败返回提示信息
	*/
	function showFormMsg(title,content) {
		 $.messager.show({
					title:title,
					msg:content,
					showType:'slide',
					style:{
						right:'',
						top:document.body.scrollTop+document.documentElement.scrollTop,
						bottom:''
					}
	     });
	}

	function forward(){
		var adminuser = $('#adminuser').val().replace(/[ ]/g,"");
		var adminpwd = $('#adminpwd').val().replace(/[ ]/g,"");
		$.ajax({
			type : "post",
			url : "${base_url}installThirdServlet",
			dataType:'json',
			data : "adminuser="+adminuser+"&adminpwd="+adminpwd,
		    success: function(data){
		    	if(data.code==109){
		    		window.location.href="${base_url}installStepServlet?step=3";
		    	}
		    } 
		});  
	}
	</script>
	</body>
</html>
