$(function(){
	var user = fetchUSER();

	fetchMerch = function(){ // 提取商户信息
		if(user.hostId==''||user.hostId=='0'){
			gotoLogin();
			return;
		}
		$("#merchId").val(user.hostId);

		if(user.utype=='2'){ // 当前登陆人是商户
			var _path=buildHTTP("/ebus/merchant/fetch.shtml?merchId="+user.hostId);
			$.ajax({
				url: _path, 
				type: "get", 
				dataType: "json",
				contentType:"application/json",
				success: function(data) { 
					if(data.code=='01'){
						var merch = data.content;
						$("#merchId").val(merch.merchId);
						$("#name").val(merch.name);
						$("#link").val(merch.link);
						$("#phone").val(merch.phone);
						$("#payType").val(merch.payType);
						$("#payAcct").val(merch.payAcct);
						$("#info").val(merch.info);
						
						var vhtml = "您已经开通有店铺，以下是您的店铺基本信息<br>";
						if(merch.status=='1'){
							vhtml +="店铺状态：<b>正常</b>";
						}
						else{
							vhtml +="店铺状态：<b class='red-span'>锁定</b>";
						}
						vhtml +="&nbsp;&nbsp;"+merch.degree+"级";
						
						$("#_tip").html(vhtml);
					}else{
						$("#_tip").html("没有任何店铺信息，请通过以下方式申请");
					}
				}
			});
		}
	};

	fetchMerch();

	$("#saveBtn").click(function(){

		var merchId = $("#merchId").val();
		var name=$("#name").val();
		var link=$("#link").val();
		var phone=$("#phone").val();
		var payType=$("#payType").val();
		var payAcct=$("#payAcct").val();
		var info=$("#info").val();

		var	_httpURL = buildHTTP("/ebus/merchant/save.shtml");
		if(name==''||link==''||phone==''||payAcct==''||info==''){
			$.toast("商户信息为空，请检查");
			return;
		}

		$.confirm("确认提交当前申请？", function(){
			var params={
					merchId:merchId,
					name:name,
					link:link,
					phone:phone,
					payType:payType,
					payAcct:payAcct,
					info:info,
			};
			$.ajax({
				url: _httpURL, 
				type: "post", 
				dataType: "json",
				data:$.toJSON(params),   
				contentType:"application/json",
				success: function(data) { 
					if(data.code=='01'){
						$.toast("保存成功");
					}else{
						$.toast(data.msg);
					}
				}
			});
		});
	});

	$.init();
});