desktop.html 27.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>坐标补齐</title>
<style>
  :root {
    --bg: #eef1f5; --surface: #fff; --sunk: #f7f9fb;
    --line: #dfe4eb; --line-soft: #ebeef3;
    --ink: #131820; --ink-2: #414c5c; --muted: #6d7887;
    --accent: #1552b8; --accent-soft: #e8f0fb; --accent-line: #c3d6f0;
    --warn: #9a4f13; --warn-soft: #fdf3e7; --bad: #a52d24; --bad-soft: #fceeed;
    --good: #166b46; --good-soft: #e9f6f0;
    --space-1: 4px; --space-2: 8px; --space-3: 12px; --space-4: 16px; --space-5: 24px; --space-6: 32px;
    --fs-xs: 11.5px; --fs-sm: 12.5px; --fs-md: 13.5px; --fs-lg: 15px; --fs-xl: 19px; --fs-2xl: 26px;
    --mono: ui-monospace, "Cascadia Mono", Consolas, "Courier New", monospace;
    --radius: 8px;
    --shadow: 0 1px 2px rgba(19, 24, 32, .05), 0 6px 18px rgba(19, 24, 32, .05);
  }
  * { box-sizing: border-box; }
  [hidden] { display: none !important; }
  html { -webkit-text-size-adjust: 100%; }
  body {
    margin: 0; padding: var(--space-5) var(--space-4) var(--space-6);
    background: var(--bg); color: var(--ink);
    font: var(--fs-md)/1.65 "Segoe UI Variable Text", "Segoe UI", "Microsoft YaHei", system-ui, sans-serif;
  }
  .wrap { max-width: 1240px; margin: 0 auto; }

  header.top { display: flex; flex-wrap: wrap; gap: var(--space-2) var(--space-4); align-items: baseline; margin-bottom: var(--space-5); }
  h1 { font-size: var(--fs-2xl); line-height: 1.2; margin: 0; letter-spacing: -.01em; font-weight: 650; }
  .lede { color: var(--ink-2); max-width: 62ch; }
  .lede .where { color: var(--muted); font-size: var(--fs-sm); }

  .layout { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--space-4); }
  @media (min-width: 1080px) {
    .layout { grid-template-columns: minmax(0, 1.55fr) minmax(360px, .85fr); align-items: start; }
    .side { position: sticky; top: var(--space-4); }
  }
  .col { display: grid; gap: var(--space-3); align-content: start; }

  section {
    background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
    padding: var(--space-4) var(--space-4) var(--space-4); box-shadow: var(--shadow);
  }
  h2 {
    display: flex; align-items: center; gap: var(--space-2);
    font-size: var(--fs-lg); margin: 0 0 var(--space-1); font-weight: 620; letter-spacing: 0;
  }
  h2 .num {
    flex: none; font: 600 var(--fs-xs)/1 var(--mono); color: var(--accent);
    background: var(--accent-soft); border: 1px solid var(--accent-line);
    border-radius: 999px; padding: 4px 7px; letter-spacing: .04em;
  }
  .desc { color: var(--muted); font-size: var(--fs-sm); margin: 0 0 var(--space-3); max-width: 68ch; }

  label { display: block; font-weight: 600; font-size: var(--fs-sm); color: var(--ink-2); margin-bottom: 6px; letter-spacing: .01em; }
  .hint { color: var(--muted); font-weight: 400; }
  .field { min-width: 0; }
  .control { display: flex; gap: var(--space-2); align-items: flex-end; flex-wrap: wrap; }
  input[type=text], input[type=number], input[type=password] {
    flex: 1 1 240px; width: 100%; min-width: 0; padding: 9px 11px; font: inherit; font-size: var(--fs-md);
    color: var(--ink); background: #fff; border: 1px solid var(--line); border-radius: 6px;
    font-variant-numeric: tabular-nums;
  }
  input.path { font: var(--fs-sm)/1.5 var(--mono); letter-spacing: 0; }
  input::placeholder { color: #9aa4b1; }
  input:hover:not(:disabled) { border-color: #c8d0da; }
  input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }
  input:disabled, button:disabled { opacity: .45; cursor: not-allowed; }
  .unit { flex: none; align-self: center; color: var(--muted); font-size: var(--fs-sm); margin-left: -4px; }

  button {
    font: inherit; font-size: var(--fs-md); font-weight: 600; padding: 9px 15px; border-radius: 6px;
    border: 1px solid var(--accent); background: var(--accent); color: #fff; cursor: pointer;
    transition: background .12s, border-color .12s, color .12s;
  }
  button:hover:not(:disabled) { background: #0f469e; border-color: #0f469e; }
  button:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }
  button.ghost { background: #fff; color: var(--accent); border-color: var(--accent-line); }
  button.ghost:hover:not(:disabled) { background: var(--accent-soft); color: #0f469e; border-color: var(--accent); }
  button.quiet { background: none; border: 0; color: var(--accent); padding: 4px 6px; font-weight: 600; }
  button.danger { border-color: #e2b6b1; background: #fff; color: var(--bad); }
  button.danger:hover:not(:disabled) { background: var(--bad-soft); border-color: var(--bad); color: var(--bad); }
  button.big { padding: 11px 18px; font-size: var(--fs-lg); }
  /* 弱按钮(清单芯片、快捷目录)别被通用 hover 的实色底盖掉文字 */
  button.quiet:hover:not(:disabled) { background: var(--accent-soft); color: #0f469e; }

  .switch { display: flex; gap: var(--space-3); align-items: flex-start; }
  .switch input {
    appearance: none; -webkit-appearance: none; flex: none; width: 38px; height: 22px; margin: 2px 0 0;
    background: #d5dbe3; border-radius: 999px; position: relative; cursor: pointer; transition: background .15s;
  }
  .switch input::after {
    content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%;
    background: #fff; box-shadow: 0 1px 2px rgba(19, 24, 32, .3); transition: transform .15s;
  }
  .switch input:checked { background: var(--accent); }
  .switch input:checked::after { transform: translateX(16px); }
  .switch input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
  .switch label { margin: 0; font-size: var(--fs-md); color: var(--ink); font-weight: 600; }
  .switch .desc { margin: 2px 0 0; }

  .grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: var(--space-3); }
  .fields { display: grid; gap: var(--space-3); }
  .grid.tight { margin-top: var(--space-3); }
  .note { margin-top: var(--space-2); color: var(--muted); font-size: var(--fs-sm); overflow-wrap: anywhere; }
  .note.warn { color: var(--warn); }
  .note.bad { color: var(--bad); }
  .note.ok { color: var(--good); }
  .fade { opacity: .45; }

  /* 运行面板 */
  .actions { display: flex; gap: var(--space-2); align-items: center; flex-wrap: wrap; }
  .state { font-size: var(--fs-sm); color: var(--muted); }
  .state b { color: var(--ink); font-weight: 620; }
  .bar { height: 6px; border-radius: 999px; background: var(--line); overflow: hidden; margin: var(--space-3) 0 var(--space-2); }
  .bar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .25s ease-out; }
  .bar.busy i { width: 32%; animation: slide 1.5s cubic-bezier(.4, 0, .6, 1) infinite; }
  @keyframes slide { from { margin-left: -32%; } to { margin-left: 100%; } }
  .stage { font-weight: 620; font-size: var(--fs-lg); font-variant-numeric: tabular-nums; }
  .console {
    margin-top: var(--space-3); max-height: 190px; overflow: auto; background: var(--sunk);
    border: 1px solid var(--line-soft); border-radius: 6px; padding: var(--space-2) 10px;
    font: var(--fs-xs)/1.75 var(--mono); color: var(--ink-2); white-space: pre-wrap; overflow-wrap: anywhere;
  }

  /* 总结 */
  .headline {
    font-size: var(--fs-xl); font-weight: 650; letter-spacing: -.01em;
    font-variant-numeric: tabular-nums; margin-bottom: var(--space-3);
  }
  table { width: 100%; border-collapse: collapse; }
  th, td { text-align: left; padding: 8px 0; vertical-align: top; border-bottom: 1px solid var(--line-soft); }
  tr:last-child th, tr:last-child td { border-bottom: 0; }
  th { width: 45%; padding-right: var(--space-3); color: var(--muted); font-weight: 500; font-size: var(--fs-sm); }
  td { font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
  td.mono { font: var(--fs-sm)/1.6 var(--mono); }
  .files { display: grid; gap: var(--space-2); margin-top: var(--space-3); }
  .out { border: 1px solid var(--line-soft); border-radius: 6px; background: var(--sunk); padding: 8px 10px; }
  .out .line { display: flex; align-items: baseline; gap: 6px; flex-wrap: wrap; }
  .out .name { color: var(--ink); font-weight: 600; font-size: var(--fs-sm); flex: 1 1 auto; }
  .out .path { display: block; margin-top: 2px; color: var(--muted); font: var(--fs-xs)/1.6 var(--mono); overflow-wrap: anywhere; }
  .caption { color: var(--muted); font-size: var(--fs-xs); letter-spacing: .04em; margin: var(--space-4) 0 var(--space-1); }

  /* 目录选择 */
  dialog {
    border: 1px solid var(--line); border-radius: 10px; padding: 0; width: min(760px, 92vw);
    box-shadow: 0 18px 48px rgba(15, 20, 30, .18); background: var(--surface); color: var(--ink);
  }
  dialog::backdrop { background: rgba(15, 20, 30, .35); }
  .dhead { display: flex; gap: var(--space-2); align-items: center; padding: var(--space-3) var(--space-4); border-bottom: 1px solid var(--line); }
  .dlist { max-height: 54vh; overflow: auto; padding: 6px 8px var(--space-3); }
  .dlist button {
    display: block; width: 100%; text-align: left; background: none; border: 0; color: var(--ink);
    font-weight: 400; font-size: var(--fs-md); padding: 7px 10px; border-radius: 6px;
  }
  .dlist button:hover:not(:disabled) { background: var(--accent-soft); }
  .dlist .dir::before { content: "▸ "; color: var(--accent); }
  .dlist .file::before { content: "  "; }
  .size { float: right; color: var(--muted); font-size: var(--fs-xs); font-variant-numeric: tabular-nums; }
  .roots { display: flex; gap: 6px; padding: 10px var(--space-4) 0; flex-wrap: wrap; }
  .roots button { padding: 5px 11px; font-size: var(--fs-sm); background: #fff; color: var(--accent); border-color: var(--line); }
  .roots button:hover:not(:disabled) { background: var(--accent-soft); border-color: var(--accent); color: #0f469e; }
  .foot { padding: var(--space-3) var(--space-4); border-top: 1px solid var(--line); display: flex; gap: var(--space-2); justify-content: flex-end; }
</style>
</head>
<body>
<div class="wrap">
  <header class="top">
    <h1>坐标补齐</h1>
    <div class="lede">
      先读地址文本,读不出或读得可疑的那些行才用坐标问地图接口。
      <span class="where">关掉这个窗口,或按终端里的 Ctrl+C 即停止。</span>
    </div>
  </header>

  <div class="layout">
    <div class="col">
      <section>
        <h2><span class="num">01</span>源文件</h2>
        <p class="desc">第三方导出的那张表,CSV 或 Excel 都行。表里只要有经纬度;缺的三列会自动追加。</p>
        <div class="field">
          <label for="input">表格文件 <span class="hint">.csv / .xlsx / .xlsm / .xlsb / .xls</span></label>
          <div class="control">
            <input type="text" id="input" class="path" placeholder="完整路径,或点右边「浏览」">
            <button class="ghost" id="pickInput">浏览</button>
          </div>
        </div>
        <div class="note" id="inputNote"></div>
      </section>

      <section>
        <h2><span class="num">02</span>输出</h2>
        <p class="desc">永远写新文件,不碰源表。正式表格进源文件旁的 out/,处理日志与清单进源文件旁的 .temp/,工具目录不落运行产物。</p>
        <div class="fields">
          <div class="field">
            <label for="outputDir">输出目录 <span class="hint">留空 = 源文件旁/out</span></label>
            <div class="control">
              <input type="text" id="outputDir" class="path" placeholder="留空即放在源文件旁的 out 文件夹">
              <button class="ghost" id="pickDir">浏览</button>
            </div>
          </div>
          <div class="field">
            <label for="outputName">文件名 <span class="hint">留空 = 原名.已补全</span></label>
            <input type="text" id="outputName" class="path" placeholder="留空自动生成">
          </div>
        </div>
        <div class="note" id="outputNote"></div>
      </section>

      <section>
        <h2><span class="num">03</span>已查坐标缓存</h2>
        <p class="desc">问过的坐标记在这里,续跑要用同一个文件;拷走它,换电脑也不重复花钱。</p>
        <div class="field">
          <label for="cache">缓存文件 <span class="hint">留空 = 源文件旁/cache</span></label>
          <div class="control">
            <input type="text" id="cache" class="path" placeholder="留空即放在源文件旁的 cache 文件夹">
            <button class="ghost" id="pickCache">浏览</button>
          </div>
        </div>
        <div class="note" id="cacheNote"></div>
      </section>

      <section>
        <h2><span class="num">04</span>接口节奏</h2>
        <p class="desc">只有地址补不齐的行才会走到这里。并发越高越快,也越容易被限流。</p>
        <div class="grid">
          <div class="field">
            <label for="workers">同时在途请求</label>
            <input type="number" id="workers" min="1" step="1">
          </div>
          <div class="field">
            <label for="qps">每秒请求上限 <span class="hint">0 为不限</span></label>
            <input type="number" id="qps" min="0" step="1">
          </div>
          <div class="field">
            <label for="limit">只处理前几行 <span class="hint">留空为全量</span></label>
            <input type="number" id="limit" min="1" step="1" placeholder="留空">
          </div>
        </div>
        <div class="note">先用「只处理前几行」填 20 跑一遍,看取值对不对,再回来跑全量。</div>
      </section>

      <section>
        <h2><span class="num">05</span>续跑清单 <span class="hint">可留空</span></h2>
        <p class="desc">上一次运行生成的「限流待重试」清单。填了它,本次只有清单里的行允许问接口;表格与日志仍按整张表写,其余行照旧用表内原值、地址和缓存补齐。</p>
        <div class="field">
          <label for="onlyList">清单文件 <span class="hint">补齐工具写的 .csv,与生成它的那张输入表配对使用</span></label>
          <div class="control">
            <input type="text" id="onlyList" class="path" placeholder="留空 = 整张表都可以问接口">
            <button class="ghost" id="pickList">浏览</button>
          </div>
        </div>
        <div class="note" id="listNote"></div>
      </section>

      <section>
        <h2><span class="num">06</span>推断与排序</h2>
        <div class="switch">
          <input type="checkbox" id="sortIncompleteLast">
          <div>
            <label for="sortIncompleteLast">把仍有空白的记录排到表格末尾</label>
            <p class="desc">完整记录在前,导入时先拿稳的,空白的留在末尾等人工补。</p>
          </div>
        </div>
        <div class="switch" style="margin-top:var(--space-4)">
          <input type="checkbox" id="inferNearby">
          <div>
            <label for="inferNearby">允许用附近已知地点的归属补空白</label>
            <p class="desc">这类值是「附近大概是这里」,不是这一条的实测结果,日志里单独标来源,所以默认关着。打开后能少问很多接口。</p>
          </div>
        </div>
        <div class="grid tight fade" id="radii">
          <div class="field">
            <label for="streetMeters">街道半径</label>
            <div class="control"><input type="number" id="streetMeters" min="1" step="1"><span class="unit">米</span></div>
          </div>
          <div class="field">
            <label for="roadMeters">道路半径</label>
            <div class="control"><input type="number" id="roadMeters" min="1" step="1"><span class="unit">米</span></div>
          </div>
        </div>
        <div class="note">行政区不用邻近补:两个区之间隔着几公里,附近点容易把案子记到隔壁区。</div>
      </section>
    </div>

    <div class="col side">
      <section>
        <h2><span class="num">07</span>高德 Key</h2>
        <p class="desc">只有本次真有待查坐标时才用到。填一次、只用这一次,不写进配置、日志、缓存。</p>
        <div class="control">
          <input type="password" id="key" placeholder="需要问接口时再填" autocomplete="off">
          <button class="ghost" id="sendKey" hidden>继续</button>
        </div>
        <div class="note" id="keyNote">整张表都能靠地址和缓存补齐时不会用到 Key。</div>
      </section>

      <section>
        <h2>运行</h2>
        <div class="actions">
          <button id="start" class="big">开始补齐</button>
          <button class="danger" id="stop" disabled>停止</button>
        </div>
        <div class="note bad" id="blocked"></div>
      </section>

      <section id="runSection" hidden>
        <h2>正在做</h2>
        <div class="stage" id="stage">准备中…</div>
        <div class="bar" id="bar"><i></i></div>
        <div class="console" id="console"></div>
      </section>

      <section id="doneSection" hidden>
        <h2>本次结果</h2>
        <div class="headline" id="headline"></div>
        <table id="summary"></table>
        <div class="note warn" id="caution"></div>
        <div class="caption">点开看看</div>
        <div class="files" id="files"></div>
      </section>
    </div>
  </div>
</div>

<dialog id="picker">
  <div class="dhead">
    <input type="text" id="browsePath" class="path" style="flex:1">
    <button class="ghost" id="browseGo">前往</button>
  </div>
  <div class="roots" id="roots"></div>
  <div class="dlist" id="dlist"></div>
  <div class="foot">
    <button class="ghost" id="useDir">选这个目录</button>
    <button class="ghost" id="closePick">取消</button>
  </div>
</dialog>

<script type="module">
const TOKEN = '%%TOKEN%%';
const $ = (id) => document.getElementById(id);
const fields = ['input', 'outputDir', 'outputName', 'cache', 'onlyList', 'workers', 'qps', 'limit',
  'inferNearby', 'streetMeters', 'roadMeters', 'sortIncompleteLast'];

async function api(path, data) {
  const response = await fetch('/api/' + path, {
    method: 'POST',
    headers: { 'content-type': 'application/json', 'x-token': TOKEN },
    body: JSON.stringify(data || {}),
  });
  const value = await response.json();
  if (!response.ok || value.error) throw new Error(value.error || ('HTTP ' + response.status));
  return value;
}

function form() {
  const values = {};
  for (const name of fields) {
    const el = $(name);
    values[name] = el.type === 'checkbox' ? el.checked
      : el.type === 'number' ? (el.value === '' ? '' : Number(el.value)) : el.value.trim();
  }
  return values;
}

function setSize(bytes) {
  if (!bytes) return '';
  const mb = bytes / 1048576;
  return mb >= 1 ? mb.toFixed(1) + ' MB' : Math.round(bytes / 1024) + ' KB';
}

/** 缓存大小跟着当前缓存路径走:改了路径就要重算,否则提示的是另一个文件。 */
function setCacheNote(bytes) {
  const size = setSize(bytes);
  $('cacheNote').textContent = size
    ? `缓存文件现有 ${size},里面查过的坐标这次不会重复问。` : '';
}

let checkTimer = 0;
async function check() {
  clearTimeout(checkTimer);
  checkTimer = setTimeout(async () => {
    try {
      const value = await api('check', form());
      $('inputNote').textContent = value.issue || '';
      $('inputNote').className = 'note ' + (value.issue ? 'bad' : '');
      $('outputNote').textContent = value.notice;
      $('outputNote').className = 'note ' + (/会覆盖|拒绝/.test(value.notice) ? 'warn' : '');
      setCacheNote(value.cacheSize);
      $('listNote').textContent = value.listNotice || '';
      $('listNote').className = 'note ' + (/找不到|不是|不认|读不出来/.test(value.listNotice) ? 'bad' : '');
      $('blocked').textContent = value.issue || '';
      $('start').disabled = !!value.issue || $('start').dataset.running === '1';
    } catch (error) {
      $('inputNote').textContent = error.message;
    }
  }, 250);
}

function setField(name, value) {
  const el = $(name);
  if (el.type === 'checkbox') el.checked = !!value;
  else el.value = value === null || value === undefined ? '' : String(value);
}

// 浏览目录:浏览器给不出本机绝对路径,所以列目录由本机做
let pickTarget = 'input';
const join = (dir, name) => `${dir}${dir.includes('\\') ? '\\' : '/'}${name}`;

async function show(dir) {
  const kind = pickTarget === 'cache' ? 'cache' : pickTarget === 'onlyList' ? 'list' : '';
  const value = await api('browse', { dir, kind });
  $('browsePath').value = value.dir;
  const list = $('dlist');
  list.textContent = '';
  if (value.parent) {
    const up = document.createElement('button');
    up.className = 'dir';
    up.textContent = '.. 上一级';
    up.onclick = () => show(value.parent);
    list.append(up);
  }
  for (const entry of value.entries) {
    const item = document.createElement('button');
    item.className = entry.directory ? 'dir' : 'file';
    item.textContent = entry.name;
    if (!entry.directory) {
      const size = document.createElement('span');
      size.className = 'size';
      size.textContent = setSize(entry.size);
      item.append(size);
    }
    item.onclick = () => {
      if (entry.directory) show(entry.path);
      else { $(pickTarget).value = entry.path; $('picker').close(); check(); }
    };
    list.append(item);
  }
  if (value.note) $('browsePath').title = value.note;
  $('useDir').onclick = () => {
    if (pickTarget === 'cache') {
      const current = $('cache').value.split(/[\\/]/).pop() || 'amap_regeo_cache.sqlite3';
      $('cache').value = join(value.dir, current);
    } else {
      $('outputDir').value = value.dir;
    }
    $('picker').close();
    check();
  };
}

function openPicker(target, start) {
  pickTarget = target;
  // 源文件与清单都是文件:选目录对它们没有意义
  $('useDir').hidden = target === 'input' || target === 'onlyList';
  $('picker').showModal();
  show(start || '').catch((error) => { $('dlist').textContent = error.message; });
}

function renderSummary(summary) {
  $('doneSection').hidden = false;
  $('headline').textContent = summary.headline;

  const table = $('summary');
  table.textContent = '';
  for (const line of summary.rows) {
    const row = table.insertRow();
    row.insertCell().textContent = line.label;
    row.insertCell().textContent = line.value;
  }
  $('caution').textContent = summary.caution;
  $('caution').className = summary.caution ? 'note warn' : 'note';
  $('caution').hidden = !summary.caution;

  const links = $('files');
  links.textContent = '';
  for (const file of summary.files) {
    const box = document.createElement('div');
    box.className = 'out';
    const line = document.createElement('div');
    line.className = 'line';
    const name = document.createElement('span');
    name.className = 'name';
    name.textContent = file.label;
    const open = document.createElement('button');
    open.className = 'quiet';
    open.textContent = '打开';
    open.onclick = () => api('open', { path: file.value }).catch(() => undefined);
    line.append(name, open);
    // 限流那份是唯一值得再跑一次的清单:把「填回去」做成一次点击
    if (file.label === '限流待重试') {
      const again = document.createElement('button');
      again.className = 'quiet';
      again.textContent = '用它续跑';
      again.onclick = () => {
        $('onlyList').value = file.value;
        $('onlyList').focus();
        check();
      };
      line.append(again);
    }
    const path = document.createElement('span');
    path.className = 'path';
    path.textContent = file.value;
    box.append(line, path);
    links.append(box);
  }
}

const source = new EventSource('/api/events?t=' + TOKEN);
source.onmessage = (event) => {
  const data = JSON.parse(event.data);
  if (data.type === 'stage') {
    $('runSection').hidden = false;
    $('stage').textContent = data.text || ('正在' + data.label);
    const bar = $('bar');
    if (data.total && data.done !== undefined) {
      bar.className = 'bar';
      bar.firstElementChild.style.width = Math.min(100, data.done / data.total * 100) + '%';
    } else {
      bar.className = 'bar busy';
      bar.firstElementChild.style.width = '32%';
    }
  } else if (data.type === 'log') {
    append(data.message);
  } else if (data.type === 'key-needed') {
    $('sendKey').hidden = false;
    $('keyNote').textContent = '这次运行要问地图接口,请在上面填 Key 后点「继续」。';
    $('keyNote').className = 'note warn';
    $('key').focus();
    append('等待高德 Key…');
  } else if (data.type === 'done') {
    $('stage').textContent = '已完成';
    $('bar').className = 'bar';
    $('bar').firstElementChild.style.width = '100%';
    renderSummary(data.summary);
    // 跑完缓存文件长了(或中途停下来也写了一批),提示语要跟着新大小
    check();
  } else if (data.type === 'error') {
    $('stage').textContent = '停下来:' + data.message;
    const hint = data.detail ? `(内部提示:${data.detail.split('\n')[0]};完整提示在启动界面的那个终端窗口里)` : '';
    append('失败:' + data.message + hint);
    $('bar').className = 'bar';
    check();
  } else if (data.type === 'running') {
    running(data.running);
  }
};

function append(message) {
  const box = $('console');
  box.textContent += message + '\n';
  box.scrollTop = box.scrollHeight;
}

function running(on) {
  $('start').dataset.running = on ? '1' : '';
  $('start').disabled = on || !!$('blocked').textContent;
  $('stop').disabled = !on;
  for (const name of fields) $(name).disabled = on;
  $('key').disabled = false;
}

$('start').onclick = async () => {
  $('runSection').hidden = false;
  $('doneSection').hidden = true;
  $('console').textContent = '';
  $('stage').textContent = '正在开始…';
  $('bar').className = 'bar busy';
  try {
    await api('start', { ...form(), key: $('key').value.trim() });
    running(true);
  } catch (error) {
    $('stage').textContent = error.message;
  }
};
$('stop').onclick = () => api('stop', {}).catch(() => undefined);
$('sendKey').onclick = async () => {
  await api('key', { value: $('key').value.trim() });
  $('sendKey').hidden = true;
  $('keyNote').textContent = '已收到,继续跑。';
  $('keyNote').className = 'note';
};
for (const name of fields) $(name).addEventListener('input', () => {
  if (name === 'inferNearby') $('radii').classList.toggle('fade', !$(name).checked);
  check();
});
$('pickInput').onclick = () => openPicker('input', $('input').value);
$('pickCache').onclick = () => openPicker('cache', $('cache').value);
$('pickDir').onclick = () => openPicker('outputDir', $('outputDir').value);
$('pickList').onclick = () => openPicker('onlyList', $('onlyList').value);
$('browseGo').onclick = () => show($('browsePath').value);
$('closePick').onclick = () => $('picker').close();

const boot = await api('boot', {});
for (const name of fields) setField(name, boot.state[name]);
$('radii').classList.toggle('fade', !$('inferNearby').checked);
setCacheNote(boot.cacheSize);
const roots = $('roots');
for (const spot of boot.quickRoots) {
  const button = document.createElement('button');
  button.textContent = spot.name;
  button.onclick = () => show(spot.path);
  roots.append(button);
}
running(boot.running);
if (boot.savedExists && boot.state.input) append('已载入上次的路径与参数。');
check();
</script>
</body>
</html>