<?php include_once "public.php";?>
<div class="row">
	<div class="col-sm-2">
		<div class="input-group form-group">
			<input type="text" id="addons_modal_search" value="<?php echo $search; ?>" placeholder="输入客户邮箱" class="form-control">
			<div class="input-group-btn">
				<input type="button" value="搜索" class="btn btn-primary"  onclick="_search('downreseller_reseller')">
			</div>
		</div>
	</div>
	<div class="col-sm-10">
		<div class="form-group">
			<button class="btn btn-success pull-right" onclick="addons_modal('addons_modal_add','show')" >添加代理商</button>
		</div>
	</div>
</div>
<div class="tablebg">
	<table id="sortabletbl0" class="datatable" width="100%" border="0" cellspacing="1" cellpadding="3">
		<tbody>
			<tr>
				<th>客户ID</th>
				<th>姓名</th>
				<th>邮箱</th>
				<th>API密钥</th>
				<th>消费（总计/余额）</th>
				<th>状态</th>
				<th width="20"></th>
				<th width="20"></th>
			</tr>
			<?php foreach($listing as $v){ ?>
			<tr data-clients='<?php echo json_encode($v); ?>'>
				<td><a href="clientssummary.php?userid=<?php echo $v['userid']; ?>"><?php echo $v['userid']; ?></a></td>
				<td><a href="clientssummary.php?userid=<?php echo $v['userid']; ?>"><?php echo $v['lastname'].$v['firstname']; ?></a></td>
				<td><a href="clientssummary.php?userid=<?php echo $v['userid']; ?>"><?php echo $v['email']; ?></a></td>
				<td><?php echo $v['apikey']; ?></td>			
				<td style="text-align:right;"><?php $total = empty($v['total'])?0.00:$v['total'];echo $total.'('.$v['credit'].')'; ?></td>
				<td><?php if($v['disabled']==1) echo '<span style="color:#f00;">禁用</span>'; else echo '<span style="color:#5cb85c;">启用</span>' ?></td>
				<td>
					<a class="addons_modal_edit" href="javascript:;"><img src="images/edit.gif" width="16" height="16" border="0" alt="修改"></a>
				</td>
				<td>					
					<a class="addons_modal_del"  href="javascript:;" ><img src="images/delete.gif" width="16" height="16" border="0" alt="删除"></a>
				</td>
			</tr>
			<?php } ?>
		</tbody>
	</table>
</div>
<div class="body" data-target=".body" data-spy="scroll" data-twttr-rendered="true" id="mg-wrapper">
	<div class="text-center">
		<ul class="pagination">
			<?php echo $page; ?>
		</ul>
	</div>
</div>

<!-- <script src="/modules/addons/idcsmart_reseller/tpl/assets/js/public.js"></script> -->
<!--添加代理-->
<div class="modal fade" id="addons_modal_add" role="dialog" aria-labelledby="ModuleSuspendLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content panel panel-primary">
            <div class="modal-header panel-heading">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">&times;</span>
                    <span class="sr-only">关闭</span>
                </button>
                <h4 class="modal-title">添加代理</h4>
            </div>
            <div class="modal-body panel-body">
                <form>
				<div class="row">
					<div class="col-sm-3"></div>
					<div class="margin-top-bottom-20 text-center col-sm-6">
						<div class="form-group">
							<p class="text-left">客户邮箱</p>					
							<input name="email" type="text" class="form-control input-inline input-300"/>
						</div>
						
						<p class="addons_modal_msg"></p>
					</div>
				</div>
				</form>
            </div>
            <div class="modal-footer panel-footer">
                <button type="button" onclick="_post('tclick','addons_modal_add','downreseller_resellerAdd')" class="btn btn-primary tclick"><div class="module_addons_loading"><span></span></div> <span class="display-block">确定</span></button>
				<button type="button"  class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
	</div>
