<?php
$title='监控管理';
include __DIR__ . '/head.php';
?>

<div class="p-4 md:p-6 space-y-6">
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
        <div class="p-4 md:p-6 border-b border-gray-200">
            <h2 class="text-base md:text-lg font-semibold text-gray-800">监控设置</h2>
        </div>
        <div class="p-4 md:p-6">
            <form onsubmit="saveSetting(this);return false;" method="post" class="space-y-6">
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">监控密钥</label>
                    <div class="md:col-span-3">
                        <input type="text" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm" name="cronkey" value="<?php echo $conf['cronkey']?>">
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">监控框架QQ状态</label>
                    <div class="md:col-span-3 flex items-center gap-2">
                        <input type="number" class="flex-1 px-3 py-2 border border-gray-300 rounded-lg text-sm" name="get_frame_qq_time" value="<?php echo isset($conf_frame['get_frame_qq_time']) ? intval($conf_frame['get_frame_qq_time']) : 60; ?>" min="10" max="600">
                        <span class="text-sm text-gray-600">秒</span>
                    </div>
                </div>
                <p class="text-xs text-gray-500 md:col-start-2 md:col-span-3">建议10-60秒，频率太快可能会导致框架卡顿闪退</p>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">秒赞任务监控频率</label>
                    <div class="md:col-span-3 flex items-center gap-2">
                        <input type="number" class="flex-1 px-3 py-2 border border-gray-300 rounded-lg text-sm" name="qzone_cron_interval" value="<?php echo isset($conf['qzone_cron_interval']) ? intval($conf['qzone_cron_interval']) : 60; ?>" min="10" max="3600">
                        <span class="text-sm text-gray-600">秒</span>
                    </div>
                </div>
                <p class="text-xs text-gray-500 md:col-start-2 md:col-span-3">建议30-120秒，避免触发过于频繁</p>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">秒赞多线程</label>
                    <div class="md:col-span-3 flex items-center gap-4">
                        <label class="inline-flex items-center gap-2 cursor-pointer">
                            <input type="hidden" name="qzone_multithread" value="0">
                            <input type="checkbox" name="qzone_multithread" value="1" class="rounded border-gray-300" <?php echo (isset($conf['qzone_multithread']) && $conf['qzone_multithread'] == '1') ? 'checked' : ''; ?>>
                            <span class="text-sm text-gray-600">开启多线程</span>
                        </label>
                    </div>
                </div>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">线程数</label>
                    <div class="md:col-span-3 flex items-center gap-2">
                        <input type="number" class="flex-1 px-3 py-2 border border-gray-300 rounded-lg text-sm" name="qzone_thread_count" value="<?php echo isset($conf['qzone_thread_count']) ? intval($conf['qzone_thread_count']) : 4; ?>" min="2" max="16">
                        <span class="text-sm text-gray-600">个</span>
                    </div>
                </div>
                <p class="text-xs text-gray-500 md:col-start-2 md:col-span-3">2-16，开启多线程时有效</p>
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-center">
                    <label class="text-xs md:text-sm font-medium text-gray-700">每线程运行账号数</label>
                    <div class="md:col-span-3 flex items-center gap-2">
                        <input type="number" class="flex-1 px-3 py-2 border border-gray-300 rounded-lg text-sm" name="qzone_accounts_per_thread" value="<?php echo isset($conf['qzone_accounts_per_thread']) ? intval($conf['qzone_accounts_per_thread']) : 0; ?>" min="0" max="500">
                        <span class="text-sm text-gray-600">个</span>
                    </div>
                </div>
                <p class="text-xs text-gray-500 md:col-start-2 md:col-span-3">0=无限制（处理所有账号）。多线程模式下为每线程账号数；非多线程模式下为每轮最大处理账号数</p>
                <div class="pt-4 border-t border-gray-200">
                    <button type="submit" class="w-full md:w-auto px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition text-sm">
                        保存
                    </button>
                </div>
            </form>
        </div>
    </div>

    <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
        <div class="p-4 md:p-6 border-b border-gray-200">
            <h2 class="text-base md:text-lg font-semibold text-gray-800">监控列表</h2>
        </div>
        <div class="p-4 md:p-6">
            <div class="mb-6 p-4 bg-gradient-to-r from-green-400 to-cyan-400 rounded-lg">
                <p class="text-sm font-medium text-white">
                    <i class="fas fa-info-circle mr-2"></i>
                    有些任务需要定时访问某个网址才能正常运行。如果您是用【宝塔】搭建网站的,可在【计划任务-任务类型-访问URL】把下方的网址添加到宝塔里即可。或者用其他的定时访问RUL的软件进行监控。
                </p>
            </div>


            <div class="space-y-6">
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-start">
                    <label class="text-xs md:text-sm font-medium text-gray-700">框架QQ状态监控</label>
                    <div class="md:col-span-3">
                        <div class="flex flex-wrap gap-2">
                            <input type="text" class="flex-1 px-3 py-2 border border-gray-300 rounded-lg text-sm bg-gray-50" id="a" value="<?php echo $hosturl.'/cron/cron.php?key='.$conf['cronkey']?>&act=qqzt" readonly>
                            <button type="button" class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition text-sm" onclick="showresult('a')">手动执行一次</button>
                        </div>
                        <p class="mt-1 text-xs text-red-600">当框架关卡死或关闭时，网站QQ状态就会设置成离线</p>
                    </div>
                </div>

                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-start">
                    <label class="text-xs md:text-sm font-medium text-gray-700">QQ过期监控</label>
                    <div class="md:col-span-3">
                        <div class="flex flex-wrap gap-2">
                            <input type="text" class="flex-1 px-3 py-2 border border-gray-300 rounded-lg text-sm bg-gray-50" id="a1" value="<?php echo $hosturl.'/cron/cron.php?key='.$conf['cronkey']?>&act=end" readonly>
                            <button type="button" class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition text-sm" onclick="showresult('a1')">手动执行一次</button>
                        </div>
                        <p class="mt-1 text-xs text-red-600">过期后N天后删除系统QQ+框架QQ数据+QQ剩余N天邮件通知</p>
                    </div>
                </div>

                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-start">
                    <label class="text-xs md:text-sm font-medium text-gray-700">对接节点监控</label>
                    <div class="md:col-span-3">
                        <div class="flex flex-wrap gap-2">
                            <input type="text" class="flex-1 px-3 py-2 border border-gray-300 rounded-lg text-sm bg-gray-50" id="c" value="<?php echo $hosturl.'/cron/node_link_cron.php?key='.$conf['cronkey']?>" readonly>
                            <button type="button" class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition text-sm" onclick="showresult('c')">手动执行一次</button>
                        </div>
                        <p class="mt-1 text-xs text-red-600">对接节点授权到期提醒 + 节点同步超时/拉取失败告警</p>
                    </div>
                </div>

                <div class="grid grid-cols-1 md:grid-cols-4 gap-4 items-start">
                    <label class="text-xs md:text-sm font-medium text-gray-700">秒赞任务监控</label>
                    <div class="md:col-span-3">
                        <div class="flex flex-wrap gap-2">
                            <input type="text" class="flex-1 px-3 py-2 border border-gray-300 rounded-lg text-sm bg-gray-50" id="b" value="<?php echo $hosturl.'/cron/qzone_cron.php?key='.$conf['cronkey']?>" readonly>
                            <button type="button" class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition text-sm" onclick="showresult('b')">手动执行一次</button>
                        </div>
                        <p class="mt-1 text-xs text-red-600">用于执行秒赞、秒评、签到等空间任务</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div id="showresult" class="fixed inset-0 bg-black bg-opacity-50 hidden items-center justify-center z-50 p-4">
    <div class="bg-white rounded-xl shadow-lg w-full max-w-4xl lg:max-w-5xl max-h-[90vh] flex flex-col">
        <div class="p-4 border-b border-gray-200 flex items-center justify-between">
            <h3 class="text-base font-semibold text-gray-800">手动执行</h3>
            <button type="button" onclick="closeModal()" class="text-gray-400 hover:text-gray-600">
                <i class="fas fa-times"></i>
            </button>
        </div>
        <div class="p-4 overflow-y-auto flex-1 min-h-0" id="result_content">
            <div class="text-center py-6">
                <i class="fas fa-spinner fa-spin text-blue-600 text-2xl"></i>
                <p class="mt-2 text-sm text-gray-600">正在加载...</p>
            </div>
        </div>
        <div class="p-4 border-t border-gray-200">
            <button type="button" onclick="closeModal()" class="w-full px-4 py-2 bg-gray-200 hover:bg-gray-300 text-gray-700 font-medium rounded-lg transition text-sm">
                关闭
            </button>
        </div>
    </div>
