<?php include_once(mvc::$cfg['PATH_TPL'].'public/headBase.php');?>
<div class="layui-card">
	<div class="layui-card-body">
		<fieldset class="layui-elem-field">
			<legend>API 连通性测试</legend>
			<div class="layui-field-box">
				<p>测试 AccessKey：<code>LTAIlsNeXzn3cdyG</code>（与 OthAppPush 共用）</p>
				<p>测试账期：<code><?php echo $billCycle;?></code></p>
				<p class="gray">说明：本页面仅用于验证阿里云 BssOpenApi 是否能接通，正式功能待后续实现。</p>
			</div>
		</fieldset>

		<fieldset class="layui-elem-field">
			<legend>1. QueryAccountBalance（账户余额）</legend>
			<div class="layui-field-box">
				<?php if($balanceRes['status']){?>
					<p class="green">✓ 接口调用成功</p>
				<?php }else{?>
					<p class="red">✗ 接口调用失败 [<?php echo htmlspecialchars($balanceRes['code']);?>]</p>
					<p>错误信息：<?php echo htmlspecialchars($balanceRes['msg']);?></p>
				<?php }?>
				<pre class="layui-code"><?php echo htmlspecialchars(print_r($balanceRes, true));?></pre>
			</div>
		</fieldset>

		<fieldset class="layui-elem-field">
			<legend>2. QueryInstanceBill（实例账单明细 - <?php echo $billCycle;?>）</legend>
			<div class="layui-field-box">
				<?php if($instanceBillRes['status']){?>
					<p class="green">✓ 接口调用成功</p>
				<?php }else{?>
					<p class="red">✗ 接口调用失败 [<?php echo htmlspecialchars($instanceBillRes['code']);?>]</p>
					<p>错误信息：<?php echo htmlspecialchars($instanceBillRes['msg']);?></p>
				<?php }?>
				<pre class="layui-code"><?php echo htmlspecialchars(print_r($instanceBillRes, true));?></pre>
			</div>
		</fieldset>

		<fieldset class="layui-elem-field">
			<legend>历史数据同步（2024-01 至 <?php echo date('Y-m');?>）</legend>
			<div class="layui-field-box">
				<p>逐月同步 QueryInstanceBill 数据到 api_tongji.money_aliyun 表。</p>
				<p>每月同步前会先清除该账期旧数据，避免重复。同步过程请勿关闭页面。</p>
				<button class="layui-btn" id="btnSync">开始同步</button>
				<div id="syncLog" style="margin-top:10px;max-height:400px;overflow:auto;background:#f9f9f9;padding:10px;font-size:12px;border:1px solid #e6e6e6;"></div>
			</div>
		</fieldset>

	</div>
</div>
<?php include_once(mvc::$cfg['PATH_TPL'].'public/footBase.php');?>
<script>
$('#btnSync').on('click', function(){
	var months = [];
	var start = new Date('2024-01-01');
	var end   = new Date('<?php echo date('Y-m');?>-01');
	while (start <= end) {
		var y = start.getFullYear();
		var m = ('0' + (start.getMonth() + 1)).slice(-2);
		months.push(y + '-' + m);
		start.setMonth(start.getMonth() + 1);
	}
	var log = $('#syncLog');
	log.html('<p>共 ' + months.length + ' 个月待同步</p>');
	var btn = $(this);
	btn.attr('disabled', true).text('同步中...');
	var i = 0;
	function syncNext() {
		if (i >= months.length) {
			log.append('<p class="green" style="font-weight:bold;">========== 全部同步完成 ==========</p>');
			btn.attr('disabled', false).text('开始同步');
			return;
		}
		var m = months[i];
		log.append('<p>同步 ' + m + ' ...</p>');
		$.getJSON('<?php echo mvc::$cfg['ADMIN_URI'];?>sys/aliyunBssSync', {cycle: m, isAjax: 1}, function(res) {
			if (res.status) {
				log.append('<p class="green">  ✓ ' + m + ' 成功，入库 ' + res.count + ' 条（API共 ' + (res.total||0) + ' 条）</p>');
			} else {
				log.append('<p class="red">  ✗ ' + m + ' 失败：' + (res.msg || '未知错误') + '</p>');
			}
			log.scrollTop(log[0].scrollHeight);
			i++;
			syncNext();
		}).fail(function(xhr) {
			log.append('<p class="red">  ✗ ' + m + ' 请求失败：HTTP ' + xhr.status + '</p>');
			i++;
			syncNext();
		});
	}
	syncNext();
});
</script>
