<?php

use xmvc\Encoder;
use xmvc\MVC;

$menus = [];
$contents = file_get_contents(realpath(dirname(__FILE__) . '/../') . '/Cmd.php');
preg_match_all('/\/\*\*(.*?)\*\/\s+public\s+function\s+([^a-zA-Z0-9_]*)([a-zA-Z0-9_]+)(?=\()/s', $contents, $m);
if ($m) {
    foreach ((array)$m[0] as $i => $v) {
        if (strpos($v, '@menu') !== false) {
            $pattern = '/\/\*\*\s*\*\s*(.*?)\s*@/s';
            if (preg_match($pattern, $v, $m2)) {
                $remark = trim($m2[1], '*');
            }
            $funcName = $m[3][$i];
            $reflector = new \ReflectionMethod($this, $funcName);
            $reflectionParameters = $reflector->getParameters();
            $params = [];
            if ($reflectionParameters) {
                foreach ($reflectionParameters as $k => $pv) {
                    preg_match('/@param[^\$]+\$' . $pv->name . '\s+([^\r|\n]+)/', $v, $m3);
                    $params[$k] = new \stdClass();
                    $params[$k]->name = $pv->name;
                    $params[$k]->remark = $m3 ? $m3[1] : $pv->name;
                }
            }
            $menus[] = [$funcName, $remark, $params];
        }
    }
}
$liArr = [];
foreach ($menus as $v) {
    list($funcName, $remark, $params) = $v;
    if ($params) {
        $inputs = [];
        foreach ($params as $k => $pv) {
            $inputs[] = '<span style="font-size:.8em">' . $pv->remark . ':</span> <input type="text" style="width:100%" name="' . $pv->name . '">';
        }

        $liArr[] = '<li style="padding-top:.5em;font-size:1.1em;" class="border hover">'
            . $remark . '<br />'
            . implode('<br />', $inputs)
            . ' <br />
                <a href="' . HOST . '/xmvc/core/cmd-' . $funcName . '" onclick="return bindLiInputParamValue(this)" target="_blank">执行</a></li>';
    } else {
        $liArr[] = '<li style="padding-top:.5em" class="border hover"><a href="' . HOST . '/xmvc/core/cmd-' . $funcName . '" target="_blank">' . $remark . '</a></li>';
    }
}
$liAutoHtml = implode('', $liArr);

// 日志
$liLogHtml = '';
$liLastErrorLogHtml = '';
forEachDir(DIR_LOG, function (string $path, string $basename, string $type,) use (&$liLogHtml, &$liLastErrorLogHtml) {
    if ($type == 'dir') {
        $files = scandir($path);
        $files = array_diff($files, array('.', '..'));
        sort($files);
        $endFile =  end($files);
        $endFile = $path . '/' . $endFile;
        $liLogHtml .= '<li style="padding-left:2em"><a href="' . HOST . '/xmvc/core/cmd-showLogFile~file-' . Encoder::encode($endFile) . '" target="_blank">' . basename($endFile) . '</a></li>';
        if ($basename == 'error' && $liLastErrorLogHtml == '') {
            $liLastErrorLogHtml = '<li style="padding-left:2em"><a href="' . HOST . '/xmvc/core/cmd-showLastErrorLogFile~file-' . Encoder::encode($endFile) . '" target="_blank" style="color:#880088">Last Error Log</a></li>';
        }
    }
});
$liLogHtml = $liLastErrorLogHtml . $liLogHtml;

$liHtml = '';
$liHtml .= '<li>
            <strong>帮助文档</strong>
            <ul>
                <li style="padding-left:2em"><a href="' . HOST . '/xmvc/doc/phpdoc/index-index~type-xmvc" target="_blank">查看 xmvc 文档</a></li>
                <li style="padding-left:2em">-------- <a href="' . HOST . '/xmvc/doc/phpdoc/index-create~type-xmvc" target="_blank" onclick="return confirm(\'确认要生成 xmvc 文档吗?\')">生成 xmvc 文档</a></li>
                <li style="padding-left:2em">---------------- <a href="' . HOST . '/xmvc/doc/phpdoc/index-clear~type-xmvc" target="_blank" onclick="return confirm(\'确认要清空 xmvc 文档吗?\')">清空 xmvc 文档</a></li>
                <li style="padding-left:2em"><a href="' . HOST . '/xmvc/doc/phpdoc/index-index~type-project" target="_blank">查看 ' . PROJECT . ' 文档</a></li>
                <li style="padding-left:2em">-------- <a href="' . HOST . '/xmvc/doc/phpdoc/index-create~type-project" target="_blank" onclick="return confirm(\'确认要生成 ' . PROJECT . ' 文档吗?\')">生成 ' . PROJECT . ' 文档</a></li>
                <li style="padding-left:2em">---------------- <a href="' . HOST . '/xmvc/doc/phpdoc/index-clear~type-project" target="_blank" onclick="return confirm(\'确认要清空 ' . PROJECT . ' 文档吗?\')">清空 ' . PROJECT . ' 文档</a></li>
            </ul>
        </li>';
