<div class="opwindow">
	<form name="theform2" id="theform2" method="post" action="{echo \befen\get_url()}">
	<div class="layui-form layui-row">
		<div class="layui-form-item">
			<div class="layui-col layui-col-md12">
				<label>商户名称</label> <span class="red">*</span>
				<div class="layui-input-inline" style="width:100%;">
					<input type="text" name="merchant_name" id="merchant_name" class="layui-input" value="" placeholder="商户名称">
				</div>
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-col layui-col-md4">
				<label>商户类型</label> <span class="red">*</span>
				<div class="layui-input-inline layui-input-wdith">
					<select name="merchant_type" id="merchant_type" class="layui-select">
						<option value="">选择商户类型</option>
						<!--{loop model('Merchant')->get_type() $key $val}-->
						<option value="{$key}">{$val}</option>
						<!--{/loop}-->
					</select>
				</div>
			</div>
			<div class="layui-col layui-col-md8">
				<label>经营类目</label> <span class="red">*</span>
				<div class="layui-input-inline" style="width:100%;">
					<select name="mcc_code" id="mcc_code" class="layui-select" lay-verify="required" lay-search="">
						<option value="">请选择经营类目</option>
						<!--{loop $mcc_list $key $val}-->
						<option value="{$key}">{$val}</option>
						<!--{/loop}-->
					</select>
				</div>
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-col layui-col-md4">
				<label>支付宝账号</label> <span class="red">*</span>
				<input type="text" name="alipay" id="alipay" class="layui-input" value="" placeholder="">
			</div>
			<div class="layui-col layui-col-md4">
				<label>营业执照号码</label>
				<input type="text" name="license_number" id="license_number" class="layui-input" value="" placeholder="">
			</div>
			<div class="layui-col layui-col-md4">
				<label>商户签约费率</label> <span class="red">*</span>
				<input type="number" name="trade_rates" id="trade_rates" class="layui-input" value="0.00" placeholder="" min="0.3" max="0.6" step="0.01">
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-col layui-col-md4">
				<label>负责人名称</label> <span class="red">*</span>
				<input type="text" name="per_name" id="per_name" class="layui-input" value="" placeholder="">
			</div>
			<div class="layui-col layui-col-md4">
				<label>负责人邮箱</label> <span class="red">*</span>
				<input type="text" name="per_email" id="per_email" class="layui-input" value="" placeholder="">
			</div>
			<div class="layui-col layui-col-md4">
				<label>负责人电话</label> <span class="red">*</span>
				<input type="text" name="per_phone" id="per_phone" class="layui-input" value="" placeholder="">
			</div>
		</div>
		<div class="layui-form-item">
			<div class="layui-col layui-col-md2">
				<label>营业执照</label>
				<div class="mch-box">
					<img id="license_copy" class="mch-img" src="">
					<input type="hidden" name="license_copy" class="mch-obj" value="">
				</div>
			</div>
			<div class="layui-col layui-col-md2">
				<label>授权函</label>
				<div class="mch-box">
					<img id="license_auth" class="mch-img" src="">
					<input type="hidden" name="license_auth" class="mch-obj" value="">
				</div>
			</div>
			<div class="layui-col layui-col-md2">
				<label>商户门头照片</label> <span class="red">*</span>
				<div class="mch-box">
					<img id="store_door" class="mch-img" src="">
					<input type="hidden" name="store_door" class="mch-obj" value="">
				</div>
			</div>
			<div class="layui-col layui-col-md2">
				<label>店内环境照片</label>
				<div class="mch-box">
					<img id="store_inside" class="mch-img" src="">
					<input type="hidden" name="store_inside" class="mch-obj" value="">
				</div>
			</div>
			<div class="layui-col layui-col-md2">
				<label>特殊资质证明</label>
				<div class="mch-box">
					<img id="qualifications" class="mch-img" src="">
					<input type="hidden" name="qualifications" class="mch-obj" value="">
				</div>
			</div>
		</div>
		<!--{if !isset($value['order_status']) || in_array($value['order_status'], ['', 'MERCHANT_INFO_HOLD', 'MERCHANT_CONFIRM_TIME_OUT', 'MERCHANT_APPLY_ORDER_CANCELED'])}-->
		<div class="layui-form-item">
			<div class="layui-col layui-col-md12" style="text-align:right;">
				<button type="submit" name="save" value="save" id="save" class="layui-btn">保存</button>
				<!--{if request()->action() == 'merchant_detail'}-->
				<button type="submit" name="submit2" value="submit2" id="submit2" class="layui-btn">提交申请</button>
				<!--{/if}-->
				<button type="button" id="business_license" class="layui-btn layui-btn-normal">自动识别营业执照</button>
			</div>
		</div>
		<!--{/if}-->
	</div>
	</form>
