<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<title>{echo admin_title()}</title>
<!--header-->
{template _:header}
</head>
<body>
<div class="layui-layout layui-layout-admin">
<!--top-->
{template _main_top}
<!--nav-->
{template _main_nav}
	<div class="position"><h1>代理列表</h1></div>
	<div class="layui-body admin-body">
		<div class="admin-content">
			<div class="searcher">
				<div class="right"><button type="button" class="layui-btn layui-add">添加代理</button></div>
				<form name="sch_form" id="sch_form" method="get" action="{echo \befen\get_url()}">
					<input type="hidden" name="up_id" id="up_id" value="{echo input('param.up_id')}">
					<div class="layui-input-inline"><input type="text" name="agent_name" id="agent_name" class="layui-input" value="{echo input('param.agent_name')}" placeholder="代理编号或名称"></div>
					<div class="layui-input-inline layui-left-margin"><button type="submit" id="search" class="layui-btn">查询</button></div>
					<div class="layui-input-inline layui-left-margin"><button type="submit" name="action" value="export" class="layui-btn">导出</button></div>
				</form>
				<div class="clear"></div>
			</div>
			<form name="theform" id="theform" method="post" action="{echo \befen\get_url()}">
			<table class="layui-table">
				<thead>
					<tr>
						<th>代理名称</th>
						<th>代理编号</th>
						<th>联系电话</th>
						<th>代理级别</th>
						<th>代理费</th>
						<th>代理状态</th>
						<th>微信绑定</th>
						<th>管理操作</th>
					</tr>
				</thead>
				<tbody>
					<!--{loop $list $key $val}-->
					<tr id="">
						<td><a class="goto_panel" href="javascript:;" agent_id="{$val[agent_id]}" agent_url="{echo url('/agent/login/console', ['agent_id' => '__authid__'])}">{$val[agent_name]}</a></td>
						<td>{$val[agent_no]}</td>
						<td>{$val[per_phone]}</td>
						<td class="op">{echo $this->AgentLevel[$val['level_id']]['level_name']}</td>
						<td>
						<!--{if $val['join_cost']}-->
						<span op="join_cost" val="{$val[agent_id]}" class="layui-badge layui-bg-cyan">{$val[join_cost]}(实缴)</span>
						<!--{else}-->
						<span op="join_cost" val="{$val[agent_id]}" class="layui-badge layui-bg-gray">{echo $this->AgentLevel[$val['level_id']]['join_cost']}(默认)</span>
						<!--{/if}-->
						</td>
						<td class="op"><span op="agent_status" val="{$val[agent_status]}" lay-url="{echo url('agent/status', ['agent_id' => $val['agent_id']])}" class="layui-badge layui-bg-status-{$val[agent_status]}">{echo model('Agent')->getStatus($val[agent_status])}</span></td>
						<td id="weixin_{$val[agent_id]}"><!--{if $val['openid']}--><!--{if $val['nickname']}-->{$val[nickname]}<!--{else}-->是<!--{/if}--><!--{else}-->-<!--{/if}--></td>
						<td class="op">
							<a op="detail" val="{$val[agent_id]}" lay-url="{echo url('agent/detail', ['agent_id' => $val['agent_id']])}" class="layui-btn layui-btn-xs layui-btn-normal">代理详情</a>
							<a op="passwd" val="{$val[agent_id]}" lay-url="{echo url('agent/passwd', ['agent_id' => $val['agent_id']])}" class="layui-btn layui-btn-xs layui-btn-normal">重置密码</a>
							<!--{if !$val['openid']}-->
							<a op="bind_wechat" agent_id="{$val[agent_id]}" agent_name="{$val[agent_name]}" lay-url="{echo url('agent/bind_wechat', ['agent_id' => $val['agent_id']])}" class="layui-btn layui-btn-xs layui-btn-normal">绑定微信</a>
							<!--{else}-->
							<a op="unbind_wechat" agent_id="{$val[agent_id]}" agent_name="{$val[agent_name]}" lay-url="{echo url('agent/unbind_wechat', ['agent_id' => $val['agent_id']])}" class="layui-btn layui-btn-xs layui-btn-normal">解绑微信</a>
							<!--{/if}-->
							<a op="subagent" val="{$val[agent_id]}" lay-url="{echo url('agent/index', ['up_id' => $val['agent_id']])}" class="layui-btn layui-btn-xs layui-btn-normal">子代理</a>
							<a op="merchant" val="{$val[agent_id]}" lay-url="{echo url('merchant/index', ['agent_id' => $val['agent_id']])}" class="layui-btn layui-btn-xs layui-btn-normal">商户列表</a>
						</td>
					</tr>
					<!--{/loop}-->
				</tbody>
			</table>
			</form>
			<div class="listpages">{$pagenav}</div>
		</div>
	</div>