</div>

<?php include 'foot.php'; ?>

<script type="text/javascript">
	function showresult(id){
		var url = $('#'+id).val();
		$("#showresult").removeClass('hidden').addClass('flex');
		$("#result_content").html('<div class="text-center py-6"><i class="fas fa-spinner fa-spin text-blue-600 text-2xl"></i><p class="mt-2 text-sm text-gray-600">正在加载...</p></div>');
		$.ajax({
			url: url,
			type: 'GET',
			dataType: 'text',
			timeout: 300000,
			success: function(text) {
				var esc = $('<div/>').text(text).html();
				$("#result_content").html('<pre class="m-0 p-3 bg-gray-50 border border-gray-200 rounded-lg text-xs text-gray-800 whitespace-pre-wrap break-words max-h-[65vh] overflow-y-auto font-mono leading-relaxed">' + esc + '</pre>');
			},
			error: function(xhr, status, err) {
				var msg = '请求失败';
				if (status === 'timeout') msg = '请求超时（监控任务可能仍在执行，可稍后查看日志）';
				else if (xhr && xhr.status) msg += ' HTTP ' + xhr.status;
				if (err) msg += ' ' + err;
				var esc = $('<div/>').text(msg).html();
				$("#result_content").html('<pre class="m-0 p-3 bg-red-50 border border-red-200 rounded-lg text-sm text-red-700 whitespace-pre-wrap break-words">' + esc + '</pre>');
			}
		});
	}
	function closeModal(){
		$("#showresult").addClass('hidden').removeClass('flex');
		$("#result_content").html('');
	}
	function saveSetting(obj){
		showLoading();
		$.ajax({
			type : 'POST',
			url : 'ajax.php?act=frame_conf',
			data : $(obj).serialize(),
			dataType : 'json',
			success : function(data) {
				hideLoading();
				if(data.code == 1){
					showToast(data.msg, 'success');
				}else{
					showToast(data.msg || '保存失败', 'error');
				}
			},
			error:function(data){
				hideLoading();
				showToast('服务器错误', 'error');
				return false;
			}
		});
	}
	function saveSetting2(){
		showLoading();
		$.ajax({
			type : 'POST',
			url : 'ajax.php?act=frame_conf',
			data : {open_web_cron:$('select[name="open_web_cron"]').val()},
			dataType : 'json',
			success : function(data) {
				hideLoading();
				showToast(data.msg, 'success');
			},
			error:function(data){
				hideLoading();
				showToast('服务器错误', 'error');
				return false;
			}
		});
	}
	var items = $("select[default]");
	for (i = 0; i < items.length; i++) {
		$(items[i]).val($(items[i]).attr("default")||0);
	}
</script>