</div>
<link rel="stylesheet" href="//at.alicdn.com/t/font_1614161_8o9u8s6h5fn.css" />
<style>
.layui-btn-download {width:96px;margin-top:5px;}
.mch-box { position:relative;width:96px;height:96px; }
.mch-box .mch-img { width:100%;height:100%;cursor:pointer; }
.mch-box .mch-view {
	position:absolute;
	top:0px;
	left:0px;
	width:100%;
	height:100%;
	background-color:rgba(0, 0, 0, .5);
	padding-top:35px;
	text-align:center;
	display:none;
	box-sizing:border-box;
	-moz-box-sizing:border-box;
	-webkit-box-sizing:border-box;
}
.mch-box .mch-view .iconfont {color:#FFF;font-size:24px;margin:0 3px;}
</style>
<script type="text/javascript">
var merchant_id = "{echo input('param.merchant_id')}";
init_Form();
$(".mch-box").each(function(index){
	var mch_box = $(this);
	var mch_img = $(this).find(".mch-img");
	var mch_obj = $(this).find(".mch-obj");
	mch_box.hover(function(){
		if($(this).hasClass("hasImg")) {
			$(this).find(".mch-view").fadeIn(200);
		}
	}, function(){
		$(this).find(".mch-view").hide();
	});
	if(mch_obj.val()) {
		mch_box.addClass("hasImg");
		mch_img.attr("src", mch_obj.val());
	} else {
		mch_box.removeClass("hasImg");
		mch_img.attr("src", "/public/system/add-img.png");
	}
	add_upload(mch_img);
	mch_box.append('<p class="mch-view"><a href="javascript:;" class="iconfont icon-browse" title="预览"></a><a href="javascript:;" class="iconfont icon-ashbin" title="删除"></a></p>');
});
$(".icon-ashbin").click(function(){
	$(this).parent().hide();
	$(this).parent().parent().removeClass("hasImg");
	$(this).parent().parent().find(".mch-obj").val("");
	$(this).parent().parent().find(".mch-img").attr("src", "/public/system/add-img.png");
});
$(".icon-browse").click(function(){
	$(this).parent().hide();
	layer.photos({
		anim: 5,
		photos: {
			title: "",
			id: 0,
			start: 0,
			data: [
				{
					alt: "",
					pid: 0,
					src: $(this).parent().parent().find(".mch-obj").val(),
					thumb: $(this).parent().parent().find(".mch-obj").val(),
				}
			]
		}
	});
});
function add_upload(elem) {
	if(!elem) {
		elem = '.item-upload';
	}
	init_upload({
		elem: elem,
		url: "{echo url('merchant/upload', ['merchant_id' => input('param.merchant_id')])}",
		exts: "png|jpg",
		acceptMime: "image/*",
		callback: function(item, data, index, upload){
			//console.log(item);
			//console.log(data);
			if(data.status == 0) {
				showAlert(data.message);
			} else {
				item.parent().addClass("hasImg");
				item.attr("src", data.message);
				item.parent().find(".mch-obj").val(data.message);
			}
		}
	});
}
$("#save").click(function(){
	if(!$('#theform2 input[name=merchant_id]').val()) {
		showAlert("请选择商户");
		return false;
	}
	ajaxSubmit(this, "", "#theform2", function(){
		<!--{if request()->action() == 'merchant_add'}-->
		reload();
		<!--{/if}-->
	});
});
$("#submit2").click(function(){
	if(!$('#theform2 input[name=merchant_id]').val()) {
		showAlert("请选择商户");
		return false;
	}
	ajaxSubmit(this, "", "#theform2", function(){
		<!--{if request()->action() == 'merchant_detail'}-->
		reload();
		<!--{/if}-->
	});
});
$('#theform2 input[name=merchant_name]').autoComplete({
	url: "{echo url('merchant/check')}",
	field: "",
	value: "{echo isset($value['merchant_name']) ? $value['merchant_name'] : input('param.merchant_name')}",
	name_hidden: "merchant_id",
	value_hidden: "{echo isset($value['merchant_id']) ? $value['merchant_id'] : input('param.merchant_id')}",
}, function(o, _self, _target){
	if(_target.val()) {
		_self.attr("readonly", "readonly").addClass("layui-disabled");
		merchant_id = _target.val();
		$.ajax({
			type: "POST",
			dataType: "json",
			cache: false,
			timeout: 10000,
			url: "{echo url('PayAlipay/get_mch_info')}",
			data: {
				ajax: "json",
				merchant_id: merchant_id,
			},
			success: function(data){
				$("#merchant_name").val(data.contents.merchant_name);
				$("#merchant_type option:contains('" + data.contents.merchant_type + "')").attr("selected", "selected");
				$("#mcc_code").val(data.contents.mcc_code);
				$("#alipay").val(data.contents.alipay);
				$("#license_number").val(data.contents.license_number);
				$("#trade_rates").val(get_rates(data.contents.trade_rates));
				$("#per_name").val(data.contents.per_name);
				$("#per_email").val(data.contents.per_email);
				$("#per_phone").val(data.contents.per_phone);
				init_Form('select');
				if(data.contents.license_copy) {
					$("#license_copy").parent().addClass("hasImg");
					$("#license_copy").attr("src", data.contents.license_copy);
					$("input[name='license_copy']").val(data.contents.license_copy);
				} else {
					$("#license_copy").parent().removeClass("hasImg");
					$("#license_copy").attr("src", "/public/system/add-img.png");
					$("input[name='license_copy']").val("");
				}
				if(data.contents.license_auth) {
					$("#license_auth").parent().addClass("hasImg");
					$("#license_auth").attr("src", data.contents.license_auth);
					$("input[name='license_auth']").val(data.contents.license_auth);
				} else {
					$("#license_auth").parent().removeClass("hasImg");
					$("#license_auth").attr("src", "/public/system/add-img.png");
					$("input[name='license_auth']").val("");
				}
				if(data.contents.store_door) {
					$("#store_door").parent().addClass("hasImg");
					$("#store_door").attr("src", data.contents.store_door);
					$("input[name='store_door']").val(data.contents.store_door);
				} else {
					$("#store_door").parent().removeClass("hasImg");
					$("#store_door").attr("src", "/public/system/add-img.png");
					$("input[name='store_door']").val("");
				}
				if(data.contents.store_inside) {
					$("#store_inside").parent().addClass("hasImg");
					$("#store_inside").attr("src", data.contents.store_inside);
					$("input[name='store_inside']").val(data.contents.store_inside);
				} else {
					$("#store_inside").parent().removeClass("hasImg");
					$("#store_inside").attr("src", "/public/system/add-img.png");
					$("input[name='store_inside']").val("");
				}
				if(data.contents.qualifications) {
					$("#qualifications").parent().addClass("hasImg");
					$("#qualifications").attr("src", data.contents.qualifications);
					$("input[name='qualifications']").val(data.contents.qualifications);
				} else {
					$("#qualifications").parent().removeClass("hasImg");
					$("#qualifications").attr("src", "/public/system/add-img.png");
					$("input[name='qualifications']").val("");
				}
			},
			error: function(xhr, status){
				console.log(status);
				console.log(xhr);
				showAlert("获取商户信息失败");
			},
			complete: function(){
				hideLoader();
			},
			beforeSend: function(){
				showLoader();
			}
		});
	}
});
$("#business_license").click(function(){
	ocr("business_license", null, $("input[name='license_copy']").val());
});
function ocr(type, side, filename) {
	$.ajax({
		type: "POST",
		dataType: "json",
		cache: false,
		timeout: 20000,
		url: "{echo url('PayAlipay/get_ocr_body')}",
		data: {
			ajax: "json",
			type: type,
			side: side,
			filename: filename,
		},
		success: function(data){
			//console.log(data);
			if(data.status != 1) {
				showAlert(data.message);
			} else {
				if(type == "business_license") {
					$("#license_number").val(data.contents.reg_num);
					if(data.contents.name != $("#merchant_name").val()) {
						var merchant_name = data.contents.name;
						layer.open({
							anim: 0,
							closeBtn: 0,
							btn: ['确认', '取消'],
							content: "商户名称与营业执照不一致，确认自动更正！",
							yes: function(index, layero){
								$.ajax({
									type: "POST",
									dataType: "json",
									cache: false,
									timeout: 10000,
									url: "{echo url('merchant/set_name')}",
									data: {
										ajax: "json",
										merchant_id: merchant_id,
										merchant_name: merchant_name,
									},
									success: function(data){
										layer.close(index);
										$("#merchant_name").val(merchant_name);
									},
									error: function(xhr, status){
										console.log(status);
										console.log(xhr);
									}
								});
							},
							btn2: function(index, layero){
								layer.close(index);
							},
							cancel: function(index, layero){
								layer.close(index);
							}
						});
					}
				}
				if(type == "idcard") {
					if(side == "face") {
						$("#id_card_name").val(data.contents.name);
						$("#id_card_number").val(data.contents.num);
					} else {
						$("#card_period_begin").val(data.contents.start_date);
						$("#card_period_end").val(data.contents.end_date);
					}
				}
			}
		},
		error: function(xhr, status){
			console.log(status);
			console.log(xhr);
			showAlert("系统错误");
		},
		complete: function(){
			hideLoader();
		},
		beforeSend: function(){
			showLoader();
		}
	});
}
</script>