</div>
<!--修改代理-->
<div class="modal fade" id="addons_modal_edit" role="dialog" aria-labelledby="ModuleSuspendLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content panel panel-primary">
            <div class="modal-header panel-heading">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">&times;</span>
                    <span class="sr-only">关闭</span>
                </button>
                <h4 class="modal-title">编辑代理</h4>
            </div>
            <div class="modal-body panel-body">
			<form>
			<input type="hidden" name="userid" value="">
			<div class="row">
				<div class="col-sm-3"></div>
                
				<div class="margin-top-bottom-20 text-center col-sm-6">
					<div class="form-group">
						<p class="text-left">API密钥</p>
						<div class="input-group form-group">						
							<input type="text" name="apikey" value="" class="form-control">
							<div class="input-group-btn">
								<input type="button" value="随机生成" class="btn btn-primary" id="apikey">
							</div>
						</div>
					</div>
					<div class="form-group">
						<p class="text-left">状态</p>	
						<select name="disabled" class="form-control input-inline input-300">
							<option value="1">禁用</option>
							<option value="0">启用</option>
						</select>
					</div>
					<p class="addons_modal_msg"></p>
				</div>
			</div>
			</form>
            </div>
            <div class="modal-footer panel-footer">
                <button type="button"  onclick="_post('tclick','addons_modal_edit','downreseller_resellerEdit')" class="btn btn-primary tclick"><div class="module_addons_loading"><span></span></div> <span class="display-block">确定</span></button>
				<button type="button"  class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
	</div>
</div>
<!--删除代理-->
<div class="modal fade" id="addons_modal_del" role="dialog" aria-labelledby="ModuleSuspendLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content panel panel-primary">
            <div class="modal-header panel-heading">
                <button type="button" class="close" data-dismiss="modal">
                    <span aria-hidden="true">&times;</span>
                    <span class="sr-only">关闭</span>
                </button>
                <h4 class="modal-title">删除代理</h4>
            </div>
            <div class="modal-body panel-body">
                <form>
				<input type="hidden" name="userid" value="">
				<div class="row">
					<div class="col-sm-3"></div>
					<div class="margin-top-bottom-20 text-center col-sm-6">
						<p>确定删除代理？</p>
						<div class="form-group addons_modal_del_content">
							
						</div>
						<p style="color:#f00;">说明：这里只会删除代理的API信息</p>
						<p class="addons_modal_msg"></p>
					</div>
				</div>
				</form>
            </div>
            <div class="modal-footer panel-footer">
                <button type="button" onclick="_post('tclick','addons_modal_del','downreseller_resellerDel')" class="btn btn-primary tclick"><div class="module_addons_loading"><span></span></div> <span class="display-block">确定</span></button>
				<button type="button"  class="btn btn-default" data-dismiss="modal">取消</button>
            </div>
        </div>
	</div>
</div>
<script type="text/javascript">
	function _randomString(e) {  
	  e = e || 32;
	  var t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz0123456789",
	  a = t.length,
	  n = "";
	  for (i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a));
	  return n;
	}

	$(function(){
		//编辑代理商
		$(".addons_modal_edit").click(function(){		
			$("#addons_modal_edit").modal("show");
			var clients=$(this).parent().parent().data("clients");
			$("#addons_modal_edit input[name='apikey']").val(clients.apikey);
			$("#addons_modal_edit input[name='userid']").val(clients.userid);
			$("#addons_modal_edit select[name='disabled'] option").each(function(index){
				if($(this).attr("value")==clients.disabled){
					$(this).prop("selected",true);
				} 
			});		
		});
		$("#apikey").click(function(){
			$("#addons_modal_edit input[name='apikey']").val(_randomString(20));
		});
		//删除代理商
		$(".addons_modal_del").click(function(){		
			$("#addons_modal_del").modal("show");
			var clients=$(this).parent().parent().data("clients");
			$("#addons_modal_del input[name='userid']").val(clients.userid);
			$("#addons_modal_del .addons_modal_del_content").html("<p>客户姓名:"+clients.lastname+clients.firstname+"</p><p>客户邮箱:"+clients.email+"</p>");	
		});
	});
	function addons_modal(id,status,data){	
		$("#"+id).modal(status);
	}
	function _post(tclick,tag,action){
		var _this = $("#"+tag+" ."+tclick);
		_this.data("submit","submit");
		$(".module_addons_loading").show();
		$.post("addonmodules.php?module=idcsmart_reseller&module_addons="+action,$("#"+tag+" form").serialize(), function(data){
			$(".module_addons_loading").hide();
			if(data.status==200){
				$("#"+tag+" .addons_modal_msg").html('<span style="color:#5cb85c;">'+data.msg+'</span>');
				setTimeout(function(){
					$("#"+tag).modal('hide');
					window.location.reload();
				},1500);
				
			}else{
				$("#"+tag+" .addons_modal_msg").html('<span style="color:#f00;">'+data.msg+'</span>');
			}
			_this.removeData("submit");
		}, "json")
	}
	function _search(action){
		window.location.href="addonmodules.php?module=idcsmart_reseller&module_addons="+action+'&search='+$("#addons_modal_search").val();
	}


</script>