<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>{echo admin_title()}</title>
	{template _header}
</head>
<body>
	<div class="weui-tab">
		<div class="weui-tab__panel">
			<div class="filter">
				<a href="javascript:;" id="datePickerBtn">
					<span class="date">{echo input('param.time_create') ? gsdate('Y年n月j日', gstime(input('param.time_create'))) : '选择日期';}</span> <i class="iconfont icon-unfold"></i>
				</a>
				<a href="javascript:;" id="pickerBtn" style="float:right">{$selected_merchant[merchant_name]}<span></span></a>
			</div>
			<div class="weui-cells"></div>
			<div class="more" id="more"></div>
		</div>
		{template _main_nav}
	</div>

	<script>
	//全局请求参数
	var filter = {};
	location.search && location.search.substr(1).split('&').map(param => {
		param = param.split('=');
		filter[param[0]] = param[1]
	})

	$('#datePickerBtn').click(function(){
		weui.datePicker({
			start: {echo $start_year ?? gsdate('Y')},
			end: {echo gsdate('Y')},
			defaultValue: [{echo input('param.time_create') ? gsdate('Y', gstime(input('param.time_create'))) : gsdate('Y')}, {echo input('param.time_create') ? gsdate('n', gstime(input('param.time_create'))) : gsdate('n')}, {echo input('param.time_create') ? gsdate('j', gstime(input('param.time_create'))) : gsdate('j')}],
			onChange: function(result){},
			onConfirm: (result)=>{
				var date = [result[0].label, result[1].label, result[2].label].join('');
				$(this).children('span.date').text(date)
				let time_create = [result[0].value, result[1].value, result[2].value].join('-');
				filter.time_create = time_create;
				reload();
			},
			id: 'datePicker'
		});
	})
	//自动加载
	var isloading = 0;
	var load_more = () => {
		if(filter.page == 'last' || isloading) return false;
		filter.page = filter.page ? filter.page + 1 : 1;
		$('#more').text('加载中...');
		isloading = 1;
		$.post('/mo/trade/profit', filter, (result) => {
			isloading = 0;
			if(result.status == 0){
				alert(result.message);
				return false;
			}
			$('#more').text('加载更多');
			if(result.contents.total == 0){
				filter.page = 'last';
				$('#more').text('暂无数据');
			}
			if(result.contents.last_page <= filter.page){
				filter.page = 'last';
				$('#more').text('~没有更多了');
			}
			let list = [];
			result.contents.list.map((v, i) => {
				list.push([
					'<div class="weui-cell weui-cell_trade">',
					'<div class="weui-cell__hd ' + ({alipay:'ali', weixin:'wx'}[v['trade_gate']] || '') + '"></div>',
					'<div class="weui-cell__bd">',
					'<p>' + v['merchant_name'] + '<br><span>' + v['time_create'] + '</span></p>',
					'</div>',
					'<div class="weui-cell__ft">' + v['commission'] + '<br><span ' + (v['time_status'] != 0 ? 'style="color:#06AE56"' : '') + '>' + v['time_status_text'] + '</span></div>',
					'</div>'
				].join(''));
			});
			$('.weui-tab__panel').children('.weui-cells').append(list.join(''));
			$('#datePickerBtn .sum').remove();
			if(result.contents.sum !== undefined){
				$('#datePickerBtn').append('<span class="sum" style="display: block; font-size: 13px; color: rgba(0,0,0,.5);">收入 ￥' + result.contents.sum + '</span>');
			}
		})
	}

	var reload = ()=>{
		filter.page = 0;
		$('.weui-tab__panel').children('.weui-cells').empty();
		load_more();
	}

	$('.weui-tab__panel').scroll(function(e){
		if( this.scrollHeight <= $(this).height() + $(this).scrollTop() + 1 ){//加一容错
			load_more();
		}
	})
	$('#more').click(load_more)
	load_more();
	</script>
</body>
</html>