</div>
<!--footer-->
{template _:footer}
<style>
.layui-badge {cursor:pointer;}
</style>
<script type="text/javascript">
$(".layui-add").click(function(){
	showWindow('添加代理', '{echo url('agent/add')}', '800px');
});
$("a[op=detail]").click(function(){
	showWindow('代理详情', $(this).attr('lay-url'), '800px');
});
$("a[op=passwd]").click(function(){
	showWindow('重置密码', $(this).attr('lay-url'), '300px');
});
$("a[op=bind_wechat]").click(function(){
	var agent_id = $(this).attr('agent_id');
	var agent_name = $(this).attr('agent_name');
	$.ajax({
		type: "POST",
		dataType: "html",
		timeout: 30000,
		url: $(this).attr('lay-url'),
		data: {
			ajax: "html",
			agent_id: agent_id,
		},
		success: function(html){
			init_Layer(function(){
				layer.open({
					title: '微信绑定 - ' + agent_name,
					shade: 0.5,
					isOutAnim: true,
					content: '<div style="text-align:center"><div id="bind_wechat_qrcode" style="width:250px;height:250px;margin:0 auto 1em;"></div></div>',
					btn: false
				});
				var qrcode = new QRCode(document.getElementById("bind_wechat_qrcode"), {
					width: 250,
					height: 250
				});
				qrcode.makeCode(html);
			});
		},
		complete: function(){
			hideLoader();
		},
		beforeSend: function(){
			showLoader();
		}
	});
});
$("a[op=unbind_wechat]").click(function(){
	var agent_id = $(this).attr('agent_id');
	op("确认要解绑吗？", $(this).attr('lay-url'), function(){
		reload();
	});
});
$("a[op=subagent]").click(function(){
	gotourl($(this).attr('lay-url'));
});
$("a[op=merchant]").click(function(){
	gotourl($(this).attr('lay-url'));
});
$("span[op=agent_status]").click(function(){
	var status = $(this).attr("val");
	if(status == 0) {
		op_msg = "开启";
	} else {
		op_msg = "禁用";
	}
	op("确认 " + op_msg + " 代理状态吗？", $(this).attr('lay-url'));
});
$(".goto_panel").click(function(e){
	e.preventDefault();
	e.stopPropagation();
	var _window = window.open("about:blank", "_blank");
	var agent_id = $(this).attr("agent_id");
	var agent_url = $(this).attr("agent_url");
	$.ajax({
		type: "POST",
		dataType: "json",
		timeout: 10000,
		url: "{echo url('agent/get_id_authcode')}",
		data: {
			ajax: "json",
			agent_id: agent_id,
		},
		success: function(data){
			_window.blur();
			_window.opener.focus();
			_window.location = agent_url.replace("__authid__", data.contents.agent_id);
		},
		error: function(xhr, status){
			console.log(status);
			console.log(xhr);
		},
		complete: function(){
			hideLoader();
		},
		beforeSend: function(){
			showLoader();
		}
	});
});
$("span[op=join_cost]").click(function(){
	var agent_id = $(this).attr("val");
	var value = $(this).text().replace(/[^\d.]/g, "");
	init_Layer(function(){
		layer.prompt({
			title: "请输入实缴代理费",
			value: value,
		}, function(value, index){
			$.ajax({
				type: "POST",
				dataType: "json",
				cache: false,
				timeout: 30000,
				url: "{echo url('agent/join_cost')}",
				data: {
					ajax: "json",
					agent_id: agent_id,
					join_cost: value,
				},
				success: function(data){
					if(data.status == 1) {
						showInfo(data.message, function(){
							reload();
						});
					} else {
						showAlert(data.message);
					}
				},
				error: function(xhr, status){
					console.log(xhr);
				},
				complete: function(){
					hideLoader();
				},
				beforeSend: function(){
					showLoader();
				}
			});
		});
	});
});
$('#sch_form input[name=agent_name]').autoComplete({
	url: "{echo url('agent/index')}",
	field: "agent_no",
	value: "{echo input('param.agent_name')}",
	name_hidden: "agent_id",
	value_hidden: "{echo input('param.agent_id')}",
});
</script>
</body>
</html>