$liHtml .= '<li>
            <strong>日志</strong>
            <ul>
                ' . $liLogHtml . '
            </ul>
        </li>';
$liHtml .= '<li>
            <strong>运行时状态</strong>
            <ul>
                <li style="padding-left:2em"><a href="' . URL_RUNTIME . '/runtime.html" target="_blank">Runtime</a></li>
            </ul>
        </li>';
$liHtml .= '<li>
            <strong>控制台</strong>
            <ul>
                <li style="padding-left:2em"><a href="' . URL_RUNTIME . '/cli_dump.html" target="_blank">dump(),dd() 函数输出 ( HTML 版 )</a></li>
                <li style="padding-left:2em"><a href="' . URL_RUNTIME . '/cli_dump.json" target="_blank">dump(),dd() 函数输出 ( JSON 版 )</a></li>
                <li style="padding-left:2em"><a href="' . URL_RUNTIME . '/cli_error.html" target="_blank">error 错误输出 ( HTML版 )</a></li>
            </ul>
        </li>';
$liHtml .= '<li>
            <strong>语言包</strong>
            <ul>
                <li style="padding-left:2em"><a href="' . HOST . '/xmvc/core/cmd-lang~type-xmvc" target="_blank">管理 xmvc 语言包</a></li>
                <li style="padding-left:2em"><a href="' . HOST . '/xmvc/core/cmd-lang~type-project" target="_blank">管理 ' . PROJECT . ' 语言包</a></li>
            </ul>
        </li>';
$header = '
<title>CMD - CORE</title>
<style>
html {height: 100%;}
body, .body {
    display: flex;
    margin: 0;
    font-size: 12px;
    height: 100%;
    overflow-y: hidden;
}

.div-left {
    min-width: 300px;
    width: 300px;
    height: 100%;
    flex: 0 0 auto;
    overflow-y: auto;
    font-size:13px;
}

.div-right {
    height: 100%;
    padding:10px;
    flex: 1 1 auto;
    overflow-y: auto;
}
.div-left strong { font-size:15px;}
.div-left ul { padding:0 0 10px 0}

ul.menu-auto {  
  display: grid;  
  grid-gap: 1px 10px; /* 设置列间距 */  
  list-style-position: inside; /* 如果需要的话，将列表标记放在列内 */  
}    
/* 当视口宽度小于 600px 时，只显示一列 */  
@media (max-width: 600px) {  
  ul.menu-auto {  
    grid-template-columns: repeat(1, 1fr);   
  }  
}  
  
/* 当视口宽度在 800px 到 1100px 之间时，显示两列 */  
@media (min-width: 800px) and (max-width: 1099px) {  
  ul.menu-auto {  
    grid-template-columns: repeat(2, 1fr);   
  }  
}  
  
/* 当视口宽度在 1100px 到 1280px 之间时，显示三列 */  
@media (min-width: 1100px) and (max-width: 1279px) {  
  ul.menu-auto {  
    grid-template-columns: repeat(3, 1fr);   
  }  
}
  
/* 当视口宽度大于 1280px 时，显示四列 */  
@media (min-width: 1280px)  {  
  ul.menu-auto {  
    grid-template-columns: repeat(4, 1fr);   
  }  
}

ul.menu-auto li {  
  break-inside: avoid; /* 尝试避免列表项在列之间断开 */  
}
</style>
';
$msg = '
<div style="padding:0 .5em 1em 1em" class="body">
    <div class="div-left">
        <div style="font-size:1.5rem;font-weight:bold">CMD 命令台 <a href="' . HOST . '/xmvc/core/Cmd-logout" target="_blank" style="font-size:1rem;color:#c00" onclick="return confirm(\'确认要系统命令台吗?\')">安全退出</a></div>
        <ul>{:1}</ul>
    </div>
    <div class="div-right">
        <ul class="menu-auto">{:2}</ul>
    </div>
</div>';
$msg = qgprintf($msg, $liHtml, $liAutoHtml);
MVC::echo($msg, true, $header);
